Class: Arel::Join

Inherits:
Object
  • Object
show all
Includes:
Relation
Defined in:
lib/arel/algebra/relations/operations/join.rb,
lib/arel/engines/memory/relations/operations.rb,
lib/arel/engines/sql/relations/operations/join.rb

Direct Known Subclasses

InnerJoin, OuterJoin, StringJoin

Instance Attribute Summary

Attributes included from Relation

#count

Instance Method Summary collapse

Methods included from Relation

#bind, #call, #christener, #compiler, #exclusion_predicate_sql, #externalize, #inclusion_predicate_sql, #primary_key, #session, #to_sql

Methods included from Relation::DefaultOperations

#groupings, #havings, #inserts, #locked, #orders, #projections, #skipped, #sources, #taken

Methods included from Relation::AttributeAccessable

#[], #find_attribute_matching_attribute, #find_attribute_matching_name, #position_of

Methods included from Relation::Operable

#alias, #join, #lock, #outer_join

Methods included from Relation::Operable::Writable

#delete, #insert, #update

Methods included from Relation::Enumerable

#each, #first

Constructor Details

#initialize(relation1, relation2 = Nil.instance, *predicates) ⇒ Join

Returns a new instance of Join.



9
10
11
# File 'lib/arel/algebra/relations/operations/join.rb', line 9

def initialize(relation1, relation2 = Nil.instance, *predicates)
  @relation1, @relation2, @predicates = relation1, relation2, predicates
end

Instance Method Details

#attributesObject



21
22
23
# File 'lib/arel/algebra/relations/operations/join.rb', line 21

def attributes
  @attributes ||= (relation1.externalize.attributes | relation2.externalize.attributes).bind(self)
end

#engineObject



43
44
45
# File 'lib/arel/algebra/relations/operations/join.rb', line 43

def engine
  relation1.engine != relation2.engine ? Memory::Engine.new : relation1.engine
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/arel/algebra/relations/operations/join.rb', line 17

def eql?(other)
  self == other
end

#evalObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/arel/engines/memory/relations/operations.rb', line 54

def eval
  result = []
  relation1.call.each do |row1|
    relation2.call.each do |row2|
      combined_row = row1.combine(row2, self)
      if predicates.all? { |p| p.eval(combined_row) }
        result << combined_row
      end
    end
  end
  result
end

#externalizable?Boolean

TESTME

Returns:

  • (Boolean)


35
36
37
# File 'lib/arel/algebra/relations/operations/join.rb', line 35

def externalizable?
  relation1.externalizable? or relation2.externalizable?
end

#hashObject



13
14
15
# File 'lib/arel/algebra/relations/operations/join.rb', line 13

def hash
  @hash ||= :relation1.hash
end

#join?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/arel/algebra/relations/operations/join.rb', line 39

def join?
  true
end

#joins(environment, formatter = Sql::TableReference.new(environment)) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/arel/engines/sql/relations/operations/join.rb', line 7

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.relation).to_sql(Sql::WhereClause.new(environment)) }.join(' AND ')
    ].compact.join(" ")
    [relation1.joins(environment), this_join, relation2.joins(environment)].compact.join(" ")
  end
end

#onsObject



30
31
32
# File 'lib/arel/algebra/relations/operations/join.rb', line 30

def ons
  @ons ||= @predicates.collect { |p| p.bind(self) }
end

#table_sql(formatter = Sql::TableReference.new(self)) ⇒ Object



3
4
5
# File 'lib/arel/engines/sql/relations/operations/join.rb', line 3

def table_sql(formatter = Sql::TableReference.new(self))
  relation1.externalize.table_sql(formatter)
end

#wheresObject



25
26
27
28
# File 'lib/arel/algebra/relations/operations/join.rb', line 25

def wheres
  # TESTME bind to self?
  relation1.externalize.wheres
end