Skip to content

Commit

Permalink
[as400] Fixed ordering when limit and offset present
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruslan authored and kares committed Sep 24, 2017
1 parent 9c84820 commit 15d7d1f
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions lib/arjdbc/db2/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -497,29 +497,26 @@ def add_limit_offset!(sql, options)

# @private shared with {Arel::Visitors::DB2}
def replace_limit_offset!(sql, limit, offset, orders = nil)
limit = limit.to_i
# Ordering is done somewhere before this method gets called

if offset # && limit
over_order_by = nil # NOTE: orders matching got reverted as it was not complete and there were no case covering it ...
# Create the limit and offset sql
param_sql = ''

start_sql = "SELECT B.* FROM (SELECT A.*, row_number() OVER (#{over_order_by}) AS internal$rownum FROM (SELECT"
end_sql = ") A ) B WHERE B.internal$rownum > #{offset} AND B.internal$rownum <= #{limit + offset.to_i}"

if sql.is_a?(String)
sql.sub!(/SELECT/i, start_sql)
sql << end_sql
else # AR 4.2 sql.class ... Arel::Collectors::Bind
sql.parts[0] = start_sql # sql.sub! /SELECT/i
sql.parts[ sql.parts.length ] = end_sql
end
if offset.present? && limit.present?
param_sql << " LIMIT #{limit} OFFSET #{offset} "
else
limit_sql = limit == 1 ? " FETCH FIRST ROW ONLY" : " FETCH FIRST #{limit} ROWS ONLY"
if sql.is_a?(String)
sql << limit_sql
else # AR 4.2 sql.class ... Arel::Collectors::Bind
sql.parts[ sql.parts.length ] = limit_sql
end
param_sql << " OFFSET #{offset}" if offset.present?
param_sql << if limit.present?
limit == 1 ? ' FETCH FIRST ROW ONLY' : " FETCH FIRST #{limit} ROWS ONLY"
end
end

if sql.is_a?(String)
sql << param_sql
else # AR 4.2 sql.class ... Arel::Collectors::Bind
sql.parts[sql.parts.length] = param_sql
end

sql
end

Expand Down

0 comments on commit 15d7d1f

Please sign in to comment.