Коммит
c24893b8ab
|
@ -94,7 +94,7 @@ ELSE ()
|
|||
ENDIF ()
|
||||
|
||||
# Set local include path
|
||||
include_directories( CppUnit/include Foundation/include XML/include Net/include NetSSL_OpenSSL/include Util/include Data/include WebWidgets/include Zip/include Crypto/include Web/include JSON/include PDF/include)
|
||||
include_directories( CppUnit/include Foundation/include XML/include Net/include NetSSL_OpenSSL/include Util/include Data/include WebWidgets/include Zip/include Crypto/include Web/include JSON/include MongoDB/include PDF/include)
|
||||
|
||||
include(CheckTypeSize)
|
||||
include(FindCygwin)
|
||||
|
@ -159,6 +159,7 @@ ENDIF ()
|
|||
add_subdirectory(Foundation)
|
||||
add_subdirectory(XML)
|
||||
add_subdirectory(JSON)
|
||||
add_subdirectory(MongoDB)
|
||||
add_subdirectory(PDF)
|
||||
add_subdirectory(Util)
|
||||
add_subdirectory(Net)
|
||||
|
|
14
Makefile
14
Makefile
|
@ -31,7 +31,7 @@ poco: libexecs $(if $(TESTS),tests) $(if $(SAMPLES),samples)
|
|||
all: libexecs tests samples
|
||||
|
||||
INSTALLDIR = $(DESTDIR)$(POCO_PREFIX)
|
||||
COMPONENTS = Foundation XML Util Net Crypto NetSSL_OpenSSL Data Data/SQLite Data/ODBC Data/MySQL Zip PageCompiler PageCompiler/File2Page PDF CppParser JSON
|
||||
COMPONENTS = Foundation XML Util Net Crypto NetSSL_OpenSSL Data Data/SQLite Data/ODBC Data/MySQL Zip PageCompiler PageCompiler/File2Page PDF CppParser JSON MongoDB
|
||||
|
||||
cppunit:
|
||||
$(MAKE) -C $(POCO_BASE)/CppUnit
|
||||
|
@ -51,8 +51,8 @@ install: libexecs
|
|||
find $(POCO_BUILD)/lib -name "libPoco*" -type f -exec cp -f {} $(INSTALLDIR)/lib$(LIB64SUFFIX) \;
|
||||
find $(POCO_BUILD)/lib -name "libPoco*" -type l -exec cp -Rf {} $(INSTALLDIR)/lib$(LIB64SUFFIX) \;
|
||||
|
||||
libexecs = Foundation-libexec XML-libexec JSON-libexec Util-libexec Net-libexec Crypto-libexec NetSSL_OpenSSL-libexec Data-libexec Data/SQLite-libexec Data/ODBC-libexec Data/MySQL-libexec Zip-libexec PageCompiler-libexec PageCompiler/File2Page-libexec PDF-libexec CppParser-libexec
|
||||
tests = Foundation-tests XML-tests JSON-tests Util-tests Net-tests Crypto-tests NetSSL_OpenSSL-tests Data-tests Data/SQLite-tests Data/ODBC-tests Data/MySQL-tests Zip-tests PDF-tests CppParser-tests
|
||||
libexecs = Foundation-libexec XML-libexec JSON-libexec Util-libexec Net-libexec Crypto-libexec NetSSL_OpenSSL-libexec Data-libexec Data/SQLite-libexec Data/ODBC-libexec Data/MySQL-libexec Zip-libexec PageCompiler-libexec PageCompiler/File2Page-libexec PDF-libexec CppParser-libexec MongoDB-libexec
|
||||
tests = Foundation-tests XML-tests JSON-tests Util-tests Net-tests Crypto-tests NetSSL_OpenSSL-tests Data-tests Data/SQLite-tests Data/ODBC-tests Data/MySQL-tests Zip-tests PDF-tests CppParser-tests MongoDB-tests
|
||||
samples = Foundation-samples XML-samples JSON-samples Util-samples Net-samples Crypto-samples NetSSL_OpenSSL-samples Data-samples Zip-samples PageCompiler-samples PDF-samples
|
||||
|
||||
.PHONY: poco all libexecs cppunit tests samples clean distclean install
|
||||
|
@ -187,6 +187,12 @@ CppParser-libexec: Foundation-libexec
|
|||
CppParser-tests: CppParser-libexec
|
||||
$(MAKE) -C $(POCO_BASE)/CppParser/testsuite
|
||||
|
||||
MongoDB-libexec: Foundation-libexec Net-libexec
|
||||
$(MAKE) -C $(POCO_BASE)/MongoDB
|
||||
|
||||
MongoDB-tests: MongoDB-libexec Net-libexec cppunit
|
||||
$(MAKE) -C $(POCO_BASE)/MongoDB/testsuite
|
||||
|
||||
|
||||
clean:
|
||||
$(MAKE) -C $(POCO_BASE)/Foundation clean
|
||||
|
@ -231,6 +237,8 @@ clean:
|
|||
$(MAKE) -C $(POCO_BASE)/JSON clean
|
||||
$(MAKE) -C $(POCO_BASE)/JSON/testsuite clean
|
||||
$(MAKE) -C $(POCO_BASE)/JSON/samples clean
|
||||
$(MAKE) -C $(POCO_BASE)/MongoDB clean
|
||||
$(MAKE) -C $(POCO_BASE)/MongoDB/testsuite clean
|
||||
|
||||
distclean:
|
||||
rm -rf $(POCO_BUILD)/lib
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
set(LIBNAME "PocoMongoDB")
|
||||
|
||||
aux_source_directory(src SRCS)
|
||||
|
||||
if (NOT POCO_STATIC)
|
||||
add_definitions(-DMONGODB_EXPORTS)
|
||||
endif (NOT POCO_STATIC)
|
||||
|
||||
add_library( ${LIBNAME} ${LIB_MODE} ${SRCS} )
|
||||
set_target_properties( ${LIBNAME}
|
||||
PROPERTIES
|
||||
VERSION ${SHARED_LIBRARY_VERSION} SOVERSION ${SHARED_LIBRARY_VERSION} )
|
||||
target_link_libraries( ${LIBNAME} PocoFoundation)
|
||||
|
||||
install(
|
||||
DIRECTORY include/Poco
|
||||
DESTINATION include
|
||||
PATTERN ".svn" EXCLUDE
|
||||
)
|
||||
|
||||
install(
|
||||
TARGETS ${LIBNAME}
|
||||
DESTINATION lib
|
||||
)
|
||||
|
||||
if (ENABLE_TESTS)
|
||||
add_subdirectory(testsuite)
|
||||
endif ()
|
||||
|
|
@ -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 Cursor DeleteRequest Database \
|
||||
Document Element GetMoreRequest InsertRequest JavaScriptCode \
|
||||
KillCursorsRequest Message MessageHeader ObjectId QueryRequest \
|
||||
RegularExpression ReplicaSet RequestMessage ResponseMessage \
|
||||
UpdateRequest
|
||||
|
||||
target = PocoMongoDB
|
||||
target_version = $(LIBVERSION)
|
||||
target_libs = PocoFoundation PocoNet
|
||||
|
||||
include $(POCO_BASE)/build/rules/lib
|
|
@ -0,0 +1,2 @@
|
|||
Foundation
|
||||
Net
|
|
@ -0,0 +1,95 @@
|
|||
//
|
||||
// Array.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Array
|
||||
//
|
||||
// Definition of the Array class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_Array_included
|
||||
#define _MongoDB_Array_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API Array : public Document
|
||||
/// Implements the BSON Array
|
||||
{
|
||||
public:
|
||||
typedef SharedPtr<Array> Ptr;
|
||||
|
||||
|
||||
Array();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Array();
|
||||
/// Destructor
|
||||
|
||||
|
||||
std::string toString(int indent = 0) const;
|
||||
};
|
||||
|
||||
// BSON Embedded Array
|
||||
// spec: document
|
||||
template<>
|
||||
struct ElementTraits<Array::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x04 };
|
||||
|
||||
static std::string toString(const Array::Ptr& value, int indent = 0)
|
||||
{
|
||||
//TODO:
|
||||
return value.isNull() ? "null" : value->toString();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<Array::Ptr>(Array::Ptr& to)
|
||||
{
|
||||
to->read(_reader);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<Array::Ptr>(Array::Ptr& from)
|
||||
{
|
||||
from->write(_writer);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_Array_included
|
|
@ -0,0 +1,103 @@
|
|||
//
|
||||
// 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/BinaryReader.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API BSONReader
|
||||
/// Class for reading BSON from a Poco::BinaryReader
|
||||
{
|
||||
public:
|
||||
BSONReader(const Poco::BinaryReader& reader) : _reader(reader)
|
||||
/// Constructor
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~BSONReader()
|
||||
/// Destructor
|
||||
{
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void read(T& t)
|
||||
/// Reads the value from the reader. The default implementation uses the >> operator to
|
||||
/// the given argument. Special types can write their own version.
|
||||
{
|
||||
_reader >> t;
|
||||
}
|
||||
|
||||
std::string readCString();
|
||||
/// Reads a cstring from the reader.
|
||||
/// 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
|
|
@ -0,0 +1,90 @@
|
|||
//
|
||||
// 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/MongoDB/MongoDB.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API BSONWriter
|
||||
/// Class for writing BSON to a Poco::BinaryWriter.
|
||||
{
|
||||
public:
|
||||
BSONWriter(const Poco::BinaryWriter& writer) : _writer(writer)
|
||||
/// Constructor
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual ~BSONWriter()
|
||||
/// Destructor
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void write(T& t)
|
||||
/// Writes the value to the writer. The default implementation uses
|
||||
/// the << operator. Special types can write their own version.
|
||||
{
|
||||
_writer << t;
|
||||
}
|
||||
|
||||
|
||||
void writeCString(const std::string& value);
|
||||
/// Writes a cstring to the writer. A cstring is a string
|
||||
/// terminated with 0x00
|
||||
|
||||
private:
|
||||
|
||||
Poco::BinaryWriter _writer;
|
||||
};
|
||||
|
||||
inline void BSONWriter::writeCString(const std::string& value)
|
||||
{
|
||||
_writer.writeRaw(value);
|
||||
_writer << (unsigned char) 0x00;
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_BSONWriter_included
|
|
@ -0,0 +1,153 @@
|
|||
//
|
||||
// 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/MongoDB/Element.h"
|
||||
#include "Poco/Base64Encoder.h"
|
||||
#include "Poco/Buffer.h"
|
||||
#include "Poco/StreamCopier.h"
|
||||
#include "Poco/MemoryStream.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API Binary
|
||||
/// Implements BSON Binary. It's a wrapper around a Poco::Buffer<unsigned char>.
|
||||
{
|
||||
public:
|
||||
typedef SharedPtr<Binary> Ptr;
|
||||
|
||||
|
||||
Binary();
|
||||
/// Constructor
|
||||
|
||||
|
||||
Binary(Poco::Int32 size, unsigned char subtype);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Binary();
|
||||
/// Destructor
|
||||
|
||||
|
||||
Buffer<unsigned char>& buffer();
|
||||
/// Returns a reference to the buffer
|
||||
|
||||
|
||||
unsigned char subtype() const;
|
||||
/// Returns the subtype
|
||||
|
||||
|
||||
void subtype(unsigned char type);
|
||||
/// Sets the subtype
|
||||
|
||||
|
||||
std::string toString(int indent = 0) const;
|
||||
/// Returns the binary encoded in Base64
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Buffer<unsigned char> _buffer;
|
||||
|
||||
|
||||
unsigned char _subtype;
|
||||
};
|
||||
|
||||
|
||||
inline unsigned char Binary::subtype() const
|
||||
{
|
||||
return _subtype;
|
||||
}
|
||||
|
||||
|
||||
inline void Binary::subtype(unsigned char type)
|
||||
{
|
||||
_subtype = type;
|
||||
}
|
||||
|
||||
|
||||
inline Buffer<unsigned char>& Binary::buffer()
|
||||
{
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
|
||||
// BSON Embedded Document
|
||||
// spec: binary
|
||||
template<>
|
||||
struct ElementTraits<Binary::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x05 };
|
||||
|
||||
static std::string toString(const Binary::Ptr& value, int indent = 0)
|
||||
{
|
||||
return value.isNull() ? "" : value->toString();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<Binary::Ptr>(Binary::Ptr& to)
|
||||
{
|
||||
Poco::Int32 size;
|
||||
_reader >> size;
|
||||
|
||||
to->buffer().resize(size);
|
||||
|
||||
unsigned char subtype;
|
||||
_reader >> subtype;
|
||||
to->subtype(subtype);
|
||||
|
||||
_reader.readRaw((char*) to->buffer().begin(), size);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<Binary::Ptr>(Binary::Ptr& from)
|
||||
{
|
||||
_writer << from->subtype();
|
||||
_writer.writeRaw((char*) from->buffer().begin(), from->buffer().size());
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_Binary_included
|
|
@ -0,0 +1,132 @@
|
|||
//
|
||||
// Connection.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Connection
|
||||
//
|
||||
// Definition of the Connection class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_Connection_h
|
||||
#define _MongoDB_Connection_h
|
||||
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/Net/StreamSocket.h"
|
||||
#include "Poco/Mutex.h"
|
||||
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/ResponseMessage.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API Connection
|
||||
/// Represents a connection to a MongoDB server
|
||||
{
|
||||
public:
|
||||
|
||||
typedef Poco::SharedPtr<Connection> Ptr;
|
||||
|
||||
|
||||
Connection();
|
||||
/// Default constructor. Use this when you want to
|
||||
/// connect later on.
|
||||
|
||||
|
||||
Connection(const std::string& hostAndPort);
|
||||
/// Constructor which connects to the given MongoDB host/port.
|
||||
/// The host and port must be separated with a colon.
|
||||
|
||||
|
||||
Connection(const std::string& host, int port);
|
||||
/// Constructor which connects to the given MongoDB host/port.
|
||||
|
||||
|
||||
Connection(const Net::SocketAddress& addrs);
|
||||
/// Constructor which connects to the given MongoDB host/port.
|
||||
|
||||
|
||||
virtual ~Connection();
|
||||
/// Destructor
|
||||
|
||||
|
||||
Net::SocketAddress address() const;
|
||||
/// Returns the address of the MongoDB connection
|
||||
|
||||
|
||||
void connect(const std::string& hostAndPort);
|
||||
/// Connects to the given MongoDB server. The host and port must be separated
|
||||
/// with a colon.
|
||||
|
||||
|
||||
void connect(const std::string& host, int port);
|
||||
/// Connects to the given MongoDB server.
|
||||
|
||||
|
||||
void connect(const Net::SocketAddress& addrs);
|
||||
/// Connects to the given MongoDB server.
|
||||
|
||||
|
||||
void disconnect();
|
||||
/// Disconnects from the MongoDB server
|
||||
|
||||
|
||||
void sendRequest(RequestMessage& request);
|
||||
/// Sends a request to the MongoDB server
|
||||
/// Only use this when the request hasn't a response.
|
||||
|
||||
|
||||
void sendRequest(RequestMessage& request, ResponseMessage& response);
|
||||
/// Sends a request to the MongoDB server and receives the response.
|
||||
/// Use this when a response is expected: only a query or getmore
|
||||
/// request will return a response.
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Net::SocketAddress _address;
|
||||
|
||||
|
||||
Net::StreamSocket _socket;
|
||||
|
||||
|
||||
void connect();
|
||||
/// Connects to the MongoDB server
|
||||
};
|
||||
|
||||
inline Net::SocketAddress Connection::address() const
|
||||
{
|
||||
return _address;
|
||||
}
|
||||
|
||||
}} // Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_Connection_h
|
|
@ -0,0 +1,97 @@
|
|||
//
|
||||
// Cursor.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Cursor
|
||||
//
|
||||
// Definition of the Cursor 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_Cursor_included
|
||||
#define _MongoDB_Cursor_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
#include "Poco/MongoDB/QueryRequest.h"
|
||||
#include "Poco/MongoDB/ResponseMessage.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API Cursor : public Document
|
||||
/// Cursor is an helper class for querying multiple documents
|
||||
{
|
||||
public:
|
||||
Cursor(const std::string& dbname, const std::string& collectionName, QueryRequest::Flags flags = QueryRequest::QUERY_NONE);
|
||||
/// Constructor
|
||||
|
||||
|
||||
Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags = QueryRequest::QUERY_NONE);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Cursor();
|
||||
/// Destructor
|
||||
|
||||
|
||||
ResponseMessage& next(Connection& connection);
|
||||
/// Try to get the next documents. As long as ResponseMessage has a
|
||||
/// cursor id next can be called to retrieve the next bunch of documents.
|
||||
/// kill must be called when not all documents are needed.
|
||||
|
||||
|
||||
QueryRequest& query();
|
||||
/// Returns the associated query
|
||||
|
||||
|
||||
void kill(Connection& connection);
|
||||
/// Kills the cursor and reset it so that it can be reused.
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
QueryRequest _query;
|
||||
|
||||
|
||||
ResponseMessage _response;
|
||||
};
|
||||
|
||||
|
||||
inline QueryRequest& Cursor::query()
|
||||
{
|
||||
return _query;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_Cursor_included
|
|
@ -0,0 +1,89 @@
|
|||
//
|
||||
// Database.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Database
|
||||
//
|
||||
// Definition of the Database class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
#ifndef _MongoDB_Database_included
|
||||
#define _MongoDB_Database_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
#include "Poco/MongoDB/QueryRequest.h"
|
||||
#include "Poco/MongoDB/InsertRequest.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API Database
|
||||
/// Database is a helper class for creating requests. MongoDB works with
|
||||
/// collection names and uses the part before the first dot as the name of
|
||||
/// the database.
|
||||
{
|
||||
public:
|
||||
Database(const std::string& db);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Database();
|
||||
/// Destructor
|
||||
|
||||
|
||||
double count(Connection& connection, const std::string& collectionName) const;
|
||||
/// Sends a count request for the given collection to MongoDB. When
|
||||
/// the command fails, -1 is returned.
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> createQueryRequest(const std::string& collectionName) const;
|
||||
/// Creates a QueryRequest. The collectionname must not contain the database name!
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> createCountRequest(const std::string& collectionName) const;
|
||||
/// Creates a QueryRequest to count the given collection. The collectionname must not contain
|
||||
/// the database name!
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> createInsertRequest(const std::string& collectionName) const;
|
||||
/// Creates an InsertRequest to insert new documents in the given collection.
|
||||
/// The collectionname must not contain the database name!
|
||||
|
||||
|
||||
private:
|
||||
std::string _dbname;
|
||||
};
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_Database_included
|
|
@ -0,0 +1,134 @@
|
|||
//
|
||||
// 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.
|
||||
///
|
||||
/// Specific flags for this request
|
||||
/// - DELETE_NONE
|
||||
/// No flags
|
||||
/// - DELETE_SINGLE_REMOVE
|
||||
/// Delete only the first document
|
||||
{
|
||||
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".
|
||||
|
||||
|
||||
DeleteRequest(const std::string& collectionName, bool justOne);
|
||||
/// 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". When justOne is true, only the first matching document will
|
||||
/// be removed (the same as using flag DELETE_SINGLE_REMOVE).
|
||||
|
||||
|
||||
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
|
|
@ -0,0 +1,235 @@
|
|||
//
|
||||
// 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 <algorithm>
|
||||
|
||||
#include "Poco/BinaryReader.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Element.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
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
|
||||
/// Represents a BSON document
|
||||
{
|
||||
public:
|
||||
|
||||
typedef SharedPtr<Document> Ptr;
|
||||
|
||||
|
||||
typedef std::vector<Document::Ptr> Vector;
|
||||
|
||||
|
||||
Document();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Document();
|
||||
/// Destructor
|
||||
|
||||
|
||||
void addElement(Element::Ptr element);
|
||||
/// Add an element to the document
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add(const std::string& name, T value)
|
||||
/// Creates an element with the given name and value
|
||||
// adds it to the document.
|
||||
{
|
||||
addElement(new ConcreteElement<T>(name, value));
|
||||
}
|
||||
|
||||
|
||||
void clear();
|
||||
/// Removes all elements from the document.
|
||||
|
||||
|
||||
void elementNames(std::vector<std::string>& keys) const;
|
||||
/// Puts all element names into std::vector.
|
||||
|
||||
|
||||
bool empty() const;
|
||||
/// Returns true when the document doesn't contain any documents.
|
||||
|
||||
|
||||
bool exists(const std::string& name);
|
||||
/// Returns true when the document has an element with the given name
|
||||
|
||||
|
||||
template<typename T>
|
||||
T get(const std::string& name)
|
||||
/// Returns the element with the given name and tries to convert
|
||||
/// it to the template type. When the element is not found, a
|
||||
/// NotFoundException will be thrown. When the element can't be
|
||||
/// converted a BadCastException will be thrown.
|
||||
{
|
||||
Element::Ptr element = get(name);
|
||||
if ( element.isNull() )
|
||||
{
|
||||
throw NotFoundException(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ElementTraits<T>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
if ( concrete != NULL )
|
||||
{
|
||||
return concrete->value();
|
||||
}
|
||||
}
|
||||
throw BadCastException("Invalid type mismatch!");
|
||||
}
|
||||
}
|
||||
|
||||
Element::Ptr get(const std::string& name);
|
||||
/// Returns the element with the given name.
|
||||
/// An empty element will be returned when the element is not found.
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool isType(const std::string& name)
|
||||
/// Returns true when the type of the element equals the TypeId of ElementTrait
|
||||
{
|
||||
Element::Ptr element = get(name);
|
||||
if ( element.isNull() )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return ElementTraits<T>::TypeId == element->type();
|
||||
}
|
||||
|
||||
|
||||
void read(BinaryReader& reader);
|
||||
/// Reads a document from the reader
|
||||
|
||||
|
||||
virtual std::string toString(int indent = 0) const;
|
||||
/// Returns a String representation of the document.
|
||||
|
||||
|
||||
void write(BinaryWriter& writer);
|
||||
/// Writes a document to the reader
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
ElementSet _elements;
|
||||
};
|
||||
|
||||
|
||||
inline void Document::clear()
|
||||
{
|
||||
_elements.clear();
|
||||
}
|
||||
|
||||
|
||||
inline bool Document::empty() const
|
||||
{
|
||||
return _elements.empty();
|
||||
}
|
||||
|
||||
|
||||
inline void Document::elementNames(std::vector<std::string>& keys) const
|
||||
{
|
||||
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
keys.push_back((*it)->name());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline bool Document::exists(const std::string& name)
|
||||
{
|
||||
return std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name)) != _elements.end();
|
||||
}
|
||||
|
||||
|
||||
// BSON Embedded Document
|
||||
// spec: document
|
||||
template<>
|
||||
struct ElementTraits<Document::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x03 };
|
||||
|
||||
static std::string toString(const Document::Ptr& value, int indent = 0)
|
||||
{
|
||||
return value.isNull() ? "null" : value->toString(indent);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<Document::Ptr>(Document::Ptr& to)
|
||||
{
|
||||
to->read(_reader);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<Document::Ptr>(Document::Ptr& from)
|
||||
{
|
||||
from->write(_writer);
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_Document_included
|
|
@ -0,0 +1,320 @@
|
|||
//
|
||||
// Element.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Element
|
||||
//
|
||||
// Definition of the Element class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_Element_included
|
||||
#define _MongoDB_Element_included
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
#include "Poco/BinaryReader.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
#include "Poco/SharedPtr.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
#include "Poco/RegularExpression.h"
|
||||
#include "Poco/Nullable.h"
|
||||
#include "Poco/NumberFormatter.h"
|
||||
#include "Poco/DateTimeFormatter.h"
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/BSONReader.h"
|
||||
#include "Poco/MongoDB/BSONWriter.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API Element
|
||||
/// Represents an element of a Document or an Array
|
||||
{
|
||||
public:
|
||||
|
||||
Element(const std::string& name);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~Element();
|
||||
/// Destructor
|
||||
|
||||
|
||||
std::string name() const;
|
||||
/// Returns the name of the element
|
||||
|
||||
|
||||
virtual std::string toString(int indent = 0) const = 0;
|
||||
/// Returns a string representation of the element.
|
||||
|
||||
|
||||
virtual int type() const = 0;
|
||||
/// Returns the MongoDB type of the element.
|
||||
|
||||
|
||||
typedef Poco::SharedPtr<Element> Ptr;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
virtual void read(BinaryReader& reader) = 0;
|
||||
|
||||
|
||||
virtual void write(BinaryWriter& writer) = 0;
|
||||
|
||||
|
||||
friend class Document;
|
||||
|
||||
|
||||
std::string _name;
|
||||
};
|
||||
|
||||
|
||||
inline std::string Element::name() const
|
||||
{
|
||||
return _name;
|
||||
}
|
||||
|
||||
|
||||
class ElementComparator
|
||||
{
|
||||
public:
|
||||
bool operator()(const Element::Ptr& s1, const Element::Ptr& s2)
|
||||
{
|
||||
return s1->name() < s2->name();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
typedef std::set<Element::Ptr, ElementComparator> ElementSet;
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct ElementTraits
|
||||
{
|
||||
};
|
||||
|
||||
// BSON Floating point
|
||||
// spec: double
|
||||
template<>
|
||||
struct ElementTraits<double>
|
||||
{
|
||||
enum { TypeId = 0x01 };
|
||||
|
||||
static std::string toString(const double& value, int indent = 0)
|
||||
{
|
||||
return Poco::NumberFormatter::format(value);
|
||||
}
|
||||
};
|
||||
|
||||
// BSON UTF-8 string
|
||||
// spec: int32 (byte*) "\x00"
|
||||
// int32 is the number bytes in byte* + 1 (for trailing "\x00"
|
||||
template<>
|
||||
struct ElementTraits<std::string>
|
||||
{
|
||||
enum { TypeId = 0x02 };
|
||||
|
||||
static std::string toString(const std::string& value, int indent = 0)
|
||||
{
|
||||
return '"' + value + '"';
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<std::string>(std::string& to)
|
||||
{
|
||||
Poco::Int32 size;
|
||||
_reader >> size;
|
||||
_reader.readRaw(size, to);
|
||||
to.erase(to.end() - 1); // remove terminating 0
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<std::string>(std::string& from)
|
||||
{
|
||||
_writer << (Poco::Int32) (from.length() + 1);
|
||||
writeCString(from);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// BSON bool
|
||||
// spec: "\x00" "\x01"
|
||||
template<>
|
||||
struct ElementTraits<bool>
|
||||
{
|
||||
enum { TypeId = 0x08 };
|
||||
|
||||
static std::string toString(const bool& value, int indent = 0)
|
||||
{
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<bool>(bool& to)
|
||||
{
|
||||
unsigned char b;
|
||||
_reader >> b;
|
||||
to = b != 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<bool>(bool& from)
|
||||
{
|
||||
unsigned char b = from ? 0x01 : 0x00;
|
||||
_writer << b;
|
||||
}
|
||||
|
||||
// BSON 32-bit integer
|
||||
// spec: int32
|
||||
template<>
|
||||
struct ElementTraits<Int32>
|
||||
{
|
||||
enum { TypeId = 0x10 };
|
||||
|
||||
|
||||
static std::string toString(const Int32& value, int indent = 0)
|
||||
{
|
||||
return Poco::NumberFormatter::format(value);
|
||||
}
|
||||
};
|
||||
|
||||
// BSON UTC datetime
|
||||
// spec: int64
|
||||
template<>
|
||||
struct ElementTraits<Timestamp>
|
||||
{
|
||||
enum { TypeId = 0x09 };
|
||||
|
||||
static std::string toString(const Timestamp& value, int indent = 0)
|
||||
{
|
||||
return DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z");
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<Timestamp>(Timestamp& to)
|
||||
{
|
||||
Poco::Int64 value;
|
||||
_reader >> value;
|
||||
to = Timestamp::fromEpochTime(value / 1000);
|
||||
to += (value % 1000 * 1000);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<Timestamp>(Timestamp& from)
|
||||
{
|
||||
_writer << (from.epochMicroseconds() / 1000);
|
||||
}
|
||||
|
||||
typedef Nullable<unsigned char> NullValue;
|
||||
|
||||
// BSON Null Value
|
||||
// spec:
|
||||
template<>
|
||||
struct ElementTraits<NullValue>
|
||||
{
|
||||
enum { TypeId = 0x0A };
|
||||
|
||||
static std::string toString(const NullValue& value, int indent = 0)
|
||||
{
|
||||
return "null";
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<NullValue>(NullValue& to)
|
||||
{
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<NullValue>(NullValue& from)
|
||||
{
|
||||
}
|
||||
|
||||
// BSON 64-bit integer
|
||||
// spec: int64
|
||||
template<>
|
||||
struct ElementTraits<Int64>
|
||||
{
|
||||
enum { TypeId = 0x12 };
|
||||
|
||||
static std::string toString(const Int64& value, int indent = 0)
|
||||
{
|
||||
return NumberFormatter::format(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
class ConcreteElement : public Element
|
||||
{
|
||||
public:
|
||||
ConcreteElement(const std::string& name, const T& init) : Element(name), _value(init)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ConcreteElement()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
T value() const { return _value; }
|
||||
|
||||
|
||||
std::string toString(int indent = 0) const { return ElementTraits<T>::toString(_value, indent); }
|
||||
|
||||
|
||||
int type() const { return ElementTraits<T>::TypeId; }
|
||||
|
||||
|
||||
void read(BinaryReader& reader)
|
||||
{
|
||||
BSONReader(reader).read(_value);
|
||||
}
|
||||
|
||||
void write(BinaryWriter& writer)
|
||||
{
|
||||
BSONWriter(writer).write(_value);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
T _value;
|
||||
};
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_Element_included
|
|
@ -0,0 +1,111 @@
|
|||
//
|
||||
// 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.
|
||||
/// By default the numberToReturn is set to 100.
|
||||
|
||||
|
||||
virtual ~GetMoreRequest();
|
||||
/// Destructor
|
||||
|
||||
|
||||
Int32 getNumberToReturn() const;
|
||||
/// Returns the limit of returned documents
|
||||
|
||||
|
||||
void setNumberToReturn(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::getNumberToReturn() const
|
||||
{
|
||||
return _numberToReturn;
|
||||
}
|
||||
|
||||
inline void GetMoreRequest::setNumberToReturn(Int32 n)
|
||||
{
|
||||
_numberToReturn = n;
|
||||
}
|
||||
|
||||
inline Int64 GetMoreRequest::cursorID() const
|
||||
{
|
||||
return _cursorID;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_GetMoreRequest_included
|
|
@ -0,0 +1,98 @@
|
|||
//
|
||||
// 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
|
||||
|
||||
|
||||
Document::Vector& documents();
|
||||
/// Returns the documents to insert into the database
|
||||
|
||||
protected:
|
||||
|
||||
void buildRequest(BinaryWriter& writer);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Int32 _flags;
|
||||
|
||||
std::string _fullCollectionName;
|
||||
|
||||
Document::Vector _documents;
|
||||
};
|
||||
|
||||
|
||||
inline Document::Vector& InsertRequest::documents()
|
||||
{
|
||||
return _documents;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_InsertRequest_included
|
|
@ -0,0 +1,123 @@
|
|||
//
|
||||
// JavaScriptCode.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: JavaScriptCode
|
||||
//
|
||||
// Definition of the JavaScriptCode 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_JavaScriptCode_included
|
||||
#define _MongoDB_JavaScriptCode_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/BSONReader.h"
|
||||
#include "Poco/MongoDB/BSONWriter.h"
|
||||
#include "Poco/MongoDB/Element.h"
|
||||
#include "Poco/SharedPtr.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API JavaScriptCode
|
||||
/// Represents JavaScript type in BSON
|
||||
{
|
||||
public:
|
||||
typedef SharedPtr<JavaScriptCode> Ptr;
|
||||
|
||||
|
||||
JavaScriptCode();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~JavaScriptCode();
|
||||
/// Destructor
|
||||
|
||||
|
||||
void setCode(const std::string& s);
|
||||
/// Set the code
|
||||
|
||||
|
||||
std::string getCode() const;
|
||||
/// Get the code
|
||||
|
||||
private:
|
||||
|
||||
std::string _code;
|
||||
};
|
||||
|
||||
|
||||
inline void JavaScriptCode::setCode(const std::string& s)
|
||||
{
|
||||
_code = s;
|
||||
}
|
||||
|
||||
|
||||
inline std::string JavaScriptCode::getCode() const
|
||||
{
|
||||
return _code;
|
||||
}
|
||||
|
||||
// BSON JavaScript code
|
||||
// spec: string
|
||||
template<>
|
||||
struct ElementTraits<JavaScriptCode::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x0D };
|
||||
|
||||
static std::string toString(const JavaScriptCode::Ptr& value, int indent = 0)
|
||||
{
|
||||
return value.isNull() ? "" : value->getCode();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& to)
|
||||
{
|
||||
std::string code;
|
||||
BSONReader(_reader).read(code);
|
||||
to = new JavaScriptCode();
|
||||
to->setCode(code);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& from)
|
||||
{
|
||||
std::string code = from->getCode();
|
||||
BSONWriter(_writer).write(code);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_JavaScriptCode_included
|
|
@ -0,0 +1,81 @@
|
|||
//
|
||||
// KillCursorsRequest.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: KillCursorsRequest
|
||||
//
|
||||
// Definition of the KillCursorsRequest class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_KillCursorsRequest_included
|
||||
#define _MongoDB_KillCursorsRequest_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API KillCursorsRequest : public RequestMessage
|
||||
/// Class for creating an OP_KILL_CURSORS client request. This
|
||||
/// request is used to kill cursors, which are still open,
|
||||
/// returned by query requests.
|
||||
{
|
||||
public:
|
||||
KillCursorsRequest();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~KillCursorsRequest();
|
||||
/// Destructor
|
||||
|
||||
|
||||
std::vector<Int64>& cursors();
|
||||
/// Return the cursors
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
void buildRequest(BinaryWriter& writer);
|
||||
|
||||
std::vector<Int64> _cursors;
|
||||
};
|
||||
|
||||
inline std::vector<Int64>& KillCursorsRequest::cursors()
|
||||
{
|
||||
return _cursors;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_KillCursorsRequest_included
|
|
@ -0,0 +1,92 @@
|
|||
//
|
||||
// Message.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Message
|
||||
//
|
||||
// Definition of the Message class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_Message_included
|
||||
#define _MongoDB_Message_included
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "Poco/Net/Socket.h"
|
||||
#include "Poco/BinaryReader.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/MessageHeader.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API Message
|
||||
/// Base class for all messages send or retrieved from MongoDB server
|
||||
{
|
||||
public:
|
||||
Message(MessageHeader::OpCode opcode);
|
||||
/// Constructor
|
||||
|
||||
virtual ~Message();
|
||||
/// Destructor
|
||||
|
||||
|
||||
MessageHeader& header();
|
||||
/// Returns the message header
|
||||
|
||||
protected:
|
||||
|
||||
MessageHeader _header;
|
||||
|
||||
|
||||
void messageLength(Int32 length);
|
||||
/// Sets the message length in the message header
|
||||
};
|
||||
|
||||
|
||||
inline MessageHeader& Message::header()
|
||||
{
|
||||
return _header;
|
||||
}
|
||||
|
||||
|
||||
inline void Message::messageLength(Int32 length)
|
||||
{
|
||||
_header.setMessageLength(length);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_Message_included
|
|
@ -0,0 +1,157 @@
|
|||
//
|
||||
// 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 MongoDB_API 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
|
||||
|
||||
|
||||
Int32 getMessageLength() const;
|
||||
/// Returns the message length
|
||||
|
||||
|
||||
OpCode opCode() const;
|
||||
/// Returns the OpCode
|
||||
|
||||
|
||||
Int32 getRequestID() const;
|
||||
/// Returns the request id of the current message
|
||||
|
||||
|
||||
void setRequestID(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 setMessageLength(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::getMessageLength() const
|
||||
{
|
||||
return _messageLength;
|
||||
}
|
||||
|
||||
inline void MessageHeader::setMessageLength(Int32 length)
|
||||
{
|
||||
_messageLength = MSG_HEADER_SIZE + length;
|
||||
}
|
||||
|
||||
inline void MessageHeader::setRequestID(Int32 id)
|
||||
{
|
||||
_requestID = id;
|
||||
}
|
||||
|
||||
inline Int32 MessageHeader::getRequestID() const
|
||||
{
|
||||
return _requestID;
|
||||
}
|
||||
|
||||
inline Int32 MessageHeader::responseTo() const
|
||||
{
|
||||
return _responseTo;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_MessageHeader_included
|
|
@ -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
|
|
@ -0,0 +1,132 @@
|
|||
//
|
||||
// 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"
|
||||
#include "Poco/Timestamp.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API ObjectId
|
||||
/// ObjectId is a 12-byte BSON type, constructed using:
|
||||
///
|
||||
/// - a 4-byte timestamp,
|
||||
/// - a 3-byte machine identifier,
|
||||
/// - a 2-byte process id, and
|
||||
/// - a 3-byte counter, starting with a random value.
|
||||
///
|
||||
/// In MongoDB, documents stored in a collection require a unique _id field that acts
|
||||
/// as a primary key. Because ObjectIds are small, most likely unique, and fast to generate,
|
||||
/// MongoDB uses ObjectIds as the default value for the _id field if the _id field is not
|
||||
/// specified; i.e., the mongod adds the _id field and generates a unique ObjectId to assign
|
||||
/// as its value.
|
||||
{
|
||||
public:
|
||||
|
||||
typedef SharedPtr<ObjectId> Ptr;
|
||||
|
||||
|
||||
ObjectId();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~ObjectId();
|
||||
/// Destructor
|
||||
|
||||
|
||||
Timestamp timestamp() const;
|
||||
/// Returns the timestamp which is stored in the first four bytes of the id
|
||||
|
||||
|
||||
std::string toString() const;
|
||||
/// Returns the id in string format
|
||||
|
||||
|
||||
private:
|
||||
unsigned char _id[12];
|
||||
|
||||
friend class BSONWriter;
|
||||
friend class BSONReader;
|
||||
};
|
||||
|
||||
|
||||
inline Timestamp ObjectId::timestamp() const
|
||||
{
|
||||
int time;
|
||||
char* T = (char *) &time;
|
||||
T[0] = _id[3];
|
||||
T[1] = _id[2];
|
||||
T[2] = _id[1];
|
||||
T[3] = _id[0];
|
||||
return Timestamp::fromEpochTime((time_t) time);
|
||||
}
|
||||
|
||||
// BSON Embedded Document
|
||||
// spec: ObjectId
|
||||
template<>
|
||||
struct ElementTraits<ObjectId::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x07 };
|
||||
|
||||
|
||||
static std::string toString(const ObjectId::Ptr& id, int indent = 0)
|
||||
{
|
||||
return id->toString();
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<ObjectId::Ptr>(ObjectId::Ptr& to)
|
||||
{
|
||||
_reader.readRaw((char*) to->_id, 12);
|
||||
}
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<ObjectId::Ptr>(ObjectId::Ptr& from)
|
||||
{
|
||||
_writer.writeRaw((char*) from->_id, 12);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
|
||||
#endif //_MongoDB_ObjectId_included
|
|
@ -0,0 +1,92 @@
|
|||
#ifndef _MongoDB_PoolableConnectionFactory_included
|
||||
#define _MongoDB_PoolableConnectionFactory_included
|
||||
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
#include "Poco/ObjectPool.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
|
||||
|
||||
template<>
|
||||
class MongoDB_API PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
|
||||
/// PoolableObjectFactory specialisation for Connection. New connections
|
||||
/// are created with the given address.
|
||||
{
|
||||
public:
|
||||
PoolableObjectFactory(Net::SocketAddress& address)
|
||||
: _address(address)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
PoolableObjectFactory(const std::string& address)
|
||||
: _address(address)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
MongoDB::Connection::Ptr createObject()
|
||||
{
|
||||
return new MongoDB::Connection(_address);
|
||||
}
|
||||
|
||||
|
||||
bool validateObject(MongoDB::Connection::Ptr pObject)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void activateObject(MongoDB::Connection::Ptr pObject)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void deactivateObject(MongoDB::Connection::Ptr pObject)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void destroyObject(MongoDB::Connection::Ptr pObject)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Net::SocketAddress _address;
|
||||
};
|
||||
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
class MongoDB_API PooledConnection
|
||||
/// Helper class for borrowing and returning a connection automatically from a pool.
|
||||
{
|
||||
public:
|
||||
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool)
|
||||
{
|
||||
_connection = _pool.borrowObject();
|
||||
}
|
||||
|
||||
virtual ~PooledConnection()
|
||||
{
|
||||
_pool.returnObject(_connection);
|
||||
}
|
||||
|
||||
operator Connection::Ptr () { return _connection; }
|
||||
|
||||
private:
|
||||
Poco::ObjectPool<Connection, Connection::Ptr>& _pool;
|
||||
|
||||
Connection::Ptr _connection;
|
||||
};
|
||||
|
||||
} // MongoDB
|
||||
|
||||
|
||||
} // Poco
|
||||
|
||||
#endif //_MongoDB_PoolableConnectionFactory_included
|
|
@ -0,0 +1,193 @@
|
|||
//
|
||||
// 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/MongoDB/Document.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 getFlags() const;
|
||||
/// Returns the flags
|
||||
|
||||
|
||||
void setFlags(Flags flag);
|
||||
/// Set the flags
|
||||
|
||||
|
||||
std::string fullCollectionName() const;
|
||||
/// Returns the <db>.<collection> used for this query
|
||||
|
||||
|
||||
Int32 getNumberToSkip() const;
|
||||
/// Returns the number of documents to skip
|
||||
|
||||
|
||||
void setNumberToSkip(Int32 n);
|
||||
/// Sets the number of documents to skip
|
||||
|
||||
|
||||
Int32 getNumberToReturn() const;
|
||||
/// Returns the number to return
|
||||
|
||||
|
||||
void setNumberToReturn(Int32 n);
|
||||
/// Sets the number to return (limit)
|
||||
|
||||
|
||||
Document& query();
|
||||
/// Returns the query document
|
||||
|
||||
|
||||
Document& returnFieldSelector();
|
||||
/// Returns the selector document
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
void buildRequest(BinaryWriter& writer);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Flags _flags;
|
||||
|
||||
std::string _fullCollectionName;
|
||||
|
||||
Int32 _numberToSkip;
|
||||
|
||||
Int32 _numberToReturn;
|
||||
|
||||
Document _query;
|
||||
|
||||
Document _returnFieldSelector;
|
||||
};
|
||||
|
||||
|
||||
inline QueryRequest::Flags QueryRequest::getFlags() const
|
||||
{
|
||||
return _flags;
|
||||
}
|
||||
|
||||
|
||||
inline void QueryRequest::setFlags(QueryRequest::Flags flags)
|
||||
{
|
||||
_flags = flags;
|
||||
}
|
||||
|
||||
|
||||
inline std::string QueryRequest::fullCollectionName() const
|
||||
{
|
||||
return _fullCollectionName;
|
||||
}
|
||||
|
||||
|
||||
inline Document& QueryRequest::query()
|
||||
{
|
||||
return _query;
|
||||
}
|
||||
|
||||
|
||||
inline Document& QueryRequest::returnFieldSelector()
|
||||
{
|
||||
return _returnFieldSelector;
|
||||
}
|
||||
|
||||
|
||||
inline Int32 QueryRequest::getNumberToSkip() const
|
||||
{
|
||||
return _numberToSkip;
|
||||
}
|
||||
|
||||
|
||||
inline void QueryRequest::setNumberToSkip(Int32 n)
|
||||
{
|
||||
_numberToSkip = n;
|
||||
}
|
||||
|
||||
|
||||
inline Int32 QueryRequest::getNumberToReturn() const
|
||||
{
|
||||
return _numberToReturn;
|
||||
}
|
||||
|
||||
|
||||
inline void QueryRequest::setNumberToReturn(Int32 n)
|
||||
{
|
||||
_numberToReturn = n;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_QueryRequest_included
|
|
@ -0,0 +1,152 @@
|
|||
//
|
||||
// RegularExpression.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: RegularExpression
|
||||
//
|
||||
// Definition of the RegularExpression 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_RegularExpression_included
|
||||
#define _MongoDB_RegularExpression_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Element.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API RegularExpression
|
||||
/// Represents a regular expression in BSON format
|
||||
{
|
||||
public:
|
||||
|
||||
typedef SharedPtr<RegularExpression> Ptr;
|
||||
|
||||
|
||||
RegularExpression();
|
||||
/// Constructor
|
||||
|
||||
|
||||
RegularExpression(const std::string& pattern, const std::string& options);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~RegularExpression();
|
||||
/// Destructor
|
||||
|
||||
|
||||
SharedPtr<Poco::RegularExpression> createRE() const;
|
||||
/// Tries to create a Poco::RegularExpression
|
||||
|
||||
|
||||
std::string getOptions() const;
|
||||
/// Returns the options
|
||||
|
||||
|
||||
void setOptions(const std::string& options);
|
||||
/// Sets the options
|
||||
|
||||
|
||||
std::string getPattern() const;
|
||||
/// Returns the pattern
|
||||
|
||||
|
||||
void setPattern(const std::string& pattern);
|
||||
/// Sets the pattern
|
||||
|
||||
private:
|
||||
|
||||
std::string _pattern;
|
||||
std::string _options;
|
||||
};
|
||||
|
||||
|
||||
inline std::string RegularExpression::getPattern() const
|
||||
{
|
||||
return _pattern;
|
||||
}
|
||||
|
||||
|
||||
inline void RegularExpression::setPattern(const std::string& pattern)
|
||||
{
|
||||
_pattern = pattern;
|
||||
}
|
||||
|
||||
|
||||
inline std::string RegularExpression::getOptions() const
|
||||
{
|
||||
return _options;
|
||||
}
|
||||
|
||||
|
||||
inline void RegularExpression::setOptions(const std::string& options)
|
||||
{
|
||||
_options = options;
|
||||
}
|
||||
|
||||
// BSON Regex
|
||||
// spec: cstring cstring
|
||||
template<>
|
||||
struct ElementTraits<RegularExpression::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x0B };
|
||||
|
||||
static std::string toString(const RegularExpression::Ptr& value, int indent = 0)
|
||||
{
|
||||
//TODO
|
||||
return "RE: not implemented yet";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
inline void BSONReader::read<RegularExpression::Ptr>(RegularExpression::Ptr& to)
|
||||
{
|
||||
std::string pattern = readCString();
|
||||
std::string options = readCString();
|
||||
|
||||
to = new RegularExpression(pattern, options);
|
||||
}
|
||||
|
||||
|
||||
template<>
|
||||
inline void BSONWriter::write<RegularExpression::Ptr>(RegularExpression::Ptr& from)
|
||||
{
|
||||
writeCString(from->getPattern());
|
||||
writeCString(from->getOptions());
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif // _MongoDB_RegularExpression_included
|
|
@ -0,0 +1,73 @@
|
|||
//
|
||||
// ReplicaSet.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: ReplicaSet
|
||||
//
|
||||
// Definition of the ReplicaSet class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_ReplicaSet_h
|
||||
#define _MongoDB_ReplicaSet_h
|
||||
|
||||
#include <vector>
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API ReplicaSet
|
||||
/// Class for working with a replicaset
|
||||
{
|
||||
public:
|
||||
ReplicaSet(const std::vector<Net::SocketAddress>& addresses);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~ReplicaSet();
|
||||
/// Destructor
|
||||
|
||||
|
||||
Connection::Ptr findMaster();
|
||||
/// Tries to find the master MongoDB instance from the addresses
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Connection::Ptr isMaster(const Net::SocketAddress& host);
|
||||
|
||||
|
||||
std::vector<Net::SocketAddress> _addresses;
|
||||
};
|
||||
|
||||
#endif // _MongoDB_ReplicaSet_h
|
||||
}} // Poco::MongoDB
|
|
@ -0,0 +1,74 @@
|
|||
//
|
||||
// RequestMessage.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: RequestMessage
|
||||
//
|
||||
// Definition of the RequestMessage class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_RequestMessage_included
|
||||
#define _MongoDB_RequestMessage_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Message.h"
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API RequestMessage : public Message
|
||||
/// Base class for a request
|
||||
{
|
||||
public:
|
||||
RequestMessage(MessageHeader::OpCode opcode);
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~RequestMessage();
|
||||
/// Destructor
|
||||
|
||||
|
||||
void send(std::ostream& ostr);
|
||||
/// Sends the request to stream
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
virtual void buildRequest(BinaryWriter& ss) = 0;
|
||||
};
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_RequestMessage_included
|
|
@ -0,0 +1,110 @@
|
|||
//
|
||||
// ResponseMessage.h
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: ResponseMessage
|
||||
//
|
||||
// Definition of the ResponseMessage class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#ifndef _MongoDB_ResponseMessage_included
|
||||
#define _MongoDB_ResponseMessage_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Message.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API ResponseMessage : public Message
|
||||
/// Class that represents a response (OP_REPLY) from MongoDB
|
||||
{
|
||||
public:
|
||||
ResponseMessage();
|
||||
/// Constructor
|
||||
|
||||
|
||||
virtual ~ResponseMessage();
|
||||
/// Destructor
|
||||
|
||||
|
||||
void read(std::istream& istr);
|
||||
/// Reads the response from the stream
|
||||
|
||||
|
||||
Document::Vector& 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;
|
||||
|
||||
|
||||
Document::Vector _documents;
|
||||
};
|
||||
|
||||
|
||||
inline Document::Vector& ResponseMessage::documents()
|
||||
{
|
||||
return _documents;
|
||||
}
|
||||
|
||||
|
||||
inline Int64 ResponseMessage::cursorID() const
|
||||
{
|
||||
return _cursorID;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_ResponseMessage_included
|
|
@ -0,0 +1,136 @@
|
|||
//
|
||||
// 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/MongoDB/Document.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
|
||||
|
||||
|
||||
Document& selector();
|
||||
/// Returns the selector document
|
||||
|
||||
|
||||
Document& 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;
|
||||
|
||||
Document _selector;
|
||||
|
||||
Document _update;
|
||||
};
|
||||
|
||||
|
||||
inline UpdateRequest::Flags UpdateRequest::flags() const
|
||||
{
|
||||
return _flags;
|
||||
}
|
||||
|
||||
inline void UpdateRequest::flags(UpdateRequest::Flags flags)
|
||||
{
|
||||
_flags = flags;
|
||||
}
|
||||
|
||||
inline Document& UpdateRequest::selector()
|
||||
{
|
||||
return _selector;
|
||||
}
|
||||
|
||||
inline Document& UpdateRequest::update()
|
||||
{
|
||||
return _update;
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
||||
|
||||
#endif //_MongoDB_UpdateRequest_included
|
|
@ -0,0 +1,93 @@
|
|||
//
|
||||
// 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 <sstream>
|
||||
|
||||
#include "Poco/MongoDB/Array.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
Array::Array() : Document()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Array::~Array()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
std::string Array::toString(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "[";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
|
||||
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
if ( it != _elements.begin() )
|
||||
{
|
||||
if ( indent > 0 )
|
||||
{
|
||||
for(int i = 0; i < indent; ++i)
|
||||
{
|
||||
oss << ' ';
|
||||
}
|
||||
}
|
||||
oss << ",";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
}
|
||||
oss << (*it)->toString();
|
||||
}
|
||||
oss << "]";
|
||||
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::Mongo
|
|
@ -0,0 +1,69 @@
|
|||
//
|
||||
// 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()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
std::string Binary::toString(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
Base64Encoder encoder(oss);
|
||||
MemoryInputStream mis((const char*) _buffer.begin(), _buffer.size());
|
||||
StreamCopier::copyStream(mis, encoder);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
|
@ -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 <iostream>
|
||||
|
||||
#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
|
|
@ -0,0 +1,95 @@
|
|||
//
|
||||
// Cursor.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Cursor
|
||||
//
|
||||
// Implementation of the Cursor 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/Cursor.h"
|
||||
#include "Poco/MongoDB/GetMoreRequest.h"
|
||||
#include "Poco/MongoDB/KillCursorsRequest.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
Cursor::Cursor(const std::string& db, const std::string& collection, QueryRequest::Flags flags)
|
||||
: _query(db + '.' + collection, flags)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Cursor::Cursor(const std::string& fullCollectionName, QueryRequest::Flags flags)
|
||||
: _query(fullCollectionName, flags)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Cursor::~Cursor()
|
||||
{
|
||||
poco_assert_dbg(_response.cursorID());
|
||||
}
|
||||
|
||||
|
||||
ResponseMessage& Cursor::next(Connection& connection)
|
||||
{
|
||||
if ( _response.cursorID() == 0 )
|
||||
{
|
||||
connection.sendRequest(_query, _response);
|
||||
}
|
||||
else
|
||||
{
|
||||
Poco::MongoDB::GetMoreRequest getMore(_query.fullCollectionName(), _response.cursorID());
|
||||
getMore.setNumberToReturn(_query.getNumberToReturn());
|
||||
_response.clear();
|
||||
connection.sendRequest(getMore, _response);
|
||||
}
|
||||
return _response;
|
||||
}
|
||||
|
||||
|
||||
void Cursor::kill(Connection& connection)
|
||||
{
|
||||
if ( _response.cursorID() != 0 )
|
||||
{
|
||||
KillCursorsRequest killRequest;
|
||||
killRequest.cursors().push_back(_response.cursorID());
|
||||
connection.sendRequest(killRequest);
|
||||
}
|
||||
_response.clear();
|
||||
}
|
||||
|
||||
} } // Namespace Poco::MongoDB
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
//
|
||||
// Database.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: Database
|
||||
//
|
||||
// Implementation of the Database class.
|
||||
//
|
||||
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
#include "Poco/MongoDB/Database.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
Database::Database( const std::string& db) : _dbname(db)
|
||||
{
|
||||
}
|
||||
|
||||
Database::~Database()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
double Database::count(Connection& connection, const std::string& collectionName) const
|
||||
{
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> countRequest = createCountRequest(collectionName);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
connection.sendRequest(*countRequest, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
return doc->get<double>("n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::QueryRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCountRequest(const std::string& collectionName) const
|
||||
{
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd");
|
||||
request->setNumberToReturn(1);
|
||||
request->query().add("count", collectionName);
|
||||
return request;
|
||||
}
|
||||
|
||||
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> Database::createInsertRequest(const std::string& collectionName) const
|
||||
{
|
||||
return new Poco::MongoDB::InsertRequest(_dbname + '.' + collectionName);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
|
@ -0,0 +1,77 @@
|
|||
//
|
||||
// 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"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::Flags flags)
|
||||
: RequestMessage(MessageHeader::Delete),
|
||||
_flags(flags),
|
||||
_fullCollectionName(collectionName),
|
||||
_selector()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
DeleteRequest::DeleteRequest(const std::string& collectionName, bool justOne)
|
||||
: RequestMessage(MessageHeader::Delete),
|
||||
_flags(justOne ? DELETE_SINGLE_REMOVE : DELETE_NONE),
|
||||
_fullCollectionName(collectionName),
|
||||
_selector()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
DeleteRequest::~DeleteRequest()
|
||||
{
|
||||
}
|
||||
|
||||
void DeleteRequest::buildRequest(BinaryWriter& writer)
|
||||
{
|
||||
writer << 0; // 0 - reserved for future use
|
||||
BSONWriter(writer).writeCString(_fullCollectionName);
|
||||
writer << _flags;
|
||||
_selector.write(writer);
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -0,0 +1,198 @@
|
|||
//
|
||||
// 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 <sstream>
|
||||
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
#include "Poco/MongoDB/Binary.h"
|
||||
#include "Poco/MongoDB/ObjectId.h"
|
||||
#include "Poco/MongoDB/Array.h"
|
||||
#include "Poco/MongoDB/RegularExpression.h"
|
||||
#include "Poco/MongoDB/JavaScriptCode.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
Document::Document()
|
||||
{
|
||||
}
|
||||
|
||||
Document::~Document()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Element::Ptr Document::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;
|
||||
}
|
||||
|
||||
|
||||
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<double>::TypeId:
|
||||
element = new ConcreteElement<double>(name, 0);
|
||||
break;
|
||||
case ElementTraits<Int32>::TypeId:
|
||||
element = new ConcreteElement<Int32>(name, 0);
|
||||
break;
|
||||
case ElementTraits<std::string>::TypeId:
|
||||
element = new ConcreteElement<std::string>(name, "");
|
||||
break;
|
||||
case ElementTraits<Document::Ptr>::TypeId:
|
||||
element = new ConcreteElement<Document::Ptr>(name, new Document());
|
||||
break;
|
||||
case ElementTraits<Array::Ptr>::TypeId:
|
||||
element = new ConcreteElement<Array::Ptr>(name, new Array());
|
||||
break;
|
||||
case ElementTraits<Binary::Ptr>::TypeId:
|
||||
element = new ConcreteElement<Binary::Ptr>(name, new Binary());
|
||||
break;
|
||||
case ElementTraits<ObjectId::Ptr>::TypeId:
|
||||
element = new ConcreteElement<ObjectId::Ptr>(name, new ObjectId());
|
||||
break;
|
||||
case ElementTraits<bool>::TypeId:
|
||||
element = new ConcreteElement<bool>(name, false);
|
||||
break;
|
||||
case ElementTraits<Poco::Timestamp>::TypeId:
|
||||
element = new ConcreteElement<Poco::Timestamp>(name, Poco::Timestamp());
|
||||
break;
|
||||
case ElementTraits<NullValue>::TypeId:
|
||||
element = new ConcreteElement<NullValue>(name, NullValue(0));
|
||||
break;
|
||||
case ElementTraits<RegularExpression::Ptr>::TypeId:
|
||||
element = new ConcreteElement<RegularExpression::Ptr>(name, new RegularExpression());
|
||||
break;
|
||||
case ElementTraits<JavaScriptCode::Ptr>::TypeId:
|
||||
element = new ConcreteElement<JavaScriptCode::Ptr>(name, new JavaScriptCode());
|
||||
break;
|
||||
case ElementTraits<Int64>::TypeId:
|
||||
element = new ConcreteElement<Int64>(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(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "{" << std::endl;
|
||||
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
if ( it != _elements.begin() )
|
||||
{
|
||||
oss << ", ";
|
||||
}
|
||||
oss << '"' << (*it)->name() << '"' << " : " << (*it)->toString();
|
||||
}
|
||||
oss << "}" << std::endl;
|
||||
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
|
|
@ -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
|
|
@ -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(100),
|
||||
_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
|
|
@ -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/InsertRequest.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
InsertRequest::InsertRequest(const std::string& collectionName, Flags flags)
|
||||
: RequestMessage(MessageHeader::Insert),
|
||||
_flags(flags),
|
||||
_fullCollectionName(collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
InsertRequest::~InsertRequest()
|
||||
{
|
||||
}
|
||||
|
||||
void InsertRequest::buildRequest(BinaryWriter& writer)
|
||||
{
|
||||
//TODO: throw exception when no document is added
|
||||
|
||||
writer << _flags;
|
||||
BSONWriter bsonWriter(writer);
|
||||
bsonWriter.writeCString(_fullCollectionName);
|
||||
for(Document::Vector::iterator it = _documents.begin(); it != _documents.end(); ++it)
|
||||
{
|
||||
bsonWriter.write(*it);
|
||||
}
|
||||
}
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -0,0 +1,52 @@
|
|||
//
|
||||
// JavaScriptCode.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: JavaScriptCode
|
||||
//
|
||||
// Implementation of the JavaScriptCode 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/JavaScriptCode.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
JavaScriptCode::JavaScriptCode()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
JavaScriptCode::~JavaScriptCode()
|
||||
{
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
|
@ -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<Int64>::iterator it = _cursors.begin(); it != _cursors.end(); ++it)
|
||||
{
|
||||
writer << *it;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// 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
|
||||
{
|
||||
std::string s;
|
||||
|
||||
for(int i = 0; i < 12; ++i)
|
||||
{
|
||||
s += format("%x", (unsigned int) _id[i]);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
|
@ -0,0 +1,77 @@
|
|||
//
|
||||
// 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"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flags flags)
|
||||
: RequestMessage(MessageHeader::Query),
|
||||
_flags(flags),
|
||||
_fullCollectionName(collectionName),
|
||||
_numberToSkip(0),
|
||||
_numberToReturn(100),
|
||||
_query(),
|
||||
_returnFieldSelector()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QueryRequest::~QueryRequest()
|
||||
{
|
||||
}
|
||||
|
||||
void QueryRequest::buildRequest(BinaryWriter& writer)
|
||||
{
|
||||
writer << _flags;
|
||||
BSONWriter(writer).writeCString(_fullCollectionName);
|
||||
writer << _numberToSkip;
|
||||
writer << _numberToReturn;
|
||||
_query.write(writer);
|
||||
|
||||
if ( ! _returnFieldSelector.empty() )
|
||||
{
|
||||
_returnFieldSelector.write(writer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -0,0 +1,91 @@
|
|||
//
|
||||
// RegularExpression.cpp
|
||||
//
|
||||
// $Id$
|
||||
//
|
||||
// Library: MongoDB
|
||||
// Package: MongoDB
|
||||
// Module: RegularExpression
|
||||
//
|
||||
// Implementation of the RegularExpression 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 <sstream>
|
||||
|
||||
#include "Poco/MongoDB/RegularExpression.h"
|
||||
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
RegularExpression::RegularExpression()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
RegularExpression::RegularExpression(const std::string& pattern, const std::string& options) : _pattern(pattern), _options(options)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
RegularExpression::~RegularExpression()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
SharedPtr<Poco::RegularExpression> RegularExpression::createRE() const
|
||||
{
|
||||
int options = 0;
|
||||
for(std::string::const_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);
|
||||
}
|
||||
|
||||
}} // Namespace Poco::MongoDB
|
|
@ -0,0 +1,108 @@
|
|||
//
|
||||
// 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<Net::SocketAddress> &addresses) : _addresses(addresses)
|
||||
{
|
||||
}
|
||||
|
||||
ReplicaSet::~ReplicaSet()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Connection::Ptr ReplicaSet::findMaster()
|
||||
{
|
||||
Connection::Ptr master;
|
||||
|
||||
for(std::vector<Net::SocketAddress>::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.setNumberToReturn(1);
|
||||
request.query().add("isMaster", 1);
|
||||
|
||||
ResponseMessage response;
|
||||
conn->sendRequest(request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Document::Ptr doc = response.documents()[0];
|
||||
if ( doc->get<bool>("ismaster") )
|
||||
{
|
||||
return conn;
|
||||
}
|
||||
else if ( doc->exists("primary") )
|
||||
{
|
||||
return isMaster(Net::SocketAddress(doc->get<std::string>("primary")));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
conn = NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
}} // Poco::MongoDB
|
|
@ -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
|
|
@ -0,0 +1,86 @@
|
|||
//
|
||||
// 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/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()
|
||||
{
|
||||
_responseFlags = 0;
|
||||
_startingFrom = 0;
|
||||
_cursorID = 0;
|
||||
_numberReturned = 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)
|
||||
{
|
||||
Document::Ptr doc = new Document();
|
||||
doc->read(reader);
|
||||
_documents.push_back(doc);
|
||||
}
|
||||
}
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -0,0 +1,68 @@
|
|||
//
|
||||
// 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"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
UpdateRequest::UpdateRequest(const std::string& collectionName, UpdateRequest::Flags flags)
|
||||
: RequestMessage(MessageHeader::Update),
|
||||
_flags(flags),
|
||||
_fullCollectionName(collectionName),
|
||||
_selector(),
|
||||
_update()
|
||||
{
|
||||
}
|
||||
|
||||
UpdateRequest::~UpdateRequest()
|
||||
{
|
||||
}
|
||||
|
||||
void UpdateRequest::buildRequest(BinaryWriter& writer)
|
||||
{
|
||||
writer << 0; // 0 - reserved for future use
|
||||
BSONWriter(writer).writeCString(_fullCollectionName);
|
||||
writer << _flags;
|
||||
_selector.write(writer);
|
||||
_update.write(writer);
|
||||
}
|
||||
|
||||
}} // Namespace MongoDB
|
|
@ -0,0 +1,20 @@
|
|||
set( TEST_SRCS
|
||||
src/Driver.cpp
|
||||
src/MongoDBTest.cpp
|
||||
src/MongoDBTest.h
|
||||
src/MongoDBTestSuite.cpp
|
||||
src/MongoDBTestSuite.h
|
||||
)
|
||||
|
||||
#include_directories()
|
||||
|
||||
set( WIN_TEST_SRCS
|
||||
src/WinDriver.cpp
|
||||
)
|
||||
|
||||
set(TESTUNIT "${LIBNAME}-testrunner")
|
||||
|
||||
add_executable( ${TESTUNIT} ${TEST_SRCS} )
|
||||
#set_target_properties( ${TESTUNIT} PROPERTIES COMPILE_FLAGS ${RELEASE_CXX_FLAGS} )
|
||||
target_link_libraries( ${TESTUNIT} PocoMongoDB PocoFoundation CppUnit )
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
#
|
||||
# Makefile
|
||||
#
|
||||
# $Id$
|
||||
#
|
||||
# Makefile for Poco MongoDB testsuite
|
||||
#
|
||||
|
||||
include $(POCO_BASE)/build/rules/global
|
||||
|
||||
objects = Driver MongoDBTest MongoDBTestSuite
|
||||
|
||||
target = testrunner
|
||||
target_version = 1
|
||||
target_libs = PocoMongoDB PocoFoundation PocoNet CppUnit
|
||||
|
||||
include $(POCO_BASE)/build/rules/exec
|
|
@ -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)
|
|
@ -0,0 +1,422 @@
|
|||
//
|
||||
// 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 <iostream>
|
||||
|
||||
#include "Poco/DateTime.h"
|
||||
#include "Poco/ObjectPool.h"
|
||||
|
||||
#include "Poco/MongoDB/InsertRequest.h"
|
||||
#include "Poco/MongoDB/QueryRequest.h"
|
||||
#include "Poco/MongoDB/DeleteRequest.h"
|
||||
#include "Poco/MongoDB/GetMoreRequest.h"
|
||||
#include "Poco/MongoDB/PoolableConnectionFactory.h"
|
||||
#include "Poco/MongoDB/Database.h"
|
||||
#include "Poco/MongoDB/Cursor.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.setNumberToReturn(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<std::string>("lastname");
|
||||
assert(lastname.compare("Braem") == 0);
|
||||
std::string firstname = doc->get<std::string>("firstname");
|
||||
assert(firstname.compare("Franky") == 0);
|
||||
Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
|
||||
Poco::DateTime birthDate(birthDateTimestamp);
|
||||
assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
|
||||
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
|
||||
assert(doc->isType<NullValue>("unknown"));
|
||||
bool active = doc->get<bool>("active");
|
||||
assert(!active);
|
||||
|
||||
std::string id = doc->get("_id")->toString();
|
||||
std::cout << id << std::endl;
|
||||
}
|
||||
catch(Poco::NotFoundException& nfe)
|
||||
{
|
||||
fail(nfe.message() + " not found.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("No document returned");
|
||||
}
|
||||
}
|
||||
|
||||
void MongoDBTest::testDBQueryRequest()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createQueryRequest("players");
|
||||
request->query().add("lastname" , std::string("Braem"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
_mongo.sendRequest(*request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
|
||||
try
|
||||
{
|
||||
std::string lastname = doc->get<std::string>("lastname");
|
||||
assert(lastname.compare("Braem") == 0);
|
||||
std::string firstname = doc->get<std::string>("firstname");
|
||||
assert(firstname.compare("Franky") == 0);
|
||||
Poco::Timestamp birthDateTimestamp = doc->get<Poco::Timestamp>("birthdate");
|
||||
Poco::DateTime birthDate(birthDateTimestamp);
|
||||
assert(birthDate.year() == 1969 && birthDate.month() == 3 && birthDate.day() == 9);
|
||||
Poco::Timestamp lastupdatedTimestamp = doc->get<Poco::Timestamp>("lastupdated");
|
||||
assert(doc->isType<NullValue>("unknown"));
|
||||
|
||||
std::string id = doc->get("_id")->toString();
|
||||
std::cout << id << std::endl;
|
||||
}
|
||||
catch(Poco::NotFoundException& nfe)
|
||||
{
|
||||
fail(nfe.message() + " not found.");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("No document returned");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testCountCommand()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.setNumberToReturn(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<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Didn't get a response from the count command");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testDBCountCommand()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = db.createCountRequest("players");
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
_mongo.sendRequest(*request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Didn't get a response from the count command");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testDBCount2Command()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
double count = db.count(_mongo, "players");
|
||||
assert(count == 1);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testCursorRequest()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::Database db("team");
|
||||
Poco::SharedPtr<Poco::MongoDB::InsertRequest> insertRequest = db.createInsertRequest("numbers");
|
||||
for(int i = 0; i < 10000; ++i)
|
||||
{
|
||||
Document::Ptr doc = new Document();
|
||||
doc->add("number", i);
|
||||
insertRequest->documents().push_back(doc);
|
||||
}
|
||||
_mongo.sendRequest(*insertRequest);
|
||||
|
||||
double count = db.count(_mongo, "numbers");
|
||||
assert(count == 10000);
|
||||
|
||||
Poco::MongoDB::Cursor cursor("team", "numbers");
|
||||
|
||||
int n = 0;
|
||||
Poco::MongoDB::ResponseMessage& response = cursor.next(_mongo);
|
||||
while(response.cursorID() != 0)
|
||||
{
|
||||
n += response.documents().size();
|
||||
response = cursor.next(_mongo);
|
||||
}
|
||||
std::cout << "n= " << n << std::endl;
|
||||
assert(n == 10000);
|
||||
|
||||
Poco::MongoDB::QueryRequest drop("team.$cmd");
|
||||
drop.setNumberToReturn(1);
|
||||
drop.query().add("drop", std::string("numbers"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage responseDrop;
|
||||
_mongo.sendRequest(drop, responseDrop);
|
||||
|
||||
if ( responseDrop.documents().size() > 0 )
|
||||
{
|
||||
std::cout << responseDrop.documents()[0]->toString(2) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testBuildInfo()
|
||||
{
|
||||
if ( ! _connected )
|
||||
{
|
||||
std::cout << "test skipped." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("buildInfo", 1);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
|
||||
try
|
||||
{
|
||||
_mongo.sendRequest(request, response);
|
||||
}
|
||||
catch(Poco::NotImplementedException& nie)
|
||||
{
|
||||
std::cout << nie.message() << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
std::cout << doc->toString();
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Didn't get a response from the buildinfo command");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void MongoDBTest::testConnectionPool()
|
||||
{
|
||||
Poco::PoolableObjectFactory<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> factory("localhost:27017");
|
||||
Poco::ObjectPool<Poco::MongoDB::Connection, Poco::MongoDB::Connection::Ptr> pool(factory, 10, 15);
|
||||
|
||||
Poco::MongoDB::PooledConnection pooledConnection(pool);
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("count", std::string("players"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
((Connection::Ptr) pooledConnection)->sendRequest(request, response);
|
||||
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
fail("Didn't get a response from the count command");
|
||||
}
|
||||
}
|
||||
|
||||
CppUnit::Test* MongoDBTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest");
|
||||
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testInsertRequest);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testQueryRequest);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testDBQueryRequest);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testCountCommand);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testDBCountCommand);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testDBCount2Command);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest);
|
||||
|
||||
return pSuite;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
//
|
||||
// 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 testDBQueryRequest();
|
||||
|
||||
|
||||
void testCountCommand();
|
||||
|
||||
|
||||
void testDBCountCommand();
|
||||
|
||||
|
||||
void testDBCount2Command();
|
||||
|
||||
|
||||
void testDeleteRequest();
|
||||
|
||||
|
||||
void testBuildInfo();
|
||||
|
||||
|
||||
void testConnectionPool();
|
||||
|
||||
|
||||
void testCursorRequest();
|
||||
|
||||
|
||||
void setUp();
|
||||
|
||||
|
||||
void tearDown();
|
||||
|
||||
|
||||
static CppUnit::Test* suite();
|
||||
|
||||
private:
|
||||
|
||||
bool _connected;
|
||||
|
||||
Poco::MongoDB::Connection _mongo;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // MongoDBTest_INCLUDED
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -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 <cstdlib>
|
||||
|
||||
|
||||
int _tmain(int argc, wchar_t* argv[])
|
||||
{
|
||||
std::vector<std::string> 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;
|
||||
}
|
|
@ -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;
|
|
@ -18,3 +18,4 @@ PDF
|
|||
CppParser
|
||||
PocoDoc
|
||||
ProGen
|
||||
MongoDB
|
||||
|
|
Загрузка…
Ссылка в новой задаче