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

113 lines
3.1 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 17:56:16 -04:00
o = order_hacks(o)
2010-09-24 14:17:17 -04:00
# 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 /
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
2010-09-24 17:56:16 -04:00
o = o.dup
limit = o.limit.to_i
offset = o.offset
o.limit = nil
2010-09-24 13:24:31 -04:00
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_
2010-12-14 23:53:19 -05:00
WHERE rownum <= #{offset.expr.to_i + limit}
2010-09-24 13:24:31 -04:00
)
WHERE #{visit offset}
eosql
end
if o.limit
2010-09-24 17:56:16 -04:00
o = o.dup
limit = o.limit
o.limit = nil
return "SELECT * FROM (#{super(o)}) WHERE ROWNUM <= #{limit}"
end
if o.offset
o = o.dup
offset = o.offset
o.offset = nil
sql = super(o)
return <<-eosql
SELECT * FROM (
SELECT raw_sql_.*, rownum raw_rnum_
FROM (#{sql}) raw_sql_
)
WHERE #{visit offset}
eosql
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
2010-12-14 23:53:19 -05:00
"raw_rnum_ > #{visit o.expr}"
2010-09-24 13:24:31 -04:00
end
2010-09-24 14:17:17 -04:00
###
# Hacks for the order clauses specific to Oracle
def order_hacks o
2010-09-24 17:56:16 -04:00
return o if o.orders.empty?
return o unless o.cores.any? do |core|
2010-09-24 14:17:17 -04:00
core.projections.any? do |projection|
/DISTINCT.*FIRST_VALUE/ === projection
end
end
# Previous version with join and split broke ORDER BY clause
# if it contained functions with several arguments (separated by ',').
#
# orders = o.orders.map { |x| visit x }.join(', ').split(',')
orders = o.orders.map do |x|
string = visit x
if string.include?(',')
split_order_string(string)
else
string
end
end.flatten
2010-09-24 14:17:17 -04:00
o.orders = []
orders.each_with_index do |order, i|
o.orders <<
2010-09-24 18:09:26 -04:00
Nodes::SqlLiteral.new("alias_#{i}__#{' DESC' if /\bdesc$/i === order}")
2010-09-24 14:17:17 -04:00
end
2010-09-24 17:56:16 -04:00
o
2010-09-24 14:17:17 -04:00
end
# Split string by commas but count opening and closing brackets
# and ignore commas inside brackets.
def split_order_string(string)
array = []
i = 0
string.split(',').each do |part|
if array[i]
array[i] << ',' << part
else
# to ensure that array[i] will be String and not Arel::Nodes::SqlLiteral
array[i] = '' << part
end
i += 1 if array[i].count('(') == array[i].count(')')
end
array
end
2010-09-24 12:41:48 -04:00
end
end
end