Add MongoDB classes

This commit is contained in:
fbraem
2013-02-02 21:52:49 +01:00
parent 5964ae0a80
commit 749e7cd2ca
49 changed files with 4896 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
//
// Array.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Array
//
// Definition of the Array 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_Array_included
#define _MongoDB_Array_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
namespace Poco {
namespace MongoDB {
class MongoDB_API Array : public Document
/// Implements the BSON Array
{
public:
typedef SharedPtr<Array> Ptr;
Array();
/// Constructor
virtual ~Array();
/// Destructor
};
/*
// BSON Embedded Array
// spec: document
template<>
struct ElementTraits<Array::Ptr>
{
enum { TypeId = 0x04 };
};
template<>
inline void BSONReader::read<Array::Ptr>(Array::Ptr& to)
{
to->read(_reader);
}
template<>
inline void BSONWriter::write<Array::Ptr>(Array::Ptr& from)
{
from->write(_writer);
}
*/
}} // Namespace Poco::MongoDB
#endif //_MongoDB_Array_included

View File

@@ -0,0 +1,101 @@
//
// BSONReader.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: BSONReader
//
// Definition of the BSONReader 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_BSONReader_included
#define _MongoDB_BSONReader_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API BSONReader
/// Class for reading BSON from a Poco::BinaryReader
{
public:
BSONReader(Poco::BinaryReader& reader) : _reader(reader)
/// Constructor
{
}
virtual ~BSONReader()
/// Destructor
{
}
void read(Document& v);
/// Reads the value from the reader. The default implementation uses the >> operator to
/// the given argument. Special types can write their own version.
std::string readCString();
/// Reads a cstring from the reader.
/// A cstring is a string terminated with a 0x00.
private:
Poco::BinaryReader _reader;
};
inline std::string BSONReader::readCString()
{
std::string val;
while(_reader.good())
{
char c;
_reader >> c;
if ( _reader.good() )
{
if (c == 0x00)
{
return val;
}
else
{
val += c;
}
}
}
return val;
}
}} // Namespace Poco::MongoDB
#endif // _MongoDB_BSONReader_included

View File

@@ -0,0 +1,77 @@
//
// BSONWriter.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: BSONWriter
//
// Definition of the BSONWriter 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_BSONWriter_included
#define _MongoDB_BSONWriter_included
#include "Poco/BinaryWriter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API BSONWriter
/// Class for writing BSON to a Poco::BinaryWriter.
{
public:
BSONWriter(Poco::BinaryWriter& writer);
/// Constructor
virtual ~BSONWriter();
/// Destructor
void write(const Document& v);
void writeCString(const std::string& value);
/// Writes a cstring to the writer. A cstring is a string
/// terminated with 0x00
private:
Poco::BinaryWriter _writer;
};
}} // Namespace Poco::MongoDB
#endif // _MongoDB_BSONWriter_included

View File

