diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-08-22 19:47:53 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-08-22 19:47:53 +0100 |
commit | b932b50414c806f2fa590a01ccf3cfd94d27165f (patch) | |
tree | a8fb07a7b3113b1fb79639a034c8cb284f33c138 | |
parent | Workaround Clang warning about evaluation with typeid (diff) | |
download | libdbpp-b932b50414c806f2fa590a01ccf3cfd94d27165f.tar.bz2 libdbpp-b932b50414c806f2fa590a01ccf3cfd94d27165f.tar.xz libdbpp-b932b50414c806f2fa590a01ccf3cfd94d27165f.zip |
Structured binding
Replace Row::value method with one that supports structured binding of
field values
-rw-r--r-- | libdbpp/selectcommand.h | 20 | ||||
-rw-r--r-- | libdbpp/selectcommandUtil.impl.h | 7 | ||||
m--------- | libdbpp/unittests/libdbpp-postgresql | 0 | ||||
-rw-r--r-- | libdbpp/unittests/testUtils.cpp | 22 |
4 files changed, 46 insertions, 3 deletions
diff --git a/libdbpp/selectcommand.h b/libdbpp/selectcommand.h index 0d2a4ca..ee4c9ad 100644 --- a/libdbpp/selectcommand.h +++ b/libdbpp/selectcommand.h @@ -13,6 +13,12 @@ #include <visibility.h> #include <exception.h> +#ifndef BOOST_TEST_MODULE +#define DEPRECATE __attribute__((deprecated)) +#else +#define DEPRECATE +#endif + namespace DB { class Column; class SelectCommand; @@ -38,7 +44,10 @@ namespace DB { /// Get value of column C in current row. template<unsigned int C> - typename std::tuple_element<C, std::tuple<Fn...>>::type value() const; + typename std::tuple_element<C, std::tuple<Fn...>>::type value() const DEPRECATE ; + + template<unsigned int C> + typename std::tuple_element<C, std::tuple<Fn...>>::type get() const; }; template<typename ... Fn> @@ -137,5 +146,14 @@ namespace DB { }; } +namespace std { + template<typename ... Fn> struct tuple_size<DB::Row<Fn...>> { + static constexpr auto value = sizeof...(Fn); + }; + template<size_t C, typename ... Fn> struct tuple_element<C, DB::Row<Fn...>> { + typedef typename std::tuple_element<C, std::tuple<Fn...>>::type type; + }; +} + #endif diff --git a/libdbpp/selectcommandUtil.impl.h b/libdbpp/selectcommandUtil.impl.h index 6446de0..a6d17ca 100644 --- a/libdbpp/selectcommandUtil.impl.h +++ b/libdbpp/selectcommandUtil.impl.h @@ -93,6 +93,13 @@ namespace DB { template<unsigned int C> inline typename std::tuple_element<C, std::tuple<Fn...>>::type Row<Fn...>::value() const { + return get<C>(); + } + + template<typename ... Fn> + template<unsigned int C> + inline typename std::tuple_element<C, std::tuple<Fn...>>::type Row<Fn...>::get() const + { typename std::tuple_element<C, std::tuple<Fn...>>::type a; sel->operator[](C) >> a; return a; diff --git a/libdbpp/unittests/libdbpp-postgresql b/libdbpp/unittests/libdbpp-postgresql -Subproject f6686d1bf35611b779189f80b46079070809489 +Subproject a4c6328efab2e614a46b4a1c78fb0c5678c3fd7 diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index 04fad4f..30d5bc4 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -65,9 +65,27 @@ BOOST_AUTO_TEST_CASE( stdforOverRows ) count += 1; BOOST_REQUIRE_EQUAL("a", row[0].name); BOOST_REQUIRE_EQUAL(1, row["c"].colNo); + // Test old function int64_t a = row.value<0>(); totalOfa += a; - totalOfc += row.value<1>(); + totalOfc += row.get<1>(); + } + BOOST_REQUIRE_EQUAL(count, 2); + BOOST_REQUIRE_EQUAL(totalOfa, 3); + BOOST_REQUIRE_EQUAL(totalOfc, "Some textSome text"); +} + +BOOST_AUTO_TEST_CASE( stdforOverRowsStructuredBinding ) +{ + auto db = DB::MockDatabase::openConnectionTo("pqmock"); + unsigned int count = 0; + int64_t totalOfa = 0; + std::string totalOfc; + auto sel = db->select("SELECT a, c FROM forEachRow ORDER BY a DESC"); + for (const auto [ a, c ] : sel->as<int64_t, std::string>()) { + count += 1; + totalOfa += a; + totalOfc += c; } BOOST_REQUIRE_EQUAL(count, 2); BOOST_REQUIRE_EQUAL(totalOfa, 3); @@ -262,7 +280,7 @@ BOOST_AUTO_TEST_CASE( bindIntPtr ) BOOST_AUTO_TEST_CASE( testBlobRaw ) { - DB::Blob ptr(this, 1); + DB::Blob ptr(this, 1); BOOST_REQUIRE_EQUAL(ptr.data, this); BOOST_REQUIRE_EQUAL(ptr.len, 1); } |