Cleaning up code, add docs, add getMore sample ...

This commit is contained in:
fbraem
2013-02-15 22:32:24 +01:00
parent 58958d2655
commit bf315df7a6
34 changed files with 801 additions and 402 deletions

View File

@@ -59,7 +59,7 @@ public:
/// Destructor /// Destructor
std::string toString() const; std::string toString(int indent = 0) const;
}; };
// BSON Embedded Array // BSON Embedded Array
@@ -69,7 +69,7 @@ struct ElementTraits<Array::Ptr>
{ {
enum { TypeId = 0x04 }; enum { TypeId = 0x04 };
static std::string toString(const Array::Ptr& value) static std::string toString(const Array::Ptr& value, int indent = 0)
{ {
//TODO: //TODO:
return value.isNull() ? "null" : value->toString(); return value.isNull() ? "null" : value->toString();

View File

@@ -39,11 +39,10 @@
#define _MongoDB_BSONReader_included #define _MongoDB_BSONReader_included
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/BinaryReader.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API BSONReader class MongoDB_API BSONReader
/// Class for reading BSON from a Poco::BinaryReader /// Class for reading BSON from a Poco::BinaryReader

View File

@@ -41,10 +41,8 @@
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/BinaryWriter.h" #include "Poco/BinaryWriter.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API BSONWriter class MongoDB_API BSONWriter
@@ -56,11 +54,13 @@ public:
{ {
} }
virtual ~BSONWriter() virtual ~BSONWriter()
/// Destructor /// Destructor
{ {
} }
template<typename T> template<typename T>
void write(T& t) void write(T& t)
/// Writes the value to the writer. The default implementation uses /// Writes the value to the writer. The default implementation uses
@@ -69,6 +69,7 @@ public:
_writer << t; _writer << t;
} }
void writeCString(const std::string& value); void writeCString(const std::string& value);
/// Writes a cstring to the writer. A cstring is a string /// Writes a cstring to the writer. A cstring is a string
/// terminated with 0x00 /// terminated with 0x00

View File

@@ -81,7 +81,7 @@ public:
/// Sets the subtype /// Sets the subtype
std::string toString() const; std::string toString(int indent = 0) const;
/// Returns the binary encoded in Base64 /// Returns the binary encoded in Base64
@@ -119,7 +119,7 @@ struct ElementTraits<Binary::Ptr>
{ {
enum { TypeId = 0x05 }; enum { TypeId = 0x05 };
static std::string toString(const Binary::Ptr& value) static std::string toString(const Binary::Ptr& value, int indent = 0)
{ {
return value.isNull() ? "" : value->toString(); return value.isNull() ? "" : value->toString();
} }

View File

@@ -45,10 +45,8 @@
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/ResponseMessage.h" #include "Poco/MongoDB/ResponseMessage.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API Connection class MongoDB_API Connection
/// Represents a connection to a MongoDB server /// Represents a connection to a MongoDB server

View File

@@ -42,15 +42,19 @@
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/Document.h" #include "Poco/MongoDB/Document.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API DeleteRequest : public RequestMessage class MongoDB_API DeleteRequest : public RequestMessage
/// Class for creating an OP_DELETE client request. This request /// Class for creating an OP_DELETE client request. This request
/// is used to delete one ore more documents from a database. /// 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: public:
@@ -67,6 +71,14 @@ public:
/// "foo.bar". /// "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(); virtual ~DeleteRequest();
/// Destructor /// Destructor
@@ -84,6 +96,7 @@ public:
protected: protected:
void buildRequest(BinaryWriter& writer); void buildRequest(BinaryWriter& writer);
/// Writes the OP_DELETE request to the writer /// Writes the OP_DELETE request to the writer
@@ -91,21 +104,26 @@ private:
Flags _flags; Flags _flags;
std::string _fullCollectionName; std::string _fullCollectionName;
Document _selector; Document _selector;
}; };
inline DeleteRequest::Flags DeleteRequest::flags() const inline DeleteRequest::Flags DeleteRequest::flags() const
{ {
return _flags; return _flags;
} }
inline void DeleteRequest::flags(DeleteRequest::Flags flags) inline void DeleteRequest::flags(DeleteRequest::Flags flags)
{ {
_flags = flags; _flags = flags;
} }
inline Document& DeleteRequest::selector() inline Document& DeleteRequest::selector()
{ {
return _selector; return _selector;

View File

@@ -68,6 +68,7 @@ private:
}; };
class MongoDB_API Document class MongoDB_API Document
/// Represents a BSON document
{ {
public: public:
@@ -78,64 +79,76 @@ public:
Document(); Document();
/// Constructor
virtual ~Document(); virtual ~Document();
/// Destructor
void read(BinaryReader& reader); void addElement(Element::Ptr element);
/// Add an element to the document
void write(BinaryWriter& writer); 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> template<typename T>
T get(const std::string& name) 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); Element::Ptr element = get(name);
if ( element.isNull() ) if ( element.isNull() )
{ {
throw Poco::NotFoundException(name); throw NotFoundException(name);
} }
else else
{ {
if ( ElementTraits<T>::TypeId == element->type() ) if ( ElementTraits<T>::TypeId == element->type() )
{ {
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get()); ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
if ( concrete != NULL )
{
return concrete->value(); return concrete->value();
} }
else }
{ throw BadCastException("Invalid type mismatch!");
throw std::runtime_error("Invalid type mismatch!");
}
} }
} }
Element::Ptr get(const std::string& name) Element::Ptr get(const std::string& name);
{ /// Returns the element with the given name.
Element::Ptr element; /// An empty element will be returned when the element is not found.
ElementSet::iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
if ( it != _elements.end() )
{
return *it;
}
return element;
}
void elements(std::vector<std::string>& keys) const;
bool exists(const std::string& name)
{
return std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name)) != _elements.end();
}
template<typename T> template<typename T>
bool isType(const std::string& name) bool isType(const std::string& name)
/// Returns true when the type of the element equals the TypeId of ElementTrait
{ {
Element::Ptr element = get(name); Element::Ptr element = get(name);
if ( element.isNull() ) if ( element.isNull() )
@@ -147,23 +160,16 @@ public:
} }
void addElement(Element::Ptr element); void read(BinaryReader& reader);
/// Reads a document from the reader
template<typename T> virtual std::string toString(int indent = 0) const;
void add(const std::string& name, T value) /// Returns a String representation of the document.
{
addElement(new ConcreteElement<T>(name, value));
}
bool empty() const; void write(BinaryWriter& writer);
/// Writes a document to the reader
void clear();
virtual std::string toString() const;
protected: protected:
@@ -172,19 +178,19 @@ protected:
}; };
inline bool Document::empty() const
{
return _elements.empty();
}
inline void Document::clear() inline void Document::clear()
{ {
_elements.clear(); _elements.clear();
} }
inline void Document::elements(std::vector<std::string>& keys) const 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) for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
{ {
@@ -192,6 +198,13 @@ inline void Document::elements(std::vector<std::string>& keys) const
} }
} }
inline bool Document::exists(const std::string& name)
{
return std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name)) != _elements.end();
}
// BSON Embedded Document // BSON Embedded Document
// spec: document // spec: document
template<> template<>
@@ -199,9 +212,9 @@ struct ElementTraits<Document::Ptr>
{ {
enum { TypeId = 0x03 }; enum { TypeId = 0x03 };
static std::string toString(const Document::Ptr& value) static std::string toString(const Document::Ptr& value, int indent = 0)
{ {
return value.isNull() ? "null" : value->toString(); return value.isNull() ? "null" : value->toString(indent);
} }
}; };

