Add getLastErrorDoc and getLastError

This commit is contained in:
fbraem 2013-02-19 22:32:11 +01:00
parent 13380b5eb9
commit 79c1edbdfb
2 changed files with 39 additions and 0 deletions

View File

@ -80,6 +80,14 @@ public:
/// The collectionname must not contain the database name!
Document::Ptr getLastErrorDoc(Connection& connection) const;
/// Sends the getLastError command to the database and returns the document
std::string getLastError(Connection& connection) const;
/// Sends the getLastError command to the database and returns the err element
/// from the error document. When err is null, an empty string is returned.
private:
std::string _dbname;
};

View File

@ -69,6 +69,37 @@ double Database::count(Connection& connection, const std::string& collectionName
}
Document::Ptr Database::getLastErrorDoc(Connection& connection) const
{
Document::Ptr errorDoc;
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd");
request->setNumberToReturn(1);
request->query().add("getLastError", 1);
Poco::MongoDB::ResponseMessage response;
connection.sendRequest(*request, response);
if ( response.documents().size() > 0 )
{
errorDoc = response.documents()[0];
}
return errorDoc;
}
std::string Database::getLastError(Connection& connection) const
{
Document::Ptr errorDoc = getLastErrorDoc(connection);
if ( !errorDoc.isNull() && errorDoc->isType<std::string>("err") )
{
return errorDoc->get<std::string>("err");
}
return "";
}
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const std::string& collectionName) const
{
return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);