Class: QueryHelper::SqlFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/query_helper/sql_filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_values: [], column_maps: [], qualify_filters: [], options: {}) ⇒ SqlFilter

Returns a new instance of SqlFilter.



8
9
10
11
12
13
# File 'lib/query_helper/sql_filter.rb', line 8

def initialize(filter_values: [], column_maps: [], qualify_filters: [], options: {})
  @column_maps = column_maps
  @filter_values = filter_values
  @options = options
  @qualify_filters = qualify_filters
end

Instance Attribute Details

#column_mapsObject

Returns the value of attribute column_maps.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def column_maps
  @column_maps
end

#filter_valuesObject

Returns the value of attribute filter_values.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def filter_values
  @filter_values
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def options
  @options
end

#qualify_filtersObject

Returns the value of attribute qualify_filters.



6
7
8
# File 'lib/query_helper/sql_filter.rb', line 6

def qualify_filters
  @qualify_filters
end

Instance Method Details

#aggregated_attribute?(comparate:) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/query_helper/sql_filter.rb', line 34

def aggregated_attribute?(comparate:)
  @options['qualify_clause'] && qualify_filters.include?(comparate)
end

#bind_variablesObject



50
51
52
# File 'lib/query_helper/sql_filter.rb', line 50

def bind_variables
  Hash[@filters.collect { |f| [f.bind_variable, f.criterion] }]
end

#create_filtersObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/query_helper/sql_filter.rb', line 15

def create_filters
  @filters = []

  @filter_values.each do |comparate_alias, criteria|
    # Find the sql mapping if it exists
    map = @column_maps.find { |m| m.alias_name == comparate_alias }
    raise InvalidQueryError.new("cannot filter by #{comparate_alias}") unless map

    # create the filter
    @filters << QueryHelper::Filter.new(
      operator_code: criteria.keys.first,
      criterion: criteria.values.first,
      comparate: map.sql_expression,
      aggregate: map.aggregate,
      qualify_clause: aggregated_attribute?(comparate: map.sql_expression)
    )
  end
end

#having_clausesObject



46
47
48
# File 'lib/query_helper/sql_filter.rb', line 46

def having_clauses
  @filters.select{ |f| f.aggregate == true }.map(&:sql_string)
end

#qualify_clausesObject



38
39
40
# File 'lib/query_helper/sql_filter.rb', line 38

def qualify_clauses
  @filters.select{ |f| aggregated_attribute?(comparate: f.comparate) }.map(&:sql_string)
end

#where_clausesObject



42
43
44
# File 'lib/query_helper/sql_filter.rb', line 42

def where_clauses
  @filters.select{ |f| f.aggregate == false && !f.qualify_clause }.map(&:sql_string)
end