Methods

Class Index [+]

Quicksearch

Arel::Visitors::ToSql

Attributes

last_column[RW]

Public Class Methods

new(connection) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 9
 9:       def initialize connection
10:         @connection     = connection
11:         @schema_cache   = connection.schema_cache
12:         @quoted_tables  = {}
13:         @quoted_columns = {}
14:         @last_column    = nil
15:       end

Public Instance Methods

accept(object) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 17
17:       def accept object
18:         self.last_column = nil
19:         super
20:       end

Private Instance Methods

build_subselect(key, o) click to toggle source

FIXME: we should probably have a 2-pass visitor for this

    # File lib/arel/visitors/to_sql.rb, line 31
31:       def build_subselect key, o
32:         stmt             = Nodes::SelectStatement.new
33:         core             = stmt.cores.first
34:         core.froms       = o.relation
35:         core.wheres      = o.wheres
36:         core.projections = [key]
37:         stmt.limit       = o.limit
38:         stmt.orders      = o.orders
39:         stmt
40:       end
column_cache() click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 104
104:       def column_cache
105:         @schema_cache.columns_hash
106:       end
column_for(attr) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 95
 95:       def column_for attr
 96:         name    = attr.name.to_s
 97:         table   = attr.relation.table_name
 98: 
 99:         return nil unless table_exists? table
