Class: BaseQueryBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
app/queries/base_query_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ BaseQueryBuilder

Returns a new instance of BaseQueryBuilder.



6
7
8
9
10
11
# File 'app/queries/base_query_builder.rb', line 6

def initialize(query, options={})
  @query = query
  @table_prefix = options[:table_prefix] || @query.table_name
  @join_table_key = options[:join_table_key] || "#{@table_prefix.singularize}_id"
  @options = options
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'app/queries/base_query_builder.rb', line 3

def query
  @query
end

Instance Method Details

#array_check(array_column, value, exclusion_column = nil) ⇒ Object (protected)



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/queries/base_query_builder.rb', line 15

def array_check(array_column, value, exclusion_column = nil)
  exclusion_column ||= "#{@table_prefix}.exclude_#{array_column}".pluralize
  alternate_value_comparison = ":alternate_value = ANY()"
  array_splat = [value].compact.map(&:presence).flatten.compact
  if array_splat.present?
    array_value_expression = ActiveRecord::Base.escape_sql("ARRAY[?]::character varying[]", array_splat)
    reflect(query.where("(
                           (#{exclusion_column} is null or #{exclusion_column} = false)
                           AND
                           (
                             #{array_value_expression} && #{@table_prefix}.#{array_column}
                             OR #{@table_prefix}.#{array_column} = '{}'
                             OR #{@table_prefix}.#{array_column} IS NULL
                           )
                         )
                         OR
                         (
                           #{exclusion_column} = true
                           AND
                           (
                             NOT ( #{array_value_expression} && #{@table_prefix}.#{array_column} )
                             OR #{@table_prefix}.#{array_column} = '{}'
                             OR #{@table_prefix}.#{array_column} IS NULL
                           )
                         )" ))
  else
    reflect(query.where(array_column.to_sym => []))
  end
end

#habtm_check(value, join_table, foreign_key, exclusion_column = nil) ⇒ Object (protected)

When we check for conditions we need to check if the
association has the value in it, if the association doesn't
have the value in it if its set to exclude
or if the association as no values at all which is
implicit to accept all the values



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/queries/base_query_builder.rb', line 50

def habtm_check(value, join_table, foreign_key, exclusion_column = nil)
  exclusion_column ||= "#{@table_prefix}.exclude_#{foreign_key}".pluralize
  sql_filter = %Q{
    (
      /* non exclusion detection, has to have the value in question */
      (#{exclusion_column} is null or #{exclusion_column} = false)
      and
      exists(select 1 from #{join_table}
             where #{join_table}.#{foreign_key} = :value
             and #{join_table}.#{@join_table_key} = #{@table_prefix}.id )

    )
    or /* exclusion detection, can't use filters matching this value */
    (
      #{exclusion_column} = true
      and
      not exists(select 1 from #{join_table}
          where #{join_table}.#{foreign_key} = :value
          and #{join_table}.#{@join_table_key} = #{@table_prefix}.id )

    )
    or /* no criteria specified so it doesn't matter */
    not exists(select 1 from #{join_table}
         where #{join_table}.#{@join_table_key} = #{@table_prefix}.id )
  }
  reflect( query.where(sql_filter, value: value) )
end

#reflect(query) ⇒ Object (protected)



80
81
82
# File 'app/queries/base_query_builder.rb', line 80

def reflect(query)
  self.class.new(query)
end