class Babik::QuerySet::AbstractAggregationFunction

Abstract aggregation function. Do not use

Attributes

field_name[R]
model[R]
selection[R]

Public Class Methods

db_adapter() click to toggle source

Return the database adapter @return [String] Database adapter: 'mysql2', 'postgres' o 'sqlite'

# File lib/babik/queryset/components/aggregation.rb, line 77
def self.db_adapter
  Babik::Database.config[:adapter]
end
new(aggregation_path) click to toggle source

Construct a aggregation function for a field @param aggregation_path [String] Field or foreign field path.

# File lib/babik/queryset/components/aggregation.rb, line 46
def initialize(aggregation_path)
  @aggregation_path = aggregation_path
end

Public Instance Methods

left_joins_by_alias() click to toggle source

Return the joins grouped by alias @return [Hash{alias: Babik::QuerySet::Join}] Hash where the value is the alias of the table and the value is a Babik::Join

# 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) click to toggle source

Prepare the aggregation function for a model class and a field @param model [ActiveRecord::Base] model that will be used as origin for association paths. @param field_name [String, nil] Name that will take the computed aggregation operation.

If nil, it will take the value <table_alias>__<agg_function>.
# 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() click to toggle source

Return aggregation function SQL @return [String] Aggregation function SQL

# 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