From 9b4e28d4f8dcb5fd93005c972ed46ce59a8ce980 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Jan 2017 15:38:38 +0000 Subject: Add the DB command options factory --- libdbpp/command.cpp | 8 +++++++- libdbpp/command.h | 6 +++++- libdbpp/unittests/Jamfile.jam | 1 + libdbpp/unittests/testConnection.cpp | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/libdbpp/command.cpp b/libdbpp/command.cpp index c6d7a7c..a687ccc 100644 --- a/libdbpp/command.cpp +++ b/libdbpp/command.cpp @@ -1,4 +1,10 @@ #include "command.h" +#include "connection.h" +#include + +INSTANTIATEFACTORY(DB::CommandOptions, std::size_t, const DB::CommandOptionsMap &); +NAMEDFACTORY("", DB::CommandOptions, DB::CommandOptionsFactory); +PLUGINRESOLVER(DB::CommandOptions, DB::Connection::resolvePlugin); DB::Command::Command(const std::string & s) : sql(s) @@ -17,7 +23,7 @@ DB::ParameterOutOfRange::ParameterOutOfRange() { } -DB::CommandOptions::CommandOptions(std::size_t h) : +DB::CommandOptions::CommandOptions(std::size_t h, const CommandOptionsMap &) : hash(h) { } diff --git a/libdbpp/command.h b/libdbpp/command.h index 2a7f9ae..aaadfa9 100644 --- a/libdbpp/command.h +++ b/libdbpp/command.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "error.h" @@ -21,12 +22,14 @@ namespace DB { ParameterOutOfRange(); }; + typedef std::map CommandOptionsMap; + /// Represents the basic options that can be passed when creating new commands. class DLL_PUBLIC CommandOptions { public: CommandOptions() = default; /// Constructor which populates the hash value only. - CommandOptions(std::size_t hash); + CommandOptions(std::size_t hash, const CommandOptionsMap & = CommandOptionsMap()); virtual ~CommandOptions() = default; /// An (optional) hash of the SQL statement. @@ -103,6 +106,7 @@ namespace DB { void bindParamS(unsigned int, char * const); }; typedef boost::shared_ptr CommandPtr; + typedef AdHoc::Factory CommandOptionsFactory; } #endif diff --git a/libdbpp/unittests/Jamfile.jam b/libdbpp/unittests/Jamfile.jam index 54c4afc..5aec066 100644 --- a/libdbpp/unittests/Jamfile.jam +++ b/libdbpp/unittests/Jamfile.jam @@ -16,6 +16,7 @@ run ..//dbppcore ..//adhocutil boost_utf + dbpp-local-postgresql parseTest.sql unterminatedComment.sql unterminatedString.sql diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index f1df3b0..a1ab9f6 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -162,3 +163,21 @@ BOOST_AUTO_TEST_CASE( savepoints ) delete mock; } +BOOST_AUTO_TEST_CASE( commandOptions ) +{ + auto optsDefault = DB::CommandOptionsFactory::createNew("", 1234, {}); + BOOST_REQUIRE(optsDefault); + BOOST_REQUIRE(optsDefault->hash); + BOOST_REQUIRE_EQUAL(1234, *optsDefault->hash); +} + +BOOST_AUTO_TEST_CASE( commandOptionsPq ) +{ + auto optsBase = DB::CommandOptionsFactory::createNew("postgresql", 1234, {}); + BOOST_REQUIRE(optsBase); + auto optsPq = dynamic_cast(optsBase); + BOOST_REQUIRE(optsPq); + BOOST_REQUIRE(optsBase->hash); + BOOST_REQUIRE_EQUAL(1234, *optsBase->hash); +} + -- cgit v1.2.3