From 53ad8ff1422266a89e667dddc0c73b2d0f91a373 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 27 Aug 2021 19:23:39 +0100 Subject: Support binding date/time values to MySQL --- lib/input/mysqlBindings.cpp | 28 +++++++++++++++++++++++++++ lib/input/mysqlBindings.h | 46 +++++++++++++++++++++++++++++++++++++++++++++ lib/mysql_types.h | 3 +++ test/test-mysql.cpp | 1 - 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 lib/input/mysqlBindings.cpp diff --git a/lib/input/mysqlBindings.cpp b/lib/input/mysqlBindings.cpp new file mode 100644 index 0000000..3895821 --- /dev/null +++ b/lib/input/mysqlBindings.cpp @@ -0,0 +1,28 @@ +#include "mysqlBindings.h" + +namespace MyGrate::Input { + MYSQL_TIME & + operator<<(MYSQL_TIME & t, const Date & dt) + { + t.year = dt.year; + t.month = dt.month; + t.day = dt.day; + return t; + } + + MYSQL_TIME & + operator<<(MYSQL_TIME & t, const Time & dt) + { + t.hour = dt.hour; + t.minute = dt.minute; + t.second = dt.second; + return t; + } + + MYSQL_TIME & + operator<<(MYSQL_TIME & t, const DateTime & dt) + { + return t << (Date)dt << (Time)dt; + } + +} diff --git a/lib/input/mysqlBindings.h b/lib/input/mysqlBindings.h index 36ef2bf..449c465 100644 --- a/lib/input/mysqlBindings.h +++ b/lib/input/mysqlBindings.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,21 @@ namespace MyGrate::Input { my_bool null; }; + MYSQL_TIME & operator<<(MYSQL_TIME & t, const Date &); + MYSQL_TIME & operator<<(MYSQL_TIME & t, const Time &); + MYSQL_TIME & operator<<(MYSQL_TIME & t, const DateTime & dt); + struct Bindings { + struct Buffer { + virtual ~Buffer() = default; + }; + template struct BufferOf : public Buffer { + template BufferOf(const S & src) : val {} + { + val << src; + } + T val; + }; explicit Bindings(const std::span vs) { binds.reserve(vs.size()); @@ -59,12 +74,43 @@ namespace MyGrate::Input { b.is_null = &data.emplace_back(0, 1).null; } void + operator()(const DateTime & dt) + { + auto & b = binds.emplace_back(); + auto t = std::make_unique>(dt); + b.buffer_type = MySQL::CType::type; + b.buffer = &t->val; + b.buffer_length = sizeof(t->val); + buffers.push_back(std::move(t)); + } + void + operator()(const Time & dt) + { + auto & b = binds.emplace_back(); + auto t = std::make_unique>(dt); + b.buffer_type = MySQL::CType