Make it work again

This commit is contained in:
fbraem 2013-02-13 19:10:57 +01:00
parent 913020e89d
commit 9765ccf587
27 changed files with 531 additions and 195 deletions

View File

@ -59,13 +59,20 @@ public:
/// Destructor
};
/*
// BSON Embedded Array
// spec: document
template<>
struct ElementTraits<Array::Ptr>
{
enum { TypeId = 0x04 };
static std::string toString(const Array::Ptr& value)
{
//TODO:
return value.isNull() ? "null" : "[]";
}
};
template<>
@ -79,7 +86,7 @@ inline void BSONWriter::write<Array::Ptr>(Array::Ptr& from)
{
from->write(_writer);
}
*/
}} // Namespace Poco::MongoDB

View File

@ -39,7 +39,6 @@
#define _MongoDB_BSONReader_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
@ -50,7 +49,7 @@ class MongoDB_API BSONReader
/// Class for reading BSON from a Poco::BinaryReader
{
public:
BSONReader(Poco::BinaryReader& reader) : _reader(reader)
BSONReader(const Poco::BinaryReader& reader) : _reader(reader)
/// Constructor
{
}
@ -60,9 +59,13 @@ public:
{
}
void read(Document& v);
template<typename T>
void read(T& t)
/// Reads the value from the reader. The default implementation uses the >> operator to
/// the given argument. Special types can write their own version.
{
_reader >> t;
}
std::string readCString();
/// Reads a cstring from the reader.

View File

@ -38,10 +38,8 @@
#ifndef _MongoDB_BSONWriter_included
#define _MongoDB_BSONWriter_included
#include "Poco/BinaryWriter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
#include "Poco/BinaryWriter.h"
namespace Poco
{
@ -53,15 +51,23 @@ class MongoDB_API BSONWriter
/// Class for writing BSON to a Poco::BinaryWriter.
{
public:
BSONWriter(Poco::BinaryWriter& writer);
BSONWriter(const Poco::BinaryWriter& writer) : _writer(writer)
/// Constructor
{
}
virtual ~BSONWriter();
virtual ~BSONWriter()
/// Destructor
{
}
void write(const Document& v);
template<typename T>
void write(T& t)
/// Writes the value to the writer. The default implementation uses
/// the << operator. Special types can write their own version.
{
_writer << t;
}
void writeCString(const std::string& value);
/// Writes a cstring to the writer. A cstring is a string
@ -72,6 +78,12 @@ private:
Poco::BinaryWriter _writer;
};
inline void BSONWriter::writeCString(const std::string& value)
{
_writer.writeRaw(value);
_writer << (unsigned char) 0x00;
}
}} // Namespace Poco::MongoDB
#endif // _MongoDB_BSONWriter_included

View File

