Module: Arel::Relation

Includes:
Enumerable
Included in:
Array, Compound, Join, Nil, Table
Defined in:
lib/arel/algebra/relations/relation.rb

Defined Under Namespace

Classes: JoinOperation

Constant Summary collapse

@@connection_tables_primary_keys =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/arel/algebra/relations/relation.rb', line 7

def count
  @count
end

Instance Method Details

#[](index) ⇒ Object



176
177
178
# File 'lib/arel/algebra/relations/relation.rb', line 176

def [](index)
  attributes[index]
end

#aliasObject



154
155
156
# File 'lib/arel/algebra/relations/relation.rb', line 154

def alias
  Alias.new(self)
end

#attributesObject



192
# File 'lib/arel/algebra/relations/relation.rb', line 192

def attributes;             Header.new  end

#bind(relation) ⇒ Object



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

def bind(relation)
  self
end

#callObject



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

def call
  engine.read(self)
end

#christenerObject



48
49
50
# File 'lib/arel/algebra/relations/relation.rb', line 48

def christener
  @christener ||= Sql::Christener.new
end

#compilerObject



33
34
35
36
37
38
39
# File 'lib/arel/algebra/relations/relation.rb', line 33

def compiler
  @compiler ||=  begin
    Arel::SqlCompiler.const_get("#{engine.adapter_name}Compiler").new(self)
  rescue
    Arel::SqlCompiler::GenericCompiler.new(self)
  end
end

#deleteObject



166
167
168
# File 'lib/arel/algebra/relations/relation.rb', line 166

def delete
  session.delete Deletion.new(self)
end

#eachObject



101
102
103
# File 'lib/arel/algebra/relations/relation.rb', line 101

def each
  session.read(self).each { |e| yield e }
end

#exclusion_predicate_sqlObject



56
57
58
# File 'lib/arel/algebra/relations/relation.rb', line 56

def exclusion_predicate_sql
  "NOT IN"
end

#externalizable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/arel/algebra/relations/relation.rb', line 29

def externalizable?
  false
end

#externalizeObject



25
26
27
# File 'lib/arel/algebra/relations/relation.rb', line 25

def externalize
  @externalized ||= externalizable?? Externalization.new(self) : self
end

#find_attribute_matching_name(name) ⇒ Object



180
181
182
# File 'lib/arel/algebra/relations/relation.rb', line 180

def find_attribute_matching_name(name)
  attributes.detect { |a| a.named?(name) } || Attribute.new(self, name)
end

#from(thing) ⇒ Object



146
147
148
# File 'lib/arel/algebra/relations/relation.rb', line 146

def from thing
  From.new self, thing
end

#from_clausesObject



81
82
83
# File 'lib/arel/algebra/relations/relation.rb', line 81

def from_clauses
  sources.empty? ? table_sql : sources
end

#group_clausesObject



89
90
91
# File 'lib/arel/algebra/relations/relation.rb', line 89

def group_clauses
  groupings.collect { |g| g.to_sql(Sql::GroupClause.new(self)) }
end

#groupingsObject



197
# File 'lib/arel/algebra/relations/relation.rb', line 197

def groupings;              []          end

#having_clausesObject



93
94
95
# File 'lib/arel/algebra/relations/relation.rb', line 93

def having_clauses
  havings.collect { |g| g.to_sql(Sql::HavingClause.new(self)) }
end

#havingsObject



198
# File 'lib/arel/algebra/relations/relation.rb', line 198

def havings;                []          end

#inclusion_predicate_sqlObject



52
53
54
# File 'lib/arel/algebra/relations/relation.rb', line 52

def inclusion_predicate_sql
  "IN"
end

#insert(record) ⇒ Object



158
159
160
# File 'lib/arel/algebra/relations/relation.rb', line 158

def insert(record)
  session.create Insert.new(self, record)
end

#insertsObject



196
# File 'lib/arel/algebra/relations/relation.rb', line 196

def inserts;                []          end

#join(other_relation = nil, join_class = InnerJoin) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/arel/algebra/relations/relation.rb', line 105

def join(other_relation = nil, join_class = InnerJoin)
  case other_relation
  when String
    StringJoin.new(self, other_relation)
  when Relation
    JoinOperation.new(join_class, self, other_relation)
  else
    self
  end
