diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/arel/collectors/sql_string.rb | 28 | ||||
-rw-r--r-- | lib/arel/nodes/node.rb | 6 | ||||
-rw-r--r-- | lib/arel/select_manager.rb | 4 | ||||
-rw-r--r-- | lib/arel/tree_manager.rb | 6 | ||||
-rw-r--r-- | lib/arel/visitors/bind_substitute.rb | 9 | ||||
-rw-r--r-- | lib/arel/visitors/bind_visitor.rb | 12 | ||||
-rw-r--r-- | lib/arel/visitors/dot.rb | 3 | ||||
-rw-r--r-- | lib/arel/visitors/mysql.rb | 61 | ||||
-rw-r--r-- | lib/arel/visitors/oracle.rb | 47 | ||||
-rw-r--r-- | lib/arel/visitors/postgresql.rb | 21 | ||||
-rw-r--r-- | lib/arel/visitors/reduce.rb | 25 | ||||
-rw-r--r-- | lib/arel/visitors/sqlite.rb | 5 | ||||
-rw-r--r-- | lib/arel/visitors/to_sql.rb | 555 | ||||
-rw-r--r-- | lib/arel/visitors/where_sql.rb | 5 |
14 files changed, 524 insertions, 263 deletions
diff --git a/lib/arel/collectors/sql_string.rb b/lib/arel/collectors/sql_string.rb new file mode 100644 index 0000000000..824b0a1712 --- /dev/null +++ b/lib/arel/collectors/sql_string.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 + +module Arel + module Collectors + class SQLString + def initialize + @str = '' + end + + def value + @str + end + + def << str + @str << str + self + end + + def start; self; end + def finish; self; end + + def add_bind bind + self << bind + self + end + end + end +end diff --git a/lib/arel/nodes/node.rb b/lib/arel/nodes/node.rb index 36e7628612..239c4fd766 100644 --- a/lib/arel/nodes/node.rb +++ b/lib/arel/nodes/node.rb @@ -1,3 +1,5 @@ +require 'arel/collectors/sql_string' + module Arel module Nodes ### @@ -42,7 +44,9 @@ module Arel # # Maybe we should just use `Table.engine`? :'( def to_sql engine = Table.engine - engine.connection.visitor.accept self + collector = Arel::Collectors::SQLString.new + collector = engine.connection.visitor.accept self, collector + collector.value end # Iterate through AST, nodes will be yielded depth-first diff --git a/lib/arel/select_manager.rb b/lib/arel/select_manager.rb index a3bfa9a230..fe0d26a16e 100644 --- a/lib/arel/select_manager.rb +++ b/lib/arel/select_manager.rb @@ -1,3 +1,5 @@ +require 'arel/collectors/sql_string' + module Arel class SelectManager < Arel::TreeManager include Arel::Crud @@ -167,7 +169,7 @@ module Arel return if @ctx.wheres.empty? viz = Visitors::WhereSql.new @engine.connection - Nodes::SqlLiteral.new viz.accept @ctx + Nodes::SqlLiteral.new viz.accept(@ctx, Collectors::SQLString.new).value end def union operation, other = nil diff --git a/lib/arel/tree_manager.rb b/lib/arel/tree_manager.rb index 1adb230991..87887800d0 100644 --- a/lib/arel/tree_manager.rb +++ b/lib/arel/tree_manager.rb @@ -1,3 +1,5 @@ +require 'arel/collectors/sql_string' + module Arel class TreeManager include Arel::FactoryMethods @@ -21,7 +23,9 @@ module Arel end def to_sql - visitor.accept @ast + collector = Arel::Collectors::SQLString.new + collector = visitor.accept @ast, collector + collector.value end def initialize_copy other diff --git a/lib/arel/visitors/bind_substitute.rb b/lib/arel/visitors/bind_substitute.rb new file mode 100644 index 0000000000..0503a9c986 --- /dev/null +++ b/lib/arel/visitors/bind_substitute.rb @@ -0,0 +1,9 @@ +module Arel + module Visitors + class BindSubstitute + def initialize delegte + @delegate = delegate + end + end + end +end diff --git a/lib/arel/visitors/bind_visitor.rb b/lib/arel/visitors/bind_visitor.rb index 5cb251ffde..c316c8d702 100644 --- a/lib/arel/visitors/bind_visitor.rb +++ b/lib/arel/visitors/bind_visitor.rb @@ -6,24 +6,26 @@ module Arel super end - def accept node, &block + def accept node, collector, &block @block = block if block_given? super end private - def visit_Arel_Nodes_Assignment o + def visit_Arel_Nodes_Assignment o, collector if o.right.is_a? Arel::Nodes::BindParam - "#{visit o.left} = #{visit o.right}" + collector = visit o.left, collector + collector << " = " + visit o.right, collector else super end end - def visit_Arel_Nodes_BindParam o + def visit_Arel_Nodes_BindParam o, collector if @block - @block.call + @block.call(collector) else super end diff --git a/lib/arel/visitors/dot.rb b/lib/arel/visitors/dot.rb index 99f4c467d2..ba35223ac9 100644 --- a/lib/arel/visitors/dot.rb +++ b/lib/arel/visitors/dot.rb @@ -23,11 +23,12 @@ module Arel end def accept object - super + visit object to_dot end private + def visit_Arel_Nodes_Ordering o visit_edge o, "expr" end diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index 3b911e826f..cf590598e1 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -2,25 +2,31 @@ module Arel module Visitors class MySQL < Arel::Visitors::ToSql private - def visit_Arel_Nodes_Union o, suppress_parens = false - left_result = case o.left + def visit_Arel_Nodes_Union o, collector, suppress_parens = false + unless suppress_parens + collector << "( " + end + + collector = case o.left when Arel::Nodes::Union - visit_Arel_Nodes_Union o.left, true + visit_Arel_Nodes_Union o.left, collector, true else - visit o.left + visit o.left, collector end - right_result = case o.right + collector << " UNION " + + collector = case o.right when Arel::Nodes::Union - visit_Arel_Nodes_Union o.right, true + visit_Arel_Nodes_Union o.right, collector, true else - visit o.right + visit o.right, collector end if suppress_parens - "#{left_result} UNION #{right_result}" + collector else - "( #{left_result} UNION #{right_result} )" + collector << " )" end end @@ -31,26 +37,43 @@ module Arel ### # :'( # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 - def visit_Arel_Nodes_SelectStatement o + def visit_Arel_Nodes_SelectStatement o, collector if o.offset && !o.limit o.limit = Arel::Nodes::Limit.new(Nodes.build_quoted(18446744073709551615)) end super end - def visit_Arel_Nodes_SelectCore o + def visit_Arel_Nodes_SelectCore o, collector o.froms ||= Arel.sql('DUAL') super end - def visit_Arel_Nodes_UpdateStatement o - [ - "UPDATE #{visit o.relation}", - ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), - ("WHERE #{o.wheres.map { |x| visit x }.join ' AND '}" unless o.wheres.empty?), - ("ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?), - (visit(o.limit) if o.limit), - ].compact.join ' ' + def visit_Arel_Nodes_UpdateStatement o, collector + collector << "UPDATE " + collector = visit o.relation, collector + + unless o.values.empty? + collector << "SET " + collector = inject_join o.values, collector, ', ' + end + + unless o.wheres.empty? + collector << "SET " + collector = inject_join o.wheres, collector, ' AND ' + end + + unless o.orders.empty? + collector << "ORDER BY " + collector = inject_join o.wheres, collector, ', ' + end + + if o.limit + collector << " " + visit(o.limit, collector) + else + collector + end end end diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb index 0cd0179931..2cdbafadad 100644 --- a/lib/arel/visitors/oracle.rb +++ b/lib/arel/visitors/oracle.rb @@ -3,7 +3,7 @@ module Arel class Oracle < Arel::Visitors::ToSql private - def visit_Arel_Nodes_SelectStatement o + def visit_Arel_Nodes_SelectStatement o, collector o = order_hacks(o) # if need to select first records without ORDER BY and GROUP BY and without DISTINCT @@ -20,49 +20,58 @@ module Arel limit = o.limit.expr.to_i offset = o.offset o.offset = nil - sql = super(o) - return <<-eosql + collector << " SELECT * FROM ( SELECT raw_sql_.*, rownum raw_rnum_ - FROM (#{sql}) raw_sql_ + FROM (" + + collector = super(o, collector) + collector << ") raw_sql_ WHERE rownum <= #{offset.expr.to_i + limit} ) - WHERE #{visit offset} - eosql + WHERE " + return visit(offset, collector) end if o.limit o = o.dup limit = o.limit.expr - return "SELECT * FROM (#{super(o)}) WHERE ROWNUM <= #{visit limit}" + collector << "SELECT * FROM (" + collector = super(o, collector) + collector << ") WHERE ROWNUM <= " + return visit limit, collector end if o.offset o = o.dup offset = o.offset o.offset = nil - sql = super(o) - return <<-eosql - SELECT * FROM ( + collector << "SELECT * FROM ( SELECT raw_sql_.*, rownum raw_rnum_ - FROM (#{sql}) raw_sql_ + FROM (" + collector = super(o, collector) + collector << ") raw_sql_ ) - WHERE #{visit offset} - eosql + WHERE " + return visit offset, collector end super end - def visit_Arel_Nodes_Limit o + def visit_Arel_Nodes_Limit o, collector + collector end - def visit_Arel_Nodes_Offset o - "raw_rnum_ > #{visit o.expr}" + def visit_Arel_Nodes_Offset o, collector + collector << "raw_rnum_ > " + visit o.expr, collector end - def visit_Arel_Nodes_Except o - "( #{visit o.left } MINUS #{visit o.right} )" + def visit_Arel_Nodes_Except o, collector + collector << "( " + collector = infix_value o, collector, " MINUS " + collector << " )" end def visit_Arel_Nodes_UpdateStatement o @@ -91,7 +100,7 @@ module Arel # # orders = o.orders.map { |x| visit x }.join(', ').split(',') orders = o.orders.map do |x| - string = visit x + string = visit(x, Arel::Collectors::SQLString.new).value if string.include?(',') split_order_string(string) else diff --git a/lib/arel/visitors/postgresql.rb b/lib/arel/visitors/postgresql.rb index 49f7482e7d..60878ddd20 100644 --- a/lib/arel/visitors/postgresql.rb +++ b/lib/arel/visitors/postgresql.rb @@ -3,24 +3,25 @@ module Arel class PostgreSQL < Arel::Visitors::ToSql private - def visit_Arel_Nodes_Matches o - "#{visit o.left} ILIKE #{visit o.right}" + def visit_Arel_Nodes_Matches o, collector + infix_value o, collector, ' ILIKE ' end - def visit_Arel_Nodes_DoesNotMatch o - "#{visit o.left} NOT ILIKE #{visit o.right}" + def visit_Arel_Nodes_DoesNotMatch o, collector + infix_value o, collector, ' NOT ILIKE ' end - def visit_Arel_Nodes_Regexp o - "#{visit o.left} ~ #{visit o.right}" + def visit_Arel_Nodes_Regexp o, collector + infix_value o, collector, ' ~ ' end - def visit_Arel_Nodes_NotRegexp o - "#{visit o.left} !~ #{visit o.right}" + def visit_Arel_Nodes_NotRegexp o, collector + infix_value o, collector, ' !~ ' end - def visit_Arel_Nodes_DistinctOn o - "DISTINCT ON ( #{visit o.expr} )" + def visit_Arel_Nodes_DistinctOn o, collector + collector << "DISTINCT ON ( " + visit(o.expr, collector) << " )" end end end diff --git a/lib/arel/visitors/reduce.rb b/lib/arel/visitors/reduce.rb new file mode 100644 index 0000000000..9670cad27c --- /dev/null +++ b/lib/arel/visitors/reduce.rb @@ -0,0 +1,25 @@ +require 'arel/visitors/visitor' + +module Arel + module Visitors + class Reduce < Arel::Visitors::Visitor + def accept object, collector + visit object, collector + end + + private + + def visit object, collector + send dispatch[object.class], object, collector + rescue NoMethodError => e + raise e if respond_to?(dispatch[object.class], true) + superklass = object.class.ancestors.find { |klass| + respond_to?(dispatch[klass], true) + } + raise(TypeError, "Cannot visit #{object.class}") unless superklass + dispatch[object.class] = dispatch[superklass] + retry + end + end + end +end diff --git a/lib/arel/visitors/sqlite.rb b/lib/arel/visitors/sqlite.rb index 2a509e95b5..ff6fc1fea4 100644 --- a/lib/arel/visitors/sqlite.rb +++ b/lib/arel/visitors/sqlite.rb @@ -4,10 +4,11 @@ module Arel private # Locks are not supported in SQLite - def visit_Arel_Nodes_Lock o + def visit_Arel_Nodes_Lock o, collector + collector end - def visit_Arel_Nodes_SelectStatement o + def visit_Arel_Nodes_SelectStatement o, collector o.limit = Arel::Nodes::Limit.new(-1) if o.offset && !o.limit super end diff --git a/lib/arel/visitors/to_sql.rb b/lib/arel/visitors/to_sql.rb index 84034829a5..8e254d504b 100644 --- a/lib/arel/visitors/to_sql.rb +++ b/lib/arel/visitors/to_sql.rb @@ -1,9 +1,10 @@ require 'bigdecimal' require 'date' +require 'arel/visitors/reduce' module Arel module Visitors - class ToSql < Arel::Visitors::Visitor + class ToSql < Arel::Visitors::Reduce ## # This is some roflscale crazy stuff. I'm roflscaling this because # building SQL queries is a hotspot. I will explain the roflscale so that @@ -66,11 +67,15 @@ module Arel private - def visit_Arel_Nodes_DeleteStatement o - [ - "DELETE FROM #{visit o.relation}", - ("WHERE #{o.wheres.map { |x| visit x }.join AND}" unless o.wheres.empty?) - ].compact.join ' ' + def visit_Arel_Nodes_DeleteStatement o, collector + collector << "DELETE FROM " + collector = visit o.relation, collector + if o.wheres.any? + collector << " WHERE " + inject_join o.wheres, collector, AND + else + collector + end end # FIXME: we should probably have a 2-pass visitor for this @@ -85,18 +90,29 @@ module Arel stmt end - def visit_Arel_Nodes_UpdateStatement o + def visit_Arel_Nodes_UpdateStatement o, collector if o.orders.empty? && o.limit.nil? wheres = o.wheres else wheres = [Nodes::In.new(o.key, [build_subselect(o.key, o)])] end - [ - "UPDATE #{visit o.relation}", - ("SET #{o.values.map { |value| visit value }.join ', '}" unless o.values.empty?), - ("WHERE #{wheres.map { |x| visit x }.join ' AND '}" unless wheres.empty?), - ].compact.join ' ' + collector << "UPDATE " + collector = visit o.relation, collector + values = false + unless o.values.empty? + values = true + collector << " SET " + collector = inject_join o.values, collector, ", " + end + + unless wheres.empty? + collector << " " if values + collector << "WHERE " + collector = inject_join wheres, collector, " AND " + end + + collector end def visit_Arel_Nodes_InsertStatement o @@ -111,25 +127,31 @@ module Arel ].compact.join ' ' end - def visit_Arel_Nodes_Exists o - "EXISTS (#{visit o.expressions})#{ - o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Exists o, collector + collector << "EXISTS (" + collector = visit(o.expressions, collector) << ")" + if o.alias + collector << " AS " + visit o.alias, collector + else + collector + end end - def visit_Arel_Nodes_Casted o - quoted o.val, o.attribute + def visit_Arel_Nodes_Casted o, collector + collector << quoted(o.val, o.attribute).to_s end - def visit_Arel_Nodes_Quoted o - quoted o.expr, nil + def visit_Arel_Nodes_Quoted o, collector + collector << quoted(o.expr, nil).to_s end - def visit_Arel_Nodes_True o - "TRUE" + def visit_Arel_Nodes_True o, collector + collector << "TRUE" end - def visit_Arel_Nodes_False o - "FALSE" + def visit_Arel_Nodes_False o, collector + collector << "FALSE" end def table_exists? name @@ -160,92 +182,104 @@ module Arel }.join ', '})" end - def visit_Arel_Nodes_SelectStatement o - str = '' - + def visit_Arel_Nodes_SelectStatement o, collector if o.with - str << visit(o.with) - str << SPACE + collector = visit o.with, collector + collector << SPACE end - o.cores.each { |x| str << visit_Arel_Nodes_SelectCore(x) } + collector = o.cores.inject(collector) { |c,x| + visit_Arel_Nodes_SelectCore(x, c) + } unless o.orders.empty? - str << SPACE - str << ORDER_BY + collector << SPACE + collector << ORDER_BY len = o.orders.length - 1 o.orders.each_with_index { |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i } end - str << " #{visit(o.limit)}" if o.limit - str << " #{visit(o.offset)}" if o.offset - str << " #{visit(o.lock)}" if o.lock + collector = maybe_visit o.limit, collector + collector = maybe_visit o.offset, collector + collector = maybe_visit o.lock, collector - str.strip! - str + collector end - def visit_Arel_Nodes_SelectCore o - str = "SELECT" + def visit_Arel_Nodes_SelectCore o, collector + collector << "SELECT" - str << " #{visit(o.top)}" if o.top - str << " #{visit(o.set_quantifier)}" if o.set_quantifier + if o.top + collector << " " + collector = visit o.top, collector + end + + if o.set_quantifier + collector << " " + collector = visit o.set_quantifier, collector + end unless o.projections.empty? - str << SPACE + collector << SPACE len = o.projections.length - 1 o.projections.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str << " FROM #{visit(o.source)}" if o.source && !o.source.empty? + if o.source && !o.source.empty? + collector << " FROM " + collector = visit o.source, collector + end unless o.wheres.empty? - str << WHERE + collector << WHERE len = o.wheres.length - 1 o.wheres.each_with_index do |x, i| - str << visit(x) - str << AND unless len == i + collector = visit(x, collector) + collector << AND unless len == i end end unless o.groups.empty? - str << GROUP_BY + collector << GROUP_BY len = o.groups.length - 1 o.groups.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str << " #{visit(o.having)}" if o.having + if o.having + collector << " " + collector = visit(o.having, collector) + end unless o.windows.empty? - str << WINDOW + collector << WINDOW len = o.windows.length - 1 o.windows.each_with_index do |x, i| - str << visit(x) - str << COMMA unless len == i + collector = visit(x, collector) + collector << COMMA unless len == i end end - str + collector end def visit_Arel_Nodes_Bin o visit o.expr end - def visit_Arel_Nodes_Distinct o - DISTINCT + def visit_Arel_Nodes_Distinct o, collector + collector << DISTINCT end - def visit_Arel_Nodes_DistinctOn o + def visit_Arel_Nodes_DistinctOn o, collector raise NotImplementedError, 'DISTINCT ON not implemented for this db' end @@ -253,64 +287,91 @@ module Arel "WITH #{o.children.map { |x| visit x }.join(', ')}" end - def visit_Arel_Nodes_WithRecursive o - "WITH RECURSIVE #{o.children.map { |x| visit x }.join(', ')}" + def visit_Arel_Nodes_WithRecursive o, collector + collector << "WITH RECURSIVE " + inject_join o.children, collector, ', ' end - def visit_Arel_Nodes_Union o - "( #{visit o.left} UNION #{visit o.right} )" + def visit_Arel_Nodes_Union o, collector + collector << "( " + infix_value(o, collector, " UNION ") << " )" end - def visit_Arel_Nodes_UnionAll o - "( #{visit o.left} UNION ALL #{visit o.right} )" + def visit_Arel_Nodes_UnionAll o, collector + collector << "( " + infix_value(o, collector, " UNION ALL ") << " )" end - def visit_Arel_Nodes_Intersect o - "( #{visit o.left} INTERSECT #{visit o.right} )" + def visit_Arel_Nodes_Intersect o, collector + collector << "( " + infix_value(o, collector, " INTERSECT ") << " )" end - def visit_Arel_Nodes_Except o - "( #{visit o.left} EXCEPT #{visit o.right} )" + def visit_Arel_Nodes_Except o, collector + collector << "( " + infix_value(o, collector, " EXCEPT ") << " )" end - def visit_Arel_Nodes_NamedWindow o - "#{quote_column_name o.name} AS #{visit_Arel_Nodes_Window o}" + def visit_Arel_Nodes_NamedWindow o, collector + collector << quote_column_name(o.name) + collector << " AS " + visit_Arel_Nodes_Window o, collector end - def visit_Arel_Nodes_Window o - s = [ - ("ORDER BY #{o.orders.map { |x| visit(x) }.join(', ')}" unless o.orders.empty?), - (visit o.framing if o.framing) - ].compact.join ' ' - "(#{s})" + def visit_Arel_Nodes_Window o, collector + collector << "(" + if o.orders.any? + collector << "ORDER BY " + collector = inject_join o.orders, collector, ", " + end + + if o.framing + collector = visit o.framing, collector + end + + collector << ")" end - def visit_Arel_Nodes_Rows o + def visit_Arel_Nodes_Rows o, collector if o.expr - "ROWS #{visit o.expr}" + collector << "ROWS " + visit o.expr, collector else - "ROWS" + collector << "ROWS" end end - def visit_Arel_Nodes_Range o + def visit_Arel_Nodes_Range o, collector if o.expr - "RANGE #{visit o.expr}" + collector << "RANGE " + visit o.expr, collector else - "RANGE" + collector << "RANGE" end end - def visit_Arel_Nodes_Preceding o - "#{o.expr ? visit(o.expr) : 'UNBOUNDED'} PRECEDING" + def visit_Arel_Nodes_Preceding o, collector + collector = if o.expr + visit o.expr, collector + else + collector << "UNBOUNDED" + end + + collector << " PRECEDING" end - def visit_Arel_Nodes_Following o - "#{o.expr ? visit(o.expr) : 'UNBOUNDED'} FOLLOWING" + def visit_Arel_Nodes_Following o, collector + collector = if o.expr + visit o.expr, collector + else + collector << "UNBOUNDED" + end + + collector << " FOLLOWING" end - def visit_Arel_Nodes_CurrentRow o - "CURRENT ROW" + def visit_Arel_Nodes_CurrentRow o, collector + collector << "CURRENT ROW" end def visit_Arel_Nodes_Over o @@ -326,236 +387,287 @@ module Arel end end - def visit_Arel_Nodes_Having o - "HAVING #{visit o.expr}" + def visit_Arel_Nodes_Having o, collector + collector << "HAVING " + visit o.expr, collector end - def visit_Arel_Nodes_Offset o - "OFFSET #{visit o.expr}" + def visit_Arel_Nodes_Offset o, collector + collector << "OFFSET " + visit o.expr, collector end - def visit_Arel_Nodes_Limit o - "LIMIT #{visit o.expr}" + def visit_Arel_Nodes_Limit o, collector + collector << "LIMIT " + visit o.expr, collector end # FIXME: this does nothing on most databases, but does on MSSQL - def visit_Arel_Nodes_Top o - "" + def visit_Arel_Nodes_Top o, collector + collector end - def visit_Arel_Nodes_Lock o - visit o.expr + def visit_Arel_Nodes_Lock o, collector + visit o.expr, collector end - def visit_Arel_Nodes_Grouping o - "(#{visit o.expr})" + def visit_Arel_Nodes_Grouping o, collector + collector << "(" + visit(o.expr, collector) << ")" end - def visit_Arel_SelectManager o - "(#{o.to_sql.rstrip})" + def visit_Arel_SelectManager o, collector + collector << "(#{o.to_sql.rstrip})" end - def visit_Arel_Nodes_Ascending o - "#{visit o.expr} ASC" + def visit_Arel_Nodes_Ascending o, collector + visit(o.expr, collector) << " ASC" end - def visit_Arel_Nodes_Descending o - "#{visit o.expr} DESC" + def visit_Arel_Nodes_Descending o, collector + visit(o.expr, collector) << " DESC" end - def visit_Arel_Nodes_Group o - visit o.expr + def visit_Arel_Nodes_Group o, collector + visit o.expr, collector end - def visit_Arel_Nodes_NamedFunction o - "#{o.name}(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x - }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_NamedFunction o, collector + collector << o.name + collector << "(" + collector << "DISTINCT " if o.distinct + collector = inject_join(o.expressions, collector, ", ") << ")" + if o.alias + collector << " AS " + visit o.alias, collector + else + collector + end end def visit_Arel_Nodes_Extract o "EXTRACT(#{o.field.to_s.upcase} FROM #{visit o.expr})#{o.alias ? " AS #{visit o.alias}" : ''}" end - def visit_Arel_Nodes_Count o - "COUNT(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x - }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Count o, collector + aggregate "COUNT", o, collector end - def visit_Arel_Nodes_Sum o - "SUM(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x}.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Sum o, collector + aggregate "SUM", o, collector end - def visit_Arel_Nodes_Max o - "MAX(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x}.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Max o, collector + aggregate "MAX", o, collector end - def visit_Arel_Nodes_Min o - "MIN(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Min o, collector + aggregate "MIN", o, collector end - def visit_Arel_Nodes_Avg o - "AVG(#{o.distinct ? 'DISTINCT ' : ''}#{o.expressions.map { |x| - visit x }.join(', ')})#{o.alias ? " AS #{visit o.alias}" : ''}" + def visit_Arel_Nodes_Avg o, collector + aggregate "AVG", o, collector end - def visit_Arel_Nodes_TableAlias o - "#{visit o.relation} #{quote_table_name o.name}" + def visit_Arel_Nodes_TableAlias o, collector + collector = visit o.relation, collector + collector << " " + collector << quote_table_name(o.name) end - def visit_Arel_Nodes_Between o - "#{visit o.left} BETWEEN #{visit o.right}" + def visit_Arel_Nodes_Between o, collector + collector = visit o.left, collector + collector << " BETWEEN " + visit o.right, collector end - def visit_Arel_Nodes_GreaterThanOrEqual o - "#{visit o.left} >= #{visit o.right}" + def visit_Arel_Nodes_GreaterThanOrEqual o, collector + collector = visit o.left, collector + collector << " >= " + visit o.right, collector end - def visit_Arel_Nodes_GreaterThan o - "#{visit o.left} > #{visit o.right}" + def visit_Arel_Nodes_GreaterThan o, collector + collector = visit o.left, collector + collector << " > " + visit o.right, collector end - def visit_Arel_Nodes_LessThanOrEqual o - "#{visit o.left} <= #{visit o.right}" + def visit_Arel_Nodes_LessThanOrEqual o, collector + collector = visit o.left, collector + collector << " <= " + visit o.right, collector end - def visit_Arel_Nodes_LessThan o - "#{visit o.left} < #{visit o.right}" + def visit_Arel_Nodes_LessThan o, collector + collector = visit o.left, collector + collector << " < " + visit o.right, collector end - def visit_Arel_Nodes_Matches o - "#{visit o.left} LIKE #{visit o.right}" + def visit_Arel_Nodes_Matches o, collector + collector = visit o.left, collector + collector << " LIKE " + visit o.right, collector end - def visit_Arel_Nodes_DoesNotMatch o - "#{visit o.left} NOT LIKE #{visit o.right}" + def visit_Arel_Nodes_DoesNotMatch o, collector + collector = visit o.left, collector + collector << " NOT LIKE " + visit o.right, collector end - def visit_Arel_Nodes_Regexp o - raise NotImplementedError, '~ not implemented for this db' + def visit_Arel_Nodes_JoinSource o, collector + if o.left + collector = visit o.left, collector + end + if o.right.any? + collector << " " if o.left + collector = inject_join o.right, collector, ' ' + end + collector end - def visit_Arel_Nodes_NotRegexp o - raise NotImplementedError, '!~ not implemented for this db' + def visit_Arel_Nodes_Regexp o, collector + raise NotImplementedError, '~ not implemented for this db' end - def visit_Arel_Nodes_JoinSource o - [ - (visit(o.left) if o.left), - o.right.map { |j| visit j }.join(' ') - ].compact.join ' ' + def visit_Arel_Nodes_NotRegexp o, collector + raise NotImplementedError, '!~ not implemented for this db' end - def visit_Arel_Nodes_StringJoin o - visit o.left + def visit_Arel_Nodes_StringJoin o, collector + visit o.left, collector end def visit_Arel_Nodes_FullOuterJoin o "FULL OUTER JOIN #{visit o.left} #{visit o.right}" end - def visit_Arel_Nodes_OuterJoin o - "LEFT OUTER JOIN #{visit o.left} #{visit o.right}" + def visit_Arel_Nodes_OuterJoin o, collector + collector << "LEFT OUTER JOIN " + collector = visit o.left, collector + collector << " " + visit o.right, collector end def visit_Arel_Nodes_RightOuterJoin o "RIGHT OUTER JOIN #{visit o.left} #{visit o.right}" end - def visit_Arel_Nodes_InnerJoin o - s = "INNER JOIN #{visit o.left}" + def visit_Arel_Nodes_InnerJoin o, collector + collector << "INNER JOIN " + collector = visit o.left, collector if o.right - s << SPACE - s << visit(o.right) + collector << SPACE + visit(o.right, collector) + else + collector end - s end - def visit_Arel_Nodes_On o - "ON #{visit o.expr}" + def visit_Arel_Nodes_On o, collector + collector << "ON " + visit o.expr, collector end - def visit_Arel_Nodes_Not o - "NOT (#{visit o.expr})" + def visit_Arel_Nodes_Not o, collector + collector << "NOT (" + visit(o.expr, collector) << ")" end - def visit_Arel_Table o + def visit_Arel_Table o, collector if o.table_alias - "#{quote_table_name o.name} #{quote_table_name o.table_alias}" + collector << "#{quote_table_name o.name} #{quote_table_name o.table_alias}" else - quote_table_name o.name + collector << quote_table_name(o.name) end end - def visit_Arel_Nodes_In o + def visit_Arel_Nodes_In o, collector if Array === o.right && o.right.empty? - '1=0' + collector << '1=0' else - "#{visit o.left} IN (#{visit o.right})" + collector = visit o.left, collector + collector << " IN (" + visit(o.right, collector) << ")" end end - def visit_Arel_Nodes_NotIn o + def visit_Arel_Nodes_NotIn o, collector if Array === o.right && o.right.empty? - '1=1' + collector << '1=1' else - "#{visit o.left} NOT IN (#{visit o.right})" + collector = visit o.left, collector + collector << " NOT IN (" + collector = visit o.right, collector + collector << ")" end end - def visit_Arel_Nodes_And o - o.children.map { |x| visit x}.join ' AND ' + def visit_Arel_Nodes_And o, collector + inject_join o.children, collector, " AND " end - def visit_Arel_Nodes_Or o - "#{visit o.left} OR #{visit o.right}" + def visit_Arel_Nodes_Or o, collector + collector = visit o.left, collector + collector << " OR " + visit o.right, collector end - def visit_Arel_Nodes_Assignment o + def visit_Arel_Nodes_Assignment o, collector case o.right when Arel::Nodes::UnqualifiedColumn, Arel::Attributes::Attribute - "#{visit o.left} = #{visit o.right}" + collector = visit o.left, collector + collector << " = " + visit o.right, collector else - right = quote(o.right, column_for(o.left)) - "#{visit o.left} = #{right}" + collector = visit o.left, collector + collector << " = " + collector << quote(o.right, column_for(o.left)).to_s end end - def visit_Arel_Nodes_Equality o + def visit_Arel_Nodes_Equality o, collector right = o.right + collector = visit o.left, collector + if right.nil? - "#{visit o.left} IS NULL" + collector << " IS NULL" else - "#{visit o.left} = #{visit right}" + collector << " = " + visit right, collector end end - def visit_Arel_Nodes_NotEqual o + def visit_Arel_Nodes_NotEqual o, collector right = o.right + collector = visit o.left, collector + if right.nil? - "#{visit o.left} IS NOT NULL" + collector << " IS NOT NULL" else - "#{visit o.left} != #{visit right}" + collector << " != " + visit right, collector end end - def visit_Arel_Nodes_As o - "#{visit o.left} AS #{visit o.right}" + def visit_Arel_Nodes_As o, collector + collector = visit o.left, collector + collector << " AS " + visit o.right, collector end - def visit_Arel_Nodes_UnqualifiedColumn o - "#{quote_column_name o.name}" + def visit_Arel_Nodes_UnqualifiedColumn o, collector + collector << "#{quote_column_name o.name}" + collector end - def visit_Arel_Attributes_Attribute o + def visit_Arel_Attributes_Attribute o, collector join_name = o.relation.table_alias || o.relation.name - "#{quote_table_name join_name}.#{quote_column_name o.name}" + collector << "#{quote_table_name join_name}.#{quote_column_name o.name}" end alias :visit_Arel_Attributes_Integer :visit_Arel_Attributes_Attribute alias :visit_Arel_Attributes_Float :visit_Arel_Attributes_Attribute @@ -564,7 +676,7 @@ module Arel alias :visit_Arel_Attributes_Time :visit_Arel_Attributes_Attribute alias :visit_Arel_Attributes_Boolean :visit_Arel_Attributes_Attribute - def literal o; o end + def literal o, collector; collector << o.to_s; end alias :visit_Arel_Nodes_BindParam :literal alias :visit_Arel_Nodes_SqlLiteral :literal @@ -594,8 +706,10 @@ module Arel alias :visit_Time :unsupported alias :visit_TrueClass :unsupported - def visit_Arel_Nodes_InfixOperation o - "#{visit o.left} #{o.operator} #{visit o.right}" + def visit_Arel_Nodes_InfixOperation o, collector + collector = visit o.left, collector + collector << " #{o.operator} " + visit o.right, collector end alias :visit_Arel_Nodes_Addition :visit_Arel_Nodes_InfixOperation @@ -603,8 +717,8 @@ module Arel alias :visit_Arel_Nodes_Multiplication :visit_Arel_Nodes_InfixOperation alias :visit_Arel_Nodes_Division :visit_Arel_Nodes_InfixOperation - def visit_Array o - o.map { |x| visit x }.join(', ') + def visit_Array o, collector + inject_join o, collector, ", " end def quote value, column = nil @@ -620,6 +734,43 @@ module Arel def quote_column_name name @quoted_columns[name] ||= Arel::Nodes::SqlLiteral === name ? name : @connection.quote_column_name(name) end + + def maybe_visit thing, collector + return collector unless thing + collector << " " + visit thing, collector + end + + def inject_join list, collector, join_str + len = list.length - 1 + list.each_with_index.inject(collector) { |c, (x,i)| + if i == len + visit x, c + else + visit(x, c) << join_str + end + } + end + + def infix_value o, collector, value + collector = visit o.left, collector + collector << value + visit o.right, collector + end + + def aggregate name, o, collector + collector << "#{name}(" + if o.distinct + collector << "DISTINCT " + end + collector = inject_join(o.expressions, collector, ", ") << ")" + if o.alias + collector << " AS " + visit o.alias, collector + else + collector + end + end end end end diff --git a/lib/arel/visitors/where_sql.rb b/lib/arel/visitors/where_sql.rb index acd84cd631..27dde73673 100644 --- a/lib/arel/visitors/where_sql.rb +++ b/lib/arel/visitors/where_sql.rb @@ -1,8 +1,9 @@ module Arel module Visitors class WhereSql < Arel::Visitors::ToSql - def visit_Arel_Nodes_SelectCore o - "WHERE #{o.wheres.map { |x| visit x}.join ' AND ' }" + def visit_Arel_Nodes_SelectCore o, collector + collector << "WHERE " + inject_join o.wheres, collector, ' AND ' end end end |