@ -39,13 +39,16 @@
#define _MongoDB_Binary_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/Buffer.h"
#include "Poco/MongoDB/Element.h"
#include "Poco/Base64Encoder.h"
#include "Poco/Buffer.h"
#include "Poco/StreamCopier.h"
#include "Poco/MemoryStream.h"
namespace Poco
{
namespace MongoDB
{
#include <sstream>
namespace Poco {
namespace MongoDB {
class MongoDB_API Binary
/// Implements BSON Binary. It's a wrapper around a Poco::Buffer<unsigned char>.
@ -63,14 +66,11 @@ public:
virtual ~Binary();
/// Destructor
unsigned char* begin();
/// Returns the start of the buffer
Poco::Int32 size() const;
/// Returns the size of the buffer
Buffer<unsigned char>& buffer();
/// Returns a reference to the buffer
unsigned char subtype() const;
@ -81,27 +81,18 @@ public:
/// Sets the subtype
void resize(std::size_t newSize);
/// Resizes the buffer
std::string toString() const;
/// Returns the binary encoded in Base64
private:
Poco::Buffer<unsigned char> _buffer;
Buffer<unsigned char> _buffer;
unsigned char _subtype;
};
inline unsigned char* Binary::begin()
{
return _buffer.begin();
}
inline Poco::Int32 Binary::size() const
{
return _buffer.size();
}
inline unsigned char Binary::subtype() const
{
@ -114,13 +105,24 @@ inline void Binary::subtype(unsigned char type)
_subtype = type;
}
/*
inline Buffer<unsigned char>& Binary::buffer()
{
return _buffer;
}
// BSON Embedded Document
// spec: binary
template<>
struct ElementTraits<Binary::Ptr>
{
enum { TypeId = 0x05 };
static std::string toString(const Binary::Ptr& value)
{
return value.isNull() ? "" : value->toString();
}
};
template<>
@ -129,22 +131,22 @@ inline void BSONReader::read<Binary::Ptr>(Binary::Ptr& to)
Poco::Int32 size;
_reader >> size;
to->resize(size);
to->buffer().resize(size);
unsigned char subtype;
_reader >> subtype;
to->subtype(subtype);
_reader.readRaw((char*) to->begin(), size);
_reader.readRaw((char*) to->buffer().begin(), size);
}
template<>
inline void BSONWriter::write<Binary::Ptr>(Binary::Ptr& from)
{
_writer << from->subtype();
_writer.writeRaw((char*) from->begin(), from->size());
_writer.writeRaw((char*) from->buffer().begin(), from->buffer().size());
}
*/
}} // Namespace Poco::MongoDB

View File

@ -0,0 +1,69 @@
//
// Database.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Database
//
// Definition of the Database class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef _MongoDB_Database_included
#define _MongoDB_Database_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
#include "Poco/MongoDB/QueryRequest.h"
namespace Poco {
namespace MongoDB {
class MongoDB_API Database
{
public:
Database(const std::string& db);
virtual ~Database();
Poco::SharedPtr<Poco::MongoDB::QueryRequest> createQueryRequest(const std::string& collectionName);
Poco::SharedPtr<Poco::MongoDB::QueryRequest> createCountRequest(const std::string& collectionName);
private:
std::string _dbname;
};
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Database_included

View File

@ -38,18 +38,19 @@
#ifndef _MongoDB_Document_included
#define _MongoDB_Document_included
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Struct.h"
#include <algorithm>
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Element.h"
namespace Poco
{
namespace MongoDB
{
typedef Dynamic::Struct<std::string> Document;
typedef SharedPtr<Document> DocumentPtr;
typedef std::vector<DocumentPtr> Documents;
/*
class ElementFindByName
{
public:
@ -184,6 +185,12 @@ template<>
struct ElementTraits<Document::Ptr>
{
enum { TypeId = 0x03 };
static std::string toString(const Document::Ptr& value)
{
//TODO
return value.isNull() ? "null" : "{}";
}
};
template<>
@ -197,7 +204,7 @@ inline void BSONWriter::write<Document::Ptr>(Document::Ptr& from)
{
from->write(_writer);
}
*/
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Document_included

View File

@ -47,7 +47,8 @@
#include "Poco/Timestamp.h"
#include "Poco/RegularExpression.h"
#include "Poco/Nullable.h"
#include "Poco/NumberFormatter.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/BSONReader.h"
#include "Poco/MongoDB/BSONWriter.h"
@ -68,6 +69,9 @@ public:
virtual ~Element();
virtual std::string toString() const = 0;
virtual int type() const = 0;
@ -77,11 +81,11 @@ public:
private:
// virtual void read(BinaryReader& reader) = 0;
virtual void read(BinaryReader& reader) = 0;
// virtual void write(BinaryWriter& writer) = 0;
virtual void write(BinaryWriter& writer) = 0;
// friend class Document;
friend class Document;
std::string _name;
};
@ -116,6 +120,11 @@ template<>
struct ElementTraits<double>
{
enum { TypeId = 0x01 };
static std::string toString(const double& value)
{
return Poco::NumberFormatter::format(value);
}
};
// BSON UTF-8 string
@ -125,8 +134,13 @@ template<>
struct ElementTraits<std::string>
{
enum { TypeId = 0x02 };
static std::string toString(const std::string& value)
{
return value;
}
};
/*
template<>
inline void BSONReader::read<std::string>(std::string& to)
{
@ -142,7 +156,7 @@ inline void BSONWriter::write<std::string>(std::string& from)
_writer << (Poco::Int32) (from.length() + 1);
writeCString(from);
}
*/
// BSON bool
@ -151,8 +165,13 @@ template<>
struct ElementTraits<bool>
{
enum { TypeId = 0x08 };
static std::string toString(const bool& value)
{
return value ? "true" : "false";
}
};
/*
template<>
inline void BSONReader::read<bool>(bool& to)
{
@ -166,13 +185,19 @@ inline void BSONWriter::write<bool>(bool& from)
{
_writer << (from ? 0x01 : 0x00);
}
*/
// BSON 32-bit integer
// spec: int32
template<>
struct ElementTraits<Int32>
{
enum { TypeId = 0x10 };
static std::string toString(const Int32& value)
{
return Poco::NumberFormatter::format(value);
}
};
// BSON UTC datetime
@ -181,8 +206,13 @@ template<>
struct ElementTraits<Timestamp>
{
enum { TypeId = 0x09 };
static std::string toString(const Timestamp& value)
{
return DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z");
}
};
/*
template<>
inline void BSONReader::read<Timestamp>(Timestamp& to)
{
@ -197,7 +227,7 @@ inline void BSONWriter::write<Timestamp>(Timestamp& from)
{
_writer << (from.epochMicroseconds() / 1000);
}
*/
typedef Nullable<unsigned char> NullValue;
// BSON Null Value
@ -206,8 +236,13 @@ template<>
struct ElementTraits<NullValue>
{
enum { TypeId = 0x0A };
static std::string toString(const NullValue& value)
{
return "null";
}
};
/*
template<>
inline void BSONReader::read<NullValue>(NullValue& to)
{
@ -217,7 +252,7 @@ template<>
inline void BSONWriter::write<NullValue>(NullValue& from)
{
}
*/
class RegularExpression
{
@ -318,8 +353,14 @@ template<>
struct ElementTraits<RegularExpression::Ptr>
{
enum { TypeId = 0x0B };
static std::string toString(const RegularExpression::Ptr& value)
{
//TODO
return "RE: not implemented yet";
}
};
/*
template<>
inline void BSONReader::read<RegularExpression::Ptr>(RegularExpression::Ptr& to)
{
@ -335,7 +376,7 @@ inline void BSONWriter::write<RegularExpression::Ptr>(RegularExpression::Ptr& fr
writeCString(from->getPattern());
writeCString(from->getOptions());
}
*/
class JavaScriptCode
{
@ -381,8 +422,13 @@ template<>
struct ElementTraits<JavaScriptCode::Ptr>
{
enum { TypeId = 0x0D };
static std::string toString(const JavaScriptCode::Ptr& value)
{
return value.isNull() ? "" : value->code();
}
};
/*
template<>
inline void BSONReader::read<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& to)
{
@ -398,13 +444,18 @@ inline void BSONWriter::write<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& from)
std::string code = from->code();
BSONWriter(_writer).write(code);
}
*/
// BSON 64-bit integer
// spec: int64
template<>
struct ElementTraits<Int64>
{
enum { TypeId = 0x12 };
static std::string toString(const Int64& value)
{
return NumberFormatter::format(value);
}
};
@ -424,9 +475,12 @@ public:
T value() const { return _value; }
std::string toString() const { return ElementTraits<T>::toString(_value); }
int type() const { return ElementTraits<T>::TypeId; }
/*
void read(BinaryReader& reader)
{
BSONReader(reader).read(_value);
@ -436,7 +490,7 @@ public:
{
BSONWriter(writer).write(_value);
}
*/
private:
T _value;
@ -445,4 +499,3 @@ private:
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Element_included

View File

@ -71,7 +71,7 @@ public:
/// Destructor
Documents& documents();
Document::Vector& documents();
/// Returns the documents to insert into the database
protected:
@ -85,11 +85,11 @@ private:
std::string _fullCollectionName;
Documents _documents;
Document::Vector _documents;
};
inline Documents& InsertRequest::documents()
inline Document::Vector& InsertRequest::documents()
{
return _documents;
}

View File

@ -63,14 +63,23 @@ public:
private:
unsigned char _id[12];
friend class BSONWriter;
friend class BSONReader;
};
/*
// BSON Embedded Document
// spec: ObjectId
template<>
struct ElementTraits<ObjectId::Ptr>
{
enum { TypeId = 0x07 };
static std::string toString(const ObjectId::Ptr& id)
{
return id->toString();
}
};
template<>
@ -84,19 +93,9 @@ inline void BSONWriter::write<ObjectId::Ptr>(ObjectId::Ptr& from)
{
_writer.writeRaw((char*) from->_id, 12);
}
*/
}} // Namespace Poco::MongoDB
namespace Poco {
namespace Dynamic {
template<>
class VarHolderImpl<MongoDB::ObjectId>: public VarHolder
{
};
}} // Poco::Dynamic
#endif //_MongoDB_ObjectId_included

View File

@ -6,12 +6,10 @@
namespace Poco
{
namespace MongoDB
{
template<>
class PoolableObjectFactory<Connection, Connection::Ptr>
class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
{
public:
PoolableObjectFactory(Net::SocketAddress& address)
@ -26,29 +24,29 @@ public:
}
Connection::Ptr createObject()
MongoDB::Connection::Ptr createObject()
{
return new Connection(_address);
return new MongoDB::Connection(_address);
}
bool validateObject(Connection::Ptr pObject)
bool validateObject(MongoDB::Connection::Ptr pObject)
{
return true;
}
void activateObject(Connection::Ptr pObject)
void activateObject(MongoDB::Connection::Ptr pObject)
{
}
void deactivateObject(Connection::Ptr pObject)
void deactivateObject(MongoDB::Connection::Ptr pObject)
{
}
void destroyObject(Connection::Ptr pObject)
void destroyObject(MongoDB::Connection::Ptr pObject)
{
}
@ -58,7 +56,34 @@ private:
Net::SocketAddress _address;
};
namespace MongoDB
{
}} // Poco::MongoDB
class PooledConnection
{
public:
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool)
{
_connection = _pool.borrowObject();
}
virtual ~PooledConnection()
{
_pool.returnObject(_connection);
}
operator Connection::Ptr () { return _connection; }
private:
Poco::ObjectPool<Connection, Connection::Ptr>& _pool;
Connection::Ptr _connection;
};
} // MongoDB
} // Poco
#endif //_MongoDB_PoolableConnectionFactory_included

