summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-06-03 00:17:28 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2021-06-03 00:17:39 +0100
commit6e726340e23a97480b17065a633c36a9ba53cf14 (patch)
treecd9f161a542f85e5cae9119e43c870651431d0ca
parentAdd event source ptr typedef (diff)
downloadmygrate-6e726340e23a97480b17065a633c36a9ba53cf14.tar.bz2
mygrate-6e726340e23a97480b17065a633c36a9ba53cf14.tar.xz
mygrate-6e726340e23a97480b17065a633c36a9ba53cf14.zip
Determine parameter mode at point of call by DB type property
It's still constexpr, so that's all good.
-rw-r--r--lib/dbConn.h2
-rw-r--r--lib/dbStmt.h17
-rw-r--r--lib/input/mysqlConn.h2
-rw-r--r--lib/output/pq/pqConn.h2
-rw-r--r--test/test-mysql.cpp7
-rw-r--r--test/test-postgresql.cpp18
6 files changed, 27 insertions, 21 deletions
diff --git a/lib/dbConn.h b/lib/dbConn.h
index e4b056c..3f14dcb 100644
--- a/lib/dbConn.h
+++ b/lib/dbConn.h
@@ -15,6 +15,8 @@ namespace MyGrate {
};
using DbPrepStmtPtr = std::unique_ptr<DbPrepStmt>;
+ enum class ParamMode { None, DollarNum, QMark };
+
class DbConn {
public:
virtual ~DbConn() = default;
diff --git a/lib/dbStmt.h b/lib/dbStmt.h
index 81621de..80dbd71 100644
--- a/lib/dbStmt.h
+++ b/lib/dbStmt.h
@@ -9,17 +9,16 @@
#include <type_traits>
namespace MyGrate {
- class DbConn;
- enum class ParamMode { None, DollarNum, QMark };
-
- template<Support::basic_fixed_string S, ParamMode pm = ParamMode::None> class DbStmt {
+ template<Support::basic_fixed_string S> class DbStmt {
public:
// This don't account for common table expressions, hopefully won't need those :)
static constexpr auto isSelect {S.v().starts_with("SELECT") || S.v().starts_with("SHOW")
|| S.v().find("RETURNING") != std::string_view::npos};
// These don't account for string literals, which we'd prefer to avoid anyway :)
- static constexpr auto paramCount {[]() -> std::size_t {
+ static constexpr std::size_t
+ paramCount(ParamMode pm)
+ {
switch (pm) {
case ParamMode::None:
return 0LU;
@@ -42,15 +41,15 @@ namespace MyGrate {
return c == '?';
});
}
- }()};
+ }
using Return = std::conditional_t<isSelect, RecordSetPtr, std::size_t>;
- template<typename... P>
+ template<typename ConnType, typename... P>
static Return
- execute(DbConn * c, P &&... p)
+ execute(ConnType * c, P &&... p)
{
- static_assert(sizeof...(P) == paramCount);
+ static_assert(sizeof...(P) == paramCount(ConnType::paramMode));
auto stmt {c->prepare(S, sizeof...(P))};
stmt->execute({std::forward<P...>(p)...});
if constexpr (isSelect) {
diff --git a/lib/input/mysqlConn.h b/lib/input/mysqlConn.h
index 2f71262..29a34db 100644
--- a/lib/input/mysqlConn.h
+++ b/lib/input/mysqlConn.h
@@ -10,6 +10,8 @@
namespace MyGrate::Input {
class MySQLConn : public MYSQL, public DbConn {
public:
+ static constexpr auto paramMode {ParamMode::QMark};
+
MySQLConn(const char * const host, const char * const user, const char * const pass, unsigned short port);
~MySQLConn();
diff --git a/lib/output/pq/pqConn.h b/lib/output/pq/pqConn.h
index 856683d..ae5fc9a 100644
--- a/lib/output/pq/pqConn.h
+++ b/lib/output/pq/pqConn.h
@@ -14,6 +14,8 @@
namespace MyGrate::Output::Pq {
class PqConn : public DbConn {
public:
+ static constexpr auto paramMode {ParamMode::DollarNum};
+
explicit PqConn(const char * const str);
virtual ~PqConn() = default;
diff --git a/test/test-mysql.cpp b/test/test-mysql.cpp
index f5281a9..493ec02 100644
--- a/test/test-mysql.cpp
+++ b/test/test-mysql.cpp
@@ -4,6 +4,7 @@
#include <cstddef>
#include <cstdint>
#include <cstdlib>
+#include <dbConn.h>
#include <dbRecordSet.h>
#include <dbStmt.h>
#include <dbTypes.h>
@@ -42,13 +43,13 @@ BOOST_AUTO_TEST_CASE(simple)
using SomeSelect = MyGrate::DbStmt<"SELECT * FROM foo">;
using SomeShow = MyGrate::DbStmt<"SHOW MASTER STATUS">;
-using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = ? WHERE bar = ?", MyGrate::ParamMode::QMark>;
+using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = ? WHERE bar = ?">;
static_assert(std::is_same_v<SomeSelect::Return, MyGrate::RecordSetPtr>);
static_assert(std::is_same_v<SomeShow::Return, MyGrate::RecordSetPtr>);
static_assert(std::is_same_v<SomeUpdate::Return, std::size_t>);
-static_assert(SomeShow::paramCount == 0);
-static_assert(SomeUpdate::paramCount == 2);
+static_assert(SomeShow::paramCount(MyGrate::ParamMode::QMark) == 0);
+static_assert(SomeUpdate::paramCount(MyGrate::ParamMode::QMark) == 2);
BOOST_AUTO_TEST_CASE(stmt)
{
diff --git a/test/test-postgresql.cpp b/test/test-postgresql.cpp
index a24acb8..3a12f43 100644
--- a/test/test-postgresql.cpp
+++ b/test/test-postgresql.cpp
@@ -2,6 +2,7 @@
#include <boost/test/unit_test.hpp>
#include <cstddef>
+#include <dbConn.h>
#include <dbRecordSet.h>
#include <dbStmt.h>
#include <dbTypes.h>
@@ -38,18 +39,17 @@ BOOST_AUTO_TEST_CASE(simple)
c.query("DROP TABLE test");
}
-using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1", MyGrate::ParamMode::DollarNum>;
-using SomeUpdateRtn
- = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1 RETURNING x", MyGrate::ParamMode::DollarNum>;
-using SomeShow = MyGrate::DbStmt<"SHOW all", MyGrate::ParamMode::DollarNum>;
-using SomeIns = MyGrate::DbStmt<"INSERT INTO foo VALUES(..., $87)", MyGrate::ParamMode::DollarNum>;
+using SomeUpdate = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1">;
+using SomeUpdateRtn = MyGrate::DbStmt<"UPDATE foo SET blah = $2 WHERE bar = $1 RETURNING x">;
+using SomeShow = MyGrate::DbStmt<"SHOW all">;
+using SomeIns = MyGrate::DbStmt<"INSERT INTO foo VALUES(..., $87)">;
-static_assert(SomeUpdate::paramCount == 2);
+static_assert(SomeUpdate::paramCount(MyGrate::ParamMode::DollarNum) == 2);
static_assert(std::is_same_v<SomeUpdate::Return, std::size_t>);
-static_assert(SomeUpdateRtn::paramCount == 2);
+static_assert(SomeUpdateRtn::paramCount(MyGrate::ParamMode::DollarNum) == 2);
static_assert(std::is_same_v<SomeUpdateRtn::Return, MyGrate::RecordSetPtr>);
-static_assert(SomeShow::paramCount == 0);
-static_assert(SomeIns::paramCount == 87);
+static_assert(SomeShow::paramCount(MyGrate::ParamMode::DollarNum) == 0);
+static_assert(SomeIns::paramCount(MyGrate::ParamMode::DollarNum) == 87);
static_assert(std::is_same_v<SomeIns::Return, std::size_t>);
BOOST_AUTO_TEST_CASE(stmt)