Skip to content

Commit

Permalink
Make sure loose_count returns an Integer in pg_loose_count
Browse files Browse the repository at this point in the history
This is stored as a float in the PostgreSQL system catalogs,
but users probably expect a count to return an integer.
  • Loading branch information
jeremyevans committed Aug 23, 2013
1 parent 86dda7a commit 09a616c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/sequel/extensions/pg_loose_count.rb
Expand Up @@ -22,7 +22,7 @@ module LooseCount
# Look at the table statistics for the given table to get
# an approximate count of the number of rows.
def loose_count(table)
from(:pg_class).where(:oid=>regclass_oid(table)).get(:reltuples)
from(:pg_class).where(:oid=>regclass_oid(table)).get(Sequel.cast(:reltuples, Integer))
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/adapters/postgres_spec.rb
Expand Up @@ -64,6 +64,7 @@ def logger.method_missing(m, msg)
specify "should support pg_loose_count extension" do
@db.extension :pg_loose_count
@db.create_table(:tmp_dolls){text :name}
@db.loose_count(:tmp_dolls).should be_a_kind_of(Integer)
@db.loose_count(:tmp_dolls).should == 0
@db.loose_count(:public__tmp_dolls).should == 0
@db[:tmp_dolls].insert('a')
Expand Down
4 changes: 2 additions & 2 deletions spec/extensions/pg_loose_count_spec.rb
Expand Up @@ -7,11 +7,11 @@

specify "should add loose_count method getting fast count for entire table using table statistics" do
@db.loose_count(:a).should == 1
@db.sqls.should == ["SELECT reltuples FROM pg_class WHERE (oid = CAST(CAST('a' AS regclass) AS oid)) LIMIT 1"]
@db.sqls.should == ["SELECT CAST(reltuples AS integer) AS v FROM pg_class WHERE (oid = CAST(CAST('a' AS regclass) AS oid)) LIMIT 1"]
end

specify "should support schema qualified tables" do
@db.loose_count(:a__b).should == 1
@db.sqls.should == ["SELECT reltuples FROM pg_class WHERE (oid = CAST(CAST('a.b' AS regclass) AS oid)) LIMIT 1"]
@db.sqls.should == ["SELECT CAST(reltuples AS integer) AS v FROM pg_class WHERE (oid = CAST(CAST('a.b' AS regclass) AS oid)) LIMIT 1"]
end
end

0 comments on commit 09a616c

Please sign in to comment.