mirror of
https://github.com/pocoproject/poco.git
synced 2025-02-21 06:37:42 +01:00
commit
f0f7f0fd3c
@ -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;
|
||||
};
|
||||
|
@ -116,7 +116,7 @@ public:
|
||||
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& name)
|
||||
T get(const std::string& name) const
|
||||
/// Returns the element with the given name and tries to convert
|
||||
/// it to the template type. When the element is not found, a
|
||||
/// NotFoundException will be thrown. When the element can't be
|
||||
@ -141,7 +141,33 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
Element::Ptr get(const std::string& name);
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& name, const T& def) const
|
||||
/// Returns the element with the given name and tries to convert
|
||||
/// it to the template type. When the element is not found, or
|
||||
/// has the wrong type, the def argument will be returned.
|
||||
{
|
||||
Element::Ptr element = get(name);
|
||||
if ( element.isNull() )
|
||||
{
|
||||
return def;
|
||||
}
|
||||
|
||||
if ( ElementTraits<T>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
if ( concrete != NULL )
|
||||
{
|
||||
return concrete->value();
|
||||
}
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
|
||||
Element::Ptr get(const std::string& name) const;
|
||||
/// Returns the element with the given name.
|
||||
/// An empty element will be returned when the element is not found.
|
||||
|
||||
|
@ -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);
|
||||
|
@ -59,11 +59,11 @@ Document::~Document()
|
||||
}
|
||||
|
||||
|
||||
Element::Ptr Document::get(const std::string& name)
|
||||
Element::Ptr Document::get(const std::string& name) const
|
||||
{
|
||||
Element::Ptr element;
|
||||
|
||||
ElementSet::iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
|
||||
ElementSet::const_iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
|
||||
if ( it != _elements.end() )
|
||||
{
|
||||
return *it;
|
||||
|
@ -319,9 +319,11 @@ void MongoDBTest::testCursorRequest()
|
||||
|
||||
int n = 0;
|
||||
Poco::MongoDB::ResponseMessage& response = cursor.next(_mongo);
|
||||
while(response.cursorID() != 0)
|
||||
while(1)
|
||||
{
|
||||
n += response.documents().size();
|
||||
if ( response.cursorID() == 0 )
|
||||
break;
|
||||
response = cursor.next(_mongo);
|
||||
}
|
||||
std::cout << "n= " << n << std::endl;
|
||||
|
Loading…
x
Reference in New Issue
Block a user