mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-19 00:46:03 +01:00
Cleaning up code, add docs, add getMore sample ...
This commit is contained in:
parent
58958d2655
commit
bf315df7a6
@ -59,7 +59,7 @@ public:
|
||||
/// Destructor
|
||||
|
||||
|
||||
std::string toString() const;
|
||||
std::string toString(int indent = 0) const;
|
||||
};
|
||||
|
||||
// BSON Embedded Array
|
||||
@ -69,7 +69,7 @@ struct ElementTraits<Array::Ptr>
|
||||
{
|
||||
enum { TypeId = 0x04 };
|
||||
|
||||
static std::string toString(const Array::Ptr& value)
|
||||
static std::string toString(const Array::Ptr& value, int indent = 0)
|
||||
{
|
||||
//TODO:
|
||||
return value.isNull() ? "null" : value->toString();
|
||||
|
@ -39,11 +39,10 @@
|
||||
#define _MongoDB_BSONReader_included
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/BinaryReader.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API BSONReader
|
||||
/// Class for reading BSON from a Poco::BinaryReader
|
||||
|
@ -41,10 +41,8 @@
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/BinaryWriter.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API BSONWriter
|
||||
@ -56,11 +54,13 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
virtual ~BSONWriter()
|
||||
/// Destructor
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
void write(T& t)
|
||||
/// Writes the value to the writer. The default implementation uses
|
||||
@ -69,6 +69,7 @@ public:
|
||||
_writer << t;
|
||||
}
|
||||
|
||||
|
||||
void writeCString(const std::string& value);
|
||||
/// Writes a cstring to the writer. A cstring is a string
|
||||
/// terminated with 0x00
|
||||
|
@ -81,7 +81,7 @@ public:
|
||||
/// Sets the subtype
|
||||
|
||||
|
||||
std::string toString() const;
|
||||
std::string toString(int indent = 0) const;
|
||||
/// Returns the binary encoded in Base64
|
||||
|
||||
|
||||
@ -119,7 +119,7 @@ struct ElementTraits<Binary::Ptr>
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
@ -45,10 +45,8 @@
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/ResponseMessage.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MongoDB_API Connection
|
||||
/// Represents a connection to a MongoDB server
|
||||
@ -96,7 +94,7 @@ public:
|
||||
void connect(const Net::SocketAddress& addrs);
|
||||
/// Connects to the given MongoDB server.
|
||||
|
||||
|
||||
|
||||
void disconnect();
|
||||
/// Disconnects from the MongoDB server
|
||||
|
||||
|
@ -42,15 +42,19 @@
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
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:
|
||||
|
||||
@ -65,12 +69,20 @@ public:
|
||||
/// 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
|
||||
|
||||
@ -78,12 +90,13 @@ public:
|
||||
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
|
||||
|
||||
@ -91,21 +104,26 @@ 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;
|
||||
|
@ -68,6 +68,7 @@ private:
|
||||
};
|
||||
|
||||
class MongoDB_API Document
|
||||
/// Represents a BSON document
|
||||
{
|
||||
public:
|
||||
|
||||
@ -78,64 +79,76 @@ public:
|
||||
|
||||
|
||||
Document();
|
||||
/// Constructor
|
||||
|
||||
|
||||
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>
|
||||
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 Poco::NotFoundException(name);
|
||||
throw NotFoundException(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( ElementTraits<T>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<T>* concrete = dynamic_cast<ConcreteElement<T>* >(element.get());
|
||||
return concrete->value();
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("Invalid type mismatch!");
|
||||
if ( concrete != NULL )
|
||||
{
|
||||
return concrete->value();
|
||||
}
|
||||
}
|
||||
throw BadCastException("Invalid type mismatch!");
|
||||
}
|
||||
}
|
||||
|
||||
Element::Ptr get(const std::string& name)
|
||||
{
|
||||
Element::Ptr element;
|
||||
|
||||
ElementSet::iterator it = std::find_if(_elements.begin(), _elements.end(), ElementFindByName(name));
|
||||
if ( it != _elements.end() )
|
||||
{
|
||||
return *it;
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
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() )
|
||||
@ -147,23 +160,16 @@ public:
|
||||
}
|
||||
|
||||
|
||||
void addElement(Element::Ptr element);
|
||||
void read(BinaryReader& reader);
|
||||
/// Reads a document from the reader
|
||||
|
||||
|
||||
template<typename T>
|
||||
void add(const std::string& name, T value)
|
||||
{
|
||||
addElement(new ConcreteElement<T>(name, value));
|
||||
}
|
||||
virtual std::string toString(int indent = 0) const;
|
||||
/// Returns a String representation of the document.
|
||||
|
||||
|
||||
bool empty() const;
|
||||
|
||||
|
||||
void clear();
|
||||
|
||||
|
||||
virtual std::string toString() const;
|
||||
void write(BinaryWriter& writer);
|
||||
/// Writes a document to the reader
|
||||
|
||||
|
||||
protected:
|
||||
@ -172,19 +178,19 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
inline bool Document::empty() const
|
||||
{
|
||||
return _elements.empty();
|
||||
}
|
||||
|
||||
|
||||
inline void Document::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)
|
||||
{
|
||||
@ -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
|
||||
// spec: document
|
||||
template<>
|
||||
@ -199,9 +212,9 @@ struct ElementTraits<Document::Ptr>
|
||||
{
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -53,40 +53,49 @@
|
||||
#include "Poco/MongoDB/BSONReader.h"
|
||||
#include "Poco/MongoDB/BSONWriter.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
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
|
||||
|
||||
|
||||
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;
|
||||
/// Returns the MongoDB type of the element.
|
||||
|
||||
|
||||
typedef Poco::SharedPtr<Element> Ptr;
|
||||
|
||||
std::string name() const;
|
||||
|
||||
private:
|
||||
|
||||
virtual void read(BinaryReader& reader) = 0;
|
||||
|
||||
|
||||
virtual void write(BinaryWriter& writer) = 0;
|
||||
|
||||
|
||||
friend class Document;
|
||||
|
||||
|
||||
std::string _name;
|
||||
};
|
||||
|
||||
@ -96,6 +105,7 @@ inline std::string Element::name() const
|
||||
return _name;
|
||||
}
|
||||
|
||||
|
||||
class ElementComparator
|
||||
{
|
||||
public:
|
||||
@ -121,7 +131,7 @@ struct ElementTraits<double>
|
||||
{
|
||||
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);
|
||||
}
|
||||
@ -135,7 +145,7 @@ struct ElementTraits<std::string>
|
||||
{
|
||||
enum { TypeId = 0x02 };
|
||||
|
||||
static std::string toString(const std::string& value)
|
||||
static std::string toString(const std::string& value, int indent = 0)
|
||||
{
|
||||
return '"' + value + '"';
|
||||
}
|
||||
@ -166,7 +176,7 @@ struct ElementTraits<bool>
|
||||
{
|
||||
enum { TypeId = 0x08 };
|
||||
|
||||
static std::string toString(const bool& value)
|
||||
static std::string toString(const bool& value, int indent = 0)
|
||||
{
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
@ -195,7 +205,7 @@ struct ElementTraits<Int32>
|
||||
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);
|
||||
}
|
||||
@ -208,7 +218,7 @@ struct ElementTraits<Timestamp>
|
||||
{
|
||||
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");
|
||||
}
|
||||
@ -238,7 +248,7 @@ struct ElementTraits<NullValue>
|
||||
{
|
||||
enum { TypeId = 0x0A };
|
||||
|
||||
static std::string toString(const NullValue& value)
|
||||
static std::string toString(const NullValue& value, int indent = 0)
|
||||
{
|
||||
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
|
||||
// spec: int64
|
||||
template<>
|
||||
@ -453,7 +271,7 @@ struct ElementTraits<Int64>
|
||||
{
|
||||
enum { TypeId = 0x12 };
|
||||
|
||||
static std::string toString(const Int64& value)
|
||||
static std::string toString(const Int64& value, int indent = 0)
|
||||
{
|
||||
return NumberFormatter::format(value);
|
||||
}
|
||||
@ -476,7 +294,7 @@ public:
|
||||
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; }
|
||||
|
@ -41,10 +41,8 @@
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API GetMoreRequest : public RequestMessage
|
||||
@ -58,17 +56,18 @@ public:
|
||||
/// 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 numberToReturn() const;
|
||||
Int32 getNumberToReturn() const;
|
||||
/// Returns the limit of returned documents
|
||||
|
||||
|
||||
void numberToReturn(Int32 n);
|
||||
void setNumberToReturn(Int32 n);
|
||||
/// Sets the limit of returned documents
|
||||
|
||||
|
||||
@ -91,12 +90,12 @@ private:
|
||||
Int64 _cursorID;
|
||||
};
|
||||
|
||||
inline Int32 GetMoreRequest::numberToReturn() const
|
||||
inline Int32 GetMoreRequest::getNumberToReturn() const
|
||||
{
|
||||
return _numberToReturn;
|
||||
}
|
||||
|
||||
inline void GetMoreRequest::numberToReturn(Int32 n)
|
||||
inline void GetMoreRequest::setNumberToReturn(Int32 n)
|
||||
{
|
||||
_numberToReturn = n;
|
||||
}
|
||||
|
@ -42,10 +42,8 @@
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API InsertRequest : public RequestMessage
|
||||
@ -69,7 +67,7 @@ public:
|
||||
|
||||
virtual ~InsertRequest();
|
||||
/// Destructor
|
||||
|
||||
|
||||
|
||||
Document::Vector& documents();
|
||||
/// Returns the documents to insert into the database
|
||||
|
123
MongoDB/include/Poco/MongoDB/JavaScriptCode.h
Normal file
123
MongoDB/include/Poco/MongoDB/JavaScriptCode.h
Normal 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
|
@ -41,10 +41,8 @@
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API KillCursorsRequest : public RequestMessage
|
||||
@ -55,8 +53,8 @@ class MongoDB_API KillCursorsRequest : public RequestMessage
|
||||
public:
|
||||
KillCursorsRequest();
|
||||
/// Constructor
|
||||
|
||||
|
||||
|
||||
|
||||
virtual ~KillCursorsRequest();
|
||||
/// Destructor
|
||||
|
||||
|
@ -47,10 +47,8 @@
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/MessageHeader.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API Message
|
||||
@ -85,7 +83,7 @@ inline MessageHeader& Message::header()
|
||||
|
||||
inline void Message::messageLength(Int32 length)
|
||||
{
|
||||
_header.messageLength(length);
|
||||
_header.setMessageLength(length);
|
||||
}
|
||||
|
||||
|
||||
|
@ -43,12 +43,10 @@
|
||||
|
||||
#define MSG_HEADER_SIZE 16
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
class MessageHeader
|
||||
class MongoDB_API MessageHeader
|
||||
/// Represents the header which is always prepended to a request
|
||||
/// or response of MongoDB
|
||||
{
|
||||
@ -68,31 +66,31 @@ public:
|
||||
|
||||
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 messageLength() const;
|
||||
/// Returns the message length
|
||||
|
||||
|
||||
Int32 requestID() const;
|
||||
Int32 getRequestID() const;
|
||||
/// Returns the request id of the current message
|
||||
|
||||
|
||||
void requestID(Int32 id);
|
||||
|
||||
void setRequestID(Int32 id);
|
||||
/// Sets the request id of the current message
|
||||
|
||||
|
||||
|
||||
Int32 responseTo() const;
|
||||
/// Returns the request id from the original request.
|
||||
@ -101,21 +99,21 @@ private:
|
||||
|
||||
MessageHeader(OpCode opcode);
|
||||
/// Constructor.
|
||||
|
||||
|
||||
|
||||
|
||||
Int32 _messageLength;
|
||||
|
||||
|
||||
void messageLength(Int32 length);
|
||||
|
||||
void setMessageLength(Int32 length);
|
||||
/// Sets the message length
|
||||
|
||||
|
||||
|
||||
Int32 _requestID;
|
||||
|
||||
|
||||
|
||||
|
||||
Int32 _responseTo;
|
||||
|
||||
|
||||
|
||||
|
||||
OpCode _opCode;
|
||||
|
||||
|
||||
@ -128,22 +126,22 @@ inline MessageHeader::OpCode MessageHeader::opCode() const
|
||||
return _opCode;
|
||||
}
|
||||
|
||||
inline Int32 MessageHeader::messageLength() const
|
||||
inline Int32 MessageHeader::getMessageLength() const
|
||||
{
|
||||
return _messageLength;
|
||||
}
|
||||
|
||||
inline void MessageHeader::messageLength(Int32 length)
|
||||
inline void MessageHeader::setMessageLength(Int32 length)
|
||||
{
|
||||
_messageLength = MSG_HEADER_SIZE + length;
|
||||
}
|
||||
|
||||
inline void MessageHeader::requestID(Int32 id)
|
||||
inline void MessageHeader::setRequestID(Int32 id)
|
||||
{
|
||||
_requestID = id;
|
||||
}
|
||||
|
||||
inline Int32 MessageHeader::requestID() const
|
||||
inline Int32 MessageHeader::getRequestID() const
|
||||
{
|
||||
return _requestID;
|
||||
}
|
||||
|
@ -40,13 +40,24 @@
|
||||
|
||||
#include "Poco/MongoDB/MongoDB.h"
|
||||
#include "Poco/MongoDB/Element.h"
|
||||
#include "Poco/Timestamp.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
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:
|
||||
|
||||
@ -54,12 +65,20 @@ public:
|
||||
|
||||
|
||||
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];
|
||||
@ -68,6 +87,18 @@ private:
|
||||
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<>
|
||||
@ -76,7 +107,7 @@ struct ElementTraits<ObjectId::Ptr>
|
||||
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();
|
||||
}
|
||||
|
@ -9,7 +9,9 @@ namespace Poco
|
||||
|
||||
|
||||
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:
|
||||
PoolableObjectFactory(Net::SocketAddress& address)
|
||||
@ -58,9 +60,10 @@ private:
|
||||
|
||||
namespace MongoDB
|
||||
{
|
||||
|
||||
|
||||
class PooledConnection
|
||||
|
||||
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)
|
||||
|
@ -42,10 +42,8 @@
|
||||
#include "Poco/MongoDB/RequestMessage.h"
|
||||
#include "Poco/MongoDB/Document.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API QueryRequest : public RequestMessage
|
||||
@ -79,32 +77,35 @@ public:
|
||||
/// Destructor
|
||||
|
||||
|
||||
Flags flags() const;
|
||||
Flags getFlags() const;
|
||||
/// Returns the flags
|
||||
|
||||
void flags(Flags flag);
|
||||
|
||||
void setFlags(Flags flag);
|
||||
/// Set the flags
|
||||
|
||||
|
||||
Document& query();
|
||||
/// Returns the query document
|
||||
|
||||
|
||||
|
||||
Document& returnFieldSelector();
|
||||
/// Returns the selector document
|
||||
|
||||
|
||||
Int32 numberToSkip() const;
|
||||
|
||||
Int32 getNumberToSkip() const;
|
||||
/// Returns the number of documents to skip
|
||||
|
||||
void numberToSkip(Int32 n);
|
||||
|
||||
void setNumberToSkip(Int32 n);
|
||||
/// Sets the number of documents to skip
|
||||
|
||||
|
||||
Int32 numberToReturn() const;
|
||||
Int32 getNumberToReturn() const;
|
||||
/// Returns the number to return
|
||||
|
||||
|
||||
void numberToReturn(Int32 n);
|
||||
void setNumberToReturn(Int32 n);
|
||||
/// Sets the number to return (limit)
|
||||
|
||||
protected:
|
||||
@ -127,12 +128,12 @@ private:
|
||||
Document _returnFieldSelector;
|
||||
};
|
||||
|
||||
inline QueryRequest::Flags QueryRequest::flags() const
|
||||
inline QueryRequest::Flags QueryRequest::getFlags() const
|
||||
{
|
||||
return _flags;
|
||||
}
|
||||
|
||||
inline void QueryRequest::flags(QueryRequest::Flags flags)
|
||||
inline void QueryRequest::setFlags(QueryRequest::Flags flags)
|
||||
{
|
||||
_flags = flags;
|
||||
}
|
||||
@ -147,22 +148,22 @@ inline Document& QueryRequest::returnFieldSelector()
|
||||
return _returnFieldSelector;
|
||||
}
|
||||
|
||||
inline Int32 QueryRequest::numberToSkip() const
|
||||
inline Int32 QueryRequest::getNumberToSkip() const
|
||||
{
|
||||
return _numberToSkip;
|
||||
}
|
||||
|
||||
inline void QueryRequest::numberToSkip(Int32 n)
|
||||
inline void QueryRequest::setNumberToSkip(Int32 n)
|
||||
{
|
||||
_numberToSkip = n;
|
||||
}
|
||||
|
||||
inline Int32 QueryRequest::numberToReturn() const
|
||||
inline Int32 QueryRequest::getNumberToReturn() const
|
||||
{
|
||||
return _numberToReturn;
|
||||
}
|
||||
|
||||
inline void QueryRequest::numberToReturn(Int32 n)
|
||||
inline void QueryRequest::setNumberToReturn(Int32 n)
|
||||
{
|
||||
_numberToReturn = n;
|
||||
}
|
||||
|
152
MongoDB/include/Poco/MongoDB/RegularExpression.h
Normal file
152
MongoDB/include/Poco/MongoDB/RegularExpression.h
Normal 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
|
@ -42,21 +42,24 @@
|
||||
#include "Poco/Net/SocketAddress.h"
|
||||
#include "Poco/MongoDB/Connection.h"
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
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:
|
||||
|
||||
|
@ -43,10 +43,8 @@
|
||||
|
||||
#include <ostream>
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API RequestMessage : public Message
|
||||
@ -55,15 +53,15 @@ class MongoDB_API RequestMessage : public Message
|
||||
public:
|
||||
RequestMessage(MessageHeader::OpCode opcode);
|
||||
/// Constructor
|
||||
|
||||
|
||||
|
||||
|
||||
virtual ~RequestMessage();
|
||||
/// Destructor
|
||||
|
||||
|
||||
|
||||
void send(std::ostream& ostr);
|
||||
/// Sends the request to stream
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
|
@ -44,10 +44,8 @@
|
||||
|
||||
#include <istream>
|
||||
|
||||
namespace Poco
|
||||
{
|
||||
namespace MongoDB
|
||||
{
|
||||
namespace Poco {
|
||||
namespace MongoDB {
|
||||
|
||||
|
||||
class MongoDB_API ResponseMessage : public Message
|
||||
@ -56,7 +54,7 @@ class MongoDB_API ResponseMessage : public Message
|
||||
public:
|
||||
ResponseMessage();
|
||||
/// Constructor
|
||||
|
||||
|
||||
|
||||
virtual ~ResponseMessage();
|
||||
/// Destructor
|
||||
@ -64,29 +62,30 @@ public:
|
||||
|
||||
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;
|
||||
|
||||
|
||||
|
@ -51,19 +51,41 @@ Array::~Array()
|
||||
}
|
||||
|
||||
|
||||
std::string Array::toString() const
|
||||
std::string Array::toString(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "[" << std::endl;
|
||||
oss << "[";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
|
||||
for(ElementSet::const_iterator it = _elements.begin(); it != _elements.end(); ++it)
|
||||
{
|
||||
if ( it != _elements.begin() )
|
||||
{
|
||||
oss << ", ";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
for(int i = 0; i < indent; ++i)
|
||||
{
|
||||
oss << ' ';
|
||||
}
|
||||
}
|
||||
oss << ",";
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
}
|
||||
oss << (*it)->toString();
|
||||
}
|
||||
oss << "]" << std::endl;
|
||||
oss << "]";
|
||||
|
||||
if ( indent > 0 )
|
||||
{
|
||||
oss << std::endl;
|
||||
}
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
|
@ -56,7 +56,7 @@ Binary::~Binary()
|
||||
}
|
||||
|
||||
|
||||
std::string Binary::toString() const
|
||||
std::string Binary::toString(int indent) const
|
||||
{
|
||||
std::ostringstream oss;
|
||||
Base64Encoder encoder(oss);
|
||||
|
@ -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> request = createQueryRequest("$cmd");
|
||||
request->numberToReturn(1);
|
||||
request->setNumberToReturn(1);
|
||||
request->query().add("count", collectionName);
|
||||
return request;
|
||||
}
|
||||
|
@ -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()
|
||||
{
|
||||
}
|
||||
|
@ -41,6 +41,8 @@
|
||||
#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
|
||||
{
|
||||
@ -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)
|
||||
{
|
||||
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;
|
||||
oss << "{" << std::endl;
|
||||
|
@ -47,7 +47,7 @@ namespace MongoDB
|
||||
GetMoreRequest::GetMoreRequest(const std::string& collectionName, Int64 cursorID)
|
||||
: RequestMessage(MessageHeader::GetMore),
|
||||
_fullCollectionName(collectionName),
|
||||
_numberToReturn(0),
|
||||
_numberToReturn(100),
|
||||
_cursorID(cursorID)
|
||||
{
|
||||
}
|
||||
|
52
MongoDB/src/JavaScriptCode.cpp
Normal file
52
MongoDB/src/JavaScriptCode.cpp
Normal 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
|
@ -48,7 +48,7 @@ QueryRequest::QueryRequest(const std::string& collectionName, QueryRequest::Flag
|
||||
_flags(flags),
|
||||
_fullCollectionName(collectionName),
|
||||
_numberToSkip(0),
|
||||
_numberToReturn(0),
|
||||
_numberToReturn(100),
|
||||
_query(),
|
||||
_returnFieldSelector()
|
||||
{
|
||||
|
91
MongoDB/src/RegularExpression.cpp
Normal file
91
MongoDB/src/RegularExpression.cpp
Normal 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
|
@ -77,7 +77,7 @@ Connection::Ptr ReplicaSet::isMaster(const Net::SocketAddress& address)
|
||||
conn->connect(address);
|
||||
|
||||
QueryRequest request("admin.$cmd");
|
||||
request.numberToReturn(1);
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("isMaster", 1);
|
||||
|
||||
ResponseMessage response;
|
||||
|
@ -54,7 +54,10 @@ ResponseMessage::~ResponseMessage()
|
||||
|
||||
void ResponseMessage::clear()
|
||||
{
|
||||
_responseFlags = 0;
|
||||
_startingFrom = 0;
|
||||
_cursorID = 0;
|
||||
_numberReturned = 0;
|
||||
_documents.clear();
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,7 @@
|
||||
#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"
|
||||
|
||||
@ -123,7 +124,7 @@ void MongoDBTest::testQueryRequest()
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.players");
|
||||
request.query().add("lastname" , std::string("Braem"));
|
||||
request.numberToReturn(1);
|
||||
request.setNumberToReturn(1);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
|
||||
@ -216,7 +217,7 @@ void MongoDBTest::testCountCommand()
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.numberToReturn(1);
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("count", std::string("players"));
|
||||
|
||||
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()
|
||||
{
|
||||
if ( ! _connected )
|
||||
@ -301,7 +353,7 @@ void MongoDBTest::testBuildInfo()
|
||||
}
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.numberToReturn(1);
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("buildInfo", 1);
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
@ -336,7 +388,7 @@ void MongoDBTest::testConnectionPool()
|
||||
Poco::MongoDB::PooledConnection pooledConnection(pool);
|
||||
|
||||
Poco::MongoDB::QueryRequest request("team.$cmd");
|
||||
request.numberToReturn(1);
|
||||
request.setNumberToReturn(1);
|
||||
request.query().add("count", std::string("players"));
|
||||
|
||||
Poco::MongoDB::ResponseMessage response;
|
||||
@ -367,6 +419,7 @@ CppUnit::Test* MongoDBTest::suite()
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testConnectionPool);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo);
|
||||
CppUnit_addTest(pSuite, MongoDBTest, testGetMoreRequest);
|
||||
|
||||
return pSuite;
|
||||
}
|
||||
|
@ -78,6 +78,9 @@ public:
|
||||
void testConnectionPool();
|
||||
|
||||
|
||||
void testGetMoreRequest();
|
||||
|
||||
|
||||
void setUp();
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user