@@ -0,0 +1,151 @@
//
// Binary.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Binary
//
// Definition of the Binary 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_Binary_included
#define _MongoDB_Binary_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/Buffer.h"
#include "Poco/MongoDB/Element.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API Binary
/// Implements BSON Binary. It's a wrapper around a Poco::Buffer<unsigned char>.
{
public:
typedef SharedPtr<Binary> Ptr;
Binary();
/// Constructor
Binary(Poco::Int32 size, unsigned char subtype);
/// Constructor
virtual ~Binary();
unsigned char* begin();
/// Returns the start of the buffer
Poco::Int32 size() const;
/// Returns the size of the buffer
unsigned char subtype() const;
/// Returns the subtype
void subtype(unsigned char type);
/// Sets the subtype
void resize(std::size_t newSize);
/// Resizes the buffer
private:
Poco::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
{
return _subtype;
}
inline void Binary::subtype(unsigned char type)
{
_subtype = type;
}
/*
// BSON Embedded Document
// spec: binary
template<>
struct ElementTraits<Binary::Ptr>
{
enum { TypeId = 0x05 };
};
template<>
inline void BSONReader::read<Binary::Ptr>(Binary::Ptr& to)
{
Poco::Int32 size;
_reader >> size;
to->resize(size);
unsigned char subtype;
_reader >> subtype;
to->subtype(subtype);
_reader.readRaw((char*) to->begin(), size);
}
template<>
inline void BSONWriter::write<Binary::Ptr>(Binary::Ptr& from)
{
_writer << from->subtype();
_writer.writeRaw((char*) from->begin(), from->size());
}
*/
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Binary_included

View File

@@ -0,0 +1,134 @@
//
// Connection.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Connection
//
// Definition of the Connection 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_Connection_h
#define _MongoDB_Connection_h
#include "Poco/Net/SocketAddress.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Mutex.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/ResponseMessage.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API Connection
/// Represents a connection to a MongoDB server
{
public:
typedef Poco::SharedPtr<Connection> Ptr;
Connection();
/// Default constructor. Use this when you want to
/// connect later on.
Connection(const std::string& hostAndPort);
/// Constructor which connects to the given MongoDB host/port.
/// The host and port must be separated with a colon.
Connection(const std::string& host, int port);
/// Constructor which connects to the given MongoDB host/port.
Connection(const Net::SocketAddress& addrs);
/// Constructor which connects to the given MongoDB host/port.
virtual ~Connection();
/// Destructor
Net::SocketAddress address() const;
/// Returns the address of the MongoDB connection
void connect(const std::string& hostAndPort);
/// Connects to the given MongoDB server. The host and port must be separated
/// with a colon.
void connect(const std::string& host, int port);
/// Connects to the given MongoDB server.
void connect(const Net::SocketAddress& addrs);
/// Connects to the given MongoDB server.
void disconnect();
/// Disconnects from the MongoDB server
void sendRequest(RequestMessage& request);
/// Sends a request to the MongoDB server
/// Only use this when the request hasn't a response.
void sendRequest(RequestMessage& request, ResponseMessage& response);
/// Sends a request to the MongoDB server and receives the response.
/// Use this when a response is expected: only a query or getmore
/// request will return a response.
private:
Net::SocketAddress _address;
Net::StreamSocket _socket;
void connect();
/// Connects to the MongoDB server
};
inline Net::SocketAddress Connection::address() const
{
return _address;
}
}} // Poco::MongoDB
#endif //_MongoDB_Connection_h

View File

