diff options
Diffstat (limited to 'lib/arel/relations/operations/join.rb')
-rw-r--r-- | lib/arel/relations/operations/join.rb | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/lib/arel/relations/operations/join.rb b/lib/arel/relations/operations/join.rb new file mode 100644 index 0000000000..acad75c817 --- /dev/null +++ b/lib/arel/relations/operations/join.rb @@ -0,0 +1,62 @@ +module Arel + class Join < Relation + attr_reader :join_sql, :relation1, :relation2, :predicates + delegate :engine, :name, :to => :relation1 + hash_on :relation1 + + def initialize(join_sql, relation1, relation2 = Nil.new, *predicates) + @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates + end + + def table_sql(formatter = Sql::TableReference.new(self)) + relation1.externalize.table_sql(formatter) + end + + def joins(environment, formatter = Sql::TableReference.new(environment)) + @joins ||= begin + this_join = [ + join_sql, + relation2.externalize.table_sql(formatter), + ("ON" unless predicates.blank?), + (ons + relation2.externalize.wheres).collect { |p| p.bind(environment).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ') + ].compact.join(" ") + [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ") + end + end + + def attributes + @attributes ||= (relation1.externalize.attributes + + relation2.externalize.attributes).collect { |a| a.bind(self) } + end + + def wheres + relation1.externalize.wheres + end + + def ons + @ons ||= @predicates.collect { |p| p.bind(self) } + end + + # TESTME + def aggregation? + relation1.aggregation? or relation2.aggregation? + end + + def join? + true + end + + def ==(other) + Join === other and + predicates == other.predicates and + relation1 == other.relation1 and + relation2 == other.relation2 + end + end + + class Relation + def join? + false + end + end +end
\ No newline at end of file |