View File

@@ -53,40 +53,49 @@
#include "Poco/MongoDB/BSONReader.h" #include "Poco/MongoDB/BSONReader.h"
#include "Poco/MongoDB/BSONWriter.h" #include "Poco/MongoDB/BSONWriter.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API Element class MongoDB_API Element
/// Represents an element of a Document or an Array
{ {
public: public:
Element(const std::string& name); Element(const std::string& name);
/// Constructor
virtual ~Element(); virtual ~Element();
/// Destructor
virtual std::string toString() const = 0; 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; virtual int type() const = 0;
/// Returns the MongoDB type of the element.
typedef Poco::SharedPtr<Element> Ptr; typedef Poco::SharedPtr<Element> Ptr;
std::string name() const;
private: private:
virtual void read(BinaryReader& reader) = 0; virtual void read(BinaryReader& reader) = 0;
virtual void write(BinaryWriter& writer) = 0; virtual void write(BinaryWriter& writer) = 0;
friend class Document; friend class Document;
std::string _name; std::string _name;
}; };
@@ -96,6 +105,7 @@ inline std::string Element::name() const
return _name; return _name;
} }
class ElementComparator class ElementComparator
{ {
public: public:
@@ -121,7 +131,7 @@ struct ElementTraits<double>
{ {
enum { TypeId = 0x01 }; enum { TypeId = 0x01 };
static std::string toString(const double& value) static std::string toString(const double& value, int indent = 0)
{ {
return Poco::NumberFormatter::format(value); return Poco::NumberFormatter::format(value);
} }
@@ -135,7 +145,7 @@ struct ElementTraits<std::string>
{ {
enum { TypeId = 0x02 }; enum { TypeId = 0x02 };
static std::string toString(const std::string& value) static std::string toString(const std::string& value, int indent = 0)
{ {
return '"' + value + '"'; return '"' + value + '"';
} }
@@ -166,7 +176,7 @@ struct ElementTraits<bool>
{ {
enum { TypeId = 0x08 }; enum { TypeId = 0x08 };
static std::string toString(const bool& value) static std::string toString(const bool& value, int indent = 0)
{ {
return value ? "true" : "false"; return value ? "true" : "false";
} }
@@ -195,7 +205,7 @@ struct ElementTraits<Int32>
enum { TypeId = 0x10 }; enum { TypeId = 0x10 };
static std::string toString(const Int32& value) static std::string toString(const Int32& value, int indent = 0)
{ {
return Poco::NumberFormatter::format(value); return Poco::NumberFormatter::format(value);
} }
@@ -208,7 +218,7 @@ struct ElementTraits<Timestamp>
{ {
enum { TypeId = 0x09 }; enum { TypeId = 0x09 };
static std::string toString(const Timestamp& value) static std::string toString(const Timestamp& value, int indent = 0)
{ {
return DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z"); return DateTimeFormatter::format(value, "%Y-%m-%dT%H:%M:%s%z");
} }
@@ -238,7 +248,7 @@ struct ElementTraits<NullValue>
{ {
enum { TypeId = 0x0A }; enum { TypeId = 0x0A };
static std::string toString(const NullValue& value) static std::string toString(const NullValue& value, int indent = 0)
{ {
return "null"; return "null";
} }
@@ -254,198 +264,6 @@ inline void BSONWriter::write<NullValue>(NullValue& from)
{ {
} }
class RegularExpression
{
public:
typedef SharedPtr<RegularExpression> Ptr;
RegularExpression()
{
}
RegularExpression(const std::string& pattern, const std::string& options) : _pattern(pattern), _options(options) {}
virtual ~RegularExpression()
{
}
std::string getPattern() const;
void setPattern(const std::string& pattern);
std::string getOptions() const;
void setOptions(const std::string& options);
SharedPtr<Poco::RegularExpression> createRE()
{
int options = 0;
for(std::string::iterator optIt = _options.begin(); optIt != _options.end(); ++optIt)
{
switch(*optIt)
{
case 'i': // Case Insensitive
options |= Poco::RegularExpression::RE_CASELESS;
break;
case 'm': // Multiline matching
options |= Poco::RegularExpression::RE_MULTILINE;
break;
case 'x': // Verbose mode
//No equivalent in Poco
break;
case 'l': // \w \W Locale dependent
//No equivalent in Poco
break;
case 's': // Dotall mode
options |= Poco::RegularExpression::RE_DOTALL;
break;
case 'u': // \w \W Unicode
//No equivalent in Poco
break;
}
}
return new Poco::RegularExpression(_pattern, options);
}
private:
std::string _pattern;
std::string _options;
};
inline std::string RegularExpression::getPattern() const
{
return _pattern;
}
inline void RegularExpression::setPattern(const std::string& pattern)
{
_pattern = pattern;
}
inline std::string RegularExpression::getOptions() const
{
return _options;
}
inline void RegularExpression::setOptions(const std::string& options)
{
_options = options;
}
// BSON Regex
// spec: cstring cstring
template<>
struct ElementTraits<RegularExpression::Ptr>
{
enum { TypeId = 0x0B };
static std::string toString(const RegularExpression::Ptr& value)
{
//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());
}
class JavaScriptCode
{
public:
typedef SharedPtr<JavaScriptCode> Ptr;
JavaScriptCode()
{
}
virtual ~JavaScriptCode()
{
}
void code(const std::string& s);
std::string code() const;
private:
std::string _code;
};
inline void JavaScriptCode::code(const std::string& s)
{
_code = s;
}
inline std::string JavaScriptCode::code() const
{
return _code;
}
// BSON JavaScript code
// spec: string
template<>
struct ElementTraits<JavaScriptCode::Ptr>
{
enum { TypeId = 0x0D };
static std::string toString(const JavaScriptCode::Ptr& value)
{
return value.isNull() ? "" : value->code();
}
};
template<>
inline void BSONReader::read<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& to)
{
std::string code;
BSONReader(_reader).read(code);
to = new JavaScriptCode();
to->code(code);
}
template<>
inline void BSONWriter::write<JavaScriptCode::Ptr>(JavaScriptCode::Ptr& from)
{
std::string code = from->code();
BSONWriter(_writer).write(code);
}
// BSON 64-bit integer // BSON 64-bit integer
// spec: int64 // spec: int64
template<> template<>
@@ -453,7 +271,7 @@ struct ElementTraits<Int64>
{ {
enum { TypeId = 0x12 }; enum { TypeId = 0x12 };
static std::string toString(const Int64& value) static std::string toString(const Int64& value, int indent = 0)
{ {
return NumberFormatter::format(value); return NumberFormatter::format(value);
} }
@@ -476,7 +294,7 @@ public:
T value() const { return _value; } T value() const { return _value; }
std::string toString() const { return ElementTraits<T>::toString(_value); } std::string toString(int indent = 0) const { return ElementTraits<T>::toString(_value, indent); }
int type() const { return ElementTraits<T>::TypeId; } int type() const { return ElementTraits<T>::TypeId; }

View File

@@ -41,10 +41,8 @@
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API GetMoreRequest : public RequestMessage class MongoDB_API GetMoreRequest : public RequestMessage
@@ -58,17 +56,18 @@ public:
/// name with the collection name, using a "." for the concatenation. For example, /// 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 /// 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. /// "foo.bar". The cursorID has been returned by the response on the query request.
/// By default the numberToReturn is set to 100.
virtual ~GetMoreRequest(); virtual ~GetMoreRequest();
/// Destructor /// Destructor
Int32 numberToReturn() const; Int32 getNumberToReturn() const;
/// Returns the limit of returned documents /// Returns the limit of returned documents
void numberToReturn(Int32 n); void setNumberToReturn(Int32 n);
/// Sets the limit of returned documents /// Sets the limit of returned documents
@@ -91,12 +90,12 @@ private:
Int64 _cursorID; Int64 _cursorID;
}; };
inline Int32 GetMoreRequest::numberToReturn() const inline Int32 GetMoreRequest::getNumberToReturn() const
{ {
return _numberToReturn; return _numberToReturn;
} }
inline void GetMoreRequest::numberToReturn(Int32 n) inline void GetMoreRequest::setNumberToReturn(Int32 n)
{ {
_numberToReturn = n; _numberToReturn = n;
} }

View File

@@ -42,10 +42,8 @@
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/Document.h" #include "Poco/MongoDB/Document.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API InsertRequest : public RequestMessage class MongoDB_API InsertRequest : public RequestMessage

View File

@@ -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

View File

@@ -41,10 +41,8 @@
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API KillCursorsRequest : public RequestMessage class MongoDB_API KillCursorsRequest : public RequestMessage

View File

@@ -47,10 +47,8 @@
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/MessageHeader.h" #include "Poco/MongoDB/MessageHeader.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API Message class MongoDB_API Message
@@ -85,7 +83,7 @@ inline MessageHeader& Message::header()
inline void Message::messageLength(Int32 length) inline void Message::messageLength(Int32 length)
{ {
_header.messageLength(length); _header.setMessageLength(length);
} }

View File

@@ -43,12 +43,10 @@
#define MSG_HEADER_SIZE 16 #define MSG_HEADER_SIZE 16
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MessageHeader class MongoDB_API MessageHeader
/// Represents the header which is always prepended to a request /// Represents the header which is always prepended to a request
/// or response of MongoDB /// or response of MongoDB
{ {
@@ -78,19 +76,19 @@ public:
/// Writes the header /// Writes the header
Int32 getMessageLength() const;
/// Returns the message length
OpCode opCode() const; OpCode opCode() const;
/// Returns the OpCode /// Returns the OpCode
Int32 messageLength() const; Int32 getRequestID() const;
/// Returns the message length
Int32 requestID() const;
/// Returns the request id of the current message /// Returns the request id of the current message
void requestID(Int32 id); void setRequestID(Int32 id);
/// Sets the request id of the current message /// Sets the request id of the current message
@@ -106,7 +104,7 @@ private:
Int32 _messageLength; Int32 _messageLength;
void messageLength(Int32 length); void setMessageLength(Int32 length);
/// Sets the message length /// Sets the message length
@@ -128,22 +126,22 @@ inline MessageHeader::OpCode MessageHeader::opCode() const
return _opCode; return _opCode;
} }
inline Int32 MessageHeader::messageLength() const inline Int32 MessageHeader::getMessageLength() const
{ {
return _messageLength; return _messageLength;
} }
inline void MessageHeader::messageLength(Int32 length) inline void MessageHeader::setMessageLength(Int32 length)
{ {
_messageLength = MSG_HEADER_SIZE + length; _messageLength = MSG_HEADER_SIZE + length;
} }
inline void MessageHeader::requestID(Int32 id) inline void MessageHeader::setRequestID(Int32 id)
{ {
_requestID = id; _requestID = id;
} }
inline Int32 MessageHeader::requestID() const inline Int32 MessageHeader::getRequestID() const
{ {
return _requestID; return _requestID;
} }

View File

@@ -40,13 +40,24 @@
#include "Poco/MongoDB/MongoDB.h" #include "Poco/MongoDB/MongoDB.h"
#include "Poco/MongoDB/Element.h" #include "Poco/MongoDB/Element.h"
#include "Poco/Timestamp.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class ObjectId 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: public:
@@ -54,12 +65,20 @@ public:
ObjectId(); ObjectId();
/// Constructor
virtual ~ObjectId(); virtual ~ObjectId();
/// Destructor
Timestamp timestamp() const;
/// Returns the timestamp which is stored in the first four bytes of the id
std::string toString() const; std::string toString() const;
/// Returns the id in string format
private: private:
unsigned char _id[12]; unsigned char _id[12];
@@ -68,6 +87,18 @@ private:
friend class BSONReader; 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 // BSON Embedded Document
// spec: ObjectId // spec: ObjectId
template<> template<>
@@ -76,7 +107,7 @@ struct ElementTraits<ObjectId::Ptr>
enum { TypeId = 0x07 }; enum { TypeId = 0x07 };
static std::string toString(const ObjectId::Ptr& id) static std::string toString(const ObjectId::Ptr& id, int indent = 0)
{ {
return id->toString(); return id->toString();
} }

View File

@@ -9,7 +9,9 @@ namespace Poco
template<> template<>
class PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr> class MongoDB_API PoolableObjectFactory<MongoDB::Connection, MongoDB::Connection::Ptr>
/// PoolableObjectFactory specialisation for Connection. New connections
/// are created with the given address.
{ {
public: public:
PoolableObjectFactory(Net::SocketAddress& address) PoolableObjectFactory(Net::SocketAddress& address)
@@ -60,7 +62,8 @@ namespace MongoDB
{ {
class PooledConnection class MongoDB_API PooledConnection
/// Helper class for borrowing and returning a connection automatically from a pool.
{ {
public: public:
PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool) PooledConnection(Poco::ObjectPool<Connection, Connection::Ptr>& pool) : _pool(pool)

View File

@@ -42,10 +42,8 @@
#include "Poco/MongoDB/RequestMessage.h" #include "Poco/MongoDB/RequestMessage.h"
#include "Poco/MongoDB/Document.h" #include "Poco/MongoDB/Document.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API QueryRequest : public RequestMessage class MongoDB_API QueryRequest : public RequestMessage
@@ -79,12 +77,14 @@ public:
/// Destructor /// Destructor
Flags flags() const; Flags getFlags() const;
/// Returns the flags /// Returns the flags
void flags(Flags flag);
void setFlags(Flags flag);
/// Set the flags /// Set the flags
Document& query(); Document& query();
/// Returns the query document /// Returns the query document
@@ -93,18 +93,19 @@ public:
/// Returns the selector document /// Returns the selector document
Int32 numberToSkip() const; Int32 getNumberToSkip() const;
/// Returns the number of documents to skip /// Returns the number of documents to skip
void numberToSkip(Int32 n);
void setNumberToSkip(Int32 n);
/// Sets the number of documents to skip /// Sets the number of documents to skip
Int32 numberToReturn() const; Int32 getNumberToReturn() const;
/// Returns the number to return /// Returns the number to return
void numberToReturn(Int32 n); void setNumberToReturn(Int32 n);
/// Sets the number to return (limit) /// Sets the number to return (limit)
protected: protected:
@@ -127,12 +128,12 @@ private:
Document _returnFieldSelector; Document _returnFieldSelector;
}; };
inline QueryRequest::Flags QueryRequest::flags() const inline QueryRequest::Flags QueryRequest::getFlags() const
{ {
return _flags; return _flags;
} }
inline void QueryRequest::flags(QueryRequest::Flags flags) inline void QueryRequest::setFlags(QueryRequest::Flags flags)
{ {
_flags = flags; _flags = flags;
} }
@@ -147,22 +148,22 @@ inline Document& QueryRequest::returnFieldSelector()
return _returnFieldSelector; return _returnFieldSelector;
} }
inline Int32 QueryRequest::numberToSkip() const inline Int32 QueryRequest::getNumberToSkip() const
{ {
return _numberToSkip; return _numberToSkip;
} }
inline void QueryRequest::numberToSkip(Int32 n) inline void QueryRequest::setNumberToSkip(Int32 n)
{ {
_numberToSkip = n; _numberToSkip = n;
} }
inline Int32 QueryRequest::numberToReturn() const inline Int32 QueryRequest::getNumberToReturn() const
{ {
return _numberToReturn; return _numberToReturn;
} }
inline void QueryRequest::numberToReturn(Int32 n) inline void QueryRequest::setNumberToReturn(Int32 n)
{ {
_numberToReturn = n; _numberToReturn = n;
} }

View File

@@ -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

View File

@@ -42,21 +42,24 @@
#include "Poco/Net/SocketAddress.h" #include "Poco/Net/SocketAddress.h"
#include "Poco/MongoDB/Connection.h" #include "Poco/MongoDB/Connection.h"
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API ReplicaSet class MongoDB_API ReplicaSet
/// Class for working with a replicaset
{ {
public: public:
ReplicaSet(const std::vector<Net::SocketAddress>& addresses); ReplicaSet(const std::vector<Net::SocketAddress>& addresses);
/// Constructor
virtual ~ReplicaSet(); virtual ~ReplicaSet();
/// Destructor
Connection::Ptr findMaster(); Connection::Ptr findMaster();
/// Tries to find the master MongoDB instance from the addresses
private: private:

View File

@@ -43,10 +43,8 @@
#include <ostream> #include <ostream>
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API RequestMessage : public Message class MongoDB_API RequestMessage : public Message

View File

@@ -44,10 +44,8 @@
#include <istream> #include <istream>
namespace Poco namespace Poco {
{ namespace MongoDB {
namespace MongoDB
{
class MongoDB_API ResponseMessage : public Message class MongoDB_API ResponseMessage : public Message
@@ -73,6 +71,7 @@ public:
Int64 cursorID() const; Int64 cursorID() const;
/// Returns the cursor id /// Returns the cursor id
void clear(); void clear();
/// Clears the response /// Clears the response

View File

@@ -51,19 +51,41 @@ Array::~Array()
} }
std::string Array::toString() const std::string Array::toString(int indent) const
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "[" << std::endl; oss << "[";
if ( indent > 0 )
{
oss << std::endl;
}
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it) for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
{ {
if ( it != _elements.begin() ) if ( it != _elements.begin() )
{ {
if ( indent > 0 )
{
for(int i = 0; i < indent; ++i)
{
oss << ' ';
}
}
oss << ","; oss << ",";
if ( indent > 0 )
{
oss << std::endl;
}
} }
oss << (*it)->toString(); oss << (*it)->toString();
} }
oss << "]" << std::endl; oss << "]";
if ( indent > 0 )
{
oss << std::endl;
}
return oss.str(); return oss.str();
} }

View File

@@ -56,7 +56,7 @@ Binary::~Binary()
} }
std::string Binary::toString() const std::string Binary::toString(int indent) const
{ {
std::ostringstream oss; std::ostringstream oss;
Base64Encoder encoder(oss); Base64Encoder encoder(oss);

View File

@@ -78,7 +78,7 @@ Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createQueryRequest(const
Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCountRequest(const std::string& collectionName) const Poco::SharedPtr<Poco::MongoDB::QueryRequest> Database::createCountRequest(const std::string& collectionName) const
{ {
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd"); Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createQueryRequest("$cmd");
request->numberToReturn(1); request->setNumberToReturn(1);
request->query().add("count", collectionName); request->query().add("count", collectionName);
return request; return request;
} }

View File

@@ -51,6 +51,16 @@ DeleteRequest::DeleteRequest(const std::string& collectionName, DeleteRequest::F
{ {
} }
DeleteRequest::DeleteRequest(const std::string& collectionName, bool justOne)
: RequestMessage(MessageHeader::Delete),
_flags(justOne ? DELETE_SINGLE_REMOVE : DELETE_NONE),
_fullCollectionName(collectionName),
_selector()
{
}
DeleteRequest::~DeleteRequest() DeleteRequest::~DeleteRequest()
{ {
} }

View File

@@ -41,6 +41,8 @@
#include "Poco/MongoDB/Binary.h" #include "Poco/MongoDB/Binary.h"
#include "Poco/MongoDB/ObjectId.h" #include "Poco/MongoDB/ObjectId.h"
#include "Poco/MongoDB/Array.h" #include "Poco/MongoDB/Array.h"
#include "Poco/MongoDB/RegularExpression.h"
#include "Poco/MongoDB/JavaScriptCode.h"
namespace Poco namespace Poco
{ {
@@ -56,6 +58,21 @@ 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) void Document::read(BinaryReader& reader)
{ {
int size; int size;
@@ -130,7 +147,7 @@ void Document::read(BinaryReader& reader)
} }
std::string Document::toString() const std::string Document::toString(int indent) const
{ {
std::ostringstream oss; std::ostringstream oss;
oss << "{" << std::endl; oss << "{" << std::endl;

View File

@@ -47,7 +47,7 @@ namespace MongoDB
GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID) GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID)
: RequestMessage(MessageHeader::GetMore), : RequestMessage(MessageHeader::GetMore),
_fullCollectionName(collectionName), _fullCollectionName(collectionName),
_numberToReturn(0), _numberToReturn(100),
_cursorID(cursorID) _cursorID(cursorID)
{ {
} }

View File

@@ -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

View File

@@ -48,7 +48,7 @@ QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flag
_flags(flags), _flags(flags),
_fullCollectionName(collectionName), _fullCollectionName(collectionName),
_numberToSkip(0), _numberToSkip(0),
_numberToReturn(0), _numberToReturn(100),
_query(), _query(),
_returnFieldSelector() _returnFieldSelector()
{ {

View File

@@ -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

View File

@@ -77,7 +77,7 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address)
conn->connect(address); conn->connect(address);
QueryRequest request("admin.$cmd"); QueryRequest request("admin.$cmd");
request.numberToReturn(1); request.setNumberToReturn(1);
request.query().add("isMaster", 1); request.query().add("isMaster", 1);
ResponseMessage response; ResponseMessage response;

View File

@@ -54,7 +54,10 @@ ResponseMessage::~ResponseMessage()
void ResponseMessage::clear() void ResponseMessage::clear()
{ {
_responseFlags = 0;
_startingFrom = 0;
_cursorID = 0; _cursorID = 0;
_numberReturned = 0;
_documents.clear(); _documents.clear();
} }

View File

@@ -36,6 +36,7 @@
#include "Poco/MongoDB/InsertRequest.h" #include "Poco/MongoDB/InsertRequest.h"
#include "Poco/MongoDB/QueryRequest.h" #include "Poco/MongoDB/QueryRequest.h"
#include "Poco/MongoDB/DeleteRequest.h" #include "Poco/MongoDB/DeleteRequest.h"
#include "Poco/MongoDB/GetMoreRequest.h"
#include "Poco/MongoDB/PoolableConnectionFactory.h" #include "Poco/MongoDB/PoolableConnectionFactory.h"
#include "Poco/MongoDB/Database.h" #include "Poco/MongoDB/Database.h"
@@ -123,7 +124,7 @@ void MongoDBTest::testQueryRequest()
Poco::MongoDB::QueryRequest request("team.players"); Poco::MongoDB::QueryRequest request("team.players");
request.query().add("lastname" , std::string("Braem")); request.query().add("lastname" , std::string("Braem"));
request.numberToReturn(1); request.setNumberToReturn(1);
Poco::MongoDB::ResponseMessage response; Poco::MongoDB::ResponseMessage response;
@@ -216,7 +217,7 @@ void MongoDBTest::testCountCommand()
} }
Poco::MongoDB::QueryRequest request("team.$cmd"); Poco::MongoDB::QueryRequest request("team.$cmd");
request.numberToReturn(1); request.setNumberToReturn(1);
request.query().add("count", std::string("players")); request.query().add("count", std::string("players"));
Poco::MongoDB::ResponseMessage response; Poco::MongoDB::ResponseMessage response;
@@ -292,6 +293,57 @@ void MongoDBTest::testDeleteRequest()
} }
void MongoDBTest::testGetMoreRequest()
{
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::SharedPtr<Poco::MongoDB::QueryRequest> queryRequest = db.createQueryRequest("numbers");
Poco::MongoDB::ResponseMessage response;
int n = 0;
_mongo.sendRequest(*queryRequest, response);
while(response.documents().size() > 0)
{
std::cout << "CursorID: " << response.cursorID() << std::endl;
n += response.documents().size();
Poco::MongoDB::GetMoreRequest getMore("team.numbers", response.cursorID());
response.clear();
_mongo.sendRequest(getMore, response);
}
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() void MongoDBTest::testBuildInfo()
{ {
if ( ! _connected ) if ( ! _connected )
@@ -301,7 +353,7 @@ void MongoDBTest::testBuildInfo()
} }
Poco::MongoDB::QueryRequest request("team.$cmd"); Poco::MongoDB::QueryRequest request("team.$cmd");
request.numberToReturn(1); request.setNumberToReturn(1);
request.query().add("buildInfo", 1); request.query().add("buildInfo", 1);
Poco::MongoDB::ResponseMessage response; Poco::MongoDB::ResponseMessage response;
@@ -336,7 +388,7 @@ void MongoDBTest::testConnectionPool()
Poco::MongoDB::PooledConnection pooledConnection(pool); Poco::MongoDB::PooledConnection pooledConnection(pool);
Poco::MongoDB::QueryRequest request("team.$cmd"); Poco::MongoDB::QueryRequest request("team.$cmd");
request.numberToReturn(1); request.setNumberToReturn(1);
request.query().add("count", std::string("players")); request.query().add("count", std::string("players"));
Poco::MongoDB::ResponseMessage response; Poco::MongoDB::ResponseMessage response;
@@ -367,6 +419,7 @@ CppUnit::Test* MongoDBTest::suite()
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool); CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest); CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo); CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
CppUnit_addTest(pSuite, MongoDBTest, testGetMoreRequest);
return pSuite; return pSuite;
} }

View File

@@ -78,6 +78,9 @@ public:
void testConnectionPool(); void testConnectionPool();
void testGetMoreRequest();
void setUp(); void setUp();