From b932b50414c806f2fa590a01ccf3cfd94d27165f Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 22 Aug 2018 19:47:53 +0100 Subject: Structured binding Replace Row::value method with one that supports structured binding of field values --- libdbpp/selectcommand.h | 20 +++++++++++++++++++- libdbpp/selectcommandUtil.impl.h | 7 +++++++ libdbpp/unittests/libdbpp-postgresql | 2 +- libdbpp/unittests/testUtils.cpp | 22 ++++++++++++++++++++-- 4 files changed, 47 insertions(+), 4 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 #include +#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 - typename std::tuple_element>::type value() const; + typename std::tuple_element>::type value() const DEPRECATE ; + + template + typename std::tuple_element>::type get() const; }; template @@ -137,5 +146,14 @@ namespace DB { }; } +namespace std { + template struct tuple_size> { + static constexpr auto value = sizeof...(Fn); + }; + template struct tuple_element> { + typedef typename std::tuple_element>::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 @@ -92,6 +92,13 @@ namespace DB { template template inline typename std::tuple_element>::type Row::value() const + { + return get(); + } + + template + template + inline typename std::tuple_element>::type Row::get() const { typename std::tuple_element>::type a; sel->operator[](C) >> a; diff --git a/libdbpp/unittests/libdbpp-postgresql b/libdbpp/unittests/libdbpp-postgresql index f6686d1..a4c6328 160000 --- a/libdbpp/unittests/libdbpp-postgresql +++ b/libdbpp/unittests/libdbpp-postgresql @@ -1 +1 @@ -Subproject commit f6686d1bf35611b779189f80b460790708094895 +Subproject commit a4c6328efab2e614a46b4a1c78fb0c5678c3fd76 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()) { + 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); } -- cgit v1.2.3