diff --git a/MongoDB/Makefile b/MongoDB/Makefile new file mode 100644 index 000000000..5eb5a65e9 --- /dev/null +++ b/MongoDB/Makefile @@ -0,0 +1,23 @@ +# +# Makefile +# +# $Id$ +# +# Makefile for Poco MongoDB +# + +include $(POCO_BASE)/build/rules/global + +INCLUDE += -I $(POCO_BASE)/MongoDB/include/Poco/MongoDB + +objects = Array Binary Connection DeleteRequest \ + Document Element GetMoreRequest InsertRequest \ + KillCursorsRequest Message MessageHeader ObjectId \ + QueryRequest ReplicaSet RequestMessage ResponseMessage \ + UpdateRequest + +target = PocoMongoDB +target_version = $(LIBVERSION) +target_libs = PocoFoundation PocoNet + +include $(POCO_BASE)/build/rules/lib diff --git a/MongoDB/include/Poco/MongoDB/Array.h b/MongoDB/include/Poco/MongoDB/Array.h new file mode 100644 index 000000000..34c9fb1ae --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Array.h @@ -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 Ptr; + + + Array(); + /// Constructor + + + virtual ~Array(); + /// Destructor + +}; +/* +// BSON Embedded Array +// spec: document +template<> +struct ElementTraits +{ + enum { TypeId = 0x04 }; +}; + +template<> +inline void BSONReader::read(Array::Ptr& to) +{ + to->read(_reader); +} + +template<> +inline void BSONWriter::write(Array::Ptr& from) +{ + from->write(_writer); +} +*/ + +}} // Namespace Poco::MongoDB + +#endif //_MongoDB_Array_included diff --git a/MongoDB/include/Poco/MongoDB/BSONReader.h b/MongoDB/include/Poco/MongoDB/BSONReader.h new file mode 100644 index 000000000..1851e89b2 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/BSONReader.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/BSONWriter.h b/MongoDB/include/Poco/MongoDB/BSONWriter.h new file mode 100644 index 000000000..a11ef792a --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/BSONWriter.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/Binary.h b/MongoDB/include/Poco/MongoDB/Binary.h new file mode 100644 index 000000000..a487eae1d --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Binary.h @@ -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. +{ +public: + typedef SharedPtr 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 _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 +{ + enum { TypeId = 0x05 }; +}; + +template<> +inline void BSONReader::read(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& from) +{ + _writer << from->subtype(); + _writer.writeRaw((char*) from->begin(), from->size()); +} +*/ + +}} // Namespace Poco::MongoDB + +#endif // _MongoDB_Binary_included diff --git a/MongoDB/include/Poco/MongoDB/Connection.h b/MongoDB/include/Poco/MongoDB/Connection.h new file mode 100644 index 000000000..c9bb26c9b --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Connection.h @@ -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 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 diff --git a/MongoDB/include/Poco/MongoDB/DeleteRequest.h b/MongoDB/include/Poco/MongoDB/DeleteRequest.h new file mode 100644 index 000000000..2d469ee0b --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/DeleteRequest.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/Document.h b/MongoDB/include/Poco/MongoDB/Document.h new file mode 100644 index 000000000..dd68e1ae9 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Document.h @@ -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 Document; + typedef SharedPtr DocumentPtr; + typedef std::vector 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 Ptr; + + + typedef std::vector Vector; + + + Document(); + + + virtual ~Document(); + + + void read(BinaryReader& reader); + + + void write(BinaryWriter& writer); + + + template + T get(const std::string& name) + { + Element::Ptr element = get(name); + if ( element.isNull() ) + { + throw Poco::NotFoundException(name); + } + else + { + if ( ElementTraits::TypeId == element->type() ) + { + ConcreteElement* concrete = dynamic_cast* >(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 + bool isType(const std::string& name) + { + Element::Ptr element = get(name); + if ( element.isNull() ) + { + return false; + } + + return ElementTraits::TypeId == element->type(); + } + + + void addElement(Element::Ptr element); + + + template + void add(const std::string& name, T value) + { + addElement(new ConcreteElement(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 +{ + enum { TypeId = 0x03 }; +}; + +template<> +inline void BSONReader::read(Document::Ptr& to) +{ + to->read(_reader); +} + +template<> +inline void BSONWriter::write(Document::Ptr& from) +{ + from->write(_writer); +} +*/ +}} // Namespace Poco::MongoDB + +#endif // _MongoDB_Document_included diff --git a/MongoDB/include/Poco/MongoDB/Element.h b/MongoDB/include/Poco/MongoDB/Element.h new file mode 100644 index 000000000..143133423 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Element.h @@ -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 +#include + +#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 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 ElementSet; + + +template +struct ElementTraits +{ +}; + +// BSON Floating point +// spec: double +template<> +struct ElementTraits +{ + 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 +{ + enum { TypeId = 0x02 }; +}; +/* +template<> +inline void BSONReader::read(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& from) +{ + _writer << (Poco::Int32) (from.length() + 1); + writeCString(from); +} +*/ + + +// BSON bool +// spec: "\x00" "\x01" +template<> +struct ElementTraits +{ + enum { TypeId = 0x08 }; +}; +/* +template<> +inline void BSONReader::read(bool& to) +{ + Int32 b; + _reader >> b; + to = b != 0; +} + +template<> +inline void BSONWriter::write(bool& from) +{ + _writer << (from ? 0x01 : 0x00); +} +*/ +// BSON 32-bit integer +// spec: int32 +template<> +struct ElementTraits +{ + enum { TypeId = 0x10 }; +}; + +// BSON UTC datetime +// spec: int64 +template<> +struct ElementTraits +{ + enum { TypeId = 0x09 }; +}; +/* +template<> +inline void BSONReader::read(Timestamp& to) +{ + Poco::Int64 value; + _reader >> value; + to = Timestamp::fromEpochTime(value / 1000); + to += (value % 1000 * 1000); +} + +template<> +inline void BSONWriter::write(Timestamp& from) +{ + _writer << (from.epochMicroseconds() / 1000); +} +*/ +typedef Nullable NullValue; + +// BSON Null Value +// spec: +template<> +struct ElementTraits +{ + enum { TypeId = 0x0A }; +}; +/* +template<> +inline void BSONReader::read(NullValue& to) +{ +} + +template<> +inline void BSONWriter::write(NullValue& from) +{ +} +*/ + +class RegularExpression +{ +public: + + typedef SharedPtr 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 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 +{ + enum { TypeId = 0x0B }; +}; +/* +template<> +inline void BSONReader::read(RegularExpression::Ptr& to) +{ + std::string pattern = readCString(); + std::string options = readCString(); + + to = new RegularExpression(pattern, options); +} + +template<> +inline void BSONWriter::write(RegularExpression::Ptr& from) +{ + writeCString(from->getPattern()); + writeCString(from->getOptions()); +} +*/ + +class JavaScriptCode +{ +public: + typedef SharedPtr 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 +{ + enum { TypeId = 0x0D }; +}; +/* +template<> +inline void BSONReader::read(JavaScriptCode::Ptr& to) +{ + std::string code; + BSONReader(_reader).read(code); + to = new JavaScriptCode(); + to->code(code); +} + +template<> +inline void BSONWriter::write(JavaScriptCode::Ptr& from) +{ + std::string code = from->code(); + BSONWriter(_writer).write(code); +} +*/ +// BSON 64-bit integer +// spec: int64 +template<> +struct ElementTraits +{ + enum { TypeId = 0x12 }; +}; + + +template +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::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 + diff --git a/MongoDB/include/Poco/MongoDB/GetMoreRequest.h b/MongoDB/include/Poco/MongoDB/GetMoreRequest.h new file mode 100644 index 000000000..9b79c38c1 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/GetMoreRequest.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/InsertRequest.h b/MongoDB/include/Poco/MongoDB/InsertRequest.h new file mode 100644 index 000000000..2fd8f0ba9 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/InsertRequest.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h b/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h new file mode 100644 index 000000000..23c0f04e6 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/KillCursorsRequest.h @@ -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& cursors(); + /// Return the cursors + + +protected: + + void buildRequest(BinaryWriter& writer); + + std::vector _cursors; +}; + +inline std::vector& KillCursorsRequest::cursors() +{ + return _cursors; +} + + +}} // Namespace Poco::MongoDB + +#endif //_MongoDB_KillCursorsRequest_included diff --git a/MongoDB/include/Poco/MongoDB/Message.h b/MongoDB/include/Poco/MongoDB/Message.h new file mode 100644 index 000000000..e0697e3e1 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/Message.h @@ -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 + +#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 diff --git a/MongoDB/include/Poco/MongoDB/MessageHeader.h b/MongoDB/include/Poco/MongoDB/MessageHeader.h new file mode 100644 index 000000000..991ae1ce2 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/MessageHeader.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/MongoDB.h b/MongoDB/include/Poco/MongoDB/MongoDB.h new file mode 100644 index 000000000..d2751dd9f --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/MongoDB.h @@ -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 diff --git a/MongoDB/include/Poco/MongoDB/ObjectId.h b/MongoDB/include/Poco/MongoDB/ObjectId.h new file mode 100644 index 000000000..ce6e6d8c5 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/ObjectId.h @@ -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 Ptr; + + + ObjectId(); + + + virtual ~ObjectId(); + + + std::string toString() const; + +private: + unsigned char _id[12]; +}; +/* +// BSON Embedded Document +// spec: ObjectId +template<> +struct ElementTraits +{ + enum { TypeId = 0x07 }; +}; + +template<> +inline void BSONReader::read(ObjectId::Ptr& to) +{ + _reader.readRaw((char*) to->_id, 12); +} + +template<> +inline void BSONWriter::write(ObjectId::Ptr& from) +{ + _writer.writeRaw((char*) from->_id, 12); +} +*/ + +}} // Namespace Poco::MongoDB + +namespace Poco { +namespace Dynamic { + +template<> +class VarHolderImpl: public VarHolder +{ + +}; + +}} // Poco::Dynamic + +#endif //_MongoDB_ObjectId_included diff --git a/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h b/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h new file mode 100644 index 000000000..c6b309518 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/PoolableConnectionFactory.h @@ -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 +{ +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 diff --git a/MongoDB/include/Poco/MongoDB/QueryRequest.h b/MongoDB/include/Poco/MongoDB/QueryRequest.h new file mode 100644 index 000000000..3bceabc7c --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/QueryRequest.h @@ -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& query(); + /// Returns the query document + + + Dynamic::Struct& 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 _query; + + Dynamic::Struct _returnFieldSelector; +}; + +inline QueryRequest::Flags QueryRequest::flags() const +{ + return _flags; +} + +inline void QueryRequest::flags(QueryRequest::Flags flags) +{ + _flags = flags; +} + +inline Dynamic::Struct& QueryRequest::query() +{ + return _query; +} + +inline Dynamic::Struct& 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 diff --git a/MongoDB/include/Poco/MongoDB/ReplicaSet.h b/MongoDB/include/Poco/MongoDB/ReplicaSet.h new file mode 100644 index 000000000..2d8899785 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/ReplicaSet.h @@ -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 +#include "Poco/Net/SocketAddress.h" +#include "Poco/MongoDB/Connection.h" + +namespace Poco +{ +namespace MongoDB +{ + +class MongoDB_API ReplicaSet +{ +public: + ReplicaSet(const std::vector& addresses); + + + virtual ~ReplicaSet(); + + + Connection::Ptr findMaster(); + +private: + + Connection::Ptr isMaster(const Net::SocketAddress& host); + + + std::vector _addresses; +}; + +#endif // _MongoDB_ReplicaSet_h +}} // Poco::MongoDB diff --git a/MongoDB/include/Poco/MongoDB/RequestMessage.h b/MongoDB/include/Poco/MongoDB/RequestMessage.h new file mode 100644 index 000000000..7b97b9ac9 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/RequestMessage.h @@ -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 + +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 diff --git a/MongoDB/include/Poco/MongoDB/ResponseMessage.h b/MongoDB/include/Poco/MongoDB/ResponseMessage.h new file mode 100644 index 000000000..9aad25686 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/ResponseMessage.h @@ -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 + +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 diff --git a/MongoDB/include/Poco/MongoDB/UpdateRequest.h b/MongoDB/include/Poco/MongoDB/UpdateRequest.h new file mode 100644 index 000000000..efb67dd06 --- /dev/null +++ b/MongoDB/include/Poco/MongoDB/UpdateRequest.h @@ -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& selector(); + /// Returns the selector document + + + Dynamic::Struct& 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 _selector; + + Dynamic::Struct _update; +}; + + +inline UpdateRequest::Flags UpdateRequest::flags() const +{ + return _flags; +} + +inline void UpdateRequest::flags(UpdateRequest::Flags flags) +{ + _flags = flags; +} + +inline Dynamic::Struct& UpdateRequest::selector() +{ + return _selector; +} + +inline Dynamic::Struct& UpdateRequest::update() +{ + return _update; +} + + +}} // Namespace Poco::MongoDB + +#endif //_MongoDB_UpdateRequest_included diff --git a/MongoDB/premake4.lua b/MongoDB/premake4.lua new file mode 100644 index 000000000..7ec81ed5f --- /dev/null +++ b/MongoDB/premake4.lua @@ -0,0 +1,147 @@ +-- Where is POCO? + +--poco_dir = "C:/Users/u77589/Documents/poco-1.4.3p1-all" +poco_dir = "/home/bronx/Development/PocoTrunk" +poco_lib_dir = poco_dir .. "/lib" + +-- Solution + +solution "PocoMongoDB" + configurations { "Debug", "Release" } + platforms { "x32", "x64" } + + location ( "build/" .. _ACTION ) + + configuration "Debug" + targetdir "bin/Debug" + defines { "DEBUG" } + flags { "Symbols" } + + configuration "Release" + targetdir "bin/Release" + + configuration { "linux", "Release" } + libdirs { + "bin/Release" + } + configuration { "linux", "Debug" } + libdirs { + "bin/Debug" + } + +project "PocoMongoDB" + kind "SharedLib" + language "C++" + targetname "PocoMongoDB" + + location ( solution().location ) + + configuration "Debug" + targetsuffix "d" + + configuration { } + + defines { "MongoDB_EXPORTS" } + + files { + "src/*.cpp" + , "include/**.h" + } + + libdirs { + poco_lib_dir + } + + includedirs { + poco_dir .. "/Foundation/include" + , poco_dir .. "/Net/include" + , poco_dir .. "/Util/include" + , poco_dir .. "/XML/include" + , "include" + } + + configuration "Debug" + links { + "PocoNetd" + , "PocoFoundationd" + } + + configuration "Release" + links { + "PocoNet" + , "PocoFoundation" + } + +project "MongoDBTestSuite" + kind "ConsoleApp" + language "C++" + + configuration "Debug" + targetsuffix "d" + + configuration { } + + files { + "testsuite/src/*.cpp" + } + + includedirs { + poco_dir .. "/Foundation/include" + , poco_dir .. "/Net/include" + , poco_dir .. "/CppUnit/include" + , "include" + } + + libdirs { + poco_lib_dir + } + + configuration "Debug" + links { + "PocoFoundationd" + , "PocoMongoDBd" + } + + configuration "Release" + links { + "PocoFoundation" + , "PocoMongoDB" + } + +project "MongoDBSample" + kind "ConsoleApp" + language "C++" + targetname "MongoDBSample" + + configuration "Debug" + + targetsuffix "d" + + configuration { } + + files { + "samples/**.cpp" + } + + includedirs { + poco_dir .. "/Foundation/include" + , poco_dir .. "/Net/include" + , "include" + } + + libdirs { + poco_lib_dir + } + + configuration "Debug" + links { + "PocoFoundationd" + , "PocoMongoDBd" + } + + configuration "Release" + links { + "PocoFoundation" + , "PocoMongoDB" + } + diff --git a/MongoDB/src/Array.cpp b/MongoDB/src/Array.cpp new file mode 100644 index 000000000..5eb4d697c --- /dev/null +++ b/MongoDB/src/Array.cpp @@ -0,0 +1,54 @@ +// +// Array.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Array +// +// Implementation 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. +// + +#include "Poco/MongoDB/Array.h" + +namespace Poco +{ +namespace MongoDB +{ + +Array::Array() : Document() +{ +} + + +Array::~Array() +{ +} + +}} // Namespace Poco::Mongo diff --git a/MongoDB/src/BSONWriter.cpp b/MongoDB/src/BSONWriter.cpp new file mode 100644 index 000000000..b59d6c7c3 --- /dev/null +++ b/MongoDB/src/BSONWriter.cpp @@ -0,0 +1,129 @@ +// +// BSONWriter.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: BSONWriter +// +// Implementation 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. +// +#include + +#include "Poco/MongoDB/BSONWriter.h" + +namespace Poco { +namespace MongoDB { + +BSONWriter::BSONWriter(Poco::BinaryWriter& writer) : _writer(writer) +{ +} + +BSONWriter::~BSONWriter() +{ +} + +void BSONWriter::write(const Document& doc) +{ + if ( doc.empty() ) + { + _writer << 5; + } + else + { + std::stringstream sstream; + Poco::BinaryWriter tempWriter(sstream); + for(Document::ConstIterator it = doc.begin(); it != doc.end(); ++it) + { + const Dynamic::Var& element = it->second; + if ( element.isEmpty() ) + { + tempWriter << (unsigned char) 0x0A; + BSONWriter(tempWriter).writeCString(it->first); + } + else if ( element.isNumeric() ) + { + if ( element.isInteger() ) + { + tempWriter << (unsigned char) 0x10; + BSONWriter(tempWriter).writeCString(it->first); + tempWriter << (Int32) element; + } + else + { + tempWriter << (unsigned char) 0x01; + BSONWriter(tempWriter).writeCString(it->first); + tempWriter << (double) element; + } + } + else if ( element.isString() ) + { + tempWriter << (unsigned char) 0x02; + BSONWriter(tempWriter).writeCString(it->first); + const std::string& s = element.extract(); + tempWriter << (Poco::Int32) (s.length() + 1); + BSONWriter(tempWriter).writeCString(s); + } + else if ( element.isStruct() ) + { + tempWriter << (unsigned char) 0x03; + BSONWriter(tempWriter).writeCString(it->first); + BSONWriter(tempWriter).write(element.extract()); + } + else if ( element.isArray() ) // TODO + { + + } + else if ( element.type() == typeid(bool) ) + { + tempWriter << (unsigned char) 0x08; + BSONWriter(tempWriter).writeCString(it->first); + tempWriter << (element.extract() ? 0x01 : 0x00); + } + else if ( element.type() == typeid(DateTime) ) + { + + } + } + tempWriter.flush(); + + Poco::Int32 len = 5 + sstream.tellp(); /* 5 = sizeof(len) + 0-byte */ + _writer << len; + _writer.writeRaw(sstream.str()); + } + _writer << '\0'; +} + +void BSONWriter::writeCString(const std::string& value) +{ + _writer.writeRaw(value); + _writer << (unsigned char) 0x00; +} + +}} // Namespace Poco::MongoDB diff --git a/MongoDB/src/Binary.cpp b/MongoDB/src/Binary.cpp new file mode 100644 index 000000000..69014c9dc --- /dev/null +++ b/MongoDB/src/Binary.cpp @@ -0,0 +1,65 @@ +// +// Binary.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Binary +// +// Implementation 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. +// + +#include "Poco/MongoDB/Binary.h" + +namespace Poco +{ +namespace MongoDB +{ + + +Binary::Binary() : _buffer(0) +{ +} + + +Binary::Binary(Poco::Int32 size, unsigned char subtype) : _buffer(size), _subtype(subtype) +{ +} + +Binary::~Binary() +{ +} + +void Binary::resize(std::size_t newSize) +{ + //TODO: + //_buffer.resize(newSize); +} + +}} // Namespace Poco::MongoDB diff --git a/MongoDB/src/Connection.cpp b/MongoDB/src/Connection.cpp new file mode 100644 index 000000000..13d69ad00 --- /dev/null +++ b/MongoDB/src/Connection.cpp @@ -0,0 +1,125 @@ +// +// Connection.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Connection +// +// Implementation 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. +// +#include + +#include "Poco/Net/SocketStream.h" +#include "Poco/MongoDB/Connection.h" + +namespace Poco +{ +namespace MongoDB +{ + +Connection::Connection() : _address(), _socket() +{ +} + + +Connection::Connection(const std::string& hostAndPort) : _address(hostAndPort), _socket() +{ + connect(); +} + +Connection::Connection(const std::string& host, int port) : _address(host, port), _socket() +{ + connect(); +} + + +Connection::Connection(const Net::SocketAddress& addrs) : _address(addrs), _socket() +{ + connect(); +} + + +Connection::~Connection() +{ + try + { + _socket.close(); + } + catch (...) + { + } +} + + +void Connection::connect() +{ + _socket.connect(_address); +} + + +void Connection::connect(const std::string& hostAndPort) +{ + _address = Net::SocketAddress(hostAndPort); + connect(); +} + +void Connection::connect(const std::string& host, int port) +{ + _address = Net::SocketAddress(host, port); + connect(); +} + + +void Connection::connect(const Net::SocketAddress& addrs) +{ + _address = addrs; + connect(); +} + +void Connection::disconnect() +{ + _socket.close(); +} + +void Connection::sendRequest(RequestMessage& request) +{ + Net::SocketOutputStream sos(_socket); + request.send(sos); +} + +void Connection::sendRequest(RequestMessage& request, ResponseMessage& response) +{ + sendRequest(request); + + Net::SocketInputStream sis(_socket); + response.read(sis); +} + +} } // Poco::MongoDB diff --git a/MongoDB/src/DeleteRequest.cpp b/MongoDB/src/DeleteRequest.cpp new file mode 100644 index 000000000..cb91eae23 --- /dev/null +++ b/MongoDB/src/DeleteRequest.cpp @@ -0,0 +1,69 @@ +// +// DeleteRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: DeleteRequest +// +// Implementation 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. +// + +#include "Poco/MongoDB/DeleteRequest.h" +#include "Poco/MongoDB/BSONWriter.h" + +namespace Poco +{ +namespace MongoDB +{ + + +DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::Flags flags) + : RequestMessage(MessageHeader::Delete), + _flags(flags), + _fullCollectionName(collectionName), + _selector() +{ +} + +DeleteRequest::~DeleteRequest() +{ +} + +void DeleteRequest::buildRequest(BinaryWriter& writer) +{ + BSONWriter bsonWriter(writer); + writer << 0; // 0 - reserved for future use + bsonWriter.writeCString(_fullCollectionName); + writer << _flags; + bsonWriter.write(_selector); +} + + +}} // Namespace MongoDB diff --git a/MongoDB/src/Document.cpp b/MongoDB/src/Document.cpp new file mode 100644 index 000000000..3d60d1897 --- /dev/null +++ b/MongoDB/src/Document.cpp @@ -0,0 +1,175 @@ +// +// Document.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Document +// +// Implementation 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. +// + +#include + +#include "Poco/MongoDB/Document.h" +#include "Poco/MongoDB/Binary.h" +#include "Poco/MongoDB/ObjectId.h" +#include "Poco/MongoDB/Array.h" + +namespace Poco +{ +namespace MongoDB +{ + + +Document::Document() +{ +} + +Document::~Document() +{ +} + +void Document::read(BinaryReader& reader) +{ + int size; + reader >> size; + + unsigned char type; + reader >> type; + + while( type != '\0' ) + { + Element::Ptr element; + + std::string name = BSONReader(reader).readCString(); + + switch(type) + { + case ElementTraits::TypeId: + element = new ConcreteElement(name, 0); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, 0); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, ""); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new Document()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new Array()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new Binary()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new ObjectId()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, false); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, Poco::Timestamp()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, NullValue(0)); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new RegularExpression()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, new JavaScriptCode()); + break; + case ElementTraits::TypeId: + element = new ConcreteElement(name, 0); + break; + default: + { + std::stringstream ss; + ss << "Element " << name << " contains an unsupported type " << std::hex << (int) type; + throw Poco::NotImplementedException(ss.str()); + } + //TODO: x0F -> JavaScript code with scope + // xFF -> Min Key + // x7F -> Max Key + } + + element->read(reader); + _elements.insert(element); + + reader >> type; + } +} + + +std::string Document::toString() const +{ + std::ostringstream oss; + for(ElementSet::iterator it = _elements.begin(); it != _elements.end(); ++it) + { + oss << (*it)->name() << " "; + } + return oss.str(); +} + + +void Document::write(BinaryWriter& writer) +{ + if ( _elements.empty() ) + { + writer << 5; + } + else + { + std::stringstream sstream; + Poco::BinaryWriter tempWriter(sstream); + for(ElementSet::iterator it = _elements.begin(); it != _elements.end(); ++it) + { + tempWriter << (unsigned char) (*it)->type(); + BSONWriter(tempWriter).writeCString((*it)->name()); + Element::Ptr element = *it; + element->write(tempWriter); + } + tempWriter.flush(); + + Poco::Int32 len = 5 + sstream.tellp(); /* 5 = sizeof(len) + 0-byte */ + writer << len; + writer.writeRaw(sstream.str()); + } + writer << '\0'; +} + +void Document::addElement(Element::Ptr element) +{ + _elements.insert(element); +} + +}} // Namespace Poco::MongoDB diff --git a/MongoDB/src/Element.cpp b/MongoDB/src/Element.cpp new file mode 100644 index 000000000..1662ee2d3 --- /dev/null +++ b/MongoDB/src/Element.cpp @@ -0,0 +1,54 @@ +// +// Element.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Element +// +// Implementation 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. +// + +#include "Poco/MongoDB/Element.h" + +namespace Poco +{ +namespace MongoDB +{ + +Element::Element(const std::string& name) : _name(name) +{ +} + +Element::~Element() +{ +} + + +}} // Namespace Poco::MongoDB diff --git a/MongoDB/src/GetMoreRequest.cpp b/MongoDB/src/GetMoreRequest.cpp new file mode 100644 index 000000000..f521293fe --- /dev/null +++ b/MongoDB/src/GetMoreRequest.cpp @@ -0,0 +1,68 @@ +// +// GetMoreRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: GetMoreRequest +// +// Implementation 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. +// + +#include "Poco/MongoDB/GetMoreRequest.h" +#include "Poco/MongoDB/Element.h" + +namespace Poco +{ +namespace MongoDB +{ + + +GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID) + : RequestMessage(MessageHeader::GetMore), + _fullCollectionName(collectionName), + _numberToReturn(0), + _cursorID(cursorID) +{ +} + +GetMoreRequest::~GetMoreRequest() +{ +} + +void GetMoreRequest::buildRequest(BinaryWriter& writer) +{ + writer << 0; // 0 - reserved for future use + BSONWriter(writer).writeCString(_fullCollectionName); + writer << _numberToReturn; + writer << _cursorID; +} + + +}} // Namespace MongoDB diff --git a/MongoDB/src/InsertRequest.cpp b/MongoDB/src/InsertRequest.cpp new file mode 100644 index 000000000..be06a29a5 --- /dev/null +++ b/MongoDB/src/InsertRequest.cpp @@ -0,0 +1,70 @@ +// +// InsertRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: InsertRequest +// +// Implementation 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. +// +#include "Poco/MongoDB/BSONWriter.h" +#include "Poco/MongoDB/InsertRequest.h" + +namespace Poco +{ +namespace MongoDB +{ + + +InsertRequest::InsertRequest(const std::string& collectionName, Flags flags) + : RequestMessage(MessageHeader::Insert), + _fullCollectionName(collectionName), + _flags(flags) +{ +} + +InsertRequest::~InsertRequest() +{ +} + +void InsertRequest::buildRequest(BinaryWriter& writer) +{ + //TODO: throw exception when no document is added + BSONWriter bsonWriter(writer); + writer << _flags; + bsonWriter.writeCString(_fullCollectionName); + for(Documents::iterator it = _documents.begin(); it != _documents.end(); ++it) + { + BSONWriter bsonWriter(writer); + bsonWriter.write(**it); + } +} + +}} // Namespace MongoDB diff --git a/MongoDB/src/KillCursorsRequest.cpp b/MongoDB/src/KillCursorsRequest.cpp new file mode 100644 index 000000000..077db5c20 --- /dev/null +++ b/MongoDB/src/KillCursorsRequest.cpp @@ -0,0 +1,66 @@ +// +// KillCursorsRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: KillCursorsRequest +// +// Implementation 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. +// + +#include "Poco/MongoDB/KillCursorsRequest.h" + +namespace Poco +{ +namespace MongoDB +{ + + +KillCursorsRequest::KillCursorsRequest() + : RequestMessage(MessageHeader::KillCursors) +{ +} + +KillCursorsRequest::~KillCursorsRequest() +{ +} + +void KillCursorsRequest::buildRequest(BinaryWriter& writer) +{ + writer << 0; // 0 - reserved for future use + writer << _cursors.size(); + for(std::vector::iterator it = _cursors.begin(); it != _cursors.end(); ++it) + { + writer << *it; + } +} + + +}} // Namespace MongoDB diff --git a/MongoDB/src/Message.cpp b/MongoDB/src/Message.cpp new file mode 100644 index 000000000..66c8f177f --- /dev/null +++ b/MongoDB/src/Message.cpp @@ -0,0 +1,54 @@ +// +// Message.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: Message +// +// Implementation 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. +// + +#include "Poco/MongoDB/Message.h" + +namespace Poco +{ +namespace MongoDB +{ + + +Message::Message(MessageHeader::OpCode opcode) : _header(opcode) +{ +} + +Message::~Message() +{ +} + +}} // Namespace MongoDB diff --git a/MongoDB/src/MessageHeader.cpp b/MongoDB/src/MessageHeader.cpp new file mode 100644 index 000000000..0cd6fe159 --- /dev/null +++ b/MongoDB/src/MessageHeader.cpp @@ -0,0 +1,80 @@ +// +// MessageHeader.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: MessageHeader +// +// Implementation 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. +// +#include "Poco/MongoDB/Message.h" + +#include "Poco/Exception.h" +#include "Poco/Net/SocketStream.h" +#include "Poco/StreamCopier.h" + +namespace Poco +{ +namespace MongoDB +{ + +MessageHeader::MessageHeader(OpCode opCode) : _messageLength(0), _requestID(0), _responseTo(0), _opCode(opCode) +{ +} + +MessageHeader::~MessageHeader() +{ +} + +void MessageHeader::read(BinaryReader& reader) +{ + reader >> _messageLength; + reader >> _requestID; + reader >> _responseTo; + + Int32 opCode; + reader >> opCode; + _opCode = (OpCode) opCode; + + if (!reader.good()) + { + throw IOException("Failed to read from socket"); + } +} + +void MessageHeader::write(BinaryWriter& writer) +{ + writer << _messageLength; + writer << _requestID; + writer << _responseTo; + writer << (Int32) _opCode; +} + +}} // Namespace MongoDB diff --git a/MongoDB/src/ObjectId.cpp b/MongoDB/src/ObjectId.cpp new file mode 100644 index 000000000..c81509697 --- /dev/null +++ b/MongoDB/src/ObjectId.cpp @@ -0,0 +1,62 @@ +// +// ObjectId.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: ObjectId +// +// Implementation 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. +// + +#include "Poco/MongoDB/ObjectId.h" + +#include "Poco/Format.h" + +namespace Poco +{ +namespace MongoDB +{ + +ObjectId::ObjectId() +{ +} + + +ObjectId::~ObjectId() +{ +} + + +std::string ObjectId::toString() const +{ + return format("%X:%X:%X", (unsigned int) _id[0], (unsigned int) _id[4], (unsigned int) _id[8]); +} + +}} // Namespace Poco::MongoDB diff --git a/MongoDB/src/QueryRequest.cpp b/MongoDB/src/QueryRequest.cpp new file mode 100644 index 000000000..88baf3fb3 --- /dev/null +++ b/MongoDB/src/QueryRequest.cpp @@ -0,0 +1,78 @@ +// +// QueryRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: QueryRequest +// +// Implementation 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. +// + +#include "Poco/MongoDB/QueryRequest.h" +#include "Poco/MongoDB/BSONWriter.h" + +namespace Poco +{ +namespace MongoDB +{ + + +QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flags flags) + : RequestMessage(MessageHeader::Query), + _flags(flags), + _fullCollectionName(collectionName), + _numberToSkip(0), + _numberToReturn(0), + _query(), + _returnFieldSelector() +{ +} + +QueryRequest::~QueryRequest() +{ +} + +void QueryRequest::buildRequest(BinaryWriter& writer) +{ + BSONWriter bsonWriter(writer); + writer << _flags; + bsonWriter.writeCString(_fullCollectionName); + writer << _numberToSkip; + writer << _numberToReturn; + bsonWriter.write(_query); + + if ( ! _returnFieldSelector.empty() ) + { + bsonWriter.write(_returnFieldSelector); + } +} + + +}} // Namespace MongoDB diff --git a/MongoDB/src/ReplicaSet.cpp b/MongoDB/src/ReplicaSet.cpp new file mode 100644 index 000000000..326262ed6 --- /dev/null +++ b/MongoDB/src/ReplicaSet.cpp @@ -0,0 +1,111 @@ +// +// ReplicaSet.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: ReplicaSet +// +// Implementation 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. +// +#include "Poco/MongoDB/ReplicaSet.h" +#include "Poco/MongoDB/QueryRequest.h" +#include "Poco/MongoDB/ResponseMessage.h" + +namespace Poco +{ +namespace MongoDB +{ + +ReplicaSet::ReplicaSet(const std::vector &addresses) : _addresses(addresses) +{ +} + +ReplicaSet::~ReplicaSet() +{ +} + + +Connection::Ptr ReplicaSet::findMaster() +{ + Connection::Ptr master; + + for(std::vector::iterator it = _addresses.begin(); it != _addresses.end(); ++it) + { + master = isMaster(*it); + if ( ! master.isNull() ) + { + break; + } + } + + return master; +} + +Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address) +{ + Connection::Ptr conn = new Connection(); + + try + { + conn->connect(address); + + QueryRequest request("admin.$cmd"); + request.numberToReturn(1); + request.query().insert("isMaster", 1); + + ResponseMessage response; + conn->sendRequest(request, response); + + if ( response.documents().size() > 0 ) + { + DocumentPtr doc = response.documents()[0]; + Dynamic::Var isMasterVar = (*doc)["ismaster"]; + if ( !isMasterVar.isEmpty() && isMasterVar ) + { + return conn; + } + else if ( doc->contains("primary") ) + { + Dynamic::Var& primary = (*doc)["primary"]; + Net::SocketAddress primaryAddress(primary.convert()); + return isMaster(primaryAddress); + } + } + } + catch(...) + { + conn = NULL; + } + + return NULL; +} + + +}} // Poco::MongoDB diff --git a/MongoDB/src/RequestMessage.cpp b/MongoDB/src/RequestMessage.cpp new file mode 100644 index 000000000..63dd78e85 --- /dev/null +++ b/MongoDB/src/RequestMessage.cpp @@ -0,0 +1,72 @@ +// +// RequestMessage.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: RequestMessage +// +// Implementation 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. +// + +#include "Poco/MongoDB/RequestMessage.h" +#include "Poco/Net/SocketStream.h" +#include "Poco/StreamCopier.h" + +namespace Poco +{ +namespace MongoDB +{ + + +RequestMessage::RequestMessage(MessageHeader::OpCode opcode) : Message(opcode) +{ +} + +RequestMessage::~RequestMessage() +{ +} + + +void RequestMessage::send(std::ostream& ostr) +{ + std::stringstream ss; + BinaryWriter requestWriter(ss); + buildRequest(requestWriter); + requestWriter.flush(); + + messageLength(ss.tellp()); + + BinaryWriter socketWriter(ostr, BinaryWriter::LITTLE_ENDIAN_BYTE_ORDER); + _header.write(socketWriter); + StreamCopier::copyStream(ss, ostr); + ostr.flush(); +} + +}} // Namespace MongoDB diff --git a/MongoDB/src/ResponseMessage.cpp b/MongoDB/src/ResponseMessage.cpp new file mode 100644 index 000000000..145c92886 --- /dev/null +++ b/MongoDB/src/ResponseMessage.cpp @@ -0,0 +1,85 @@ +// +// ResponseMessage.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: ResponseMessage +// +// Implementation 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. +// + +#include "Poco/MongoDB/ResponseMessage.h" +#include "Poco/MongoDB/BSONReader.h" +#include "Poco/Net/SocketStream.h" + +namespace Poco +{ +namespace MongoDB +{ + +ResponseMessage::ResponseMessage() : Message(MessageHeader::Reply), _responseFlags(0), _cursorID(0), _startingFrom(0), _numberReturned(0) +{ +} + +ResponseMessage::~ResponseMessage() +{ +} + + +void ResponseMessage::clear() +{ + _cursorID = 0; + _documents.clear(); +} + + +void ResponseMessage::read(std::istream& istr) +{ + clear(); + + BinaryReader reader(istr, BinaryReader::LITTLE_ENDIAN_BYTE_ORDER); + + _header.read(reader); + + reader >> _responseFlags; + reader >> _cursorID; + reader >> _startingFrom; + reader >> _numberReturned; + + for(int i = 0; i < _numberReturned; ++i) + { + DocumentPtr doc = new Document(); + BSONReader bsonReader(reader); + bsonReader.read(*doc); + _documents.push_back(doc); + } +} + +}} // Namespace MongoDB diff --git a/MongoDB/src/UpdateRequest.cpp b/MongoDB/src/UpdateRequest.cpp new file mode 100644 index 000000000..e9dfc1b2e --- /dev/null +++ b/MongoDB/src/UpdateRequest.cpp @@ -0,0 +1,70 @@ +// +// UpdateRequest.cpp +// +// $Id$ +// +// Library: MongoDB +// Package: MongoDB +// Module: UpdateRequest +// +// Implementation 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. +// + +#include "Poco/MongoDB/UpdateRequest.h" +#include "Poco/MongoDB/BSONWriter.h" + +namespace Poco +{ +namespace MongoDB +{ + + +UpdateRequest::UpdateRequest(const std::string& collectionName, UpdateRequest::Flags flags) + : RequestMessage(MessageHeader::Update), + _fullCollectionName(collectionName), + _flags(flags), + _selector(), + _update() +{ +} + +UpdateRequest::~UpdateRequest() +{ +} + +void UpdateRequest::buildRequest(BinaryWriter& writer) +{ + BSONWriter bsonWriter(writer); + writer << 0; // 0 - reserved for future use + bsonWriter.writeCString(_fullCollectionName); + writer << _flags; + bsonWriter.write(_selector); + bsonWriter.write(_update); +} + +}} // Namespace MongoDB diff --git a/MongoDB/testsuite/src/Driver.cpp b/MongoDB/testsuite/src/Driver.cpp new file mode 100644 index 000000000..24b88cbb1 --- /dev/null +++ b/MongoDB/testsuite/src/Driver.cpp @@ -0,0 +1,39 @@ +// +// Driver.cpp +// +// $Id$ +// +// Console-based test driver for Poco MongoDB. +// +// Copyright (c) 2004-2006, 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 "CppUnit/TestRunner.h" +#include "MongoDBTestSuite.h" + + +CppUnitMain(MongoDBTestSuite) diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp new file mode 100644 index 000000000..c63e40f65 --- /dev/null +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -0,0 +1,208 @@ +// +// MongoDBTest.cpp +// +// $Id$ +// +// Copyright (c) 2004-2006, 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 + +#include "Poco/DateTime.h" + +#include "Poco/MongoDB/InsertRequest.h" +#include "Poco/MongoDB/QueryRequest.h" +#include "Poco/MongoDB/DeleteRequest.h" + +#include "Poco/Net/NetException.h" + +#include "MongoDBTest.h" +#include "CppUnit/TestCaller.h" +#include "CppUnit/TestSuite.h" + +using namespace Poco::MongoDB; + + +MongoDBTest::MongoDBTest(const std::string& name) + : CppUnit::TestCase("MongoDB") + , _connected(false) +{ +} + + +MongoDBTest::~MongoDBTest() +{ +} + + +void MongoDBTest::setUp() +{ + try + { + _mongo.connect("localhost", 27017); + _connected = true; + } + catch(Poco::Net::ConnectionRefusedException& e) + { + std::cout << "Couldn't connect to " << e.message() << ". "; + } +} + + +void MongoDBTest::tearDown() +{ + if ( _connected ) + { + _mongo.disconnect(); + } +} + + +void MongoDBTest::testInsertRequest() +{ + if ( ! _connected ) + { + std::cout << "test skipped." << std::endl; + return; + } + + Poco::MongoDB::Document::Ptr player = new Poco::MongoDB::Document(); + player->add("lastname", std::string("Braem")); + player->add("firstname", std::string("Franky")); + + Poco::DateTime birthdate; + birthdate.assign(1969, 3, 9); + player->add("birthdate", birthdate.timestamp()); + + player->add("start", 1993); + player->add("active", false); + + Poco::DateTime now; + std::cout << now.day() << " " << now.hour() << ":" << now.minute() << ":" << now.second() << std::endl; + player->add("lastupdated", now.timestamp()); + + player->add("unknown", NullValue()); + + Poco::MongoDB::InsertRequest request("team.players"); + request.documents().push_back(player); + _mongo.sendRequest(request); +} + +void MongoDBTest::testQueryRequest() +{ + if ( ! _connected ) + { + std::cout << "test skipped." << std::endl; + return; + } + + Poco::MongoDB::QueryRequest request("team.players"); + request.query().add("lastname" , std::string("Braem")); + request.numberToReturn(1); + + 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("lastname"); + assert(lastname.compare("Braem") == 0); + std::string firstname = doc->get("firstname"); + assert(firstname.compare("Franky") == 0); + Poco::Timestamp birthDateTimestamp = doc->get("birthdate"); + Poco::DateTime birthDate(birthDateTimestamp); + assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9); + Poco::Timestamp lastupdatedTimestamp = doc->get("lastupdated"); + assert(doc->isType("unknown")); + } + catch(Poco::NotFoundException& nfe) + { + fail(nfe.message() + " not found."); + } + } + else + { + fail("No document returned"); + } +} + +void MongoDBTest::testCountCommand() +{ + if ( ! _connected ) + { + std::cout << "test skipped." << std::endl; + return; + } + + Poco::MongoDB::QueryRequest request("team.$cmd"); + request.numberToReturn(1); + request.query().add("count", std::string("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("n"); + assert(count == 1); + } + else + { + fail("Didn't get a response from the count command"); + } +} + +void MongoDBTest::testDeleteRequest() +{ + if ( ! _connected ) + { + std::cout << "test skipped." << std::endl; + return; + } + + Poco::MongoDB::DeleteRequest request("team.players"); + request.selector().add("lastname", std::string("Braem")); + + _mongo.sendRequest(request); +} + +CppUnit::Test* MongoDBTest::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest"); + + CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest); + CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest); + CppUnit_addTest(pSuite, MongoDBTest, testCountCommand); + CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest); + + return pSuite; +} diff --git a/MongoDB/testsuite/src/MongoDBTest.h b/MongoDB/testsuite/src/MongoDBTest.h new file mode 100644 index 000000000..cfc245127 --- /dev/null +++ b/MongoDB/testsuite/src/MongoDBTest.h @@ -0,0 +1,83 @@ +// +// MongoDBTest.h +// +// $Id$ +// +// Definition of the MongoDBTest class. +// +// Copyright (c) 2004-2006, 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 MongoDBTest_INCLUDED +#define MongoDBTest_INCLUDED + + +#include "Poco/MongoDB/MongoDB.h" +#include "Poco/MongoDB/Connection.h" + +#include "CppUnit/TestCase.h" + + +class MongoDBTest: public CppUnit::TestCase +{ +public: + MongoDBTest(const std::string& name); + + + virtual ~MongoDBTest(); + + + void testInsertRequest(); + + + void testQueryRequest(); + + + void testCountCommand(); + + + void testDeleteRequest(); + + + void setUp(); + + + void tearDown(); + + + static CppUnit::Test* suite(); + +private: + + bool _connected; + + Poco::MongoDB::Connection _mongo; + +}; + + +#endif // MongoDBTest_INCLUDED diff --git a/MongoDB/testsuite/src/MongoDBTestSuite.cpp b/MongoDB/testsuite/src/MongoDBTestSuite.cpp new file mode 100644 index 000000000..08706118b --- /dev/null +++ b/MongoDB/testsuite/src/MongoDBTestSuite.cpp @@ -0,0 +1,44 @@ +// +// MongoDBTestSuite.cpp +// +// $Id$ +// +// Copyright (c) 2004-2006, 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 "MongoDBTestSuite.h" +#include "MongoDBTest.h" + + +CppUnit::Test* MongoDBTestSuite::suite() +{ + CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTestSuite"); + + pSuite->addTest(MongoDBTest::suite()); + + return pSuite; +} diff --git a/MongoDB/testsuite/src/MongoDBTestSuite.h b/MongoDB/testsuite/src/MongoDBTestSuite.h new file mode 100644 index 000000000..815bbf56b --- /dev/null +++ b/MongoDB/testsuite/src/MongoDBTestSuite.h @@ -0,0 +1,49 @@ +// +// MongoDBTestSuite.h +// +// $Id$ +// +// Definition of the MongoDBTestSuite class. +// +// Copyright (c) 2004-2006, 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 MongoDBTestSuite_INCLUDED +#define MongoDBTestSuite_INCLUDED + + +#include "CppUnit/TestSuite.h" + + +class MongoDBTestSuite +{ +public: + static CppUnit::Test* suite(); +}; + + +#endif // MongoDBTestSuite_INCLUDED diff --git a/MongoDB/testsuite/src/WinCEDriver.cpp b/MongoDB/testsuite/src/WinCEDriver.cpp new file mode 100644 index 000000000..e7fd5b51e --- /dev/null +++ b/MongoDB/testsuite/src/WinCEDriver.cpp @@ -0,0 +1,52 @@ +// +// WinCEDriver.cpp +// +// $Id$ +// +// Console-based test driver for Windows CE. +// +// Copyright (c) 2004-2010, 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 "CppUnit/TestRunner.h" +#include "MongoDBTestSuite.h" +#include + + +int _tmain(int argc, wchar_t* argv[]) +{ + std::vector args; + for (int i = 0; i < argc; ++i) + { + char buffer[1024]; + std::wcstombs(buffer, argv[i], sizeof(buffer)); + args.push_back(std::string(buffer)); + } + CppUnit::TestRunner runner; + runner.addTest("MongoDBTestSuite", MongoDBTestSuite::suite()); + return runner.run(args) ? 0 : 1; +} diff --git a/MongoDB/testsuite/src/WinDriver.cpp b/MongoDB/testsuite/src/WinDriver.cpp new file mode 100644 index 000000000..f63a6f768 --- /dev/null +++ b/MongoDB/testsuite/src/WinDriver.cpp @@ -0,0 +1,50 @@ +// +// WinDriver.cpp +// +// $Id$ +// +// Windows test driver for Poco MongoDB. +// +// Copyright (c) 2004-2006, 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 "WinTestRunner/WinTestRunner.h" +#include "MongoDBTestSuite.h" + + +class TestDriver: public CppUnit::WinTestRunnerApp +{ + void TestMain() + { + CppUnit::WinTestRunner runner; + runner.addTest(MongoDBTestSuite::suite()); + runner.run(); + } +}; + + +TestDriver theDriver; diff --git a/MongoDB/testsuite/src/main.cpp b/MongoDB/testsuite/src/main.cpp new file mode 100644 index 000000000..cf810002c --- /dev/null +++ b/MongoDB/testsuite/src/main.cpp @@ -0,0 +1,36 @@ +#include +#include + +#include + +#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("n") << std::endl; + } + + std::cout << "Number of documents returned: " << response.documents().size() << std::endl; + + return 0; +}