Class: Arel::SelectManager

Inherits:
TreeManager show all
Includes:
Crud
Defined in:
lib/arel/select_manager.rb

Constant Summary collapse

STRING_OR_SYMBOL_CLASS =
[Symbol, String]

Instance Attribute Summary

Attributes inherited from TreeManager

#ast

Instance Method Summary collapse

Methods included from Crud

#compile_delete, #compile_insert, #compile_update, #create_insert

Methods inherited from TreeManager

#to_dot, #to_sql

Methods included from FactoryMethods

#cast, #coalesce, #create_and, #create_false, #create_join, #create_on, #create_string_join, #create_table_alias, #create_true, #grouping, #lower

Constructor Details

#initialize(table = nil) ⇒ SelectManager

Returns a new instance of SelectManager.



9
10
11
12
# File 'lib/arel/select_manager.rb', line 9

def initialize(table = nil)
  @ast = Nodes::SelectStatement.new(table)
  @ctx = @ast.cores.last
end

Instance Method Details

#as(other) ⇒ Object



48
49
50
# File 'lib/arel/select_manager.rb', line 48

def as(other)
  create_table_alias grouping(@ast), Nodes::SqlLiteral.new(other)
end

#comment(*values) ⇒ Object



252
253
254
255
# File 'lib/arel/select_manager.rb', line 252

def comment(*values)
  @ctx.comment = Nodes::Comment.new(values)
  self
end

#constraintsObject



24
25
26
# File 'lib/arel/select_manager.rb', line 24

def constraints
  @ctx.wheres
end

#distinct(value = true) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/arel/select_manager.rb', line 154

def distinct(value = true)
  if value
    @ctx.set_quantifier = Arel::Nodes::Distinct.new
  else
    @ctx.set_quantifier = nil
  end
  self
end

#distinct_on(value) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/arel/select_manager.rb', line 163

def distinct_on(value)
  if value
    @ctx.set_quantifier = Arel::Nodes::DistinctOn.new(value)
  else
    @ctx.set_quantifier = nil
  end
  self
end

#except(other) ⇒ Object Also known as: minus



213
214
215
# File 'lib/arel/select_manager.rb', line 213

def except(other)
  Nodes::Except.new ast, other.ast
end

#existsObject

Produces an Arel::Nodes::Exists node



44
45
46
# File 'lib/arel/select_manager.rb', line 44

def exists
  Arel::Nodes::Exists.new @ast
end

#from(table) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/arel/select_manager.rb', line 85

def from(table)
  table = Nodes::SqlLiteral.new(table) if String === table

  case table
  when Nodes::Join
    @ctx.source.right << table
  else
    @ctx.source.left = table
  end

  self
end

#fromsObject



98
99
100
# File 'lib/arel/select_manager.rb', line 98

def froms
  @ast.cores.filter_map { |x| x.from }
end

#group(*columns) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/arel/select_manager.rb', line 74

def group(*columns)
  columns.each do |column|
    # FIXME: backwards compat
    column = Nodes::SqlLiteral.new(column) if String === column
    column = Nodes::SqlLiteral.new(column.to_s) if Symbol === column

    @ctx.groups.push Nodes::Group.new column
  end
  self
end

#having(expr) ⇒ Object



119
120
121
122
# File 'lib/arel/select_manager.rb', line 119

def having(expr)
  @ctx.havings << expr
  self
end

#initialize_copy(other) ⇒ Object



14
15
16
17
# File 'lib/arel/select_manager.rb', line 14

def initialize_copy(other)
  super
  @ctx = @ast.cores.last
end

#intersect(other) ⇒ Object



209
210
211
# File 'lib/arel/select_manager.rb', line 209

def intersect(other)
  Nodes::Intersect.new ast, other.ast
end

#join(relation, klass = Nodes::InnerJoin) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arel/select_manager.rb', line 102

def join(relation, klass = Nodes::InnerJoin)
  return self unless relation

  case relation
  when String, Nodes::SqlLiteral
    raise EmptyJoinError if relation.empty?
    klass = Nodes::StringJoin
  end

  @ctx.source.right << create_join(relation, nil, klass)
  self
end

#join_sourcesObject



244
245
246
# File 'lib/arel/select_manager.rb', line 244

def join_sources
  @ctx.source.right
end

#lateral(table_name = nil) ⇒ Object



218
219
220
221
# File 'lib/arel/select_manager.rb', line 218

def lateral(table_name = nil)
  base = table_name.nil? ? ast : as(table_name)
  Nodes::Lateral.new(base)
end

#limitObject Also known as: taken



19
20
21
# File 'lib/arel/select_manager.rb', line 19

def limit
  @ast.limit && @ast.limit.expr
end

#lock(locking = Arel.sql("FOR UPDATE")) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/arel/select_manager.rb', line 52

