From 0d725db8dc039941d43d6e2f73227c811cbc4d68 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 24 May 2020 11:26:20 +0100 Subject: Improve and test support for traits binding of blobs --- libdbpp/command.h | 2 +- libdbpp/unittests/testUtils.cpp | 75 ++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/libdbpp/command.h b/libdbpp/command.h index f080121..34338eb 100644 --- a/libdbpp/command.h +++ b/libdbpp/command.h @@ -127,7 +127,7 @@ namespace DB { std::is_same::value) { bindParamT(i, o); } - else if constexpr (std::is_same::value) { + else if constexpr (std::is_same::value || std::is_convertible::value) { bindParamBLOB(i, o); } else if constexpr (std::is_integral::value && !std::is_pointer::value) { diff --git a/libdbpp/unittests/testUtils.cpp b/libdbpp/unittests/testUtils.cpp index eed1322..9ceef54 100644 --- a/libdbpp/unittests/testUtils.cpp +++ b/libdbpp/unittests/testUtils.cpp @@ -303,50 +303,57 @@ BOOST_AUTO_TEST_CASE( bindIntPtr ) BOOST_FIXTURE_TEST_CASE( traits_bind, DB::TestCore ) { + using namespace std::literals; auto db = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("pqmock")); auto cmd = db->select("select x is null, format('%s', x) from (select ? x) d"); - auto check = [cmd](int line, const auto & v, bool isNull, const auto & strval) { + auto cmd2 = db->select("select x is null, x from (select ?::bytea x) d"); + auto check = [](int line, const auto & v, bool isNull, const auto & testval, auto & cmd) { BOOST_TEST_CONTEXT("line " << line) { - cmd->bindParam(0, v); - for (const auto & [null, str] : cmd->as()) { + BOOST_REQUIRE_NO_THROW(cmd->bindParam(0, v)); + for (const auto & [null, dbval] : cmd->template as>()) { BOOST_CHECK_EQUAL(null, isNull); - BOOST_CHECK_EQUAL(str, strval); + BOOST_CHECK_EQUAL(dbval, testval); } } }; - check(__LINE__, std::nullopt, true, ""); - check(__LINE__, nullptr, true, ""); - check(__LINE__, testInt, false, "43"); - check(__LINE__, std::make_unique(testInt), false, "43"); - check(__LINE__, std::unique_ptr(), true, ""); - check(__LINE__, (unsigned int)testInt, false, "43"); - check(__LINE__, &testInt, false, "43"); - check(__LINE__, testDouble, false, "3.14"); - check(__LINE__, std::make_shared(testDouble), false, "3.14"); - check(__LINE__, std::shared_ptr(), true, ""); - check(__LINE__, (float)testDouble, false, "3.14"); - check(__LINE__, &testDouble, false, "3.14"); - check(__LINE__, testString, false, testString); - check(__LINE__, &testString, false, testString); - check(__LINE__, "str", false, "str"); - check(__LINE__, "", false, ""); + check(__LINE__, std::nullopt, true, ""sv, cmd); + check(__LINE__, nullptr, true, ""sv, cmd); + check(__LINE__, testInt, false, "43"sv, cmd); + check(__LINE__, std::make_unique(testInt), false, "43"sv, cmd); + check(__LINE__, std::unique_ptr(), true, ""sv, cmd); + check(__LINE__, (unsigned int)testInt, false, "43"sv, cmd); + check(__LINE__, &testInt, false, "43"sv, cmd); + check(__LINE__, testDouble, false, "3.14"sv, cmd); + check(__LINE__, std::make_shared(testDouble), false, "3.14"sv, cmd); + check(__LINE__, std::shared_ptr(), true, ""sv, cmd); + check(__LINE__, (float)testDouble, false, "3.14"sv, cmd); + check(__LINE__, &testDouble, false, "3.14"sv, cmd); + check(__LINE__, testString, false, testString, cmd); + check(__LINE__, &testString, false, testString, cmd); + check(__LINE__, "str", false, "str"sv, cmd); + check(__LINE__, "", false, ""sv, cmd); const char * const nullstr = nullptr; - check(__LINE__, nullstr, true, ""); + check(__LINE__, nullstr, true, ""sv, cmd); const char * const str = "str"; - check(__LINE__, str, false, "str"); - check(__LINE__, std::string(), false, ""); - check(__LINE__, std::string_view(), false, ""); - check(__LINE__, Glib::ustring(), false, ""); - check(__LINE__, Glib::ustring("foo"), false, "foo"); - check(__LINE__, testDateTime, false, "2015-05-02T01:36:33"); - check(__LINE__, testInterval, false, "01:02:03"); - check(__LINE__, &testInterval, false, "01:02:03"); + check(__LINE__, str, false, "str"sv, cmd); + check(__LINE__, std::string(), false, ""sv, cmd); + check(__LINE__, std::string_view(), false, ""sv, cmd); + check(__LINE__, Glib::ustring(), false, ""sv, cmd); + check(__LINE__, Glib::ustring("foo"), false, "foo"sv, cmd); + check(__LINE__, testDateTime, false, "2015-05-02T01:36:33"sv, cmd); + check(__LINE__, testInterval, false, "01:02:03"sv, cmd); + check(__LINE__, &testInterval, false, "01:02:03"sv, cmd); decltype(testInterval) * nullInterval = nullptr; - check(__LINE__, nullInterval, true, ""); - check(__LINE__, true, false, "1"); - check(__LINE__, false, false, "0"); - check(__LINE__, std::optional(4), false, "4"); - check(__LINE__, std::optional(), true, ""); + check(__LINE__, nullInterval, true, ""sv, cmd); + check(__LINE__, true, false, "1"sv, cmd); + check(__LINE__, false, false, "0"sv, cmd); + check(__LINE__, std::optional(4), false, "4"sv, cmd); + check(__LINE__, std::optional(), true, ""sv, cmd); + check(__LINE__, testBlob, false, testBlob, cmd2); + check(__LINE__, testBlobData, false, testBlob, cmd2); + check(__LINE__, std::shared_ptr(), true, ""sv, cmd); + check(__LINE__, std::optional(), true, ""sv, cmd); + check(__LINE__, std::optional(testBlob), false, testBlob, cmd2); } BOOST_AUTO_TEST_CASE( testBlobRaw ) -- cgit v1.2.3