Class: QueryHelper::SqlFilter
- Inherits:
-
Object
- Object
- QueryHelper::SqlFilter
- Defined in:
- lib/query_helper/sql_filter.rb
Instance Attribute Summary collapse
-
#column_maps ⇒ Object
Returns the value of attribute column_maps.
-
#filter_values ⇒ Object
Returns the value of attribute filter_values.
-
#options ⇒ Object
Returns the value of attribute options.
-
#qualify_filters ⇒ Object
Returns the value of attribute qualify_filters.
Instance Method Summary collapse
- #aggregated_attribute?(comparate:) ⇒ Boolean
- #bind_variables ⇒ Object
- #create_filters ⇒ Object
- #having_clauses ⇒ Object
-
#initialize(filter_values: [], column_maps: [], qualify_filters: [], options: {}) ⇒ SqlFilter
constructor
A new instance of SqlFilter.
- #qualify_clauses ⇒ Object
- #where_clauses ⇒ Object
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 = @qualify_filters = qualify_filters end |
Instance Attribute Details
#column_maps ⇒ Object
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_values ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/query_helper/sql_filter.rb', line 6 def @options end |
#qualify_filters ⇒ Object
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
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_variables ⇒ Object
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_filters ⇒ Object
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_clauses ⇒ Object
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_clauses ⇒ Object
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_clauses ⇒ Object
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 |