100: 
101:         column_cache[table][name]
102:       end
literal(o;) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 375
375:       def literal o; o end
quote(value, column = nil) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 415
415:       def quote value, column = nil
416:         @connection.quote value, column
417:       end
quote_column_name(name) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 424
424:       def quote_column_name name
425:         @quoted_columns[name] ||= Arel::Nodes::SqlLiteral === name ? name : @connection.quote_column_name(name)
426:       end
quote_table_name(name) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 419
419:       def quote_table_name name
420:         return name if Arel::Nodes::SqlLiteral === name
421:         @quoted_tables[name] ||= @connection.quote_table_name(name)
422:       end
quoted(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 383
383:       def quoted o
384:         quote(o, last_column)
385:       end
table_exists?(name) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 91
91:       def table_exists? name
92:         @schema_cache.table_exists? name
93:       end
visit_ActiveSupport_Multibyte_Chars(o) click to toggle source
Alias for: quoted
visit_ActiveSupport_StringInquirer(o) click to toggle source
Alias for: quoted
visit_Arel_Attributes_Attribute(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 363
363:       def visit_Arel_Attributes_Attribute o
364:         self.last_column = column_for o
365:         join_name = o.relation.table_alias || o.relation.name
366:         "#{quote_table_name join_name}.#{quote_column_name o.name}"
367:       end
visit_Arel_Attributes_Boolean(o) click to toggle source
visit_Arel_Attributes_Decimal(o) click to toggle source
visit_Arel_Attributes_Float(o) click to toggle source
visit_Arel_Attributes_Integer(o) click to toggle source
visit_Arel_Attributes_String(o) click to toggle source
visit_Arel_Attributes_Time(o) click to toggle source
visit_Arel_Nodes_Addition(o) click to toggle source
visit_Arel_Nodes_And(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 322
322:       def visit_Arel_Nodes_And o
323:         o.children.map { |x| visit x }.join ' AND '
324:       end
visit_Arel_Nodes_As(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 355
355:       def visit_Arel_Nodes_As o
356:         "#{visit o.left} AS #{visit o.right}"
357:       end
visit_Arel_Nodes_Ascending(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 203
203:       def visit_Arel_Nodes_Ascending o
204:         "#{visit o.expr} ASC"
205:       end
visit_Arel_Nodes_Assignment(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 330
330:       def visit_Arel_Nodes_Assignment o
331:         right = quote(o.right, column_for(o.left))
332:         "#{visit o.left} = #{right}"
333:       end
visit_Arel_Nodes_Avg(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 242
242:       def visit_Arel_Nodes_Avg o
243:         "AVG(#{o.expressions.map { |x|
244:           visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
245:       end
visit_Arel_Nodes_Between(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 251
251:       def visit_Arel_Nodes_Between o
252:         "#{visit o.left} BETWEEN #{visit o.right}"
253:       end
visit_Arel_Nodes_Bin(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 142
142:       def visit_Arel_Nodes_Bin o
143:         visit o.expr
144:       end
visit_Arel_Nodes_BindParam(o;) click to toggle source
Alias for: literal
visit_Arel_Nodes_Count(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 221
221:       def visit_Arel_Nodes_Count o
222:         "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
223:           visit x
224:         }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
225:       end
visit_Arel_Nodes_DeleteStatement(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 23
23:       def visit_Arel_Nodes_DeleteStatement o
24:         [
25:           "DELETE FROM #{visit o.relation}",
26:           ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?)
27:         ].compact.join ' '
28:       end
visit_Arel_Nodes_Descending(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 207
207:       def visit_Arel_Nodes_Descending o
208:         "#{visit o.expr} DESC"
209:       end
visit_Arel_Nodes_Distinct(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 146
146:       def visit_Arel_Nodes_Distinct o
147:         'DISTINCT'
148:       end
visit_Arel_Nodes_DistinctOn(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 150
150:       def visit_Arel_Nodes_DistinctOn o
151:         raise NotImplementedError, 'DISTINCT ON not implemented for this db'
152:       end
visit_Arel_Nodes_Division(o) click to toggle source
visit_Arel_Nodes_DoesNotMatch(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 275
275:       def visit_Arel_Nodes_DoesNotMatch o
276:         "#{visit o.left} NOT LIKE #{visit o.right}"
277:       end
visit_Arel_Nodes_Equality(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 335
335:       def visit_Arel_Nodes_Equality o
336:         right = o.right
337: 
338:         if right.nil?
339:           "#{visit o.left} IS NULL"
340:         else
341:           "#{visit o.left} = #{visit right}"
342:         end
343:       end
visit_Arel_Nodes_Except(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 174
174:       def visit_Arel_Nodes_Except o
175:         "( #{visit o.left} EXCEPT #{visit o.right} )"
176:       end
visit_Arel_Nodes_Exists(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 78
78:       def visit_Arel_Nodes_Exists o
79:         "EXISTS (#{visit o.expressions})#{
80:           o.alias ? " AS #{visit o.alias}" : ''}"
81:       end
visit_Arel_Nodes_False(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 87
87:       def visit_Arel_Nodes_False o
88:         "FALSE"
89:       end
visit_Arel_Nodes_GreaterThan(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 259
259:       def visit_Arel_Nodes_GreaterThan o
260:         "#{visit o.left} > #{visit o.right}"
261:       end
visit_Arel_Nodes_GreaterThanOrEqual(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 255
255:       def visit_Arel_Nodes_GreaterThanOrEqual o
256:         "#{visit o.left} >= #{visit o.right}"
257:       end
visit_Arel_Nodes_Group(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 211
211:       def visit_Arel_Nodes_Group o
212:         visit o.expr
213:       end
visit_Arel_Nodes_Grouping(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 199
199:       def visit_Arel_Nodes_Grouping o
200:         "(#{visit o.expr})"
201:       end
visit_Arel_Nodes_Having(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 178
178:       def visit_Arel_Nodes_Having o
179:         "HAVING #{visit o.expr}"
180:       end
visit_Arel_Nodes_In(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 314
314:       def visit_Arel_Nodes_In o
315:         "#{visit o.left} IN (#{visit o.right})"
316:       end
visit_Arel_Nodes_InfixOperation(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 402
402:       def visit_Arel_Nodes_InfixOperation o
403:         "#{visit o.left} #{o.operator} #{visit o.right}"
404:       end
visit_Arel_Nodes_InnerJoin(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 294
294:       def visit_Arel_Nodes_InnerJoin o
295:         "INNER JOIN #{visit o.left} #{visit o.right if o.right}"
296:       end
visit_Arel_Nodes_InsertStatement(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 66
66:       def visit_Arel_Nodes_InsertStatement o
67:         [
68:           "INSERT INTO #{visit o.relation}",
69: 
70:           ("(#{o.columns.map { |x|
71:           quote_column_name x.name
72:         }.join ', '})" unless o.columns.empty?),
73: 
74:           (visit o.values if o.values),
75:         ].compact.join ' '
76:       end
visit_Arel_Nodes_Intersect(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 170
170:       def visit_Arel_Nodes_Intersect o
171:         "( #{visit o.left} INTERSECT #{visit o.right} )"
172:       end
visit_Arel_Nodes_JoinSource(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 279
279:       def visit_Arel_Nodes_JoinSource o
280:         [
281:           (visit(o.left) if o.left),
282:           o.right.map { |j| visit j }.join(' ')
283:         ].compact.join ' '
284:       end
visit_Arel_Nodes_LessThan(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 267
267:       def visit_Arel_Nodes_LessThan o
268:         "#{visit o.left} < #{visit o.right}"
269:       end
visit_Arel_Nodes_LessThanOrEqual(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 263
263:       def visit_Arel_Nodes_LessThanOrEqual o
264:         "#{visit o.left} <= #{visit o.right}"
265:       end
visit_Arel_Nodes_Limit(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 186
186:       def visit_Arel_Nodes_Limit o
187:         "LIMIT #{visit o.expr}"
188:       end
visit_Arel_Nodes_Lock(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 195
195:       def visit_Arel_Nodes_Lock o
196:         visit o.expr
197:       end
visit_Arel_Nodes_Matches(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 271
271:       def visit_Arel_Nodes_Matches o
272:         "#{visit o.left} LIKE #{visit o.right}"
273:       end
visit_Arel_Nodes_Max(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 232
232:       def visit_Arel_Nodes_Max o
233:         "MAX(#{o.expressions.map { |x|
234:           visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
235:       end
visit_Arel_Nodes_Min(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 237
237:       def visit_Arel_Nodes_Min o
238:         "MIN(#{o.expressions.map { |x|
239:           visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
240:       end
visit_Arel_Nodes_Multiplication(o) click to toggle source
visit_Arel_Nodes_NamedFunction(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 215
215:       def visit_Arel_Nodes_NamedFunction o
216:         "#{o.name}(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x|
217:           visit x
218:         }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
219:       end
visit_Arel_Nodes_Not(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 302
302:       def visit_Arel_Nodes_Not o
303:         "NOT (#{visit o.expr})"
304:       end
visit_Arel_Nodes_NotEqual(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 345
345:       def visit_Arel_Nodes_NotEqual o
346:         right = o.right
347: 
348:         if right.nil?
349:           "#{visit o.left} IS NOT NULL"
350:         else
351:           "#{visit o.left} != #{visit right}"
352:         end
353:       end
visit_Arel_Nodes_NotIn(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 318
318:       def visit_Arel_Nodes_NotIn o
319:         "#{visit o.left} NOT IN (#{visit o.right})"
320:       end
visit_Arel_Nodes_Offset(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 182
182:       def visit_Arel_Nodes_Offset o
183:         "OFFSET #{visit o.expr}"
184:       end
visit_Arel_Nodes_On(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 298
298:       def visit_Arel_Nodes_On o
299:         "ON #{visit o.expr}"
300:       end
visit_Arel_Nodes_Or(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 326
326:       def visit_Arel_Nodes_Or o
327:         "#{visit o.left} OR #{visit o.right}"
328:       end
visit_Arel_Nodes_OuterJoin(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 290
290:       def visit_Arel_Nodes_OuterJoin o
291:         "LEFT OUTER JOIN #{visit o.left} #{visit o.right}"
292:       end
visit_Arel_Nodes_SelectCore(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 129
129:       def visit_Arel_Nodes_SelectCore o
130:         [
131:           "SELECT",
132:           (visit(o.top) if o.top),
133:           (visit(o.set_quantifier) if o.set_quantifier),
134:           ("#{o.projections.map { |x| visit x }.join ', '}" unless o.projections.empty?),
135:           ("FROM #{visit(o.source)}" if o.source && !o.source.empty?),
136:           ("WHERE #{o.wheres.map { |x| visit x }.join ' AND ' }" unless o.wheres.empty?),
137:           ("GROUP BY #{o.groups.map { |x| visit x }.join ', ' }" unless o.groups.empty?),
138:           (visit(o.having) if o.having),
139:         ].compact.join ' '
140:       end
visit_Arel_Nodes_SelectStatement(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 118
118:       def visit_Arel_Nodes_SelectStatement o
119:         [
120:           (visit(o.with) if o.with),
121:           o.cores.map { |x| visit_Arel_Nodes_SelectCore x }.join,
122:           ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?),
123:           (visit(o.limit) if o.limit),
124:           (visit(o.offset) if o.offset),
125:           (visit(o.lock) if o.lock),
126:         ].compact.join ' '
127:       end
visit_Arel_Nodes_SqlLiteral(o;) click to toggle source
Alias for: literal
visit_Arel_Nodes_StringJoin(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 286
286:       def visit_Arel_Nodes_StringJoin o
287:         visit o.left
288:       end
visit_Arel_Nodes_Subtraction(o) click to toggle source
visit_Arel_Nodes_Sum(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 227
227:       def visit_Arel_Nodes_Sum o
228:         "SUM(#{o.expressions.map { |x|
229:           visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}"
230:       end
visit_Arel_Nodes_TableAlias(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 247
247:       def visit_Arel_Nodes_TableAlias o
248:         "#{visit o.relation} #{quote_table_name o.name}"
249:       end
visit_Arel_Nodes_Top(o) click to toggle source

FIXME: this does nothing on most databases, but does on MSSQL

     # File lib/arel/visitors/to_sql.rb, line 191
191:       def visit_Arel_Nodes_Top o
192:         ""
193:       end
visit_Arel_Nodes_True(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 83
83:       def visit_Arel_Nodes_True o
84:         "TRUE"
85:       end
visit_Arel_Nodes_Union(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 162
162:       def visit_Arel_Nodes_Union o
163:         "( #{visit o.left} UNION #{visit o.right} )"
164:       end
visit_Arel_Nodes_UnionAll(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 166
166:       def visit_Arel_Nodes_UnionAll o
167:         "( #{visit o.left} UNION ALL #{visit o.right} )"
168:       end
visit_Arel_Nodes_UnqualifiedColumn(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 359
359:       def visit_Arel_Nodes_UnqualifiedColumn o
360:         "#{quote_column_name o.name}"
361:       end
visit_Arel_Nodes_UpdateStatement(o) click to toggle source
    # File lib/arel/visitors/to_sql.rb, line 42
42:       def visit_Arel_Nodes_UpdateStatement o
43:         if o.orders.empty? && o.limit.nil?
44:           wheres = o.wheres
45:         else
46:           key = o.key
47:           unless key
48:             warn((#{caller.first}) Using UpdateManager without setting UpdateManager#key isdeprecated and support will be removed in ARel 4.0.0.  Please set the primarykey on UpdateManager using UpdateManager#key=) if $VERBOSE
49:             key = o.relation.primary_key
50:           end
51: 
52:           wheres = [Nodes::In.new(key, [build_subselect(key, o)])]
53:         end
54: 
55:         [
56:           "UPDATE #{visit o.relation}",
57:           ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?),
58:           ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?),
59:         ].compact.join ' '
60:       end
visit_Arel_Nodes_Values(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 108
108:       def visit_Arel_Nodes_Values o
109:         "VALUES (#{o.expressions.zip(o.columns).map { |value, attr|
110:           if Nodes::SqlLiteral === value
111:             visit value
112:           else
113:             quote(value, attr && column_for(attr))
114:           end
115:         }.join ', '})"
116:       end
visit_Arel_Nodes_With(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 154
154:       def visit_Arel_Nodes_With o
155:         "WITH #{o.children.map { |x| visit x }.join(', ')}"
156:       end
visit_Arel_Nodes_WithRecursive(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 158
158:       def visit_Arel_Nodes_WithRecursive o
159:         "WITH RECURSIVE #{o.children.map { |x| visit x }.join(', ')}"
160:       end
visit_Arel_SqlLiteral(o;) click to toggle source
Alias for: literal
visit_Arel_Table(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 306
306:       def visit_Arel_Table o
307:         if o.table_alias
308:           "#{quote_table_name o.name} #{quote_table_name o.table_alias}"
309:         else
310:           quote_table_name o.name
311:         end
312:       end
visit_Array(o) click to toggle source
     # File lib/arel/visitors/to_sql.rb, line 411
411:       def visit_Array o
412:         o.empty? ? 'NULL' : o.map { |x| visit x }.join(', ')
413:       end
visit_BigDecimal(o) click to toggle source
Alias for: quoted
visit_Bignum(o;) click to toggle source
Alias for: literal
visit_Class(o) click to toggle source
Alias for: quoted
visit_Date(o) click to toggle source
Alias for: quoted
visit_DateTime(o) click to toggle source
Alias for: quoted
visit_FalseClass(o) click to toggle source
Alias for: quoted
visit_Fixnum(o;) click to toggle source
Alias for: literal
visit_Float(o) click to toggle source
Alias for: quoted
visit_Hash(o) click to toggle source
Alias for: quoted
visit_NilClass(o) click to toggle source
Alias for: quoted
visit_String(o) click to toggle source
Alias for: quoted
visit_Symbol(o) click to toggle source
Alias for: quoted
visit_Time(o) click to toggle source
Alias for: quoted
visit_TrueClass(o) click to toggle source
Alias for: quoted

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.