235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
# File 'lib/jdbc_adapter/jdbc_mssql.rb', line 235
def add_limit_offset!(sql, options)
limit = options[:limit]
if limit
offset = (options[:offset] || 0).to_i
start_row = offset + 1
end_row = offset + limit.to_i
order = (options[:order] || determine_order_clause(sql))
sql.sub!(/ ORDER BY.*$/i, '')
find_select = /\b(SELECT(?:\s+DISTINCT)?)\b(.*)/i
whole, select, rest_of_query = find_select.match(sql).to_a
new_sql = "#{select} t.* FROM (SELECT ROW_NUMBER() OVER(ORDER BY #{order}) AS row_num, #{rest_of_query}"
new_sql << ") AS t WHERE t.row_num BETWEEN #{start_row.to_s} AND #{end_row.to_s}"
sql.replace(new_sql)
end
end
|