diff --git a/MongoDB/include/Poco/MongoDB/Array.h b/MongoDB/include/Poco/MongoDB/Array.h index 55e2628406..a3bf7f27f8 100644 --- a/MongoDB/include/Poco/MongoDB/Array.h +++ b/MongoDB/include/Poco/MongoDB/Array.h @@ -39,8 +39,31 @@ class MongoDB_API Array: public Document virtual ~Array(); /// Destroys the Array. + // Document template functions available for backward compatibility + using Document::add; + using Document::get; + template - T get(int pos) const + Document& add(T value) + /// Creates an element with the name from the current pos and value and + /// adds it to the array document. + /// + /// The active document is returned to allow chaining of the add methods. + { + return Document::add(Poco::NumberFormatter::format(size()), value); + } + + Document& add(const char* value) + /// Creates an element with a name from the current pos and value and + /// adds it to the array document. + /// + /// The active document is returned to allow chaining of the add methods. + { + return Document::add(Poco::NumberFormatter::format(size()), value); + } + + template + T get(std::size_t pos) const /// Returns the element at the given index and tries to convert /// it to the template type. If the element is not found, a /// Poco::NotFoundException will be thrown. If the element cannot be @@ -50,7 +73,7 @@ class MongoDB_API Array: public Document } template - T get(int pos, const T& deflt) const + T get(std::size_t pos, const T& deflt) const /// Returns the element at the given index and tries to convert /// it to the template type. If the element is not found, or /// has the wrong type, the deflt argument will be returned. @@ -58,12 +81,12 @@ class MongoDB_API Array: public Document return Document::get(Poco::NumberFormatter::format(pos), deflt); } - Element::Ptr get(int pos) const; + Element::Ptr get(std::size_t pos) const; /// Returns the element at the given index. /// An empty element will be returned if the element is not found. template - bool isType(int pos) const + bool isType(std::size_t pos) const /// Returns true if the type of the element equals the TypeId of ElementTrait, /// otherwise false. { @@ -72,6 +95,9 @@ class MongoDB_API Array: public Document std::string toString(int indent = 0) const; /// Returns a string representation of the Array. + +private: + friend void BSONReader::read(Array::Ptr& to); }; diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp index c6d96d1371..8505f24105 100644 --- a/MongoDB/src/Array.cpp +++ b/MongoDB/src/Array.cpp @@ -31,7 +31,7 @@ Array::~Array() } -Element::Ptr Array::get(int pos) const +Element::Ptr Array::get(std::size_t pos) const { std::string name = Poco::NumberFormatter::format(pos); return Document::get(name); diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp index 9105ea9dda..949f8e3024 100644 --- a/MongoDB/testsuite/src/MongoDBTest.cpp +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -10,6 +10,7 @@ #include "Poco/DateTime.h" #include "Poco/ObjectPool.h" +#include "Poco/MongoDB/Array.h" #include "Poco/MongoDB/InsertRequest.h" #include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/DeleteRequest.h" @@ -77,6 +78,41 @@ void MongoDBTest::testInsertRequest() _mongo->sendRequest(request); } +void MongoDBTest::testArray() +{ + Poco::MongoDB::Array::Ptr arr = new Poco::MongoDB::Array(); + + arr->add(std::string("First")); + + Poco::DateTime birthdate; + birthdate.assign(1969, 3, 9); + arr->add(birthdate.timestamp()); + + arr->add(static_cast(1993)); + arr->add(false); + + // Document-style interface + arr->add("4", "12.4"); + + assertEqual(arr->size(), 5); + assertTrue(arr->exists("0")); + assertTrue(arr->exists("1")); + assertTrue(arr->exists("2")); + assertTrue(arr->exists("3")); + assertFalse(arr->exists("4")); + + assertEqual(arr->get(0), "First"); + assertEqual(arr->get(1).raw(), birthdate.timestamp().raw()); + assertEqual(arr->get(2), 1993); + assertEqual(arr->get(3), false); + assertEqual(arr->get(4), "12.4"); + + // Document-style interface + assertEqual(arr->get("2"), 1993); + assertEqual(arr->get("4"), "12.4"); + +} + void MongoDBTest::testQueryRequest() { @@ -472,6 +508,7 @@ CppUnit::Test* MongoDBTest::suite() CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest"); CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo); CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest); + CppUnit_addTest(pSuite, MongoDBTest, testArray); CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest); CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest); CppUnit_addTest(pSuite, MongoDBTest, testCountCommand); diff --git a/MongoDB/testsuite/src/MongoDBTest.h b/MongoDB/testsuite/src/MongoDBTest.h index 4c5954612a..6ec23d728d 100644 --- a/MongoDB/testsuite/src/MongoDBTest.h +++ b/MongoDB/testsuite/src/MongoDBTest.h @@ -27,6 +27,7 @@ class MongoDBTest: public CppUnit::TestCase virtual ~MongoDBTest(); void testInsertRequest(); + void testArray(); void testQueryRequest(); void testDBQueryRequest(); void testCountCommand();