From 53d2bcd9c054723269b3334c9f960991a8c6e1dc Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 5 Jun 2017 15:17:43 +0100 Subject: Allow default constructor of Blob to support extract, increase compile time testing of extractability --- libdbpp/testCore.cpp | 17 +++++++++++++++++ libdbpp/types.cpp | 6 ++++++ libdbpp/types.h | 2 ++ libdbpp/unittests/testUtils.cpp | 31 +++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/libdbpp/testCore.cpp b/libdbpp/testCore.cpp index 08b0e99..3c71daa 100644 --- a/libdbpp/testCore.cpp +++ b/libdbpp/testCore.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace DB { @@ -62,6 +63,20 @@ TestCore::assertColumnValueHelper(DB::SelectCommand & sel, unsigned int col, con sel[col].apply(a); } +bool +operator==(const DB::Blob a, const DB::Blob b) +{ + return a.len == b.len && memcmp(a.data, b.data, a.len) == 0; +} + +AdHocFormatter(BlobDbg, "Blob[length=%?, addr=%?]"); +std::ostream & +operator<<(std::ostream & s, const DB::Blob b) +{ + BlobDbg::write(s, b.len, b.data); + return s; +} + template void TestCore::assertScalarValueHelper(SelectCommand &, const bool &) const; template void TestCore::assertScalarValueHelper(SelectCommand &, const int64_t &) const; template void TestCore::assertScalarValueHelper(SelectCommand &, const int &) const; @@ -69,6 +84,7 @@ template void TestCore::assertScalarValueHelper(SelectCommand &, const d template void TestCore::assertScalarValueHelper(SelectCommand &, const std::string &) const; template void TestCore::assertScalarValueHelper(SelectCommand &, const boost::posix_time::ptime &) const; template void TestCore::assertScalarValueHelper(SelectCommand &, const boost::posix_time::time_duration &) const; +template void TestCore::assertScalarValueHelper(SelectCommand &, const DB::Blob &) const; template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const bool &) const; template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const int &) const; @@ -77,6 +93,7 @@ template void TestCore::assertColumnValueHelper(SelectCommand &, unsigne template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const std::string &) const; template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const boost::posix_time::ptime &) const; template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const boost::posix_time::time_duration &) const; +template void TestCore::assertColumnValueHelper(SelectCommand &, unsigned int, const DB::Blob &) const; } diff --git a/libdbpp/types.cpp b/libdbpp/types.cpp index a663200..65e8598 100644 --- a/libdbpp/types.cpp +++ b/libdbpp/types.cpp @@ -1,5 +1,11 @@ #include "types.h" +DB::Blob::Blob() : + data(nullptr), + len(0) +{ +} + DB::Blob::Blob(const void * d, size_t l) : data(d), len(l) diff --git a/libdbpp/types.h b/libdbpp/types.h index 79a828d..d42d0c0 100644 --- a/libdbpp/types.h +++ b/libdbpp/types.h @@ -9,6 +9,8 @@ namespace DB { /// Wrapper class for reference an existing block of binary data. class DLL_PUBLIC Blob { public: + /// Construct a default blob pointing to no data. + Blob(); /// Construct a reference using C-style pointer and length. Blob(const void * data, size_t len); /// Construct a reference using C++ template pointer to an object. diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index 48a2801..434a574 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -304,3 +304,34 @@ BOOST_AUTO_TEST_CASE( testBlobVecStruct ) BOOST_REQUIRE_EQUAL(vec.len, 20 * 16); } +// These just compile time support, actual data extraction should be tested by the implementing connector. +template +void +testExtractT(DB::Row row) { + row.template value<0>(); +} + +template +void +testExtractT(DB::SelectCommandPtr sel) { + for (const auto & row : sel->as()) { testExtractT(row); } + for (const auto & row : sel->as>()) { testExtractT(row); } +} + +BOOST_AUTO_TEST_CASE( testExtractTypes ) +{ + auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); + auto sel = DB::SelectCommandPtr(db->newSelectCommand("SELECT 1 FROM forEachRow LIMIT 0")); + // testExtractT(sel); + // testExtractT(sel); + // testExtractT(sel); + testExtractT(sel); + testExtractT(sel); + testExtractT(sel); + // testExtractT(sel); + testExtractT(sel); + testExtractT(sel); + testExtractT(sel); + testExtractT(sel); +} + -- cgit v1.2.3