diff options
Diffstat (limited to 'lib/arel/visitors/mysql.rb')
-rw-r--r-- | lib/arel/visitors/mysql.rb | 70 |
1 files changed, 48 insertions, 22 deletions
diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index ee8483372a..70a37582c2 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -2,53 +2,79 @@ 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 - def visit_Arel_Nodes_Bin o - "BINARY #{visit o.expr}" + def visit_Arel_Nodes_Bin o, collector + collector << "BINARY " + visit o.expr, collector end ### # :'( # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 - def visit_Arel_Nodes_SelectStatement o - o.limit = Arel::Nodes::Limit.new(18446744073709551615) if o.offset && !o.limit + 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 << " WHERE " + collector = inject_join o.wheres, collector, ' AND ' + end + + unless o.orders.empty? + collector << " ORDER BY " + collector = inject_join o.orders, collector, ', ' + end + + if o.limit + collector << " " + visit(o.limit, collector) + else + collector + end end end |