提交 f381da27 编写于 作者: A Aaron Patterson

adding more oracle hacks

上级 58d82b23
......@@ -4,6 +4,8 @@ class Oracle < Arel::Visitors::ToSql
private
def visit_Arel_Nodes_SelectStatement o
order_hacks(o)
if o.limit && o.orders.empty? && !o.offset
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new('ROWNUM'), o.limit
......@@ -40,6 +42,23 @@ def visit_Arel_Nodes_SelectStatement o
def visit_Arel_Nodes_Offset o
"raw_rnum_ > #{visit o.value}"
end
###
# Hacks for the order clauses specific to Oracle
def order_hacks o
return if o.orders.empty?
return unless o.cores.any? do |core|
core.projections.any? do |projection|
/DISTINCT.*FIRST_VALUE/ === projection
end
end
orders = o.orders
o.orders = []
orders.each_with_index do |order, i|
o.orders <<
Nodes::SqlLiteral.new("alias_#{i}__ #{'DESC' if /\bdesc$/i === order}")
end
end
end
end
end
......@@ -7,6 +7,18 @@ module Visitors
@visitor = Oracle.new Table.engine
end
it 'modifies order when there is distinct and first value' do
# *sigh*
select = "DISTINCT foo.id, FIRST_VALUE(projects.name) OVER (foo) AS alias_0__"
stmt = Nodes::SelectStatement.new
stmt.cores.first.projections << Nodes::SqlLiteral.new(select)
stmt.orders << Nodes::SqlLiteral.new('foo')
sql = @visitor.accept(stmt)
sql.should be_like %{
SELECT #{select} ORDER BY alias_0__
}
end
describe 'Nodes::SelectStatement' do
describe 'limit' do
it 'adds a rownum clause' do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册