Class: Arel::Join

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

Direct Known Subclasses

InnerJoin, OuterJoin, StringJoin

Instance Attribute Summary collapse

Attributes included from Relation

#count

Instance Method Summary collapse

Methods included from Relation

#[], #alias, #bind, #call, #christener, #compiler, #delete, #each, #exclusion_predicate_sql, #externalize, #find_attribute_matching_name, #from, #from_clauses, #group_clauses, #groupings, #having_clauses, #havings, #inclusion_predicate_sql, #insert, #inserts, #join, #lock, #locked, #order_clauses, #orders, #outer_join, #position_of, #primary_key, #project, #projections, #select_clauses, #session, #skip, #skipped, #sources, #take, #taken, #update, #where, #where_clauses

Constructor Details

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

Returns a new instance of Join.



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

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

Instance Attribute Details

#predicatesObject (readonly)

Returns the value of attribute predicates.



5
6
7
# File 'lib/arel/algebra/relations/operations/join.rb', line 5

def predicates
  @predicates
end

#relation1Object (readonly)

Returns the value of attribute relation1.



5
6
7
# File 'lib/arel/algebra/relations/operations/join.rb', line 5

def relation1
  @relation1
end

#relation2Object (readonly)

Returns the value of attribute relation2.



5
6
7
# File 'lib/arel/algebra/relations/operations/join.rb', line 5

def relation2
  @relation2
end

Instance Method Details

#attributesObject



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

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

#engineObject



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

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

#evalObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arel/algebra/relations/operations/join.rb', line 60

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)


32
33
34
# File 'lib/arel/algebra/relations/operations/join.rb', line 32

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

#join?Boolean

Returns:

  • (Boolean)


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

def join?
  true
end

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



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/arel/algebra/relations/operations/join.rb', line 48

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

#nameObject



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

def name
  relation1.name
end

#onsObject



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

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

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



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

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

#to_sql(formatter = nil) ⇒ Object



73
74
75
# File 'lib/arel/algebra/relations/operations/join.rb', line 73

def to_sql(formatter = nil)
  compiler.select_sql
end

#wheresObject



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

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