View File

@ -40,8 +40,7 @@
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/Dynamic/Struct.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
@ -86,11 +85,11 @@ public:
void flags(Flags flag);
/// Set the flags
Dynamic::Struct<std::string>& query();
Document& query();
/// Returns the query document
Dynamic::Struct<std::string>& returnFieldSelector();
Document& returnFieldSelector();
/// Returns the selector document
@ -123,9 +122,9 @@ private:
Int32 _numberToReturn;
Dynamic::Struct<std::string> _query;
Document _query;
Dynamic::Struct<std::string> _returnFieldSelector;
Document _returnFieldSelector;
};
inline QueryRequest::Flags QueryRequest::flags() const
@ -138,12 +137,12 @@ inline void QueryRequest::flags(QueryRequest::Flags flags)
_flags = flags;
}
inline Dynamic::Struct<std::string>& QueryRequest::query()
inline Document& QueryRequest::query()
{
return _query;
}
inline Dynamic::Struct<std::string>& QueryRequest::returnFieldSelector()
inline Document& QueryRequest::returnFieldSelector()
{
return _returnFieldSelector;
}

View File

@ -66,7 +66,7 @@ public:
/// Reads the response from the stream
Documents& documents();
Document::Vector& documents();
/// Returns the retrieved documents
@ -90,11 +90,11 @@ private:
Int32 _numberReturned;
Documents _documents;
Document::Vector _documents;
};
inline Documents& ResponseMessage::documents()
inline Document::Vector& ResponseMessage::documents()
{
return _documents;
}

