Skip to content

Commit

Permalink
Active Record: fix paginate_by_sql for Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Smylie authored and mislav committed Sep 27, 2011
1 parent 370c97b commit c0e61aa
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions lib/will_paginate/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,27 @@ def paginate_by_sql(sql, options)
WillPaginate::Collection.create(pagenum, per_page, total) do |pager|
query = sanitize_sql(sql.dup)
original_query = query.dup
oracle = self.connection.adapter_name =~ /^(oracle|oci$)/i

# add limit, offset
query << " LIMIT #{pager.per_page} OFFSET #{pager.offset}"
if oracle
query = <<-SQL
SELECT * FROM (
SELECT rownum rnum, a.* FROM (#{query}) a
WHERE rownum <= #{pager.offset + pager.per_page}
) WHERE rnum >= #{pager.offset}
SQL
else
query << " LIMIT #{pager.per_page} OFFSET #{pager.offset}"
end

# perfom the find
pager.replace find_by_sql(query)

unless pager.total_entries
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s.]+$/mi, ''
count_query = "SELECT COUNT(*) FROM (#{count_query})"

unless self.connection.adapter_name =~ /^(oracle|oci$)/i
count_query << ' AS count_table'
end
count_query << ' AS count_table' unless oracle
# perform the count query
pager.total_entries = count_by_sql(count_query)
end
Expand Down

0 comments on commit c0e61aa

Please sign in to comment.