1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/lib/arel/visitors/oracle.rb

67 lines
1.7 KiB
Ruby
Raw Normal View History

2010-09-24 12:41:48 -04:00
module Arel
module Visitors
class Oracle < Arel::Visitors::ToSql
2010-09-24 13:24:31 -04:00
private
2010-09-24 12:41:48 -04:00
def visit_Arel_Nodes_SelectStatement o
2010-09-24 14:17:17 -04:00
order_hacks(o)
2010-09-24 13:24:31 -04:00
if o.limit && o.orders.empty? && !o.offset
2010-09-24 12:41:48 -04:00
o.cores.last.wheres.push Nodes::LessThanOrEqual.new(
Nodes::SqlLiteral.new('ROWNUM'), o.limit
)
o.limit = nil
return super
2010-09-24 12:41:48 -04:00
end
2010-09-24 13:24:31 -04:00
if o.limit && o.offset
o = o.dup
2010-09-24 13:24:31 -04:00
limit = o.limit.to_i
offset = o.offset
o.limit = nil
o.offset = nil
sql = super(o)
2010-09-24 13:24:31 -04:00
return <<-eosql
SELECT * FROM (
SELECT raw_sql_.*, rownum raw_rnum_
FROM (#{sql}) raw_sql_
WHERE rownum <= #{offset.value.to_i + limit}
)
WHERE #{visit offset}
eosql
end
if o.limit && !o.orders.empty?
o = o.dup
limit = o.limit
o.limit = nil
return "SELECT * FROM (#{super(o)}) WHERE ROWNUM <= #{limit}"
end
2010-09-24 12:41:48 -04:00
super
end
2010-09-24 13:24:31 -04:00
def visit_Arel_Nodes_Offset o
"raw_rnum_ > #{visit o.value}"
end
2010-09-24 14:17:17 -04:00
###
# 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
2010-09-24 12:41:48 -04:00
end
end
end