mirror of
https://github.com/pocoproject/poco.git
synced 2025-02-21 22:56:19 +01:00
Add getInteger
This commit is contained in:
parent
cfdf3e4134
commit
edc8d2abff
@ -153,6 +153,12 @@ public:
|
||||
/// Returns the element with the given name.
|
||||
/// An empty element will be returned when the element is not found.
|
||||
|
||||
Int64 getInteger(const std::string& name) const;
|
||||
/// Returns an integer. Useful when MongoDB returns int32, int64
|
||||
/// or double for a number (count for example). This method will always
|
||||
/// return an Int64. When the element is not found, a
|
||||
/// NotFoundException will be thrown.
|
||||
|
||||
template<typename T>
|
||||
bool isType(const std::string& name) const
|
||||
/// Returns true when the type of the element equals the TypeId of ElementTrait
|
||||
|
@ -42,16 +42,7 @@ Int64 Database::count(Connection& connection, const std::string& collectionName)
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
|
||||
if (doc->isType<double>("n")) {
|
||||
return static_cast<Int64>(doc->get<double>("n"));
|
||||
}
|
||||
else if (doc->isType<Int32>("n")) {
|
||||
return doc->get<Int32>("n");
|
||||
}
|
||||
else if (doc->isType<Int64>("n")) {
|
||||
return doc->get<Int64>("n");
|
||||
}
|
||||
return doc->getInteger("n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
@ -52,6 +52,28 @@ Element::Ptr Document::get(const std::string& name) const
|
||||
return element;
|
||||
}
|
||||
|
||||
Int64 Document::getInteger(const std::string& name) const
|
||||
{
|
||||
Element::Ptr element = get(name);
|
||||
if ( element.isNull() ) throw NotFoundException(name);
|
||||
|
||||
if ( ElementTraits<double>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<double>* concrete = dynamic_cast<ConcreteElement<double>* >(element.get());
|
||||
if ( concrete != NULL ) return concrete->value();
|
||||
}
|
||||
else if ( ElementTraits<Int32>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<Int32>* concrete = dynamic_cast<ConcreteElement<Int32>* >(element.get());
|
||||
if ( concrete != NULL ) return concrete->value();
|
||||
}
|
||||
else if ( ElementTraits<Int64>::TypeId == element->type() )
|
||||
{
|
||||
ConcreteElement<Int64>* concrete = dynamic_cast<ConcreteElement<Int64>* >(element.get());
|
||||
if ( concrete != NULL ) return concrete->value();
|
||||
}
|
||||
throw BadCastException("Invalid type mismatch!");
|
||||
}
|
||||
|
||||
void Document::read(BinaryReader& reader)
|
||||
{
|
||||
@ -184,7 +206,7 @@ void Document::write(BinaryWriter& writer)
|
||||
element->write(tempWriter);
|
||||
}
|
||||
tempWriter.flush();
|
||||
|
||||
|
||||
Poco::Int32 len = static_cast<Poco::Int32>(5 + sstream.tellp()); /* 5 = sizeof(len) + 0-byte */
|
||||
writer << len;
|
||||
writer.writeRaw(sstream.str());
|
||||
|
@ -174,8 +174,8 @@ void MongoDBTest::testCountCommand()
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
std::cout << doc->toString() << std::endl;
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
|
||||
assert(doc->getInteger("n") == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -195,8 +195,7 @@ void MongoDBTest::testDBCountCommand()
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
assert(doc->getInteger("n") == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -314,8 +313,7 @@ void MongoDBTest::testConnectionPool()
|
||||
if ( response.documents().size() > 0 )
|
||||
{
|
||||
Poco::MongoDB::Document::Ptr doc = response.documents()[0];
|
||||
double count = doc->get<double>("n");
|
||||
assert(count == 1);
|
||||
assert(doc->getInteger("n") == 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user