deprecate DB#count and passing a selector to Collection#count

This commit is contained in:
Mike Dirolf 2009-08-18 11:48:16 -04:00
parent 103224b800
commit 7b4e51e825
4 changed files with 19 additions and 18 deletions

View File

@ -359,10 +359,15 @@ EOS
@db.collections_info(@name).next_object()['options'] @db.collections_info(@name).next_object()['options']
end end
# Return the number of records that match +selector+. If +selector+ is # Get the number of documents in this collection.
# +nil+ or an empty hash, returns the count of all records. #
def count(selector={}) # Specifying a +selector+ is DEPRECATED and will be removed. Please use
@db.count(@name, selector || {}) # find(selector).count() instead.
def count(selector=nil)
if selector
warn "specifying a selector for Collection#count is deprecated and will be removed. Please use Collection.find(selector).count instead."
end
find(selector || {}).count()
end end
protected protected

View File

@ -396,17 +396,10 @@ module XGen
} }
end end
# Return the number of records in +collection_name+ that match # DEPRECATED - use Collection.find(selector).count() instead
# +selector+. If +selector+ is +nil+ or an empty hash, returns the
# count of all records. Normally called by Collection#count.
def count(collection_name, selector={}) def count(collection_name, selector={})
oh = OrderedHash.new warn "DB#count is deprecated and will be removed. Please use Collection.find(selector).count instead."
oh[:count] = collection_name collection(collection_name).find(selector).count()
oh[:query] = selector || {}
doc = db_command(oh)
return doc['n'].to_i if ok?(doc)
return 0 if doc['errmsg'] == "ns missing"
raise "Error with count command: #{doc.inspect}"
end end
# Dereference a DBRef, getting the document it points to. # Dereference a DBRef, getting the document it points to.

View File

@ -104,9 +104,12 @@ class TestCollection < Test::Unit::TestCase
@@test.drop @@test.drop
assert_equal 0, @@test.count assert_equal 0, @@test.count
@@test.save({}) @@test.save("x" => 1)
@@test.save({}) @@test.save("x" => 2)
assert_equal 2, @@test.count assert_equal 2, @@test.count
# TODO remove this test - it's deprecated
assert_equal 1, @@test.count("x" => 1)
end end
def test_find_one def test_find_one

View File

@ -536,8 +536,8 @@ class DBAPITest < Test::Unit::TestCase
@@coll.insert('a' => 3) @@coll.insert('a' => 3)
assert_equal 3, @@coll.count assert_equal 3, @@coll.count
assert_equal 1, @@coll.count('$where' => Code.new('this.a > 2')) assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count()
assert_equal 2, @@coll.count('$where' => Code.new('this.a > i', {'i' => 1})) assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count()
end end
def test_eval def test_eval