@@ -0,0 +1,116 @@
//
// DeleteRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: DeleteRequest
//
// Definition of the DeleteRequest 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_DeleteRequest_included
#define _MongoDB_DeleteRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API DeleteRequest : public RequestMessage
/// Class for creating an OP_DELETE client request. This request
/// is used to delete one ore more documents from a database.
{
public:
typedef enum
{
DELETE_NONE = 0,
DELETE_SINGLE_REMOVE = 1
} Flags;
DeleteRequest(const std::string& collectionName, Flags flags = DELETE_NONE);
/// Constructor. The full collection name is the concatenation of the database
/// name with the collection name, using a "." for the concatenation. For example,
/// for the database "foo" and the collection "bar", the full collection name is
/// "foo.bar".
virtual ~DeleteRequest();
/// Destructor
Flags flags() const;
/// Returns flags
void flags(Flags flag);
/// Sets flags
Document& selector();
/// Returns the selector document
protected:
void buildRequest(BinaryWriter& writer);
/// Writes the OP_DELETE request to the writer
private:
Flags _flags;
std::string _fullCollectionName;
Document _selector;
};
inline DeleteRequest::Flags DeleteRequest::flags() const
{
return _flags;
}
inline void DeleteRequest::flags(DeleteRequest::Flags flags)
{
_flags = flags;
}
inline Document& DeleteRequest::selector()
{
return _selector;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_DeleteRequest_included

View File

@@ -0,0 +1,203 @@
//
// Document.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Document
//
// Definition of the Document 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_Document_included
#define _MongoDB_Document_included
#include "Poco/SharedPtr.h"
#include "Poco/Dynamic/Struct.h"
namespace Poco
{
namespace MongoDB
{
typedef Dynamic::Struct<std::string> Document;
typedef SharedPtr<Document> DocumentPtr;
typedef std::vector<DocumentPtr> Documents;
/*
class ElementFindByName
{
public:
ElementFindByName(const std::string& name) : _name(name)
{
}
bool operator()(const Element::Ptr& element)
{
return !element.isNull() && element->name() == _name;
}
private:
std::string _name;
};
class MongoDB_API Document
{
public:
typedef SharedPtr<Document> Ptr;
typedef std::vector<Document::Ptr> Vector;
Document();
virtual ~Document();
void read(BinaryReader& reader);
void write(BinaryWriter& writer);
template<typename T>
T get(const std::string& name)
{
Element::Ptr element = get(name);
if ( element.isNull() )
{
throw Poco::NotFoundException(name);
}
else
{
if ( ElementTraits<T>::TypeId == element->type() )
{
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
return concrete->value();
}
else
{
throw std::runtime_error("Invalid type mismatch!");
}
}
}
Element::Ptr get(const std::string& name)
{
Element::Ptr element;
ElementSet::iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
if ( it != _elements.end() )
{
return *it;
}
return element;
}
bool exists(const std::string& name)
{
return std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name)) != _elements.end();
}
template<typename T>
bool isType(const std::string& name)
{
Element::Ptr element = get(name);
if ( element.isNull() )
{
return false;
}
return ElementTraits<T>::TypeId == element->type();
}
void addElement(Element::Ptr element);
template<typename T>
void add(const std::string& name, T value)
{
addElement(new ConcreteElement<T>(name, value));
}
bool empty() const;
void clear();
std::string toString() const;
private:
ElementSet _elements;
};
inline bool Document::empty() const
{
return _elements.empty();
}
inline void Document::clear()
{
_elements.clear();
}
// BSON Embedded Document
// spec: document
template<>
struct ElementTraits<Document::Ptr>
{
enum { TypeId = 0x03 };
};
template<>
inline void BSONReader::read<Document::Ptr>(Document::Ptr& to)
{
to->read(_reader);
}
template<>
inline void BSONWriter::write<Document::Ptr>(Document::Ptr& from)
{
from->write(_writer);
}
*/
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Document_included

View File

@@ -0,0 +1,448 @@
//
// Element.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Element
//
// Definition of the Element 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_Element_included
#define _MongoDB_Element_included
#include <string>
#include <set>
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include "Poco/SharedPtr.h"
#include "Poco/Timestamp.h"
#include "Poco/RegularExpression.h"
#include "Poco/Nullable.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/BSONReader.h"
#include "Poco/MongoDB/BSONWriter.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API Element
{
public:
Element(const std::string& name);
virtual ~Element();
virtual int type() const = 0;
typedef Poco::SharedPtr<Element> Ptr;
std::string name() const;
private:
// virtual void read(BinaryReader& reader) = 0;
// virtual void write(BinaryWriter& writer) = 0;
// friend class Document;
std::string _name;
};
inline std::string Element::name() const
{
return _name;
}
class ElementComparator
{
public:
bool operator()(const Element::Ptr& s1, const Element::Ptr& s2)
{
return s1->name() < s2->name();
}
};
typedef std::set<Element::Ptr, ElementComparator> ElementSet;
template<typename T>
struct ElementTraits
{
};
// BSON Floating point
// spec: double
template<>
struct ElementTraits<double>
{
enum { TypeId = 0x01 };
};
// BSON UTF-8 string
// spec: int32 (byte*) "\x00"
// int32 is the number bytes in byte* + 1 (for trailing "\x00"
template<>
struct ElementTraits<std::string>
{
enum { TypeId = 0x02 };
};
/*
template<>
inline void BSONReader::read<std::string>(std::string& to)
{
Poco::Int32 size;
_reader >> size;
_reader.readRaw(size, to);
to.erase(to.end() - 1); // remove terminating 0
}
template<>
inline void BSONWriter::write<std::string>(std::string& from)
{
_writer << (Poco::Int32) (from.length() + 1);
writeCString(from);
}
*/
// BSON bool
// spec: "\x00" "\x01"
template<>
struct ElementTraits<bool>
{
enum { TypeId = 0x08 };
};
/*
template<>
inline void BSONReader::read<bool>(bool& to)
{
Int32 b;
_reader >> b;
to = b != 0;
}
template<>
inline void BSONWriter::write<bool>(bool& from)
{
_writer << (from ? 0x01 : 0x00);
}
*/
// BSON 32-bit integer
// spec: int32
template<>
struct ElementTraits<Int32>
{
enum { TypeId = 0x10 };
};
// BSON UTC datetime
// spec: int64
template<>
struct ElementTraits<Timestamp>
{
enum { TypeId = 0x09 };
};
/*
template<>
inline void BSONReader::read<Timestamp>(Timestamp& to)
{
Poco::Int64 value;
_reader >> value;
to = Timestamp::fromEpochTime(value / 1000);
to += (value % 1000 * 1000);
}
template<>
inline void BSONWriter::write<Timestamp>(Timestamp& from)
{
_writer << (from.epochMicroseconds() / 1000);
}
*/
typedef Nullable<unsigned char> NullValue;
// BSON Null Value
// spec:
template<>
struct ElementTraits<NullValue>
{
enum { TypeId = 0x0A };
};
/*
template<>
inline void BSONReader::read<NullValue>(NullValue& to)
{
}
template<>
inline void BSONWriter::write<NullValue>(NullValue& from)
{
}
*/
class RegularExpression
{
public:
typedef SharedPtr<RegularExpression> Ptr;
RegularExpression()
{
}
RegularExpression(const std::string& pattern, const std::string& options) : _pattern(pattern), _options(options) {}
virtual ~RegularExpression()
{
}
std::string getPattern() const;
void setPattern(const std::string& pattern);
std::string getOptions() const;
void setOptions(const std::string& options);
SharedPtr<Poco::RegularExpression> createRE()
{
int options = 0;
for(std::string::iterator optIt = _options.begin(); optIt != _options.end(); ++optIt)
{
switch(*optIt)
{
case 'i': // Case Insensitive
options |= Poco::RegularExpression::RE_CASELESS;
break;
case 'm': // Multiline matching
options |= Poco::RegularExpression::RE_MULTILINE;
break;
case 'x': // Verbose mode
//No equivalent in Poco
break;
case 'l': // \w \W Locale dependent
//No equivalent in Poco
break;
case 's': // Dotall mode
options |= Poco::RegularExpression::RE_DOTALL;
break;
case 'u': // \w \W Unicode
//No equivalent in Poco
break;
}
}
return new Poco::RegularExpression(_pattern, options);
}
private:
std::string _pattern;
std::string _options;
};
inline std::string RegularExpression::getPattern() const
{
return _pattern;
}
inline void RegularExpression::setPattern(const std::string& pattern)
{
_pattern = pattern;
}
inline std::string RegularExpression::getOptions() const
{
return _options;
}
inline void RegularExpression::setOptions(const std::string& options)
{
_options = options;
}
// BSON Regex
// spec: cstring cstring
template<>
struct ElementTraits<RegularExpression::Ptr>
{
enum { TypeId = 0x0B };
};
/*
template<>
inline void BSONReader::read<RegularExpression::Ptr>(RegularExpression::Ptr& to)
{
std::string pattern = readCString();
std::string options = readCString();
to = new RegularExpression(pattern, options);
}
template<>
inline void BSONWriter::write<RegularExpression::Ptr>(RegularExpression::Ptr& from)
{
writeCString(from->getPattern());
writeCString(from->getOptions());
}
*/
class JavaScriptCode
{
public:
typedef SharedPtr<JavaScriptCode> Ptr;
JavaScriptCode()
{
}
virtual ~JavaScriptCode()
{
}
void code(const std::string& s);
std::string code() const;
private:
std::string _code;
};
inline void JavaScriptCode::code(const std::string& s)
{
_code = s;
}
inline std::string JavaScriptCode::code() const
{
return _code;
}
// BSON JavaScript code
// spec: string
template<>
struct ElementTraits<JavaScriptCode::Ptr>
{
enum { TypeId = 0x0D };
};
/*
template<>
inline void BSONReader::read<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& to)
{
std::string code;
BSONReader(_reader).read(code);
to = new JavaScriptCode();
to->code(code);
}
template<>
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 };
};
template<typename T>
class ConcreteElement : public Element
{
public:
ConcreteElement(const std::string& name, const T& init) : Element(name), _value(init)
{
}
virtual ~ConcreteElement()
{
}
T value() const { return _value; }
int type() const { return ElementTraits<T>::TypeId; }
/*
void read(BinaryReader& reader)
{
BSONReader(reader).read(_value);
}
void write(BinaryWriter& writer)
{
BSONWriter(writer).write(_value);
}
*/
private:
T _value;
};
}} // Namespace Poco::MongoDB
#endif // _MongoDB_Element_included

