Class: ActiveRecord::Refined::AST::Aggregate
- Includes:
- Arithmetics, Predications, Windowing
- Defined in:
- lib/active_record/refined/ast.rb
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#distinct ⇒ Object
readonly
Returns the value of attribute distinct.
-
#function ⇒ Object
readonly
Returns the value of attribute function.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
Instance Method Summary collapse
-
#filter(condition = nil, &block) ⇒ Object
FILTER (WHERE ...): the aggregate is taken over the rows the condition holds for.
-
#initialize(operand, function, distinct: false, condition: nil) ⇒ Aggregate
constructor
A new instance of Aggregate.
- #to_arel(table, model) ⇒ Object
Methods included from Windowing
Methods included from Arithmetics
#&, #*, #+, #-, #/, #<<, #>>, #^, #|, #~
Methods included from Predications
#!=, #!~, #<, #<=, #==, #=~, #>, #>=, #between?, #bury, #casecmp?, #contains?, #dig, #dig_json, #distinct_from?, #end_with?, #false?, #ilike?, #in?, #include?, #intersect?, #key?, #like?, #member?, #not_between?, #not_distinct_from?, #not_false?, #not_ilike?, #not_in?, #not_like?, #not_null?, #not_true?, #null?, #start_with?, #subset?, #superset?, #true?, #when
Methods inherited from Node
Constructor Details
#initialize(operand, function, distinct: false, condition: nil) ⇒ Aggregate
Returns a new instance of Aggregate.
925 926 927 928 929 930 931 932 933 934 935 936 |
# File 'lib/active_record/refined/ast.rb', line 925 def initialize(operand, function, distinct: false, condition: nil) if distinct && function != :count raise ArgumentError, "#{function} does not take distinct" end if distinct && operand == :* raise ArgumentError, "count(:*) does not take distinct; name a column" end @operand = operand @function = function @distinct = distinct @condition = condition end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
923 924 925 |
# File 'lib/active_record/refined/ast.rb', line 923 def condition @condition end |
#distinct ⇒ Object (readonly)
Returns the value of attribute distinct.
923 924 925 |
# File 'lib/active_record/refined/ast.rb', line 923 def distinct @distinct end |
#function ⇒ Object (readonly)
Returns the value of attribute function.
923 924 925 |
# File 'lib/active_record/refined/ast.rb', line 923 def function @function end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
923 924 925 |
# File 'lib/active_record/refined/ast.rb', line 923 def operand @operand end |
Instance Method Details
#filter(condition = nil, &block) ⇒ Object
FILTER (WHERE ...): the aggregate is taken over the rows the
condition holds for. A value or a block, as when takes them.
940 941 942 943 |
# File 'lib/active_record/refined/ast.rb', line 940 def filter(condition = nil, &block) Aggregate.new(operand, function, distinct: distinct, condition: Case.argument(:filter, condition, block)) end |
#to_arel(table, model) ⇒ Object
945 946 947 948 949 950 951 952 953 954 955 956 957 958 |
# File 'lib/active_record/refined/ast.rb', line 945 def to_arel(table, model) return aggregate(operand, table, model) unless condition # MySQL has no FILTER clause. An aggregate passes over a NULL, so # the case that yields nothing for the rows the condition misses is # the same aggregate over the same rows -- count(*) has no operand to # keep, and counts a 1 instead. if AST.adapter_family(model) == :mysql kept = Case.new.when(condition).then(operand == :* ? 1 : operand) return aggregate(kept, table, model) end aggregate(operand, table, model).filter(condition.to_arel(table, model)) end |