end

#join?Boolean

Returns:

  • (Boolean)


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

def join?
  false
end

#joins(formatter = nil) ⇒ Object

FIXME



199
# File 'lib/arel/algebra/relations/relation.rb', line 199

def joins(formatter = nil); nil         end

#lock(locking = true) ⇒ Object



150
151
152
# File 'lib/arel/algebra/relations/relation.rb', line 150

def lock(locking = true)
  Lock.new(self, locking)
end

#lockedObject



203
# File 'lib/arel/algebra/relations/relation.rb', line 203

def locked;                 []          end

#order_clausesObject



97
98
99
# File 'lib/arel/algebra/relations/relation.rb', line 97

def order_clauses
  orders.collect { |o| o.to_sql(Sql::OrderClause.new(self)) }
end

#ordersObject



195
# File 'lib/arel/algebra/relations/relation.rb', line 195

def orders;                 []          end

#outer_join(other_relation = nil) ⇒ Object



116
117
118
# File 'lib/arel/algebra/relations/relation.rb', line 116

def outer_join(other_relation = nil)
  join(other_relation, OuterJoin)
end

#position_of(attribute) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/arel/algebra/relations/relation.rb', line 184

def position_of(attribute)
  @position_of ||= {}

  return @position_of[attribute] if @position_of.key? attribute

  @position_of[attribute] = attributes.index(attributes[attribute])
end

#primary_keyObject



60
61
62
63
64
65
66
67
68
# File 'lib/arel/algebra/relations/relation.rb', line 60

def primary_key
  connection_id = engine.connection.object_id
  if @@connection_tables_primary_keys[connection_id] && @@connection_tables_primary_keys[connection_id].has_key?(table.name)
    @@connection_tables_primary_keys[connection_id][table.name]
  else
    @@connection_tables_primary_keys[connection_id] ||= {}
    @@connection_tables_primary_keys[connection_id][table.name] = engine.connection.primary_key(table.name)
  end
end

#project(*args) ⇒ Object



130
131
132
# File 'lib/arel/algebra/relations/relation.rb', line 130

def project *args
  args.empty? ? self : Project.new(self, args)
end

#projectionsObject



193
# File 'lib/arel/algebra/relations/relation.rb', line 193

def projections;            []          end

#select_clausesObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/arel/algebra/relations/relation.rb', line 70

def select_clauses
  attributes.map { |a|
    case a
    when Value
      a.value
    else
      a.to_sql(Sql::SelectClause.new(self))
    end
  }
end

#sessionObject



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

def session
  Session.instance
end

#skip(thing = nil) ⇒ Object



138
139
140
# File 'lib/arel/algebra/relations/relation.rb', line 138

def skip thing = nil
  thing ? Skip.new(self, thing) : self
end

#skippedObject



201
# File 'lib/arel/algebra/relations/relation.rb', line 201

def skipped;                nil         end

#sourcesObject



202
# File 'lib/arel/algebra/relations/relation.rb', line 202

def sources;                []          end

#take(count) ⇒ Object



142
143
144
# File 'lib/arel/algebra/relations/relation.rb', line 142

def take count
  Take.new self, count
end

#takenObject



200
# File 'lib/arel/algebra/relations/relation.rb', line 200

def taken;                  nil         end

#to_sql(formatter = nil) ⇒ Object



41
42
43
44
45
46
# File 'lib/arel/algebra/relations/relation.rb', line 41

def to_sql(formatter = nil)
  sql = compiler.select_sql

  return sql unless formatter
  formatter.select sql, self
end

#update(assignments) ⇒ Object



162
163
164
# File 'lib/arel/algebra/relations/relation.rb', line 162

def update(assignments)
  session.update Update.new(self, assignments)
end

#where(clause = nil) ⇒ Object



134
135
136
# File 'lib/arel/algebra/relations/relation.rb', line 134

def where clause = nil
  clause ? Where.new(self, [clause].flatten) : self
end

#where_clausesObject



85
86
87
# File 'lib/arel/algebra/relations/relation.rb', line 85

def where_clauses
  wheres.map { |w| w.value }
end

#wheresObject



194
# File 'lib/arel/algebra/relations/relation.rb', line 194

def wheres;                 []          end