From 7ff46e8cf51e58a114e0bc1c43b4f0db587165e4 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 10 Sep 2016 17:17:56 +0100 Subject: Fix the case of reading a single column into a sequence of primitives --- slicer/db/sqlSelectDeserializer.cpp | 59 +++++++++++++++++++++++------------ slicer/db/testSelect.cpp | 15 +++++++++ slicer/test/initial/simpleArray1.json | 1 + slicer/test/initial/simpleArray2.xml | 6 ++++ slicer/test/preprocessor.cpp | 2 +- slicer/test/serializers.cpp | 10 ++++++ slicer/test/types.ice | 1 + 7 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 slicer/test/initial/simpleArray1.json create mode 100644 slicer/test/initial/simpleArray2.xml diff --git a/slicer/db/sqlSelectDeserializer.cpp b/slicer/db/sqlSelectDeserializer.cpp index 3abb03b..7836c63 100644 --- a/slicer/db/sqlSelectDeserializer.cpp +++ b/slicer/db/sqlSelectDeserializer.cpp @@ -88,28 +88,47 @@ namespace Slicer { { auto rmp = mp->GetAnonChild(); if (rmp) { - if (typeIdColIdx) { - std::string subclass; - cmd[*typeIdColIdx] >> subclass; - rmp = rmp->GetSubclassModelPart(subclass); - } - rmp->Create(); - for (auto col = 0u; col < columnCount; col += 1) { - const DB::Column & c = cmd[col]; - if (!c.isNull()) { - auto fmpr = rmp->GetAnonChildRef([&c](Slicer::HookCommonPtr h) { - return boost::iequals(c.name, h->PartName()); - }); - if (fmpr) { - SqlSourcePtr h = new SqlSource(c); - auto fmp = fmpr->Child(); - fmp->Create(); - fmp->SetValue(h); - fmp->Complete(); + switch (rmp->GetType()) { + case Slicer::mpt_Complex: + { + if (typeIdColIdx) { + std::string subclass; + cmd[*typeIdColIdx] >> subclass; + rmp = rmp->GetSubclassModelPart(subclass); + } + rmp->Create(); + for (auto col = 0u; col < columnCount; col += 1) { + const DB::Column & c = cmd[col]; + if (!c.isNull()) { + auto fmpr = rmp->GetAnonChildRef([&c](Slicer::HookCommonPtr h) { + return boost::iequals(c.name, h->PartName()); + }); + if (fmpr) { + SqlSourcePtr h = new SqlSource(c); + auto fmp = fmpr->Child(); + fmp->Create(); + fmp->SetValue(h); + fmp->Complete(); + } + } + } + rmp->Complete(); + } + break; + case Slicer::mpt_Simple: + { + rmp->Create(); + const DB::Column & c = cmd[0]; + if (!c.isNull()) { + SqlSourcePtr h = new SqlSource(c); + rmp->SetValue(h); + } + rmp->Complete(); } - } + break; + default: + throw UnsupportedModelType(); } - rmp->Complete(); } } } diff --git a/slicer/db/testSelect.cpp b/slicer/db/testSelect.cpp index b5664a1..6cade1d 100644 --- a/slicer/db/testSelect.cpp +++ b/slicer/db/testSelect.cpp @@ -99,6 +99,21 @@ BOOST_AUTO_TEST_CASE( select_inherit_single ) BOOST_REQUIRE_EQUAL(300, d2->c); } +BOOST_AUTO_TEST_CASE( select_simple_sequence ) +{ + auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto sel = SelectPtr(db->newSelectCommand( + "SELECT string \ + FROM test \ + ORDER BY id DESC")); + auto bi = Slicer::DeserializeAny(*sel); + BOOST_REQUIRE_EQUAL(4, bi.size()); + BOOST_REQUIRE_EQUAL("text four", bi[0]); + BOOST_REQUIRE_EQUAL("text three", bi[1]); + BOOST_REQUIRE_EQUAL("text two", bi[2]); + BOOST_REQUIRE_EQUAL("text one", bi[3]); +} + BOOST_AUTO_TEST_CASE( select_inherit_sequence ) { auto db = DBPtr(DB::MockDatabase::openConnectionTo("pqmock")); diff --git a/slicer/test/initial/simpleArray1.json b/slicer/test/initial/simpleArray1.json new file mode 100644 index 0000000..f489673 --- /dev/null +++ b/slicer/test/initial/simpleArray1.json @@ -0,0 +1 @@ +["one","two","three"] diff --git a/slicer/test/initial/simpleArray2.xml b/slicer/test/initial/simpleArray2.xml new file mode 100644 index 0000000..12de178 --- /dev/null +++ b/slicer/test/initial/simpleArray2.xml @@ -0,0 +1,6 @@ + + + 1 + 2 + 3 + diff --git a/slicer/test/preprocessor.cpp b/slicer/test/preprocessor.cpp index cc67d64..e34632b 100644 --- a/slicer/test/preprocessor.cpp +++ b/slicer/test/preprocessor.cpp @@ -13,7 +13,7 @@ namespace fs = boost::filesystem; -const unsigned int COMPONENTS_IN_TEST_ICE = 39; +const unsigned int COMPONENTS_IN_TEST_ICE = 40; BOOST_FIXTURE_TEST_SUITE ( preprocessor, FileStructure ); diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 95e8e87..7c5f252 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -448,6 +448,16 @@ BOOST_AUTO_TEST_CASE( json_objectmapMember ) verifyByFile("objectmapMember.json", checkObjectMapMember); } +BOOST_AUTO_TEST_CASE( json_simpleArray ) +{ + verifyByFile("simpleArray1.json"); +} + +BOOST_AUTO_TEST_CASE( xml_simpleArray ) +{ + verifyByFile("simpleArray2.xml"); +} + BOOST_AUTO_TEST_CASE( json_streams ) { const auto tmpf = tmp / "byStream"; diff --git a/slicer/test/types.ice b/slicer/test/types.ice index c2ac8f3..311d210 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -50,6 +50,7 @@ module TestModule { int a; int b; }; + sequence SimpleSeq; sequence BuiltInSeq; sequence Classes; sequence Structs; -- cgit v1.2.3