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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -6,12 +6,10 @@
namespace Poco namespace Poco
{ {
namespace MongoDB
{
template<> template<>
class PoolableObjectFactory<Connection, Connection::Ptr> class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
{ {
public: public:
PoolableObjectFactory(Net::SocketAddress& address) 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; 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)
{ {
} }
@@ -57,8 +55,35 @@ private:
Net::SocketAddress _address; 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 #endif //_MongoDB_PoolableConnectionFactory_included

View File

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

View File

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

View File

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

View File

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

View File

@@ -37,10 +37,8 @@
#include "Poco/MongoDB/Binary.h" #include "Poco/MongoDB/Binary.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
Binary::Binary() : _buffer(0) Binary::Binary() : _buffer(0)
@@ -52,14 +50,20 @@ Binary::Binary(Poco::Int32 size, unsigned char subtype) : _buffer(size), _subtyp
{ {
} }
Binary::~Binary() Binary::~Binary()
{ {
} }
void Binary::resize(std::size_t newSize)
std::string Binary::toString() const
{ {
//TODO: std::ostringstream oss;
//_buffer.resize(newSize); Base64Encoder encoder(oss);
MemoryInputStream mis((const char*) _buffer.begin(), _buffer.size());
StreamCopier::copyStream(mis, encoder);
return oss.str();
} }
}} // Namespace Poco::MongoDB }} // 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/DeleteRequest.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco namespace Poco
{ {
@@ -58,11 +57,10 @@ DeleteRequest::~DeleteRequest()
void DeleteRequest::buildRequest(BinaryWriter& writer) void DeleteRequest::buildRequest(BinaryWriter& writer)
{ {
BSONWriter bsonWriter(writer);
writer << 0; // 0 - reserved for future use writer << 0; // 0 - reserved for future use
bsonWriter.writeCString(_fullCollectionName); BSONWriter(writer).writeCString(_fullCollectionName);
writer << _flags; 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::string Document::toString() const
{ {
std::ostringstream oss; 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() << " "; oss << (*it)->name() << " ";
} }

View File

@@ -34,7 +34,7 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
// //
#include "Poco/MongoDB/BSONWriter.h"
#include "Poco/MongoDB/InsertRequest.h" #include "Poco/MongoDB/InsertRequest.h"
namespace Poco namespace Poco
@@ -57,13 +57,13 @@ InsertRequest::~InsertRequest()
void InsertRequest::buildRequest(BinaryWriter& writer) void InsertRequest::buildRequest(BinaryWriter& writer)
{ {
//TODO: throw exception when no document is added //TODO: throw exception when no document is added
BSONWriter bsonWriter(writer);
writer << _flags; writer << _flags;
BSONWriter bsonWriter(writer);
bsonWriter.writeCString(_fullCollectionName); 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 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 }} // Namespace Poco::MongoDB

View File

@@ -36,7 +36,6 @@
// //
#include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco namespace Poco
{ {
@@ -55,22 +54,22 @@ QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flag
{ {
} }
QueryRequest::~QueryRequest() QueryRequest::~QueryRequest()
{ {
} }
void QueryRequest::buildRequest(BinaryWriter& writer) void QueryRequest::buildRequest(BinaryWriter& writer)
{ {
BSONWriter bsonWriter(writer);
writer << _flags; writer << _flags;
bsonWriter.writeCString(_fullCollectionName); BSONWriter(writer).writeCString(_fullCollectionName);
writer << _numberToSkip; writer << _numberToSkip;
writer << _numberToReturn; writer << _numberToReturn;
bsonWriter.write(_query); _query.write(writer);
if ( ! _returnFieldSelector.empty() ) 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"); QueryRequest request("admin.$cmd");
request.numberToReturn(1); request.numberToReturn(1);
request.query().insert("isMaster", 1); request.query().add("isMaster", 1);
ResponseMessage response; ResponseMessage response;
conn->sendRequest(request, response); conn->sendRequest(request, response);
if ( response.documents().size() > 0 ) if ( response.documents().size() > 0 )
{ {
DocumentPtr doc = response.documents()[0]; Document::Ptr doc = response.documents()[0];
Dynamic::Var isMasterVar = (*doc)["ismaster"]; if ( doc->get<bool>("ismaster") )
if ( !isMasterVar.isEmpty() && isMasterVar )
{ {
return conn; return conn;
} }
else if ( doc->contains("primary") ) else if ( doc->exists("primary") )
{ {
Dynamic::Var& primary = (*doc)["primary"]; return isMaster(Net::SocketAddress(doc->get<std::string>("primary")));
Net::SocketAddress primaryAddress(primary.convert<std::string>());
return isMaster(primaryAddress);
} }
} }
} }

View File

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

View File

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

View File

@@ -31,10 +31,13 @@
#include <iostream> #include <iostream>
#include "Poco/DateTime.h" #include "Poco/DateTime.h"
#include "Poco/ObjectPool.h"
#include "Poco/MongoDB/InsertRequest.h" #include "Poco/MongoDB/InsertRequest.h"
#include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/DeleteRequest.h" #include "Poco/MongoDB/DeleteRequest.h"
#include "Poco/MongoDB/PoolableConnectionFactory.h"
#include "Poco/MongoDB/Database.h"
#include "Poco/Net/NetException.h" #include "Poco/Net/NetException.h"
@@ -141,6 +144,9 @@ void MongoDBTest::testQueryRequest()
assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9); assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated"); Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
assert(doc->isType<NullValue>("unknown")); assert(doc->isType<NullValue>("unknown"));
std::string id = doc->get("_id")->toString();
std::cout << id << std::endl;
} }
catch(Poco::NotFoundException& nfe) 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() void MongoDBTest::testCountCommand()
{ {
if ( ! _connected ) 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() void MongoDBTest::testDeleteRequest()
{ {
if ( ! _connected ) if ( ! _connected )
@@ -195,13 +275,55 @@ void MongoDBTest::testDeleteRequest()
_mongo.sendRequest(request); _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::Test* MongoDBTest::suite()
{ {
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
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, testCountCommand); CppUnit_addTest(pSuite, MongoDBTest, testCountCommand);
CppUnit_addTest(pSuite, MongoDBTest, testDBCountCommand);
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest); CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
return pSuite; return pSuite;

View File

@@ -57,12 +57,21 @@ public:
void testQueryRequest(); void testQueryRequest();
void testDBQueryRequest();
void testCountCommand(); void testCountCommand();
void testDBCountCommand();
void testDeleteRequest(); void testDeleteRequest();
void testConnectionPool();
void setUp(); 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;
}