From 6e726340e23a97480b17065a633c36a9ba53cf14 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 3 Jun 2021 00:17:28 +0100 Subject: Determine parameter mode at point of call by DB type property It's still constexpr, so that's all good. --- lib/dbConn.h | 2 ++ lib/dbStmt.h | 17 ++++++++--------- lib/input/mysqlConn.h | 2 ++ lib/output/pq/pqConn.h | 2 ++ 4 files changed, 14 insertions(+), 9 deletions(-) (limited to 'lib') 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; + 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 namespace MyGrate { - class DbConn; - enum class ParamMode { None, DollarNum, QMark }; - - template class DbStmt { + template 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; - template + template 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)...}); 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; -- cgit v1.2.3