summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libdbpp/command.cpp8
-rw-r--r--libdbpp/command.h13
-rw-r--r--libdbpp/unittests/testConnection.cpp25
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 <glibmm/ustring.h>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/shared_ptr.hpp>
+#include <boost/lexical_cast.hpp>
#include <visibility.h>
#include <factory.h>
#include <type_traits>
@@ -34,6 +35,18 @@ namespace DB {
/// An (optional) hash of the SQL statement.
boost::optional<std::size_t> hash;
+
+ protected:
+ template<typename X>
+ 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<X>(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<PQ::CommandOptions *>(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<PQ::CommandOptions *>(optsBase);
+ BOOST_REQUIRE(optsPq);
+ BOOST_REQUIRE(optsBase->hash);
+ BOOST_REQUIRE_EQUAL(123456, *optsBase->hash);
+ BOOST_REQUIRE(optsPq->useCursor);
+ BOOST_REQUIRE_EQUAL(50, optsPq->fetchTuples);
}