def lock(locking = Arel.sql("FOR UPDATE"))
  case locking
  when true
    locking = Arel.sql("FOR UPDATE")
  when Arel::Nodes::SqlLiteral
  when String
    locking = Arel.sql locking
  end

  @ast.lock = Nodes::Lock.new(locking)
  self
end

#lockedObject



65
66
67
# File 'lib/arel/select_manager.rb', line 65

def locked
  @ast.lock
end

#offsetObject



28
29
30
# File 'lib/arel/select_manager.rb', line 28

def offset
  @ast.offset && @ast.offset.expr
end

#on(*exprs) ⇒ Object



69
70
71
72
# File 'lib/arel/select_manager.rb', line 69

def on(*exprs)
  @ctx.source.right.last.right = Nodes::On.new(collapse(exprs))
  self
end

#optimizer_hints(*hints) ⇒ Object



147
148
149
150
151
152
# File 'lib/arel/select_manager.rb', line 147

def optimizer_hints(*hints)
  unless hints.empty?
    @ctx.optimizer_hints = Arel::Nodes::OptimizerHints.new(hints)
  end
  self
end

#order(*expr) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/arel/select_manager.rb', line 172

def order(*expr)
  # FIXME: We SHOULD NOT be converting these to SqlLiteral automatically
  @ast.orders.concat expr.map { |x|
    STRING_OR_SYMBOL_CLASS.include?(x.class) ? Nodes::SqlLiteral.new(x.to_s) : x
  }
  self
end

#ordersObject



180
181
182
# File 'lib/arel/select_manager.rb', line 180

def orders
  @ast.orders
end

#outer_join(relation) ⇒ Object



115
116
117
# File 'lib/arel/select_manager.rb', line 115

def outer_join(relation)
  join(relation, Nodes::OuterJoin)
end

#project(*projections) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/arel/select_manager.rb', line 130

def project(*projections)
  # FIXME: converting these to SQLLiterals is probably not good, but
  # rails tests require it.
  @ctx.projections.concat projections.map { |x|
    STRING_OR_SYMBOL_CLASS.include?(x.class) ? Nodes::SqlLiteral.new(x.to_s) : x
  }
  self
end

#projectionsObject



139
140
141
# File 'lib/arel/select_manager.rb', line 139

def projections
  @ctx.projections
end

#projections=(projections) ⇒ Object



143
144
145
# File 'lib/arel/select_manager.rb', line 143

def projections=(projections)
  @ctx.projections = projections
end

#skip(amount) ⇒ Object Also known as: offset=



32
33
34
35
36
37
38
39
# File 'lib/arel/select_manager.rb', line 32

def skip(amount)
  if amount
    @ast.offset = Nodes::Offset.new(amount)
  else
    @ast.offset = nil
  end
  self
end

#sourceObject



248
249
250
# File 'lib/arel/select_manager.rb', line 248

def source
  @ctx.source
end

#take(limit) ⇒ Object Also known as: limit=



234
235
236
237
238
239
240
241
# File 'lib/arel/select_manager.rb', line 234

def take(limit)
  if limit
    @ast.limit = Nodes::Limit.new(limit)
  else
    @ast.limit = nil
  end
  self
end

#union(operation, other = nil) ⇒ Object



198
199
200
201
202
203
204
205
206
207
# File 'lib/arel/select_manager.rb', line 198

def union(operation, other = nil)
  if other
    node_class = Nodes.const_get("Union#{operation.to_s.capitalize}")
  else
    other = operation
    node_class = Nodes::Union
  end

  node_class.new self.ast, other.ast
end

#where(expr) ⇒ Object



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

def where(expr)
  if Arel::TreeManager === expr
    expr = expr.ast
  end
  @ctx.wheres << expr
  self
end

#where_sql(engine = Table.engine) ⇒ Object



192
193
194
195
196
# File 'lib/arel/select_manager.rb', line 192

def where_sql(engine = Table.engine)
  return if @ctx.wheres.empty?

  Nodes::SqlLiteral.new("WHERE #{Nodes::And.new(@ctx.wheres).to_sql(engine)}")
end

#window(name) ⇒ Object



124
125
126
127
128
# File 'lib/arel/select_manager.rb', line 124

def window(name)
  window = Nodes::NamedWindow.new(name)
  @ctx.windows.push window
  window
end

#with(*subqueries) ⇒ Object



223
224
225
226
227
228
229
230
231
232
# File 'lib/arel/select_manager.rb', line 223

def with(*subqueries)
  if subqueries.first.is_a? Symbol
    node_class = Nodes.const_get("With#{subqueries.shift.to_s.capitalize}")
  else
    node_class = Nodes::With
  end
  @ast.with = node_class.new(subqueries.flatten)

  self
end