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 # Returns the error message from the most recently executed database
# operation for this connection, or +nil+ if there was no error. # 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 def error
doc = db_command(:getlasterror => 1) doc = db_command(:getlasterror => 1)
raise "error retrieving last error: #{doc}" unless ok?(doc) raise "error retrieving last error: #{doc}" unless ok?(doc)
doc['err'] doc['err']
end 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 # Returns +true+ if an error was caused by the most recently executed
# database operation. # 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? def error?
error != nil error != nil
end end

View File

@ -176,6 +176,17 @@ class DBTest < Test::Unit::TestCase
assert_nil @@db.previous_error assert_nil @@db.previous_error
end 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 def test_text_port_number
db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]]) db = DB.new('ruby-mongo-test', [[@@host, @@port.to_s]])
# If there is no error, all is well # If there is no error, all is well