summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-07-13 20:18:09 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-07-14 01:33:36 +0100
commit964f7ec93c2b01aa2c3d171f01210df742dd0724 (patch)
treeb59e5959247821c290de88a9827485770a5df93d
parentSupress bad cppcheck suggestion (diff)
downloadmygrate-964f7ec93c2b01aa2c3d171f01210df742dd0724.tar.bz2
mygrate-964f7ec93c2b01aa2c3d171f01210df742dd0724.tar.xz
mygrate-964f7ec93c2b01aa2c3d171f01210df742dd0724.zip
Wider type support for PQ bindings
timespec not supported... honestly not sure what it should bind!
-rw-r--r--lib/output/pq/pqBindings.h28
-rw-r--r--lib/output/pq/pqStmt.cpp3
-rw-r--r--test/test-postgresql.cpp2
3 files changed, 26 insertions, 7 deletions
diff --git a/lib/output/pq/pqBindings.h b/lib/output/pq/pqBindings.h
index 5df1a34..13d0f69 100644
--- a/lib/output/pq/pqBindings.h
+++ b/lib/output/pq/pqBindings.h
@@ -1,9 +1,11 @@
#ifndef MYGRATE_OUTPUT_PQ_PQBINDINGS
#define MYGRATE_OUTPUT_PQ_PQBINDINGS
+#include <compileTimeFormatter.h>
#include <dbTypes.h>
#include <helpers.h>
#include <initializer_list>
+#include <streamSupport.h>
#include <variant>
#include <vector>
@@ -15,6 +17,7 @@ namespace MyGrate::Output::Pq {
bufs.reserve(vs.size());
values.reserve(vs.size());
lengths.reserve(vs.size());
+ formats.reserve(vs.size());
for (const auto & v : vs) {
v.visit(*this);
}
@@ -23,10 +26,7 @@ namespace MyGrate::Output::Pq {
void
operator()(const T & v)
{
- bufs.emplace_back(std::to_string(v));
- const auto & vw {bufs.back()};
- values.emplace_back(vw.data());
- lengths.emplace_back(vw.length());
+ addBuf(std::to_string(v));
}
template<Viewable T>
void
@@ -34,10 +34,16 @@ namespace MyGrate::Output::Pq {
{
values.emplace_back(v.data());
lengths.emplace_back(v.size());
+ formats.push_back(1);
}
template<typename T>
void
- operator()(const T &)
+ operator()(const T & v)
+ {
+ addBuf(scprintf<"%?">(v));
+ }
+ void
+ operator()(const timespec &)
{
throw std::logic_error("Not implemented");
}
@@ -46,11 +52,23 @@ namespace MyGrate::Output::Pq {
{
values.emplace_back(nullptr);
lengths.emplace_back(0);
+ formats.push_back(0);
}
std::vector<std::string> bufs;
std::vector<const char *> values;
std::vector<int> lengths;
+ std::vector<int> formats;
+
+ private:
+ void
+ addBuf(std::string str)
+ {
+ const auto & vw = bufs.emplace_back(std::move(str));
+ values.emplace_back(vw.data());
+ lengths.emplace_back(vw.length());
+ formats.push_back(0);
+ }
};
}
diff --git a/lib/output/pq/pqStmt.cpp b/lib/output/pq/pqStmt.cpp
index 2265f64..7e085b5 100644
--- a/lib/output/pq/pqStmt.cpp
+++ b/lib/output/pq/pqStmt.cpp
@@ -22,7 +22,8 @@ namespace MyGrate::Output::Pq {
PqPrepStmt::execute(const std::initializer_list<DbValue> & vs)
{
Bindings b {vs};
- res = {PQexecPrepared(conn, name.c_str(), (int)vs.size(), b.values.data(), b.lengths.data(), nullptr, 0),
+ res = {PQexecPrepared(
+ conn, name.c_str(), (int)vs.size(), b.values.data(), b.lengths.data(), b.formats.data(), 0),
&PQclear};
verify<PqErr>(PQresultStatus(res.get()) == PGRES_COMMAND_OK || PQresultStatus(res.get()) == PGRES_TUPLES_OK,
name, conn);
diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp
index 8adb920..15ba789 100644
--- a/test/test-postgresql.cpp
+++ b/test/test-postgresql.cpp
@@ -79,7 +79,7 @@ BOOST_AUTO_TEST_CASE(mock)
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {"string_view"}));
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {nullptr}));
BOOST_CHECK_NO_THROW(mdb.query("INSERT INTO test VALUES($1)", {1.2}));
- BOOST_CHECK_THROW(mdb.query("INSERT INTO test VALUES($1)", {MyGrate::Time {}}), std::logic_error);
+ BOOST_CHECK_THROW(mdb.query("INSERT INTO test VALUES($1)", {timespec {}}), std::logic_error);
auto rscount = MyGrate::DbStmt<"SELECT COUNT(*) FROM test">::execute(&mdb);
BOOST_CHECK_EQUAL(rscount->at(0, 0).operator unsigned int(), 4);
}