diff options
-rw-r--r-- | libdbpp/column.h | 11 | ||||
-rw-r--r-- | libdbpp/unittests/testUtils.cpp | 16 | ||||
-rw-r--r-- | libdbpp/unittests/util.sql | 1 |
3 files changed, 28 insertions, 0 deletions
diff --git a/libdbpp/column.h b/libdbpp/column.h index 37f3b88..43d2687 100644 --- a/libdbpp/column.h +++ b/libdbpp/column.h @@ -4,6 +4,7 @@ #include <glibmm/ustring.h> #include <boost/date_time/posix_time/posix_time_types.hpp> #include <boost/shared_ptr.hpp> +#include <boost/optional.hpp> #include <visibility.h> namespace DB { @@ -53,6 +54,16 @@ namespace DB { void operator>>(boost::posix_time::time_duration &) const; /// STL like date time extractor. void operator>>(boost::posix_time::ptime &) const; + template <typename T> + void operator>>(boost::optional<T> & v) const { + if (!isNull()) { + v = T(); + operator>>(v.get()); + } + else { + v = boost::none; + } + } /// This column's ordinal. const unsigned int colNo; diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index e43faae..b7947c7 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -33,6 +33,22 @@ BOOST_AUTO_TEST_CASE( forEachRow ) }); } +BOOST_AUTO_TEST_CASE( forEachRowNulls ) +{ + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT a, b, c, d, e, f FROM forEachRow ORDER BY a DESC LIMIT 1")); + sel->forEachRow<int64_t, boost::optional<double>, std::string, boost::optional<boost::posix_time::ptime>, boost::optional<boost::posix_time::time_duration>, bool>( + [](auto a, auto b, auto c, auto d, auto e, auto f) { + BOOST_REQUIRE_EQUAL(2, a); + BOOST_REQUIRE(b); + BOOST_REQUIRE_EQUAL(2.3, *b); + BOOST_REQUIRE_EQUAL("Some text", c); + BOOST_REQUIRE(!d); + BOOST_REQUIRE(!e); + BOOST_REQUIRE_EQUAL(false, f); + }); +} + BOOST_AUTO_TEST_CASE( execute ) { auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); diff --git a/libdbpp/unittests/util.sql b/libdbpp/unittests/util.sql index fe98fc9..04c6bf6 100644 --- a/libdbpp/unittests/util.sql +++ b/libdbpp/unittests/util.sql @@ -6,4 +6,5 @@ CREATE TABLE foreachrow ( e interval, f boolean); INSERT INTO foreachrow(a, b, c, d, e, f) VALUES(1, 2.3, 'Some text', '2015-11-07 13:39:17', '04:03:02', true); +INSERT INTO foreachrow(a, b, c, f) VALUES(2, 2.3, 'Some text', false); |