Add getInteger

This commit is contained in:
fbraem
2015-12-12 17:15:55 +01:00
parent cfdf3e4134
commit edc8d2abff
4 changed files with 34 additions and 17 deletions

View File

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

View File

@@ -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());