mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-29 12:18:01 +01:00
GH #440 MongoDB ObjectId string formatting
This commit is contained in:
@@ -62,6 +62,8 @@ Release 1.5.3 (2014-05-xx)
|
|||||||
- fixed GH #341: Compiling poco-1.5.2 for Cygwin
|
- fixed GH #341: Compiling poco-1.5.2 for Cygwin
|
||||||
- fixed GH #305: There are bugs in Buffer.h
|
- fixed GH #305: There are bugs in Buffer.h
|
||||||
- fixed GH #321: trivial build fixes (BB QNX build)
|
- fixed GH #321: trivial build fixes (BB QNX build)
|
||||||
|
- fixed GH #440: MongoDB ObjectId string formatting
|
||||||
|
- added SevenZip library (Guenter Obiltschnig)
|
||||||
|
|
||||||
Release 1.5.2 (2013-09-16)
|
Release 1.5.2 (2013-09-16)
|
||||||
==========================
|
==========================
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class MongoDB_API ObjectId
|
|||||||
public:
|
public:
|
||||||
typedef SharedPtr<ObjectId> Ptr;
|
typedef SharedPtr<ObjectId> Ptr;
|
||||||
|
|
||||||
ObjectId();
|
ObjectId(const std::string& id = "");
|
||||||
/// Constructor
|
/// Constructor
|
||||||
|
|
||||||
virtual ~ObjectId();
|
virtual ~ObjectId();
|
||||||
@@ -55,8 +55,10 @@ public:
|
|||||||
Timestamp timestamp() const;
|
Timestamp timestamp() const;
|
||||||
/// Returns the timestamp which is stored in the first four bytes of the id
|
/// Returns the timestamp which is stored in the first four bytes of the id
|
||||||
|
|
||||||
std::string toString() const;
|
std::string toString(const std::string& fmt = "%x") const;
|
||||||
/// Returns the id in string format
|
/// Returns the id in string format. The fmt parameter
|
||||||
|
/// specifies the formatting used for individual members
|
||||||
|
/// of the ID char array.
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned char _id[12];
|
unsigned char _id[12];
|
||||||
@@ -85,9 +87,11 @@ struct ElementTraits<ObjectId::Ptr>
|
|||||||
{
|
{
|
||||||
enum { TypeId = 0x07 };
|
enum { TypeId = 0x07 };
|
||||||
|
|
||||||
static std::string toString(const ObjectId::Ptr& id, int indent = 0)
|
static std::string toString(const ObjectId::Ptr& id,
|
||||||
|
int indent = 0,
|
||||||
|
const std::string& fmt = "%x")
|
||||||
{
|
{
|
||||||
return id->toString();
|
return id->toString(fmt);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,18 @@ namespace Poco {
|
|||||||
namespace MongoDB {
|
namespace MongoDB {
|
||||||
|
|
||||||
|
|
||||||
ObjectId::ObjectId()
|
ObjectId::ObjectId(const std::string& id)
|
||||||
{
|
{
|
||||||
|
if (id.size() == 12)
|
||||||
|
{
|
||||||
|
std::string::const_iterator it = id.begin();
|
||||||
|
std::string::const_iterator end = id.end();
|
||||||
|
for (int i = 0; i < 12; ++it, ++i) _id[i] = *it;
|
||||||
|
}
|
||||||
|
else if (id.size())
|
||||||
|
{
|
||||||
|
throw Poco::InvalidArgumentException("ID must be 12 characters long.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -34,15 +44,16 @@ ObjectId::~ObjectId()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string ObjectId::toString() const
|
std::string ObjectId::toString(const std::string& fmt) const
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
|
|
||||||
for(int i = 0; i < 12; ++i)
|
for(int i = 0; i < 12; ++i)
|
||||||
{
|
{
|
||||||
s += format("%x", (unsigned int) _id[i]);
|
s += format(fmt, (unsigned int) _id[i]);
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} } // namespace Poco::MongoDB
|
} } // namespace Poco::MongoDB
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include "Poco/MongoDB/PoolableConnectionFactory.h"
|
#include "Poco/MongoDB/PoolableConnectionFactory.h"
|
||||||
#include "Poco/MongoDB/Database.h"
|
#include "Poco/MongoDB/Database.h"
|
||||||
#include "Poco/MongoDB/Cursor.h"
|
#include "Poco/MongoDB/Cursor.h"
|
||||||
|
#include "Poco/MongoDB/ObjectId.h"
|
||||||
|
|
||||||
#include "Poco/Net/NetException.h"
|
#include "Poco/Net/NetException.h"
|
||||||
|
|
||||||
@@ -30,46 +31,58 @@
|
|||||||
using namespace Poco::MongoDB;
|
using namespace Poco::MongoDB;
|
||||||
|
|
||||||
|
|
||||||
MongoDBTest::MongoDBTest(const std::string& name)
|
bool MongoDBTest::_connected = false;
|
||||||
: CppUnit::TestCase("MongoDB")
|
Poco::MongoDB::Connection MongoDBTest::_mongo;
|
||||||
, _connected(false)
|
|
||||||
|
|
||||||
|
MongoDBTest::MongoDBTest(const std::string& name):
|
||||||
|
CppUnit::TestCase("MongoDB"),
|
||||||
|
_host("localhost"),
|
||||||
|
_port(27017)
|
||||||
{
|
{
|
||||||
|
if (!_connected)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_mongo.connect(_host, _port);
|
||||||
|
_connected = true;
|
||||||
|
std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl;
|
||||||
|
}
|
||||||
|
catch (Poco::Net::ConnectionRefusedException& e)
|
||||||
|
{
|
||||||
|
std::cout << "Couldn't connect to " << e.message() << ". " << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
MongoDBTest::~MongoDBTest()
|
MongoDBTest::~MongoDBTest()
|
||||||
{
|
{
|
||||||
|
if (_connected)
|
||||||
|
{
|
||||||
|
_mongo.disconnect();
|
||||||
|
_connected = false;
|
||||||
|
std::cout << "Disconnected from [" << _host << ':' << _port << ']' << std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MongoDBTest::setUp()
|
void MongoDBTest::setUp()
|
||||||
{
|
{
|
||||||
try
|
|
||||||
{
|
|
||||||
_mongo.connect("localhost", 27017);
|
|
||||||
_connected = true;
|
|
||||||
}
|
|
||||||
catch(Poco::Net::ConnectionRefusedException& e)
|
|
||||||
{
|
|
||||||
std::cout << "Couldn't connect to " << e.message() << ". ";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MongoDBTest::tearDown()
|
void MongoDBTest::tearDown()
|
||||||
{
|
{
|
||||||
if ( _connected )
|
|
||||||
{
|
|
||||||
_mongo.disconnect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void MongoDBTest::testInsertRequest()
|
void MongoDBTest::testInsertRequest()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,9 +110,9 @@ void MongoDBTest::testInsertRequest()
|
|||||||
|
|
||||||
void MongoDBTest::testQueryRequest()
|
void MongoDBTest::testQueryRequest()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,9 +158,9 @@ void MongoDBTest::testQueryRequest()
|
|||||||
|
|
||||||
void MongoDBTest::testDBQueryRequest()
|
void MongoDBTest::testDBQueryRequest()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,9 +204,9 @@ void MongoDBTest::testDBQueryRequest()
|
|||||||
|
|
||||||
void MongoDBTest::testCountCommand()
|
void MongoDBTest::testCountCommand()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,9 +233,9 @@ void MongoDBTest::testCountCommand()
|
|||||||
|
|
||||||
void MongoDBTest::testDBCountCommand()
|
void MongoDBTest::testDBCountCommand()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,9 +260,9 @@ void MongoDBTest::testDBCountCommand()
|
|||||||
|
|
||||||
void MongoDBTest::testDBCount2Command()
|
void MongoDBTest::testDBCount2Command()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,9 +274,9 @@ void MongoDBTest::testDBCount2Command()
|
|||||||
|
|
||||||
void MongoDBTest::testDeleteRequest()
|
void MongoDBTest::testDeleteRequest()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,9 +289,9 @@ void MongoDBTest::testDeleteRequest()
|
|||||||
|
|
||||||
void MongoDBTest::testCursorRequest()
|
void MongoDBTest::testCursorRequest()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,9 +338,9 @@ void MongoDBTest::testCursorRequest()
|
|||||||
|
|
||||||
void MongoDBTest::testBuildInfo()
|
void MongoDBTest::testBuildInfo()
|
||||||
{
|
{
|
||||||
if ( ! _connected )
|
if (!_connected)
|
||||||
{
|
{
|
||||||
std::cout << "test skipped." << std::endl;
|
std::cout << "Not connected, test skipped." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -361,7 +374,8 @@ void MongoDBTest::testBuildInfo()
|
|||||||
|
|
||||||
void MongoDBTest::testConnectionPool()
|
void MongoDBTest::testConnectionPool()
|
||||||
{
|
{
|
||||||
Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory("localhost:27017");
|
Poco::Net::SocketAddress sa(_host, _port);
|
||||||
|
Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory(sa);
|
||||||
Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);
|
Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);
|
||||||
|
|
||||||
Poco::MongoDB::PooledConnection pooledConnection(pool);
|
Poco::MongoDB::PooledConnection pooledConnection(pool);
|
||||||
@@ -385,6 +399,29 @@ void MongoDBTest::testConnectionPool()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MongoDBTest::testObjectID()
|
||||||
|
{
|
||||||
|
std::string str;
|
||||||
|
str.append(1, (char) 0x53);
|
||||||
|
str.append(1, (char) 0x6A);
|
||||||
|
str.append(1, (char) 0xEE);
|
||||||
|
str.append(1, (char) 0xBB);
|
||||||
|
str.append(1, (char) 0xA0);
|
||||||
|
str.append(1, (char) 0x81);
|
||||||
|
str.append(1, (char) 0xDE);
|
||||||
|
str.append(1, (char) 0x68);
|
||||||
|
str.append(1, (char) 0x15);
|
||||||
|
str.append(1, (char) 0x00);
|
||||||
|
str.append(1, (char) 0x00);
|
||||||
|
str.append(1, (char) 0x02);
|
||||||
|
|
||||||
|
ObjectId oid(str);
|
||||||
|
std::string str2 = oid.toString("%02x");
|
||||||
|
assert(str2 == "536aeebba081de6815000002");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
CppUnit::Test* MongoDBTest::suite()
|
CppUnit::Test* MongoDBTest::suite()
|
||||||
{
|
{
|
||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
||||||
@@ -399,6 +436,7 @@ CppUnit::Test* MongoDBTest::suite()
|
|||||||
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest);
|
||||||
|
CppUnit_addTest(pSuite, MongoDBTest, testObjectID);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,50 +30,28 @@ public:
|
|||||||
|
|
||||||
virtual ~MongoDBTest();
|
virtual ~MongoDBTest();
|
||||||
|
|
||||||
|
|
||||||
void testInsertRequest();
|
void testInsertRequest();
|
||||||
|
|
||||||
|
|
||||||
void testQueryRequest();
|
void testQueryRequest();
|
||||||
|
|
||||||
|
|
||||||
void testDBQueryRequest();
|
void testDBQueryRequest();
|
||||||
|
|
||||||
|
|
||||||
void testCountCommand();
|
void testCountCommand();
|
||||||
|
|
||||||
|
|
||||||
void testDBCountCommand();
|
void testDBCountCommand();
|
||||||
|
|
||||||
|
|
||||||
void testDBCount2Command();
|
void testDBCount2Command();
|
||||||
|
|
||||||
|
|
||||||
void testDeleteRequest();
|
void testDeleteRequest();
|
||||||
|
|
||||||
|
|
||||||
void testBuildInfo();
|
void testBuildInfo();
|
||||||
|
|
||||||
|
|
||||||
void testConnectionPool();
|
void testConnectionPool();
|
||||||
|
|
||||||
|
|
||||||
void testCursorRequest();
|
void testCursorRequest();
|
||||||
|
void testObjectID();
|
||||||
|
|
||||||
void setUp();
|
void setUp();
|
||||||
|
|
||||||
|
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|
||||||
|
|
||||||
static CppUnit::Test* suite();
|
static CppUnit::Test* suite();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool _connected;
|
std::string _host;
|
||||||
|
unsigned _port;
|
||||||
Poco::MongoDB::Connection _mongo;
|
static bool _connected;
|
||||||
|
static Poco::MongoDB::Connection _mongo;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user