diff options
-rw-r--r-- | lib/arel/visitors/oracle.rb | 10 | ||||
-rw-r--r-- | spec/arel/visitors/oracle_spec.rb | 10 |
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb index 840a7a9ace..afea683080 100644 --- a/lib/arel/visitors/oracle.rb +++ b/lib/arel/visitors/oracle.rb @@ -2,12 +2,20 @@ module Arel module Visitors class Oracle < Arel::Visitors::ToSql def visit_Arel_Nodes_SelectStatement o - if o.limit + if o.limit && o.orders.empty? o.cores.last.wheres.push Nodes::LessThanOrEqual.new( Nodes::SqlLiteral.new('ROWNUM'), o.limit ) o.limit = nil + return super end + + if o.limit && !o.orders.empty? + limit = o.limit + o.limit = nil + return "SELECT * FROM (#{super}) WHERE ROWNUM <= #{limit}" + end + super end end diff --git a/spec/arel/visitors/oracle_spec.rb b/spec/arel/visitors/oracle_spec.rb index 384725b788..87a1c378ac 100644 --- a/spec/arel/visitors/oracle_spec.rb +++ b/spec/arel/visitors/oracle_spec.rb @@ -15,6 +15,16 @@ module Arel sql = @visitor.accept stmt sql.should be_like %{ SELECT WHERE ROWNUM <= 10 } end + + it 'creates a subquery when there is order_by' do + stmt = Nodes::SelectStatement.new + stmt.orders << Nodes::SqlLiteral.new('foo') + stmt.limit = 10 + sql = @visitor.accept stmt + sql.should be_like %{ + SELECT * FROM (SELECT ORDER BY foo) WHERE ROWNUM <= 10 + } + end end end end |