add DB#last_status method

This commit is contained in:
Mike Dirolf 2009-10-01 12:01:37 -04:00
parent f1bf2eb4d6
commit 79e76be9df
2 changed files with 16 additions and 8 deletions

View File

@ -267,22 +267,19 @@ module Mongo
# Returns the error message from the most recently executed database
# operation for this connection, or +nil+ if there was no error.
#
# Note: as of this writing, errors are only detected on the db server
# for certain kinds of operations (writes). The plan is to change this
# so that all operations will set the error if needed.
def error
doc = db_command(:getlasterror => 1)
raise "error retrieving last error: #{doc}" unless ok?(doc)
doc['err']
end
# Get status information from the last operation on this connection.
def last_status
db_command(:getlasterror => 1)
end
# Returns +true+ if an error was caused by the most recently executed
# database operation.
#
# Note: as of this writing, errors are only detected on the db server
# for certain kinds of operations (writes). The plan is to change this
# so that all operations will set the error if needed.
def error?
error != nil
end

View File

@ -176,6 +176,17 @@ class DBTest < Test::Unit::TestCase
assert_nil @@db.previous_error
end
def test_last_status
@@db['test'].clear
@@db['test'].save("i" => 1)
@@db['test'].update({"i" => 1}, {"$set" => {"i" => 2}})
assert @@db.last_status()["updatedExisting"]
@@db['test'].update({"i" => 1}, {"$set" => {"i" => 500}})
assert !@@db.last_status()["updatedExisting"]
end
def test_text_port_number
db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
# If there is no error, all is well