From 559d9bbfe09034e807e5eae169bea26df780aa36 Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 21 Jan 2008 18:47:22 -0800 Subject: merging "schmoin" experiment with joining aggregations into regular "join" functionality; half-way done... --- lib/active_relation/relations/compound.rb | 2 +- lib/active_relation/relations/group.rb | 4 ++++ lib/active_relation/relations/join.rb | 18 +++++++++++++----- lib/active_relation/relations/relation.rb | 4 ++++ 4 files changed, 22 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/active_relation/relations/compound.rb b/lib/active_relation/relations/compound.rb index e870f12aff..a7595c9e3d 100644 --- a/lib/active_relation/relations/compound.rb +++ b/lib/active_relation/relations/compound.rb @@ -2,7 +2,7 @@ module ActiveRelation class Compound < Relation attr_reader :relation - delegate :projections, :attribute, :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, + delegate :projections, :attribute, :joins, :selects, :orders, :groupings, :table_sql, :inserts, :limit, :offset, :name, :alias, :aggregation?, :to => :relation end end \ No newline at end of file diff --git a/lib/active_relation/relations/group.rb b/lib/active_relation/relations/group.rb index bc363970c7..31de6f4bd8 100644 --- a/lib/active_relation/relations/group.rb +++ b/lib/active_relation/relations/group.rb @@ -13,5 +13,9 @@ module ActiveRelation def qualify Group.new(relation.qualify, *groupings.collect(&:qualify)) end + + def aggregation? + true + end end end \ No newline at end of file diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb index b624df8d72..d4cfe19239 100644 --- a/lib/active_relation/relations/join.rb +++ b/lib/active_relation/relations/join.rb @@ -26,18 +26,26 @@ module ActiveRelation relation1.send(:selects) + relation2.send(:selects) end + # this is magick!!! + def projections + relation1.send(:projections) + relation2.attributes + end + def attribute(name) relation1[name] || relation2[name] end - - protected - def projections - relation1.send(:projections) + relation2.send(:projections) - end private def join "#{join_sql} #{relation2.send(:table_sql)} ON #{predicates.collect { |p| p.to_sql(Sql::Predicate.new) }.join(' AND ')}" end + + def join + [join_sql, right_table, "ON", predicates.collect { |p| p.to_sql(Sql::Predicate.new) }.join(' AND ')].join(" ") + end + + def right_table + relation2.aggregation?? relation2.to_sql(Sql::Aggregation.new) : relation2.send(:table_sql) + 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 f1febc497c..cb981eb3de 100644 --- a/lib/active_relation/relations/relation.rb +++ b/lib/active_relation/relations/relation.rb @@ -80,6 +80,10 @@ module ActiveRelation def attributes projections.collect(&:to_attribute) end + + def aggregation? + false + end def to_sql(strategy = Sql::Select.new) strategy.select [ -- cgit v1.2.3