Skip to content

Commit

Permalink
Add back "AS count_table" alias to paginate_by_sql counter SQL
Browse files Browse the repository at this point in the history
Oh damn, turns out alias is needed for other RDBMS (thanks Matt Murphy)

This reverts commit 6dc60a9.
  • Loading branch information
mislav committed May 21, 2009
1 parent 49cfe76 commit 5fb4061
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/will_paginate/finder.rb
Expand Up @@ -140,6 +140,10 @@ def paginate_by_sql(sql, options)
unless pager.total_entries
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
count_query = "SELECT COUNT(*) FROM (#{count_query})"

unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase)
count_query << ' AS count_table'
end
# perform the count query
pager.total_entries = count_by_sql(count_query)
end
Expand Down
4 changes: 2 additions & 2 deletions test/finder_test.rb
Expand Up @@ -363,7 +363,7 @@ def test_should_use_scoped_finders_if_present
def test_paginate_by_sql
assert_respond_to Developer, :paginate_by_sql
Developer.expects(:find_by_sql).with(regexp_matches(/sql LIMIT 3(,| OFFSET) 3/)).returns([])
Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql)').returns(0)
Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0)

entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3
end
Expand All @@ -378,7 +378,7 @@ def test_paginate_by_sql_respects_total_entries_setting

def test_paginate_by_sql_strips_order_by_when_counting
Developer.expects(:find_by_sql).returns([])
Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n )").returns(0)
Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)

Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2
end
Expand Down

0 comments on commit 5fb4061

Please sign in to comment.