summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2017-06-05 15:17:43 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2017-06-05 15:17:43 +0100
commit53d2bcd9c054723269b3334c9f960991a8c6e1dc (patch)
treed9002753ea1f75d730f65b99c7cfcfb36ebbc7ae
parentFix HEAD commit for libdbpp-postgresql (diff)
downloadlibdbpp-53d2bcd9c054723269b3334c9f960991a8c6e1dc.tar.bz2
libdbpp-53d2bcd9c054723269b3334c9f960991a8c6e1dc.tar.xz
libdbpp-53d2bcd9c054723269b3334c9f960991a8c6e1dc.zip
Allow default constructor of Blob to support extract, increase compile time testing of extractability
-rw-r--r--libdbpp/testCore.cpp17
-rw-r--r--libdbpp/types.cpp6
-rw-r--r--libdbpp/types.h2
-rw-r--r--libdbpp/unittests/testUtils.cpp31
4 files changed, 56 insertions, 0 deletions
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 <boost/date_time/posix_time/posix_time.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/utility/enable_if.hpp>
+#include <compileTimeFormatter.h>
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<bool>(SelectCommand &, const bool &) const;
template void TestCore::assertScalarValueHelper<int64_t>(SelectCommand &, const int64_t &) const;
template void TestCore::assertScalarValueHelper<int>(SelectCommand &, const int &) const;
@@ -69,6 +84,7 @@ template void TestCore::assertScalarValueHelper<double>(SelectCommand &, const d
template void TestCore::assertScalarValueHelper<std::string>(SelectCommand &, const std::string &) const;
template void TestCore::assertScalarValueHelper<boost::posix_time::ptime>(SelectCommand &, const boost::posix_time::ptime &) const;
template void TestCore::assertScalarValueHelper<boost::posix_time::time_duration>(SelectCommand &, const boost::posix_time::time_duration &) const;
+template void TestCore::assertScalarValueHelper<DB::Blob>(SelectCommand &, const DB::Blob &) const;
template void TestCore::assertColumnValueHelper<bool>(SelectCommand &, unsigned int, const bool &) const;
template void TestCore::assertColumnValueHelper<int>(SelectCommand &, unsigned int, const int &) const;
@@ -77,6 +93,7 @@ template void TestCore::assertColumnValueHelper<double>(SelectCommand &, unsigne
template void TestCore::assertColumnValueHelper<std::string>(SelectCommand &, unsigned int, const std::string &) const;
template void TestCore::assertColumnValueHelper<boost::posix_time::ptime>(SelectCommand &, unsigned int, const boost::posix_time::ptime &) const;
template void TestCore::assertColumnValueHelper<boost::posix_time::time_duration>(SelectCommand &, unsigned int, const boost::posix_time::time_duration &) const;
+template void TestCore::assertColumnValueHelper<DB::Blob>(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<typename T>
+void
+testExtractT(DB::Row<T> row) {
+ row.template value<0>();
+}
+
+template<typename T>
+void
+testExtractT(DB::SelectCommandPtr sel) {
+ for (const auto & row : sel->as<T>()) { testExtractT(row); }
+ for (const auto & row : sel->as<boost::optional<T>>()) { 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<int8_t>(sel);
+ // testExtractT<int16_t>(sel);
+ // testExtractT<int32_t>(sel);
+ testExtractT<int64_t>(sel);
+ testExtractT<bool>(sel);
+ testExtractT<std::string>(sel);
+ // testExtractT<float>(sel);
+ testExtractT<double>(sel);
+ testExtractT<boost::posix_time::ptime>(sel);
+ testExtractT<boost::posix_time::time_duration>(sel);
+ testExtractT<DB::Blob>(sel);
+}
+