aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/arel/table.rb8
-rw-r--r--lib/arel/visitors/oracle.rb15
-rw-r--r--test/test_table.rb9
-rw-r--r--test/visitors/test_oracle.rb16
4 files changed, 31 insertions, 17 deletions
diff --git a/lib/arel/table.rb b/lib/arel/table.rb
index b4b4a861b8..0e7214f269 100644
--- a/lib/arel/table.rb
+++ b/lib/arel/table.rb
@@ -6,7 +6,7 @@ module Arel
@engine = nil
class << self; attr_accessor :engine; end
- attr_accessor :name, :aliases, :table_alias
+ attr_accessor :name, :table_alias
# TableAlias and Table both have a #table_name which is the name of the underlying table
alias :table_name :name
@@ -14,7 +14,6 @@ module Arel
def initialize(name, as: nil, type_caster: nil)
@name = name.to_s
@columns = nil
- @aliases = []
@type_caster = type_caster
# Sometime AR sends an :as parameter to table, to let the table know
@@ -27,9 +26,7 @@ module Arel
end
def alias name = "#{self.name}_2"
- Nodes::TableAlias.new(self, name).tap do |node|
- @aliases << node
- end
+ Nodes::TableAlias.new(self, name)
end
def from
@@ -94,7 +91,6 @@ module Arel
def eql? other
self.class == other.class &&
self.name == other.name &&
- self.aliases == other.aliases &&
self.table_alias == other.table_alias
end
alias :== :eql?
diff --git a/lib/arel/visitors/oracle.rb b/lib/arel/visitors/oracle.rb
index 99075b3e27..3b452836db 100644
--- a/lib/arel/visitors/oracle.rb
+++ b/lib/arel/visitors/oracle.rb
@@ -27,11 +27,22 @@ module Arel
FROM ("
collector = super(o, collector)
- collector << ") raw_sql_
+
+ if offset.expr.is_a? Nodes::BindParam
+ offset_bind = nil
+ collector << ') raw_sql_ WHERE rownum <= ('
+ collector.add_bind(offset.expr) { |i| offset_bind = ":a#{i}" }
+ collector << ' + '
+ collector.add_bind(limit) { |i| ":a#{i}" }
+ collector << ") ) WHERE raw_rnum_ > #{offset_bind}"
+ return collector
+ else
+ collector << ") raw_sql_
WHERE rownum <= #{offset.expr.to_i + limit}
)
WHERE "
- return visit(offset, collector)
+ return visit(offset, collector)
+ end
end
if o.limit
diff --git a/test/test_table.rb b/test/test_table.rb
index e8eaf901cc..168fde370d 100644
--- a/test/test_table.rb
+++ b/test/test_table.rb
@@ -110,10 +110,7 @@ module Arel
describe 'alias' do
it 'should create a node that proxies to a table' do
- @relation.aliases.must_equal []
-
node = @relation.alias
- @relation.aliases.must_equal [node]
node.name.must_equal 'users_2'
node[:id].relation.must_equal node
end
@@ -191,10 +188,8 @@ module Arel
describe 'equality' do
it 'is equal with equal ivars' do
relation1 = Table.new(:users)
- relation1.aliases = %w[a b c]
relation1.table_alias = 'zomg'
relation2 = Table.new(:users)
- relation2.aliases = %w[a b c]
relation2.table_alias = 'zomg'
array = [relation1, relation2]
assert_equal 1, array.uniq.size
@@ -202,11 +197,9 @@ module Arel
it 'is not equal with different ivars' do
relation1 = Table.new(:users)
- relation1.aliases = %w[a b c]
relation1.table_alias = 'zomg'
relation2 = Table.new(:users)
- relation2.aliases = %w[x y z]
- relation2.table_alias = 'zomg'
+ relation2.table_alias = 'zomg2'
array = [relation1, relation2]
assert_equal 2, array.uniq.size
end
diff --git a/test/visitors/test_oracle.rb b/test/visitors/test_oracle.rb
index e9ed9d76b3..4c22be5cbb 100644
--- a/test/visitors/test_oracle.rb
+++ b/test/visitors/test_oracle.rb
@@ -124,6 +124,21 @@ module Arel
}
end
+ it 'creates a subquery when there is limit and offset with BindParams' do
+ stmt = Nodes::SelectStatement.new
+ stmt.limit = Nodes::Limit.new(Nodes::BindParam.new)
+ stmt.offset = Nodes::Offset.new(Nodes::BindParam.new)
+ sql = compile stmt
+ sql.must_be_like %{
+ SELECT * FROM (
+ SELECT raw_sql_.*, rownum raw_rnum_
+ FROM (SELECT ) raw_sql_
+ WHERE rownum <= (:a1 + :a2)
+ )
+ WHERE raw_rnum_ > :a1
+ }
+ end
+
it 'is idempotent with different subquery' do
stmt = Nodes::SelectStatement.new
stmt.limit = Nodes::Limit.new(10)
@@ -148,7 +163,6 @@ module Arel
}
end
end
-
end
it 'modified except to be minus' do