From 63b2ca0dbdae190941d60a55c9cff99d4a75a0e1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 31 May 2021 13:18:17 +0100 Subject: Initial commit of prepstmt, selects, record sets This is full of holes, but all the basics are covered. --- lib/input/mysqlConn.cpp | 73 +++++++------------------------------------------ 1 file changed, 10 insertions(+), 63 deletions(-) (limited to 'lib/input/mysqlConn.cpp') diff --git a/lib/input/mysqlConn.cpp b/lib/input/mysqlConn.cpp index 179f9d5..46ed7c6 100644 --- a/lib/input/mysqlConn.cpp +++ b/lib/input/mysqlConn.cpp @@ -1,18 +1,17 @@ #include "mysqlConn.h" -#include "helpers.h" +#include "mysqlBindings.h" +#include "mysqlStmt.h" #include #include +#include #include +#include #include #include -#include #include -#include #include namespace MyGrate::Input { - using StmtPtr = std::unique_ptr; - MySQLConn::MySQLConn( const char * const host, const char * const user, const char * const pass, unsigned short port) : st_mysql {} @@ -36,64 +35,6 @@ namespace MyGrate::Input { verify(!mysql_query(this, q), q); } - struct Bindings { - // NOLINTNEXTLINE(hicpp-explicit-conversions) - explicit Bindings(const std::initializer_list & vs) - { - binds.reserve(vs.size()); - extras.reserve(vs.size()); - for (const auto & v : vs) { - std::visit(*this, v); - } - } - template - void - operator()(const T & v) - { - auto & b = binds.emplace_back(); - b.buffer_type = MySQL::CType::type; - b.buffer = const_cast(&v); - b.is_unsigned = std::unsigned_integral; - } - template - void - operator()(const T & v) - { - auto & b = binds.emplace_back(); - b.buffer_type = MySQL::CType::type; - b.buffer = const_cast(&v); - } - template - void - operator()(const T & v) - { - auto & b = binds.emplace_back(); - b.buffer_type = MySQL::CType::type; - b.buffer = const_cast(v.data()); - b.length = &extras.emplace_back(v.size(), 0).len; - } - void - operator()(const std::nullptr_t &) - { - auto & b = binds.emplace_back(); - b.buffer = nullptr; - b.is_null = &extras.emplace_back(0, 1).null; - } - template - void - operator()(const T &) - { - throw std::runtime_error("Not implemented"); - } - struct extra { - explicit extra(unsigned long l, my_bool n = 0) : len {l}, null {n} { } - unsigned long len; - my_bool null; - }; - std::vector binds; - std::vector extras; - }; - void MySQLConn::query(const char * const q, const std::initializer_list & vs) { @@ -104,4 +45,10 @@ namespace MyGrate::Input { verify(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch"); verify(!mysql_stmt_execute(stmt.get()), q); } + + DbPrepStmtPtr + MySQLConn::prepare(const char * const q, std::size_t) + { + return std::make_unique(q, this); + } } -- cgit v1.2.3