method
add_limit_offset!
v1.0.0 -
Show latest stable
- Class:
ActiveRecord::ConnectionAdapters::DB2Adapter
add_limit_offset!(sql, options)public
No documentation available.
# File activerecord/lib/active_record/connection_adapters/db2_adapter.rb, line 94
def add_limit_offset!(sql, options)
if options[:limit] and !options[:limit].nil?
# "FETCH FIRST 0 ROWS ONLY" is not allowed, so we have
# to use a cheap trick.
if options[:limit] == 0
if sql =~ /WHERE/i
sql.sub!(/WHERE/i, 'WHERE 1 = 2 AND ')
elsif
sql =~ /ORDER\s+BY/i
sql.sub!(/ORDER\s+BY/i, 'WHERE 1 = 2 ORDER BY')
else
sql << 'WHERE 1 = 2'
end
else
sql << " FETCH FIRST #{options[:limit]} ROWS ONLY"
end
end
if options[:offset] and !options[:offset].nil?
raise ArgumentError, ':offset option is not yet supported!'
end
end 1Note
Why gsub!
I notice other adapters use sql.sub!, not sql.gsub! and in fact I had trouble with adding the limit parameter to any query involving nested selects. Replacing sql.gsub! with sql.sub! solved that problem for me. Has anyone else had a similar experience with this method?
In other words, replace: sql.gsub!(/SELECT/i, 'SELECT B.* FROM (SELECT A., row_number() over () AS internal$rownum FROM (SELECT') With: sql.sub!(/SELECT/i, 'SELECT B. FROM (SELECT A.*, row_number() over () AS internal$rownum FROM (SELECT')