View File

@@ -0,0 +1,112 @@
//
// GetMoreRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: GetMoreRequest
//
// Definition of the GetMoreRequest 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_GetMoreRequest_included
#define _MongoDB_GetMoreRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API GetMoreRequest : public RequestMessage
/// Class for creating an OP_GETMORE client request. This request is used
/// to query the database for more documents in a collection after
/// a query request is send.
{
public:
GetMoreRequest(const std::string& collectionName, Int64 cursorID);
/// Constructor. The full collection name is the concatenation of the database
/// name with the collection name, using a "." for the concatenation. For example,
/// for the database "foo" and the collection "bar", the full collection name is
/// "foo.bar". The cursorID has been returned by the response on the query request.
virtual ~GetMoreRequest();
/// Destructor
Int32 numberToReturn() const;
/// Returns the limit of returned documents
void numberToReturn(Int32 n);
/// Sets the limit of returned documents
Int64 cursorID() const;
/// Returns the cursor id
protected:
void buildRequest(BinaryWriter& writer);
private:
Int32 _flags;
std::string _fullCollectionName;
Int32 _numberToReturn;
Int64 _cursorID;
};
inline Int32 GetMoreRequest::numberToReturn() const
{
return _numberToReturn;
}
inline void GetMoreRequest::numberToReturn(Int32 n)
{
_numberToReturn = n;
}
inline Int64 GetMoreRequest::cursorID() const
{
return _cursorID;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_GetMoreRequest_included

View File

@@ -0,0 +1,100 @@
//
// InsertRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: InsertRequest
//
// Definition of the InsertRequest 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_InsertRequest_included
#define _MongoDB_InsertRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/Document.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API InsertRequest : public RequestMessage
/// Class for creating an OP_INSERT client request. This request is used
/// to insert one or more documents to the database.
{
public:
typedef enum
{
INSERT_NONE = 0,
INSERT_CONTINUE_ON_ERROR = 1
} Flags;
InsertRequest(const std::string& collectionName, Flags flags = INSERT_NONE );
/// Constructor.
/// The full collection name is the concatenation of the database
/// name with the collection name, using a "." for the concatenation. For example,
/// for the database "foo" and the collection "bar", the full collection name is
/// "foo.bar".
virtual ~InsertRequest();
/// Destructor
Documents& documents();
/// Returns the documents to insert into the database
protected:
void buildRequest(BinaryWriter& writer);
private:
Int32 _flags;
std::string _fullCollectionName;
Documents _documents;
};
inline Documents& InsertRequest::documents()
{
return _documents;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_InsertRequest_included

View File

@@ -0,0 +1,83 @@
//
// KillCursorsRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: KillCursorsRequest
//
// Definition of the KillCursorsRequest 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_KillCursorsRequest_included
#define _MongoDB_KillCursorsRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API KillCursorsRequest : public RequestMessage
/// Class for creating an OP_KILL_CURSORS client request. This
/// request is used to kill cursors, which are still open,
/// returned by query requests.
{
public:
KillCursorsRequest();
/// Constructor
virtual ~KillCursorsRequest();
/// Destructor
std::vector<Int64>& cursors();
/// Return the cursors
protected:
void buildRequest(BinaryWriter& writer);
std::vector<Int64> _cursors;
};
inline std::vector<Int64>& KillCursorsRequest::cursors()
{
return _cursors;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_KillCursorsRequest_included

View File

@@ -0,0 +1,94 @@
//
// Message.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: Message
//
// Definition of the Message 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_Message_included
#define _MongoDB_Message_included
#include <sstream>
#include "Poco/Net/Socket.h"
#include "Poco/BinaryReader.h"
#include "Poco/BinaryWriter.h"
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/MessageHeader.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API Message
/// Base class for all messages send or retrieved from MongoDB server
{
public:
Message(MessageHeader::OpCode opcode);
/// Constructor
virtual ~Message();
/// Destructor
MessageHeader& header();
/// Returns the message header
protected:
MessageHeader _header;
void messageLength(Int32 length);
/// Sets the message length in the message header
};
inline MessageHeader& Message::header()
{
return _header;
}
inline void Message::messageLength(Int32 length)
{
_header.messageLength(length);
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_Message_included

View File

@@ -0,0 +1,159 @@
//
// MessageHeader.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: MessageHeader
//
// Definition of the MessageHeader 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_MessageHeader_included
#define _MongoDB_MessageHeader_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/MessageHeader.h"
#define MSG_HEADER_SIZE 16
namespace Poco
{
namespace MongoDB
{
class MessageHeader
/// Represents the header which is always prepended to a request
/// or response of MongoDB
{
public:
typedef enum
{
Reply = 1
, Msg = 1000
, Update = 2001
, Insert = 2002
, Query = 2004
, GetMore = 2005
, Delete = 2006
, KillCursors = 2007
} OpCode;
virtual ~MessageHeader();
/// Destructor
void read(BinaryReader& reader);
/// Reads the header
void write(BinaryWriter& writer);
/// Writes the header
OpCode opCode() const;
/// Returns the OpCode
Int32 messageLength() const;
/// Returns the message length
Int32 requestID() const;
/// Returns the request id of the current message
void requestID(Int32 id);
/// Sets the request id of the current message
Int32 responseTo() const;
/// Returns the request id from the original request.
private:
MessageHeader(OpCode opcode);
/// Constructor.
Int32 _messageLength;
void messageLength(Int32 length);
/// Sets the message length
Int32 _requestID;
Int32 _responseTo;
OpCode _opCode;
friend class Message;
};
inline MessageHeader::OpCode MessageHeader::opCode() const
{
return _opCode;
}
inline Int32 MessageHeader::messageLength() const
{
return _messageLength;
}
inline void MessageHeader::messageLength(Int32 length)
{
_messageLength = MSG_HEADER_SIZE + length;
}
inline void MessageHeader::requestID(Int32 id)
{
_requestID = id;
}
inline Int32 MessageHeader::requestID() const
{
return _requestID;
}
inline Int32 MessageHeader::responseTo() const
{
return _responseTo;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_MessageHeader_included

View File

@@ -0,0 +1,80 @@
//
// MongoDB.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: MongoDB
//
// Basic definitions for the Poco MongoDB library.
// This file must be the first file included by every other MongoDB
// header file.
//
// 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_MongoDB_INCLUDED
#define MongoDB_MongoDB_INCLUDED
#include "Poco/Foundation.h"
//
// The following block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the MongoDB_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MongoDB_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
//
#if defined(_WIN32) && defined(POCO_DLL)
#if defined(MongoDB_EXPORTS)
#define MongoDB_API __declspec(dllexport)
#else
#define MongoDB_API __declspec(dllimport)
#endif
#endif
#if !defined(MongoDB_API)
#define MongoDB_API
#endif
//
// Automatically link MongoDB library.
//
#if defined(_MSC_VER)
#if !defined(POCO_NO_AUTOMATIC_LIBS) && !defined(MongoDB_EXPORTS)
#pragma comment(lib, "PocoMongoDB" POCO_LIB_SUFFIX)
#endif
#endif
#endif // MongoDB_MongoDB_INCLUDED

View File

@@ -0,0 +1,102 @@
//
// Array.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: ObjectId
//
// Definition of the ObjectId 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_ObjectId_included
#define _MongoDB_ObjectId_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Element.h"
namespace Poco
{
namespace MongoDB
{
class ObjectId
{
public:
typedef SharedPtr<ObjectId> Ptr;
ObjectId();
virtual ~ObjectId();
std::string toString() const;
private:
unsigned char _id[12];
};
/*
// BSON Embedded Document
// spec: ObjectId
template<>
struct ElementTraits<ObjectId::Ptr>
{
enum { TypeId = 0x07 };
};
template<>
inline void BSONReader::read<ObjectId::Ptr>(ObjectId::Ptr& to)
{
_reader.readRaw((char*) to->_id, 12);
}
template<>
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

@@ -0,0 +1,64 @@
#ifndef _MongoDB_PoolableConnectionFactory_included
#define _MongoDB_PoolableConnectionFactory_included
#include "Poco/MongoDB/Connection.h"
#include "Poco/ObjectPool.h"
namespace Poco
{
namespace MongoDB
{
template<>
class PoolableObjectFactory<Connection, Connection::Ptr>
{
public:
PoolableObjectFactory(Net::SocketAddress& address)
: _address(address)
{
}
PoolableObjectFactory(const std::string& address)
: _address(address)
{
}
Connection::Ptr createObject()
{
return new Connection(_address);
}
bool validateObject(Connection::Ptr pObject)
{
return true;
}
void activateObject(Connection::Ptr pObject)
{
}
void deactivateObject(Connection::Ptr pObject)
{
}
void destroyObject(Connection::Ptr pObject)
{
}
private:
Net::SocketAddress _address;
};
}} // Poco::MongoDB
#endif //_MongoDB_PoolableConnectionFactory_included

View File

@@ -0,0 +1,174 @@
//
// QueryRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: QueryRequest
//
// Definition of the QueryRequest 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_QueryRequest_included
#define _MongoDB_QueryRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/Dynamic/Struct.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API QueryRequest : public RequestMessage
/// Class for creating an OP_QUERY client request. This request
/// is used to query documents from the database.
{
public:
typedef enum
{
QUERY_NONE = 0,
QUERY_TAILABLECURSOR = 2,
QUERY_SLAVE_OK = 4,
//QUERY_OPLOG_REPLAY = 8 (internal replication use only - drivers should not implement)
QUERY_NOCUROSR_TIMEOUT = 16,
QUERY_AWAIT_DATA = 32,
QUERY_EXHAUST = 64,
QUERY_PARTIAL = 128
} Flags;
QueryRequest(const std::string& collectionName, Flags flags = QUERY_NONE);
/// Constructor.
/// The full collection name is the concatenation of the database
/// name with the collection name, using a "." for the concatenation. For example,
/// for the database "foo" and the collection "bar", the full collection name is
/// "foo.bar".
virtual ~QueryRequest();
/// Destructor
Flags flags() const;
/// Returns the flags
void flags(Flags flag);
/// Set the flags
Dynamic::Struct<std::string>& query();
/// Returns the query document
Dynamic::Struct<std::string>& returnFieldSelector();
/// Returns the selector document
Int32 numberToSkip() const;
/// Returns the number of documents to skip
void numberToSkip(Int32 n);
/// Sets the number of documents to skip
Int32 numberToReturn() const;
/// Returns the number to return
void numberToReturn(Int32 n);
/// Sets the number to return (limit)
protected:
void buildRequest(BinaryWriter& writer);
private:
Flags _flags;
std::string _fullCollectionName;
Int32 _numberToSkip;
Int32 _numberToReturn;
Dynamic::Struct<std::string> _query;
Dynamic::Struct<std::string> _returnFieldSelector;
};
inline QueryRequest::Flags QueryRequest::flags() const
{
return _flags;
}
inline void QueryRequest::flags(QueryRequest::Flags flags)
{
_flags = flags;
}
inline Dynamic::Struct<std::string>& QueryRequest::query()
{
return _query;
}
inline Dynamic::Struct<std::string>& QueryRequest::returnFieldSelector()
{
return _returnFieldSelector;
}
inline Int32 QueryRequest::numberToSkip() const
{
return _numberToSkip;
}
inline void QueryRequest::numberToSkip(Int32 n)
{
_numberToSkip = n;
}
inline Int32 QueryRequest::numberToReturn() const
{
return _numberToReturn;
}
inline void QueryRequest::numberToReturn(Int32 n)
{
_numberToReturn = n;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_QueryRequest_included

View File

@@ -0,0 +1,70 @@
//
// ReplicaSet.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: ReplicaSet
//
// Definition of the ReplicaSet 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_ReplicaSet_h
#define _MongoDB_ReplicaSet_h
#include <vector>
#include "Poco/Net/SocketAddress.h"
#include "Poco/MongoDB/Connection.h"
namespace Poco
{
namespace MongoDB
{
class MongoDB_API ReplicaSet
{
public:
ReplicaSet(const std::vector<Net::SocketAddress>& addresses);
virtual ~ReplicaSet();
Connection::Ptr findMaster();
private:
Connection::Ptr isMaster(const Net::SocketAddress& host);
std::vector<Net::SocketAddress> _addresses;
};
#endif // _MongoDB_ReplicaSet_h
}} // Poco::MongoDB

View File

@@ -0,0 +1,76 @@
//
// RequestMessage.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: RequestMessage
//
// Definition of the RequestMessage 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_RequestMessage_included
#define _MongoDB_RequestMessage_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Message.h"
#include <ostream>
namespace Poco
{
namespace MongoDB
{
class MongoDB_API RequestMessage : public Message
/// Base class for a request
{
public:
RequestMessage(MessageHeader::OpCode opcode);
/// Constructor
virtual ~RequestMessage();
/// Destructor
void send(std::ostream& ostr);
/// Sends the request to stream
protected:
virtual void buildRequest(BinaryWriter& ss) = 0;
};
}} // Namespace Poco::MongoDB
#endif //_MongoDB_RequestMessage_included

View File

@@ -0,0 +1,111 @@
//
// ResponseMessage.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: ResponseMessage
//
// Definition of the ResponseMessage 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_ResponseMessage_included
#define _MongoDB_ResponseMessage_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Message.h"
#include "Poco/MongoDB/Document.h"
#include <istream>
namespace Poco
{
namespace MongoDB
{
class MongoDB_API ResponseMessage : public Message
/// Class that represents a response (OP_REPLY) from MongoDB
{
public:
ResponseMessage();
/// Constructor
virtual ~ResponseMessage();
/// Destructor
void read(std::istream& istr);
/// Reads the response from the stream
Documents& documents();
/// Returns the retrieved documents
Int64 cursorID() const;
/// Returns the cursor id
void clear();
/// Clears the response
private:
Int32 _responseFlags;
Int64 _cursorID;
Int32 _startingFrom;
Int32 _numberReturned;
Documents _documents;
};
inline Documents& ResponseMessage::documents()
{
return _documents;
}
inline Int64 ResponseMessage::cursorID() const
{
return _cursorID;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_ResponseMessage_included

View File

@@ -0,0 +1,137 @@
//
// UpdateRequest.h
//
// $Id$
//
// Library: MongoDB
// Package: MongoDB
// Module: UpdateRequest
//
// Definition of the UpdateRequest 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_UpdateRequest_included
#define _MongoDB_UpdateRequest_included
#include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h"
#include "Poco/Dynamic/Struct.h"
namespace Poco
{
namespace MongoDB
{
class UpdateRequest : public RequestMessage
/// Class for creating an OP_UPDATE client request. This request is used
/// to update a document.
{
public:
typedef enum
{
UPDATE_NOFLAGS = 0,
// If set, the database will insert the supplied object into the
// collection if no matching document is found.
UPDATE_UPSERT = 1,
// if set, the database will update all matching objects in the collection.
// Otherwise only updates first matching doc.
UPDATE_MULTIUPDATE = 2
} Flags;
UpdateRequest(const std::string& collectionName, Flags flags = UPDATE_NOFLAGS);
/// Constructor.
/// The full collection name is the concatenation of the database
/// name with the collection name, using a "." for the concatenation. For example,
/// for the database "foo" and the collection "bar", the full collection name is
/// "foo.bar".
virtual ~UpdateRequest();
/// Destructor
Dynamic::Struct<std::string>& selector();
/// Returns the selector document
Dynamic::Struct<std::string>& update();
/// The document to update
Flags flags() const;
/// Returns the flags
void flags(Flags flags);
/// Sets the flags
protected:
void buildRequest(BinaryWriter& writer);
private:
Flags _flags;
std::string _fullCollectionName;
Dynamic::Struct<std::string> _selector;
Dynamic::Struct<std::string> _update;
};
inline UpdateRequest::Flags UpdateRequest::flags() const
{
return _flags;
}
inline void UpdateRequest::flags(UpdateRequest::Flags flags)
{
_flags = flags;
}
inline Dynamic::Struct<std::string>& UpdateRequest::selector()
{
return _selector;
}
inline Dynamic::Struct<std::string>& UpdateRequest::update()
{
return _update;
}
}} // Namespace Poco::MongoDB
#endif //_MongoDB_UpdateRequest_included