From 26e89db0dd41373fa5e8d3ea93cd15065931f016 Mon Sep 17 00:00:00 2001 From: fbraem Date: Fri, 1 Mar 2013 21:42:51 +0100 Subject: [PATCH 1/4] Add createUpdateRequest, createDeleteRequest --- MongoDB/include/Poco/MongoDB/Database.h | 35 +++++++++++++++++++++++++ MongoDB/src/Database.cpp | 12 --------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/MongoDB/include/Poco/MongoDB/Database.h b/MongoDB/include/Poco/MongoDB/Database.h index 9da958ce2..9b3c9f1f5 100644 --- a/MongoDB/include/Poco/MongoDB/Database.h +++ b/MongoDB/include/Poco/MongoDB/Database.h @@ -42,6 +42,8 @@ #include "Poco/MongoDB/Document.h" #include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/InsertRequest.h" +#include "Poco/MongoDB/UpdateRequest.h" +#include "Poco/MongoDB/DeleteRequest.h" namespace Poco { namespace MongoDB { @@ -75,6 +77,11 @@ public: /// the database name! + Poco::SharedPtr createDeleteRequest(const std::string& collectionName) const; + /// Creates a DeleteRequest to delete documents in the given collection. + /// The collectionname must not contain the database name! + + Poco::SharedPtr createInsertRequest(const std::string& collectionName) const; /// Creates an InsertRequest to insert new documents in the given collection. /// The collectionname must not contain the database name! @@ -84,6 +91,10 @@ public: /// Creates a QueryRequest. The collectionname must not contain the database name! + Poco::SharedPtr createUpdateRequest(const std::string& collectionName) const; + /// Creates an UpdateRequest. The collectionname must not contain the database name! + + Poco::MongoDB::Document::Ptr ensureIndex(Connection& connection, const std::string& collection, const std::string& indexName, Poco::MongoDB::Document::Ptr keys, bool unique = false, bool background = false, int version = 0, int ttl = 0); /// Creates an index. The document returned is the result of a getLastError call. /// For more info look at the ensureIndex information on the MongoDB website. @@ -109,6 +120,30 @@ inline Poco::SharedPtr Database::createCommand() co return cmd; } + +inline Poco::SharedPtr Database::createDeleteRequest(const std::string& collectionName) const +{ + return new Poco::MongoDB::DeleteRequest(_dbname + '.' + collectionName); +} + + +inline Poco::SharedPtr Database::createInsertRequest(const std::string& collectionName) const +{ + return new Poco::MongoDB::InsertRequest(_dbname + '.' + collectionName); +} + + +inline Poco::SharedPtr Database::createQueryRequest(const std::string& collectionName) const +{ + return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName); +} + + +inline Poco::SharedPtr Database::createUpdateRequest(const std::string& collectionName) const +{ + return new Poco::MongoDB::UpdateRequest(_dbname + '.' + collectionName); +} + }} // Namespace Poco::MongoDB #endif // _MongoDB_Database_included diff --git a/MongoDB/src/Database.cpp b/MongoDB/src/Database.cpp index d4a1c454c..72bf756e7 100644 --- a/MongoDB/src/Database.cpp +++ b/MongoDB/src/Database.cpp @@ -147,16 +147,4 @@ Poco::SharedPtr Database::createCountRequest(const } -Poco::SharedPtr Database::createInsertRequest(const std::string& collectionName) const -{ - return new Poco::MongoDB::InsertRequest(_dbname + '.' + collectionName); -} - - -Poco::SharedPtr Database::createQueryRequest(const std::string& collectionName) const -{ - return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName); -} - - }} // Namespace Poco::MongoDB From 39fc777a6be7d9874cb42c6c7e7092df58ca5078 Mon Sep 17 00:00:00 2001 From: fbraem Date: Fri, 1 Mar 2013 21:43:12 +0100 Subject: [PATCH 2/4] Add sample11, 12 and 13 --- MongoDB/samples/SQLToMongo/src/main.cpp | 60 ++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/MongoDB/samples/SQLToMongo/src/main.cpp b/MongoDB/samples/SQLToMongo/src/main.cpp index eae1dbb74..efee461c6 100644 --- a/MongoDB/samples/SQLToMongo/src/main.cpp +++ b/MongoDB/samples/SQLToMongo/src/main.cpp @@ -411,11 +411,66 @@ void sample10(Poco::MongoDB::Connection& connection) } +// SELECT COUNT(*) FROM players WHERE birthyear > 1980 +void sample11(Poco::MongoDB::Connection& connection) +{ + std::cout << "*** SAMPLE 11 ***" << std::endl; + + Poco::MongoDB::Database db("sample"); + Poco::SharedPtr count = db.createCountRequest("players"); + count->selector().addNewDocument("query") + .addNewDocument("birthyear") + .add("$gt", 1980); + + Poco::MongoDB::ResponseMessage response; + connection.sendRequest(*count, response); + + if ( response.hasDocuments() ) + { + std::cout << "Count: " << response.documents()[0]->get("n") << std::endl; + } +} + + +//UPDATE players SET birthyear = birthyear + 1 WHERE firstname = 'Victor' +void sample12(Poco::MongoDB::Connection& connection) +{ + std::cout << "*** SAMPLE 12 ***" << std::endl; + + Poco::MongoDB::Database db("sample"); + Poco::SharedPtr request = db.createUpdateRequest("players"); + request->selector().add("firstname", "Victor"); + + request->update().addNewDocument("$inc").add("birthyear", 1); + + connection.sendRequest(*request); + + Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection); + std::cout << "Count: " << lastError->toString(2); +} + + +//DELETE players WHERE firstname = 'Victor' +void sample13(Poco::MongoDB::Connection& connection) +{ + std::cout << "*** SAMPLE 13 ***" << std::endl; + + Poco::MongoDB::Database db("sample"); + Poco::SharedPtr request = db.createDeleteRequest("players"); + request->selector().add("firstname", "Victor"); + + connection.sendRequest(*request); + + Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection); + std::cout << "Count: " << lastError->toString(2); +} + + int main(int argc, char** argv) { Poco::MongoDB::Connection connection("localhost", 27017); - sample1(connection); +// sample1(connection); sample2(connection); sample3(connection); sample4(connection); @@ -425,6 +480,9 @@ int main(int argc, char** argv) sample8(connection); sample9(connection); sample10(connection); + sample11(connection); + sample12(connection); + sample13(connection); return 0; } From e793083a0a4f915229235daaf7c4165ef4cb35ca Mon Sep 17 00:00:00 2001 From: fbraem Date: Fri, 1 Mar 2013 21:59:50 +0100 Subject: [PATCH 3/4] Correct indentation --- MongoDB/src/Document.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/MongoDB/src/Document.cpp b/MongoDB/src/Document.cpp index 90aa18fbf..6d2c2d98a 100644 --- a/MongoDB/src/Document.cpp +++ b/MongoDB/src/Document.cpp @@ -150,7 +150,13 @@ void Document::read(BinaryReader& reader) std::string Document::toString(int indent) const { std::ostringstream oss; - oss << "{" << std::endl; + + oss << "{"; + if ( indent > 0 ) + { + oss << std::endl; + } + for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) { if ( it != _elements.begin() ) @@ -189,7 +195,8 @@ std::string Document::toString(int indent) const } } - oss << "}" << std::endl; + oss << "}"; + return oss.str(); } From b860cd2cbb08751138510b0a04c3d0808b30cf89 Mon Sep 17 00:00:00 2001 From: fbraem Date: Fri, 1 Mar 2013 22:06:47 +0100 Subject: [PATCH 4/4] Add some newlines in output --- MongoDB/samples/SQLToMongo/src/main.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/MongoDB/samples/SQLToMongo/src/main.cpp b/MongoDB/samples/SQLToMongo/src/main.cpp index efee461c6..572b3ac06 100644 --- a/MongoDB/samples/SQLToMongo/src/main.cpp +++ b/MongoDB/samples/SQLToMongo/src/main.cpp @@ -368,7 +368,7 @@ void sample9(Poco::MongoDB::Connection& connection) connection.sendRequest(query, response); if ( response.hasDocuments() ) { - std::cout << response.documents()[0]->toString(2); + std::cout << response.documents()[0]->toString(2) << std::endl; } // QueryRequest can be created using the Database class @@ -378,7 +378,7 @@ void sample9(Poco::MongoDB::Connection& connection) connection.sendRequest(*queryPtr, response); if ( response.hasDocuments() ) { - std::cout << response.documents()[0]->toString(2); + std::cout << response.documents()[0]->toString(2) << std::endl; } } @@ -389,7 +389,6 @@ void sample10(Poco::MongoDB::Connection& connection) Poco::MongoDB::Database db("sample"); Poco::SharedPtr command = db.createCommand(); - std::cout << command->fullCollectionName() << std::endl; command->selector() .add("distinct", "players") @@ -446,7 +445,7 @@ void sample12(Poco::MongoDB::Connection& connection) connection.sendRequest(*request); Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection); - std::cout << "Count: " << lastError->toString(2); + std::cout << "LastError: " << lastError->toString(2) << std::endl; } @@ -462,7 +461,7 @@ void sample13(Poco::MongoDB::Connection& connection) connection.sendRequest(*request); Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(connection); - std::cout << "Count: " << lastError->toString(2); + std::cout << "LastError: " << lastError->toString(2) << std::endl; } @@ -470,7 +469,7 @@ int main(int argc, char** argv) { Poco::MongoDB::Connection connection("localhost", 27017); -// sample1(connection); + sample1(connection); sample2(connection); sample3(connection); sample4(connection);