From 9df34de64c7cd3a69380265786efa3d0068b7ba9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 8 Jan 2017 16:14:16 +0000 Subject: Implement getting PQ command option settings from map --- libdbpp/command.cpp | 8 +++++++- libdbpp/command.h | 13 +++++++++++++ libdbpp/unittests/testConnection.cpp | 25 ++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/libdbpp/command.cpp b/libdbpp/command.cpp index a687ccc..8957858 100644 --- a/libdbpp/command.cpp +++ b/libdbpp/command.cpp @@ -4,7 +4,7 @@ INSTANTIATEFACTORY(DB::CommandOptions, std::size_t, const DB::CommandOptionsMap &); NAMEDFACTORY("", DB::CommandOptions, DB::CommandOptionsFactory); -PLUGINRESOLVER(DB::CommandOptions, DB::Connection::resolvePlugin); +PLUGINRESOLVER(DB::CommandOptionsFactory, DB::Connection::resolvePlugin); DB::Command::Command(const std::string & s) : sql(s) @@ -28,6 +28,12 @@ DB::CommandOptions::CommandOptions(std::size_t h, const CommandOptionsMap &) : { } +bool +DB::CommandOptions::isSet(const CommandOptionsMap & map, const std::string & key) +{ + return (map.find(key) != map.end()); +} + void DB::Command::bindParamS(unsigned int i, const char * const o) { diff --git a/libdbpp/command.h b/libdbpp/command.h index aaadfa9..ba51031 100644 --- a/libdbpp/command.h +++ b/libdbpp/command.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -34,6 +35,18 @@ namespace DB { /// An (optional) hash of the SQL statement. boost::optional hash; + + protected: + template + static X get(const CommandOptionsMap & map, const std::string & key, const X & def) + { + auto i = map.find(key); + if (i != map.end()) { + return boost::lexical_cast(i->second); + } + return def; + } + static bool isSet(const CommandOptionsMap & map, const std::string & key); }; /// Represents the basics of any command to be executed against a database. diff --git a/libdbpp/unittests/testConnection.cpp b/libdbpp/unittests/testConnection.cpp index a1ab9f6..066b4ad 100644 --- a/libdbpp/unittests/testConnection.cpp +++ b/libdbpp/unittests/testConnection.cpp @@ -171,13 +171,32 @@ BOOST_AUTO_TEST_CASE( commandOptions ) BOOST_REQUIRE_EQUAL(1234, *optsDefault->hash); } -BOOST_AUTO_TEST_CASE( commandOptionsPq ) +BOOST_AUTO_TEST_CASE( commandOptionsPq1 ) { - auto optsBase = DB::CommandOptionsFactory::createNew("postgresql", 1234, {}); + auto optsBase = DB::CommandOptionsFactory::createNew("postgresql", 12345, { + {"no-cursor", ""}, + {"page-size", "5"} + }); BOOST_REQUIRE(optsBase); auto optsPq = dynamic_cast(optsBase); BOOST_REQUIRE(optsPq); BOOST_REQUIRE(optsBase->hash); - BOOST_REQUIRE_EQUAL(1234, *optsBase->hash); + BOOST_REQUIRE_EQUAL(12345, *optsBase->hash); + BOOST_REQUIRE(!optsPq->useCursor); + BOOST_REQUIRE_EQUAL(5, optsPq->fetchTuples); +} + +BOOST_AUTO_TEST_CASE( commandOptionsPq2 ) +{ + auto optsBase = DB::CommandOptionsFactory::createNew("postgresql", 123456, { + {"page-size", "50"} + }); + BOOST_REQUIRE(optsBase); + auto optsPq = dynamic_cast(optsBase); + BOOST_REQUIRE(optsPq); + BOOST_REQUIRE(optsBase->hash); + BOOST_REQUIRE_EQUAL(123456, *optsBase->hash); + BOOST_REQUIRE(optsPq->useCursor); + BOOST_REQUIRE_EQUAL(50, optsPq->fetchTuples); } -- cgit v1.2.3