Class: Babik::QuerySet::AbstractAggregationFunction
- Inherits:
-
Object
- Object
- Babik::QuerySet::AbstractAggregationFunction
- Defined in:
- lib/babik/queryset/components/aggregation.rb
Overview
Abstract aggregation function. Do not use
Direct Known Subclasses
Avg, Count, CountDistinct, Max, Min, PostgresMySQLAggregationFunction, Sum
Instance Attribute Summary collapse
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#selection ⇒ Object
readonly
Returns the value of attribute selection.
Class Method Summary collapse
-
.db_adapter ⇒ String
Return the database adapter.
Instance Method Summary collapse
-
#initialize(aggregation_path) ⇒ AbstractAggregationFunction
constructor
Construct a aggregation function for a field.
-
#left_joins_by_alias ⇒ Hash{alias: Babik::QuerySet::Join}
Return the joins grouped by alias.
-
#prepare(model, field_name = nil) ⇒ Object
Prepare the aggregation function for a model class and a field.
-
#sql ⇒ String
Return aggregation function SQL.
Constructor Details
#initialize(aggregation_path) ⇒ AbstractAggregationFunction
Construct a aggregation function for a field
46 47 48 |
# File 'lib/babik/queryset/components/aggregation.rb', line 46 def initialize(aggregation_path) @aggregation_path = aggregation_path end |
Instance Attribute Details
#field_name ⇒ Object (readonly)
Returns the value of attribute field_name.
42 43 44 |
# File 'lib/babik/queryset/components/aggregation.rb', line 42 def field_name @field_name end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
42 43 44 |
# File 'lib/babik/queryset/components/aggregation.rb', line 42 def model @model end |
#selection ⇒ Object (readonly)
Returns the value of attribute selection.
42 43 44 |
# File 'lib/babik/queryset/components/aggregation.rb', line 42 def selection @selection end |
Class Method Details
Instance Method Details
#left_joins_by_alias ⇒ Hash{alias: Babik::QuerySet::Join}
Return the joins grouped by alias
71 72 73 |
# File 'lib/babik/queryset/components/aggregation.rb', line 71 def left_joins_by_alias @selection.left_joins_by_alias end |
#prepare(model, field_name = nil) ⇒ Object
Prepare the aggregation function for a model class and a field
54 55 56 57 58 59 |
# File 'lib/babik/queryset/components/aggregation.rb', line 54 def prepare(model, field_name = nil) @model = model @selection = Babik::Selection::Path::Factory.build(model, @aggregation_path) @field_name = field_name || "#{self.table_alias}__#{SQL_OPERATION.downcase}" self end |
#sql ⇒ String
Return aggregation function SQL
63 64 65 66 67 |
# File 'lib/babik/queryset/components/aggregation.rb', line 63 def sql selected_field_path = "#{@selection.target_alias}.#{@selection.selected_field}" operation = self.sql_operation.sub('?field', selected_field_path) "#{operation} AS #{@field_name}" end |