View File

@ -40,8 +40,7 @@
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/Dynamic/Struct.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
@ -78,11 +77,11 @@ public:
/// Destructor
Dynamic::Struct<std::string>& selector();
Document& selector();
/// Returns the selector document
Dynamic::Struct<std::string>& update();
Document& update();
/// The document to update
@ -105,9 +104,9 @@ private:
std::string _fullCollectionName;
Dynamic::Struct<std::string> _selector;
Document _selector;
Dynamic::Struct<std::string> _update;
Document _update;
};
@ -121,12 +120,12 @@ inline void UpdateRequest::flags(UpdateRequest::Flags flags)
_flags = flags;
}
inline Dynamic::Struct<std::string>& UpdateRequest::selector()
inline Document& UpdateRequest::selector()
{
return _selector;
}
inline Dynamic::Struct<std::string>& UpdateRequest::update()
inline Document& UpdateRequest::update()
{
return _update;
}

View File

@ -37,10 +37,8 @@
#include "Poco/MongoDB/Array.h"
namespace Poco
{
namespace MongoDB
{
namespace Poco {
namespace MongoDB {
Array::Array() : Document()
{

View File

@ -37,10 +37,8 @@
#include "Poco/MongoDB/Binary.h"
namespace Poco
{
namespace MongoDB
{
namespace Poco {
namespace MongoDB {
Binary::Binary() : _buffer(0)
@ -52,14 +50,20 @@ Binary::Binary(Poco::Int32 size, unsigned char subtype) : _buffer(size), _subtyp
{
}
Binary::~Binary()
{
}
void Binary::resize(std::size_t newSize)
std::string Binary::toString() const
{
//TODO:
//_buffer.resize(newSize);
std::ostringstream oss;
Base64Encoder encoder(oss);
MemoryInputStream mis((const char*) _buffer.begin(), _buffer.size());
StreamCopier::copyStream(mis, encoder);
return oss.str();
}
}} // Namespace Poco::MongoDB

68
MongoDB/src/Database.cpp Normal file
View File

@ -0,0 +1,68 @@
//
// Database.cpp
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Database
//
// Implementation of the Database class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/MongoDB/Database.h"
namespace Poco
{
namespace MongoDB
{
Database::Database( const std::string& db) : _dbname(db)
{
}
Database::~Database()
{
}
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const std::string& collectionName)
{
return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);
}
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCountRequest(const std::string& collectionName)
{
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd");
request->numberToReturn(1);
request->query().add("count", collectionName);
return request;
}
}} // Namespace Poco::MongoDB

View File

@ -36,7 +36,6 @@
//
#include "Poco/MongoDB/DeleteRequest.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco
{
@ -58,11 +57,10 @@ DeleteRequest::~DeleteRequest()
void DeleteRequest::buildRequest(BinaryWriter& writer)
{
BSONWriter bsonWriter(writer);
writer << 0; // 0 - reserved for future use
bsonWriter.writeCString(_fullCollectionName);
BSONWriter(writer).writeCString(_fullCollectionName);
writer << _flags;
bsonWriter.write(_selector);
_selector.write(writer);
}

View File

@ -133,7 +133,7 @@ void Document::read(BinaryReader& reader)
std::string Document::toString() const
{
std::ostringstream oss;
for(ElementSet::iterator it = _elements.begin(); it != _elements.end(); ++it)
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
{
oss << (*it)->name() << " ";
}

View File

@ -34,7 +34,7 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/MongoDB/BSONWriter.h"
#include "Poco/MongoDB/InsertRequest.h"
namespace Poco
@ -57,13 +57,13 @@ InsertRequest::~InsertRequest()
void InsertRequest::buildRequest(BinaryWriter& writer)
{
//TODO: throw exception when no document is added
BSONWriter bsonWriter(writer);
writer << _flags;
BSONWriter bsonWriter(writer);
bsonWriter.writeCString(_fullCollectionName);
for(Documents::iterator it = _documents.begin(); it != _documents.end(); ++it)
for(Document::Vector::iterator it = _documents.begin(); it != _documents.end(); ++it)
{
BSONWriter bsonWriter(writer);
bsonWriter.write(**it);
bsonWriter.write(*it);
}
}

View File

@ -56,7 +56,13 @@ ObjectId::~ObjectId()
std::string ObjectId::toString() const
{
return format("%X:%X:%X", (unsigned int) _id[0], (unsigned int) _id[4], (unsigned int) _id[8]);
std::string s;
for(int i = 0; i < 12; ++i)
{
s += format("%x", (unsigned int) _id[i]);
}
return s;
}
}} // Namespace Poco::MongoDB

View File

@ -36,7 +36,6 @@
//
#include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco
{
@ -55,22 +54,22 @@ QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flag
{
}
QueryRequest::~QueryRequest()
{
}
void QueryRequest::buildRequest(BinaryWriter& writer)
{
BSONWriter bsonWriter(writer);
writer << _flags;
bsonWriter.writeCString(_fullCollectionName);
BSONWriter(writer).writeCString(_fullCollectionName);
writer << _numberToSkip;
writer << _numberToReturn;
bsonWriter.write(_query);
_query.write(writer);
if ( ! _returnFieldSelector.empty() )
{
bsonWriter.write(_returnFieldSelector);
_returnFieldSelector.write(writer);
}
}

View File

@ -78,24 +78,21 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address)
QueryRequest request("admin.$cmd");
request.numberToReturn(1);
request.query().insert("isMaster", 1);
request.query().add("isMaster", 1);
ResponseMessage response;
conn->sendRequest(request, response);
if ( response.documents().size() > 0 )
{
DocumentPtr doc = response.documents()[0];
Dynamic::Var isMasterVar = (*doc)["ismaster"];
if ( !isMasterVar.isEmpty() && isMasterVar )
Document::Ptr doc = response.documents()[0];
if ( doc->get<bool>("ismaster") )
{
return conn;
}
else if ( doc->contains("primary") )
else if ( doc->exists("primary") )
{
Dynamic::Var& primary = (*doc)["primary"];
Net::SocketAddress primaryAddress(primary.convert<std::string>());
return isMaster(primaryAddress);
return isMaster(Net::SocketAddress(doc->get<std::string>("primary")));
}
}
}

View File

@ -36,7 +36,6 @@
//
#include "Poco/MongoDB/ResponseMessage.h"
#include "Poco/MongoDB/BSONReader.h"
#include "Poco/Net/SocketStream.h"
namespace Poco
@ -75,9 +74,8 @@ void ResponseMessage::read(std::istream& istr)
for(int i = 0; i < _numberReturned; ++i)
{
DocumentPtr doc = new Document();
BSONReader bsonReader(reader);
bsonReader.read(*doc);
Document::Ptr doc = new Document();
doc->read(reader);
_documents.push_back(doc);
}
}

View File

@ -36,7 +36,6 @@
//
#include "Poco/MongoDB/UpdateRequest.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco
{
@ -59,12 +58,11 @@ UpdateRequest::~UpdateRequest()
void UpdateRequest::buildRequest(BinaryWriter& writer)
{
BSONWriter bsonWriter(writer);
writer << 0; // 0 - reserved for future use
bsonWriter.writeCString(_fullCollectionName);
BSONWriter(writer).writeCString(_fullCollectionName);
writer << _flags;
bsonWriter.write(_selector);
bsonWriter.write(_update);
_selector.write(writer);
_update.write(writer);
}
}} // Namespace MongoDB

View File

@ -31,10 +31,13 @@
#include <iostream>
#include "Poco/DateTime.h"
#include "Poco/ObjectPool.h"
#include "Poco/MongoDB/InsertRequest.h"
#include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/DeleteRequest.h"
#include "Poco/MongoDB/PoolableConnectionFactory.h"
#include "Poco/MongoDB/Database.h"
#include "Poco/Net/NetException.h"
@ -141,6 +144,9 @@ void MongoDBTest::testQueryRequest()
assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
assert(doc->isType<NullValue>("unknown"));
std::string id = doc->get("_id")->toString();
std::cout << id << std::endl;
}
catch(Poco::NotFoundException& nfe)
{
@ -153,6 +159,52 @@ void MongoDBTest::testQueryRequest()
}
}
void MongoDBTest::testDBQueryRequest()
{
if ( ! _connected )
{
std::cout << "test skipped." << std::endl;
return;
}
Database db("team");
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createQueryRequest("players");
request->query().add("lastname" , std::string("Braem"));
Poco::MongoDB::ResponseMessage response;
_mongo.sendRequest(*request, response);
if ( response.documents().size() > 0 )
{
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
try
{
std::string lastname = doc->get<std::string>("lastname");
assert(lastname.compare("Braem") == 0);
std::string firstname = doc->get<std::string>("firstname");
assert(firstname.compare("Franky") == 0);
Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
Poco::DateTime birthDate(birthDateTimestamp);
assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
assert(doc->isType<NullValue>("unknown"));
std::string id = doc->get("_id")->toString();
std::cout << id << std::endl;
}
catch(Poco::NotFoundException& nfe)
{
fail(nfe.message() + " not found.");
}
}
else
{
fail("No document returned");
}
}
void MongoDBTest::testCountCommand()
{
if ( ! _connected )
@ -181,6 +233,34 @@ void MongoDBTest::testCountCommand()
}
}
void MongoDBTest::testDBCountCommand()
{
if ( ! _connected )
{
std::cout << "test skipped." << std::endl;
return;
}
Poco::MongoDB::Database db("team");
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createCountRequest("players");
Poco::MongoDB::ResponseMessage response;
_mongo.sendRequest(*request, response);
if ( response.documents().size() > 0 )
{
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
double count = doc->get<double>("n");
assert(count == 1);
}
else
{
fail("Didn't get a response from the count command");
}
}
void MongoDBTest::testDeleteRequest()
{
if ( ! _connected )
@ -195,13 +275,55 @@ void MongoDBTest::testDeleteRequest()
_mongo.sendRequest(request);
}
void MongoDBTest::testConnectionPool()
{
Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory("localhost:27017");
Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);
Poco::MongoDB::PooledConnection pooledConnection(pool);
Poco::MongoDB::QueryRequest request("team.$cmd");
request.numberToReturn(1);
request.query().add("count", std::string("players"));
Poco::MongoDB::ResponseMessage response;
((Connection::Ptr) pooledConnection)->sendRequest(request, response);
if ( response.documents().size() > 0 )
{
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
double count = doc->get<double>("n");
assert(count == 1);
}
else
{
fail("Didn't get a response from the count command");
}
/*
Poco::MongoDB::Connection::Ptr pooledConnection1 = pool.borrowObject();
assert(!pooledConnection1.isNull());
pool.returnObject(pooledConnection1);
std::cout << "Available: " << pool.available() << std::endl;
Poco::MongoDB::Connection::Ptr pooledConnection2 = pool.borrowObject();
assert(!pooledConnection2.isNull());
pool.returnObject(pooledConnection2);*/
}
CppUnit::Test* MongoDBTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest);
CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest);
CppUnit_addTest(pSuite, MongoDBTest, testCountCommand);
CppUnit_addTest(pSuite, MongoDBTest, testDBCountCommand);
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
return pSuite;

View File

@ -57,12 +57,21 @@ public:
void testQueryRequest();
void testDBQueryRequest();
void testCountCommand();
void testDBCountCommand();
void testDeleteRequest();
void testConnectionPool();
void setUp();

View File

@ -1,36 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "Poco/MongoDB/Document.h"
#include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/Connection.h"
int main(void)
{
Poco::MongoDB::Document doc;
doc.add("test", 10);
Poco::MongoDB::Connection mongo("localhost", 27017);
Poco::MongoDB::ResponseMessage response;
// Count
Poco::MongoDB::QueryRequest request("test.$cmd");
request.numberToReturn(1);
request.query().add("count", std::string("players"));
mongo.sendRequest(request, response);
if ( response.documents().size() > 0 )
{
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
std::cout << "n= " << doc->get<double>("n") << std::endl;
}
std::cout << "Number of documents returned: " << response.documents().size() << std::endl;
return 0;
}