1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
module ActiveRelation
class Join < Relation
attr_reader :join_sql, :relation1, :relation2, :predicates
def initialize(join_sql, relation1, relation2, *predicates)
@join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates
end
def ==(other)
self.class == other.class and
predicates == other.predicates and (
(relation1 == other.relation1 and relation2 == other.relation2) or
(relation2 == other.relation1 and relation1 == other.relation2)
)
end
def qualify
descend(&:qualify)
end
def attributes
[
relation1.attributes.collect(&:to_attribute),
relation2.attributes.collect(&:to_attribute),
].flatten.collect { |a| a.bind(self) }
end
def prefix_for(attribute)
relation1.aliased_prefix_for(attribute) or
relation2.aliased_prefix_for(attribute)
end
alias_method :aliased_prefix_for, :prefix_for
def descend(&block)
Join.new(join_sql, relation1.descend(&block), relation2.descend(&block), *predicates.collect(&block))
end
protected
def joins
right_table_sql = relation2.aggregation?? relation2.to_sql(Sql::Aggregation.new) : relation2.send(:table_sql)
this_join = [join_sql, right_table_sql, "ON", predicates.collect { |p| p.bind(self).to_sql(Sql::Predicate.new) }.join(' AND ')].join(" ")
[relation1.joins, relation2.joins, this_join].compact.join(" ")
end
def selects
[
(relation1.send(:selects) unless relation1.aggregation?),
(relation2.send(:selects) unless relation2.aggregation?)
].compact.flatten
end
def table_sql
relation1.aggregation?? relation1.to_sql(Sql::Aggregation.new) : relation1.send(:table_sql)
end
end
end
|