Class: ActiveRecord::Refined::AST::Aggregate

Inherits:
Node
  • Object
show all
Includes:
Arithmetics, Predications, Windowing
Defined in:
lib/active_record/refined/ast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Windowing

#over

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

#as, #asc, #desc

Constructor Details

#initialize(operand, function, distinct: false, condition: nil) ⇒ Aggregate

Returns a new instance of Aggregate.



921
922
923
924
925
926
927
928
929
930
931
932
# File 'lib/active_record/refined/ast.rb', line 921

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

#conditionObject (readonly)

Returns the value of attribute condition.



919
920
921
# File 'lib/active_record/refined/ast.rb', line 919

def condition
  @condition
end

#distinctObject (readonly)

Returns the value of attribute distinct.



919
920
921
# File 'lib/active_record/refined/ast.rb', line 919

def distinct
  @distinct
end

#functionObject (readonly)

Returns the value of attribute function.



919
920
921
# File 'lib/active_record/refined/ast.rb', line 919

def function
  @function
end

#operandObject (readonly)

Returns the value of attribute operand.



919
920
921
# File 'lib/active_record/refined/ast.rb', line 919

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.



936
937
938
939
# File 'lib/active_record/refined/ast.rb', line 936

def filter(condition = nil, &block)
  Aggregate.new(operand, function, distinct: distinct,
                condition: Case.argument(:filter, condition, block))
end

#to_arel(table, model) ⇒ Object



941
942
943
944
945
946
947
948
949
950
951
952
953
954
# File 'lib/active_record/refined/ast.rb', line 941

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