aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--README.markdown2
-rw-r--r--lib/arel/visitors/oracle.rb6
-rw-r--r--spec/crud_spec.rb2
-rw-r--r--spec/visitors/oracle_spec.rb10
4 files changed, 17 insertions, 3 deletions
diff --git a/README.markdown b/README.markdown
index 10d3f0a021..ab05e1245a 100644
--- a/README.markdown
+++ b/README.markdown
@@ -60,7 +60,7 @@ What are called `LIMIT` and `OFFSET` in SQL are called `take` and `skip` in Arel
users.group(users[:name]) # => SELECT * FROM users GROUP BY name
-The best property of the Relational Algebra is its "composability", or closure under all operations. For example, to select AND project, just "chain" the method invocations:
+The best property of the Relational Algebra is its "composability", or closure under all operations. For example, to restrict AND project, just "chain" the method invocations:
users \
.where(users[:name].eq('amy')) \
diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb
index 659b75f16d..c691ce294e 100644
--- a/lib/arel/visitors/oracle.rb
+++ b/lib/arel/visitors/oracle.rb
@@ -6,7 +6,9 @@ module Arel
def visit_Arel_Nodes_SelectStatement o
o = order_hacks(o)
- if o.limit && o.orders.empty? && !o.offset
+ # if need to select first records without ORDER BY and GROUP BY and without DISTINCT
+ # then can use simple ROWNUM in WHERE clause
+ if o.limit && o.orders.empty? && !o.offset && o.cores.first.projections.first !~ /^DISTINCT /
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new('ROWNUM'), o.limit
)
@@ -31,7 +33,7 @@ module Arel
eosql
end
- if o.limit && !o.orders.empty?
+ if o.limit
o = o.dup
limit = o.limit
o.limit = nil
diff --git a/spec/crud_spec.rb b/spec/crud_spec.rb
index 3485df6685..36b2a0b4e4 100644
--- a/spec/crud_spec.rb
+++ b/spec/crud_spec.rb
@@ -1,3 +1,5 @@
+require 'spec_helper'
+
module Arel
class FakeCrudder < SelectManager
class FakeEngine
diff --git a/spec/visitors/oracle_spec.rb b/spec/visitors/oracle_spec.rb
index cdf0e3427f..93ade3ef3c 100644
--- a/spec/visitors/oracle_spec.rb
+++ b/spec/visitors/oracle_spec.rb
@@ -71,6 +71,16 @@ module Arel
}
end
+ it 'creates a subquery when there is DISTINCT' do
+ stmt = Nodes::SelectStatement.new
+ stmt.cores.first.projections << Nodes::SqlLiteral.new('DISTINCT id')
+ stmt.limit = 10
+ sql = @visitor.accept stmt
+ sql.should be_like %{
+ SELECT * FROM (SELECT DISTINCT id) WHERE ROWNUM <= 10
+ }
+ end
+
it 'creates a different subquery when there is an offset' do
stmt = Nodes::SelectStatement.new
stmt.limit = 10