summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-06 23:29:54 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-06 23:29:54 +0000
commit90dc1626476692be324c28b8607c64b628de278f (patch)
treee732bab803a778dde22bb3f4beaeb66dd6be49e7
parentFix test names that have nothing to do with inheritance (diff)
downloadslicer-90dc1626476692be324c28b8607c64b628de278f.tar.bz2
slicer-90dc1626476692be324c28b8607c64b628de278f.tar.xz
slicer-90dc1626476692be324c28b8607c64b628de278f.zip
Handle selecting nulls/optionals etc
-rw-r--r--slicer/db/sqlSelectDeserializer.cpp12
-rw-r--r--slicer/db/testSelect.cpp42
2 files changed, 51 insertions, 3 deletions
diff --git a/slicer/db/sqlSelectDeserializer.cpp b/slicer/db/sqlSelectDeserializer.cpp
index 5c46e11..a7dd575 100644
--- a/slicer/db/sqlSelectDeserializer.cpp
+++ b/slicer/db/sqlSelectDeserializer.cpp
@@ -40,12 +40,15 @@ namespace Slicer {
void
SqlSelectDeserializer::DeserializeSimple(Slicer::ModelPartPtr mp)
{
- auto fmp = mp->GetAnonChild();
if (!cmd.fetch()) {
- throw NoRowsReturned();
+ if (!mp->IsOptional()) {
+ throw NoRowsReturned();
+ }
+ return;
}
SqlSourcePtr h = new SqlSource(cmd[0]);
if (!h->isNull()) {
+ auto fmp = mp->GetAnonChild();
fmp->Create();
fmp->SetValue(h);
fmp->Complete();
@@ -68,7 +71,10 @@ namespace Slicer {
SqlSelectDeserializer::DeserializeObject(Slicer::ModelPartPtr mp)
{
if (!cmd.fetch()) {
- throw NoRowsReturned();
+ if (!mp->IsOptional()) {
+ throw NoRowsReturned();
+ }
+ return;
}
DeserializeRow(mp);
if (cmd.fetch()) {
diff --git a/slicer/db/testSelect.cpp b/slicer/db/testSelect.cpp
index fdcb358..cd20170 100644
--- a/slicer/db/testSelect.cpp
+++ b/slicer/db/testSelect.cpp
@@ -175,6 +175,44 @@ BOOST_AUTO_TEST_CASE( select_noRowsSimple )
BOOST_REQUIRE_THROW(BoostThrowWrapperHelper<Ice::Int>(*sel), Slicer::NoRowsReturned);
}
+BOOST_AUTO_TEST_CASE( select_noRowsSimpleOptional )
+{
+ auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock"));
+ auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test WHERE false"));
+ auto v = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, IceUtil::Optional<Ice::Int>>(*sel);
+ BOOST_REQUIRE(!v);
+}
+
+BOOST_AUTO_TEST_CASE( select_tooManyRowsSimpleOptional )
+{
+ auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock"));
+ auto sel = SelectPtr(db->newSelectCommand("SELECT id FROM test"));
+ BOOST_REQUIRE_THROW(BoostThrowWrapperHelper<IceUtil::Optional<Ice::Int>>(*sel), Slicer::TooManyRowsReturned);
+}
+
+BOOST_AUTO_TEST_CASE( select_simpleOptional )
+{
+ auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock"));
+ auto sel = SelectPtr(db->newSelectCommand("SELECT MAX(id) FROM test"));
+ auto v = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, IceUtil::Optional<Ice::Int>>(*sel);
+ BOOST_REQUIRE(v);
+ BOOST_REQUIRE_EQUAL(4, *v);
+}
+
+BOOST_AUTO_TEST_CASE( select_noRowsComplexOptional )
+{
+ auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock"));
+ auto sel = SelectPtr(db->newSelectCommand(
+ "SELECT boolean mbool, \
+ id mbyte, id mshort, id mint, id mlong, \
+ fl mdouble, fl mfloat, \
+ string mstring \
+ FROM test \
+ WHERE false"));
+ auto v = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, IceUtil::Optional<TestModule::BuiltInsPtr>>(*sel);
+ BOOST_REQUIRE(!v);
+}
+
BOOST_AUTO_TEST_CASE( select_tooManyRowsComplex )
{
auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock"));
@@ -210,5 +248,9 @@ BOOST_AUTO_TEST_CASE( select_null )
oi = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, TestModule::OptionalsPtr>(*sel);
BOOST_REQUIRE(oi->optSimple);
BOOST_REQUIRE_EQUAL(oi->optSimple.get(), 4);
+
+ sel = SelectPtr(db->newSelectCommand("SELECT MAX(id) FROM test WHERE false"));
+ auto v = Slicer::DeserializeAny<Slicer::SqlSelectDeserializer, IceUtil::Optional<Ice::Int>>(*sel);
+ BOOST_REQUIRE(!v);
}