Permalink
Browse files

supressing nested parenthesis in multiple unions on mysql. thanks jht…

…wong. fixes #58
  • Loading branch information...
1 parent 7832cd3 commit 2567b72f9582c14a591156653e2b656d48200b11 @tenderlove tenderlove committed Jul 20, 2011
Showing with 33 additions and 0 deletions.
  1. +1 −0 History.txt
  2. +22 −0 lib/arel/visitors/mysql.rb
  3. +10 −0 test/visitors/test_mysql.rb
View
@@ -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
View
@@ -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
@@ -7,6 +7,16 @@ module Visitors
@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

0 comments on commit 2567b72

Please sign in to comment.