mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-24 17:30:44 +02:00
Add UUID support
This commit is contained in:
@@ -26,6 +26,7 @@
|
|||||||
#include "Poco/Buffer.h"
|
#include "Poco/Buffer.h"
|
||||||
#include "Poco/StreamCopier.h"
|
#include "Poco/StreamCopier.h"
|
||||||
#include "Poco/MemoryStream.h"
|
#include "Poco/MemoryStream.h"
|
||||||
|
#include "Poco/UUID.h"
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
|
||||||
@@ -45,6 +46,9 @@ public:
|
|||||||
Binary(Poco::Int32 size, unsigned char subtype);
|
Binary(Poco::Int32 size, unsigned char subtype);
|
||||||
/// Constructor
|
/// Constructor
|
||||||
|
|
||||||
|
Binary(const UUID& uuid);
|
||||||
|
/// Constructor for setting a UUID in a binary element
|
||||||
|
|
||||||
virtual ~Binary();
|
virtual ~Binary();
|
||||||
/// Destructor
|
/// Destructor
|
||||||
|
|
||||||
@@ -60,6 +64,10 @@ public:
|
|||||||
std::string toString(int indent = 0) const;
|
std::string toString(int indent = 0) const;
|
||||||
/// Returns the binary encoded in Base64
|
/// Returns the binary encoded in Base64
|
||||||
|
|
||||||
|
UUID uuid() const;
|
||||||
|
/// Returns the UUID when the binary subtype is 0x04.
|
||||||
|
/// Otherwise BadCastException will be thrown
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Buffer<unsigned char> _buffer;
|
Buffer<unsigned char> _buffer;
|
||||||
unsigned char _subtype;
|
unsigned char _subtype;
|
||||||
@@ -109,7 +117,7 @@ inline void BSONReader::read<Binary::Ptr>(Binary::Ptr& to)
|
|||||||
unsigned char subtype;
|
unsigned char subtype;
|
||||||
_reader >> subtype;
|
_reader >> subtype;
|
||||||
to->subtype(subtype);
|
to->subtype(subtype);
|
||||||
|
|
||||||
_reader.readRaw((char*) to->buffer().begin(), size);
|
_reader.readRaw((char*) to->buffer().begin(), size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,13 @@ Binary::Binary(Poco::Int32 size, unsigned char subtype) : _buffer(size), _subtyp
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Binary::Binary(const UUID& uuid) : _buffer(128 / 8), _subtype(0x04)
|
||||||
|
{
|
||||||
|
unsigned char szUUID[16];
|
||||||
|
uuid.copyTo((char*) szUUID);
|
||||||
|
_buffer.assign(szUUID, 16);
|
||||||
|
}
|
||||||
|
|
||||||
Binary::~Binary()
|
Binary::~Binary()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -47,5 +54,15 @@ std::string Binary::toString(int indent) const
|
|||||||
return oss.str();
|
return oss.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UUID Binary::uuid() const
|
||||||
|
{
|
||||||
|
if ( _subtype == 0x04 && _buffer.size() == 16 )
|
||||||
|
{
|
||||||
|
UUID uuid;
|
||||||
|
uuid.copyFrom((const char*) _buffer.begin());
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
throw BadCastException("Invalid subtype");
|
||||||
|
}
|
||||||
|
|
||||||
} } // namespace Poco::MongoDB
|
} } // namespace Poco::MongoDB
|
||||||
|
|||||||
@@ -21,8 +21,10 @@
|
|||||||
#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/MongoDB/ObjectId.h"
|
||||||
|
#include "Poco/MongoDB/Binary.h"
|
||||||
|
|
||||||
#include "Poco/Net/NetException.h"
|
#include "Poco/Net/NetException.h"
|
||||||
|
#include "Poco/UUIDGenerator.h"
|
||||||
|
|
||||||
#include "MongoDBTest.h"
|
#include "MongoDBTest.h"
|
||||||
#include "CppUnit/TestCaller.h"
|
#include "CppUnit/TestCaller.h"
|
||||||
@@ -68,7 +70,6 @@ void MongoDBTest::testInsertRequest()
|
|||||||
player->add("active", false);
|
player->add("active", false);
|
||||||
|
|
||||||
Poco::DateTime now;
|
Poco::DateTime now;
|
||||||
std::cout << now.day() << " " << now.hour() << ":" << now.minute() << ":" << now.second() << std::endl;
|
|
||||||
player->add("lastupdated", now.timestamp());
|
player->add("lastupdated", now.timestamp());
|
||||||
|
|
||||||
player->add("unknown", NullValue());
|
player->add("unknown", NullValue());
|
||||||
@@ -107,7 +108,6 @@ void MongoDBTest::testQueryRequest()
|
|||||||
assert(!active);
|
assert(!active);
|
||||||
|
|
||||||
std::string id = doc->get("_id")->toString();
|
std::string id = doc->get("_id")->toString();
|
||||||
std::cout << id << std::endl;
|
|
||||||
}
|
}
|
||||||
catch(Poco::NotFoundException& nfe)
|
catch(Poco::NotFoundException& nfe)
|
||||||
{
|
{
|
||||||
@@ -146,7 +146,6 @@ void MongoDBTest::testDBQueryRequest()
|
|||||||
assert(doc->isType<NullValue>("unknown"));
|
assert(doc->isType<NullValue>("unknown"));
|
||||||
|
|
||||||
std::string id = doc->get("_id")->toString();
|
std::string id = doc->get("_id")->toString();
|
||||||
std::cout << id << std::endl;
|
|
||||||
}
|
}
|
||||||
catch(Poco::NotFoundException& nfe)
|
catch(Poco::NotFoundException& nfe)
|
||||||
{
|
{
|
||||||
@@ -173,8 +172,6 @@ void MongoDBTest::testCountCommand()
|
|||||||
if ( response.documents().size() > 0 )
|
if ( response.documents().size() > 0 )
|
||||||
{
|
{
|
||||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||||
std::cout << doc->toString() << std::endl;
|
|
||||||
|
|
||||||
assert(doc->getInteger("n") == 1);
|
assert(doc->getInteger("n") == 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -234,7 +231,6 @@ void MongoDBTest::testCursorRequest()
|
|||||||
_mongo->sendRequest(*insertRequest);
|
_mongo->sendRequest(*insertRequest);
|
||||||
|
|
||||||
Poco::Int64 count = db.count(*_mongo, "numbers");
|
Poco::Int64 count = db.count(*_mongo, "numbers");
|
||||||
std::cout << "count= " << count << std::endl;
|
|
||||||
assert(count == 10000);
|
assert(count == 10000);
|
||||||
|
|
||||||
Poco::MongoDB::Cursor cursor("team", "numbers");
|
Poco::MongoDB::Cursor cursor("team", "numbers");
|
||||||
@@ -248,7 +244,6 @@ void MongoDBTest::testCursorRequest()
|
|||||||
break;
|
break;
|
||||||
response = cursor.next(*_mongo);
|
response = cursor.next(*_mongo);
|
||||||
}
|
}
|
||||||
std::cout << "n= " << n << std::endl;
|
|
||||||
assert(n == 10000);
|
assert(n == 10000);
|
||||||
|
|
||||||
Poco::MongoDB::QueryRequest drop("team.$cmd");
|
Poco::MongoDB::QueryRequest drop("team.$cmd");
|
||||||
@@ -257,11 +252,6 @@ void MongoDBTest::testCursorRequest()
|
|||||||
|
|
||||||
Poco::MongoDB::ResponseMessage responseDrop;
|
Poco::MongoDB::ResponseMessage responseDrop;
|
||||||
_mongo->sendRequest(drop, responseDrop);
|
_mongo->sendRequest(drop, responseDrop);
|
||||||
|
|
||||||
if ( responseDrop.documents().size() > 0 )
|
|
||||||
{
|
|
||||||
std::cout << responseDrop.documents()[0]->toString(2) << std::endl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -343,16 +333,63 @@ void MongoDBTest::testCommand() {
|
|||||||
if ( response.documents().size() > 0 )
|
if ( response.documents().size() > 0 )
|
||||||
{
|
{
|
||||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||||
std::cout << doc->toString(2);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(*_mongo);
|
Poco::MongoDB::Document::Ptr lastError = db.getLastErrorDoc(*_mongo);
|
||||||
std::cout << "LastError: " << lastError->toString(2) << std::endl;
|
fail(lastError->toString(2));
|
||||||
fail("Didn't get a response from the command");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MongoDBTest::testUUID()
|
||||||
|
{
|
||||||
|
Poco::MongoDB::Document::Ptr club = new Poco::MongoDB::Document();
|
||||||
|
club->add("name", std::string("Barcelona"));
|
||||||
|
|
||||||
|
Poco::UUIDGenerator generator;
|
||||||
|
Poco::UUID uuid = generator.create();
|
||||||
|
Poco::MongoDB::Binary::Ptr uuidBinary = new Poco::MongoDB::Binary(uuid);
|
||||||
|
club->add("uuid", uuidBinary);
|
||||||
|
|
||||||
|
Poco::MongoDB::InsertRequest request("team.club");
|
||||||
|
request.documents().push_back(club);
|
||||||
|
|
||||||
|
_mongo->sendRequest(request);
|
||||||
|
|
||||||
|
Poco::MongoDB::QueryRequest queryReq("team.club");
|
||||||
|
queryReq.selector().add("name" , std::string("Barcelona"));
|
||||||
|
|
||||||
|
Poco::MongoDB::ResponseMessage response;
|
||||||
|
_mongo->sendRequest(queryReq, response);
|
||||||
|
|
||||||
|
if ( response.documents().size() > 0 )
|
||||||
|
{
|
||||||
|
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
std::string name = doc->get<std::string>("name");
|
||||||
|
assert(name.compare("Barcelona") == 0);
|
||||||
|
|
||||||
|
Poco::MongoDB::Binary::Ptr uuidBinary = doc->get<Binary::Ptr>("uuid");
|
||||||
|
assert(uuid == uuidBinary->uuid());
|
||||||
|
}
|
||||||
|
catch(Poco::NotFoundException& nfe)
|
||||||
|
{
|
||||||
|
fail(nfe.message() + " not found.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fail("No document returned");
|
||||||
|
}
|
||||||
|
|
||||||
|
Poco::MongoDB::DeleteRequest delRequest("team.club");
|
||||||
|
delRequest.selector().add("name", std::string("Barcelona"));
|
||||||
|
_mongo->sendRequest(delRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
CppUnit::Test* MongoDBTest::suite()
|
CppUnit::Test* MongoDBTest::suite()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -368,6 +405,7 @@ CppUnit::Test* MongoDBTest::suite()
|
|||||||
|
|
||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
||||||
|
|
||||||
|
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest);
|
||||||
@@ -376,10 +414,10 @@ CppUnit::Test* MongoDBTest::suite()
|
|||||||
CppUnit_addTest(pSuite, MongoDBTest, testDBCount2Command);
|
CppUnit_addTest(pSuite, MongoDBTest, testDBCount2Command);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
|
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest);
|
CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testObjectID);
|
CppUnit_addTest(pSuite, MongoDBTest, testObjectID);
|
||||||
CppUnit_addTest(pSuite, MongoDBTest, testCommand);
|
CppUnit_addTest(pSuite, MongoDBTest, testCommand);
|
||||||
|
CppUnit_addTest(pSuite, MongoDBTest, testUUID);
|
||||||
|
|
||||||
return pSuite;
|
return pSuite;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ public:
|
|||||||
void testCursorRequest();
|
void testCursorRequest();
|
||||||
void testObjectID();
|
void testObjectID();
|
||||||
void testCommand();
|
void testCommand();
|
||||||
|
void testUUID();
|
||||||
void setUp();
|
void setUp();
|
||||||
void tearDown();
|
void tearDown();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user