aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/arel/visitors/to_sql.rb
diff options
context:
space:
mode:
authorKeenan Brock <keenan@thebrocks.net>2018-11-13 15:22:08 -0500
committerKeenan Brock <keenan@thebrocks.net>2018-11-13 17:05:45 -0500
commit63dd8d8e12edb25d8d5bac324aacb1caf05bbe22 (patch)
tree17be93d6389f6723309979ca2bdb39ac9f9349b7 /activerecord/lib/arel/visitors/to_sql.rb
parent7a954211e6ffe9b482fbd65711edadbb265bf1a7 (diff)
downloadrails-63dd8d8e12edb25d8d5bac324aacb1caf05bbe22.tar.gz
rails-63dd8d8e12edb25d8d5bac324aacb1caf05bbe22.tar.bz2
rails-63dd8d8e12edb25d8d5bac324aacb1caf05bbe22.zip
Emit single pair of parens for UNION and UNION ALL
mysql has a great implementation to suppress multiple parens for union sql statements. This moves that functionality to the generic implementation This also introduces that functionality for UNION ALL
Diffstat (limited to 'activerecord/lib/arel/visitors/to_sql.rb')
-rw-r--r--activerecord/lib/arel/visitors/to_sql.rb23
1 files changed, 19 insertions, 4 deletions
diff --git a/activerecord/lib/arel/visitors/to_sql.rb b/activerecord/lib/arel/visitors/to_sql.rb
index 8e56fb55a2..34e82975ad 100644
--- a/activerecord/lib/arel/visitors/to_sql.rb
+++ b/activerecord/lib/arel/visitors/to_sql.rb
@@ -268,13 +268,11 @@ module Arel # :nodoc: all
end
def visit_Arel_Nodes_Union(o, collector)
- collector << "( "
- infix_value(o, collector, " UNION ") << " )"
+ infix_value_with_paren(o, collector, " UNION ")
end
def visit_Arel_Nodes_UnionAll(o, collector)
- collector << "( "
- infix_value(o, collector, " UNION ALL ") << " )"
+ infix_value_with_paren(o, collector, " UNION ALL ")
end
def visit_Arel_Nodes_Intersect(o, collector)
@@ -845,6 +843,23 @@ module Arel # :nodoc: all
visit o.right, collector
end
+ def infix_value_with_paren(o, collector, value, suppress_parens = false)
+ collector << '( ' unless suppress_parens
+ collector = if o.left.class == o.class
+ infix_value_with_paren(o.left, collector, value, true)
+ else
+ visit o.left, collector
+ end
+ collector << value
+ collector = if o.right.class == o.class
+ infix_value_with_paren(o.right, collector, value, true)
+ else
+ visit o.right, collector
+ end
+ collector << ' )' unless suppress_parens
+ collector
+ end
+
def aggregate(name, o, collector)
collector << "#{name}("
if o.distinct