Permalink
Browse files

generate correct select if only offset is present (in Oracle)

  • Loading branch information...
1 parent c66f025 commit ec6ac6654ed7eb678bb0b8f58fe3e7d9b932e297 @rsim rsim committed Oct 21, 2010
Showing with 30 additions and 0 deletions.
  1. +14 −0 lib/arel/visitors/oracle.rb
  2. +16 −0 test/visitors/test_oracle.rb
View
14 lib/arel/visitors/oracle.rb
@@ -40,6 +40,20 @@ def visit_Arel_Nodes_SelectStatement o
return "SELECT * FROM (#{super(o)}) WHERE ROWNUM <= #{limit}"
end
+ if o.offset
+ o = o.dup
+ offset = o.offset
+ o.offset = nil
+ sql = super(o)
+ return <<-eosql
+ SELECT * FROM (
+ SELECT raw_sql_.*, rownum raw_rnum_
+ FROM (#{sql}) raw_sql_
+ )
+ WHERE #{visit offset}
+ eosql
+ end
+
super
end
View
16 test/visitors/test_oracle.rb
@@ -105,6 +105,22 @@ module Visitors
sql.must_equal sql2
end
end
+
+ describe 'only offset' do
+ it 'creates a select from subquery with rownum condition' do
+ stmt = Nodes::SelectStatement.new
+ stmt.offset = Nodes::Offset.new(10)
+ sql = @visitor.accept stmt
+ sql.must_be_like %{
+ SELECT * FROM (
+ SELECT raw_sql_.*, rownum raw_rnum_
+ FROM (SELECT ) raw_sql_
+ )
+ WHERE raw_rnum_ > 10
+ }
+ end
+ end
+
end
end
end

0 comments on commit ec6ac66

Please sign in to comment.