mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-12 18:20:26 +01:00
Add getLastErrorDoc and getLastError
This commit is contained in:
parent
13380b5eb9
commit
79c1edbdfb
@ -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;
|
||||
};
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user