Permalink
Browse files

fix one more time order_hacks method for Oracle

correctly split order string by commas ignoring commas which separate function arguments
  • Loading branch information...
rsim committed Nov 16, 2010
1 parent ee30881 commit 237e580c4920b2414ef1bb72c14250688b479b9e
Showing with 29 additions and 8 deletions.
  1. +17 −8 lib/arel/visitors/oracle.rb
  2. +12 −0 test/visitors/test_oracle.rb
@@ -70,19 +70,14 @@ def order_hacks o
/DISTINCT.*FIRST_VALUE/ === projection
end
end
- # FIXME: previous version with join and split broke ORDER BY clause
+ # Previous version with join and split broke ORDER BY clause
# if it contained functions with several arguments (separated by ',').
- # Currently splitting is done only if there is no function calls
#
# orders = o.orders.map { |x| visit x }.join(', ').split(',')
orders = o.orders.map do |x|
string = visit x
- # if there is function call
- if string.include?('(')
- string
- # if no function call then comma splits several ORDER BY columns
- elsif string.include?(',')
- string.split(',')
+ if string.include?(',')
+ split_order_string(string)
else
string
end
@@ -94,6 +89,20 @@ def order_hacks o
end
o
end
+
+ # Split string by commas but count opening and closing brackets
+ # and ignore commas inside brackets.
+ def split_order_string(string)
+ array = []
+ i = 0
+ string.split(',').each do |part|
+ array[i] ||= ""
+ array[i] << part
+ i += 1 if array[i].count('(') == array[i].count(')')
+ end
+ array
+ end
+
end
end
end
@@ -43,6 +43,18 @@ module Visitors
}
end
+ it 'splits orders with commas and function calls' do
+ # *sigh*
+ select = "DISTINCT foo.id, FIRST_VALUE(projects.name) OVER (foo) AS alias_0__"
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.projections << Nodes::SqlLiteral.new(select)
+ stmt.orders << Nodes::SqlLiteral.new('NVL(LOWER(bar, foo), foo) DESC, UPPER(baz)')
+ sql = @visitor.accept(stmt)
+ sql.must_be_like %{
+ SELECT #{select} ORDER BY alias_0__ DESC, alias_1__
+ }
+ end
+
describe 'Nodes::SelectStatement' do
describe 'limit' do
it 'adds a rownum clause' do

0 comments on commit 237e580

Please sign in to comment.