Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Arel::Nodes::BindParam in Oracle visitor for queries using both LIMIT and OFFSET #450

Merged
merged 1 commit into from
Oct 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/arel/visitors/oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@ def visit_Arel_Nodes_SelectStatement o, collector
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
Expand Down
16 changes: 15 additions & 1 deletion test/visitors/test_oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ def compile node
}
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)
Expand All @@ -148,7 +163,6 @@ def compile node
}
end
end

end

it 'modified except to be minus' do
Expand Down