From 04c8e48311231d0b8332a7050a48defc9d7b075e Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Sun, 13 Apr 2008 18:45:20 -0700 Subject: new conception of grouping and aggregation functionality --- lib/active_relation/primitives/attribute.rb | 10 ++++++---- lib/active_relation/primitives/expression.rb | 4 ++++ lib/active_relation/relations.rb | 2 +- lib/active_relation/relations/aggregation.rb | 24 ------------------------ lib/active_relation/relations/grouping.rb | 19 +++++++++++++++++++ lib/active_relation/relations/projection.rb | 4 ++++ lib/active_relation/relations/relation.rb | 10 ++-------- 7 files changed, 36 insertions(+), 37 deletions(-) delete mode 100644 lib/active_relation/relations/aggregation.rb create mode 100644 lib/active_relation/relations/grouping.rb (limited to 'lib') diff --git a/lib/active_relation/primitives/attribute.rb b/lib/active_relation/primitives/attribute.rb index d37271490a..5b1ef4d7c0 100644 --- a/lib/active_relation/primitives/attribute.rb +++ b/lib/active_relation/primitives/attribute.rb @@ -10,6 +10,10 @@ module ActiveRelation def alias_or_name @alias || name end + + def aggregation? + false + end module Transformations def as(aliaz = nil) @@ -56,10 +60,8 @@ module ActiveRelation end def %(other) - if other - (history - other.history) + (other.history - history) - else - history + if other then (history - other.history) + (other.history - history) + else history end end end diff --git a/lib/active_relation/primitives/expression.rb b/lib/active_relation/primitives/expression.rb index 2df2888ba0..ae4c61d1b1 100644 --- a/lib/active_relation/primitives/expression.rb +++ b/lib/active_relation/primitives/expression.rb @@ -29,6 +29,10 @@ module ActiveRelation "#{function_sql}(#{attribute.to_sql})" + (@alias ? " AS #{quote_column_name(@alias)}" : '') end + def aggregation? + true + end + def ==(other) self.class == other.class and attribute == other.attribute and diff --git a/lib/active_relation/relations.rb b/lib/active_relation/relations.rb index 9cede2a6d1..d8e211c853 100644 --- a/lib/active_relation/relations.rb +++ b/lib/active_relation/relations.rb @@ -4,7 +4,7 @@ require 'active_relation/relations/compound' require 'active_relation/relations/writing' require 'active_relation/relations/table' require 'active_relation/relations/join' -require 'active_relation/relations/aggregation' +require 'active_relation/relations/grouping' require 'active_relation/relations/projection' require 'active_relation/relations/selection' require 'active_relation/relations/order' diff --git a/lib/active_relation/relations/aggregation.rb b/lib/active_relation/relations/aggregation.rb deleted file mode 100644 index 62432408e1..0000000000 --- a/lib/active_relation/relations/aggregation.rb +++ /dev/null @@ -1,24 +0,0 @@ -module ActiveRelation - class Aggregation < Compound - attr_reader :expressions, :groupings - - def initialize(relation, options) - @relation, @expressions, @groupings = relation, options[:expressions], options[:groupings] - end - - def ==(other) - self.class == other.class and - relation == other.relation and - groupings == other.groupings and - expressions == other.expressions - end - - def attributes - expressions.collect { |e| e.bind(self) } - end - - def aggregation? - true - end - end -end \ No newline at end of file diff --git a/lib/active_relation/relations/grouping.rb b/lib/active_relation/relations/grouping.rb new file mode 100644 index 0000000000..57323e90f3 --- /dev/null +++ b/lib/active_relation/relations/grouping.rb @@ -0,0 +1,19 @@ +module ActiveRelation + class Grouping < Compound + attr_reader :expressions, :groupings + + def initialize(relation, *groupings) + @relation, @groupings = relation, groupings.collect { |g| g.bind(relation) } + end + + def ==(other) + self.class == other.class and + relation == other.relation and + groupings == other.groupings + end + + def aggregation? + true + end + end +end \ No newline at end of file diff --git a/lib/active_relation/relations/projection.rb b/lib/active_relation/relations/projection.rb index c1495c1742..0f8ea5175c 100644 --- a/lib/active_relation/relations/projection.rb +++ b/lib/active_relation/relations/projection.rb @@ -15,5 +15,9 @@ module ActiveRelation relation == other.relation and projections == other.projections end + + def aggregation? + attributes.any?(&:aggregation?) + end end end \ No newline at end of file diff --git a/lib/active_relation/relations/relation.rb b/lib/active_relation/relations/relation.rb index b62a5d9fb9..8ab266c53d 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -66,8 +66,8 @@ module ActiveRelation skipped.blank?? self : Skip.new(self, skipped) end - def aggregate(*expressions) - AggregateOperation.new(self, expressions) + def group(*groupings) + groupings.all?(&:blank?) ? self : Grouping.new(self, *groupings) end module Writes @@ -90,12 +90,6 @@ module ActiveRelation Join.new(join_sql, relation1, relation2, *predicates) end end - - AggregateOperation = Struct.new(:relation, :expressions) do - def group(*groupings) - Aggregation.new(relation, :expressions => expressions, :groupings => groupings) - end - end end include Operations -- cgit v1.2.3