Class: Babik::QuerySet::AbstractAggregationFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/babik/queryset/components/aggregation.rb

Overview

Abstract aggregation function. Do not use

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aggregation_path) ⇒ AbstractAggregationFunction

Construct a aggregation function for a field

Parameters:

  • aggregation_path (String)

    Field or foreign field path.



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_nameObject (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

#modelObject (readonly)

Returns the value of attribute model.



42
43
44
# File 'lib/babik/queryset/components/aggregation.rb', line 42

def model
  @model
end

#selectionObject (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

.db_adapterString

Return the database adapter

Returns:

  • (String)

    Database adapter: ‘mysql2’, ‘postgres’ o ‘sqlite’



77
78
79
# File 'lib/babik/queryset/components/aggregation.rb', line 77

def self.db_adapter
  Babik::Database.config[:adapter]
end

Instance Method Details

#left_joins_by_aliasHash{alias: Babik::QuerySet::Join}

Return the joins grouped by alias

Returns:

  • (Hash{alias: Babik::QuerySet::Join})

    Hash where the value is the alias of the table and the value is a Babik::Join



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

Parameters:

  • model (ActiveRecord::Base)

    model that will be used as origin for association paths.

  • field_name (String, nil) (defaults to: nil)

    Name that will take the computed aggregation operation. If nil, it will take the value <table_alias>__<agg_function>.



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

#sqlString

Return aggregation function SQL

Returns:

  • (String)

    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