diff options
-rw-r--r-- | History.txt | 1 | ||||
-rw-r--r-- | lib/arel/visitors/mysql.rb | 22 | ||||
-rw-r--r-- | test/visitors/test_mysql.rb | 10 |
3 files changed, 33 insertions, 0 deletions
diff --git a/History.txt b/History.txt index ea27ae6d6d..8b69413140 100644 --- a/History.txt +++ b/History.txt @@ -3,6 +3,7 @@ * Bug Fixes * Fix depth-first traversal to understand ascending / descending nodes. + * Parentheis are suppressed with nested unions in MySQL. Thanks jhtwong! == 2.1.3 / 2011-06-27 diff --git a/lib/arel/visitors/mysql.rb b/lib/arel/visitors/mysql.rb index 763cf11aad..70166a15c5 100644 --- a/lib/arel/visitors/mysql.rb +++ b/lib/arel/visitors/mysql.rb @@ -2,6 +2,28 @@ module Arel module Visitors class MySQL < Arel::Visitors::ToSql private + def visit_Arel_Nodes_Union o, suppress_parens = false + left_result = case o.left + when Arel::Nodes::Union + visit_Arel_Nodes_Union o.left, true + else + visit o.left + end + + right_result = case o.right + when Arel::Nodes::Union + visit_Arel_Nodes_Union o.right, true + else + visit o.right + end + + if suppress_parens + "#{left_result} UNION #{right_result}" + else + "( #{left_result} UNION #{right_result} )" + end + end + def visit_Arel_Nodes_Bin o "BINARY #{visit o.expr}" end diff --git a/test/visitors/test_mysql.rb b/test/visitors/test_mysql.rb index 3c15c218b2..8d220ac04b 100644 --- a/test/visitors/test_mysql.rb +++ b/test/visitors/test_mysql.rb @@ -7,6 +7,16 @@ module Arel @visitor = MySQL.new Table.engine end + it 'squashes parenthesis on multiple unions' do + subnode = Nodes::Union.new 'left', 'right' + node = Nodes::Union.new subnode, 'topright' + assert_equal 1, @visitor.accept(node).scan('(').length + + subnode = Nodes::Union.new 'left', 'right' + node = Nodes::Union.new 'topleft', subnode + assert_equal 1, @visitor.accept(node).scan('(').length + end + ### # :'( # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214 |