summaryrefslogtreecommitdiff
path: root/lib/input/mysqlConn.cpp
blob: 179f9d517b7faac37855abcca13574c5cc57760b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "mysqlConn.h"
#include "helpers.h"
#include <cstddef>
#include <cstring>
#include <dbTypes.h>
#include <memory>
#include <mysql.h>
#include <mysql_types.h>
#include <stdexcept>
#include <variant>
#include <vector>

namespace MyGrate::Input {
	using StmtPtr = std::unique_ptr<MYSQL_STMT, decltype(&mysql_stmt_close)>;

	MySQLConn::MySQLConn(
			const char * const host, const char * const user, const char * const pass, unsigned short port) :
		st_mysql {}
	{
		mysql_init(this);
		if (!mysql_real_connect(this, host, user, pass, "", port, nullptr, 0)) {
			mysql_close(this);
			throw std::runtime_error("ConnectionError");
		}
		verify<std::runtime_error>(!mysql_set_character_set(this, "utf8"), "Set character set");
	}

	MySQLConn::~MySQLConn()
	{
		mysql_close(this);
	}

	void
	MySQLConn::query(const char * const q)
	{
		verify<std::runtime_error>(!mysql_query(this, q), q);
	}

	struct Bindings {
		// NOLINTNEXTLINE(hicpp-explicit-conversions)
		explicit Bindings(const std::initializer_list<DbValue> & vs)
		{
			binds.reserve(vs.size());
			extras.reserve(vs.size());
			for (const auto & v : vs) {
				std::visit(*this, v);
			}
		}
		template<std::integral T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<T *>(&v);
			b.is_unsigned = std::unsigned_integral<T>;
		}
		template<std::floating_point T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<T *>(&v);
		}
		template<Viewable T>
		void
		operator()(const T & v)
		{
			auto & b = binds.emplace_back();
			b.buffer_type = MySQL::CType<T>::type;
			b.buffer = const_cast<typename T::value_type *>(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<typename T>
		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<MYSQL_BIND> binds;
		std::vector<extra> extras;
	};

	void
	MySQLConn::query(const char * const q, const std::initializer_list<DbValue> & vs)
	{
		StmtPtr stmt {mysql_stmt_init(this), &mysql_stmt_close};
		verify<std::runtime_error>(!mysql_stmt_prepare(stmt.get(), q, strlen(q)), q);
		verify<std::logic_error>(mysql_stmt_param_count(stmt.get()) == vs.size(), "Param count mismatch");
		Bindings b {vs};
		verify<std::runtime_error>(!mysql_stmt_bind_param(stmt.get(), b.binds.data()), "Param count mismatch");
		verify<std::runtime_error>(!mysql_stmt_execute(stmt.get()), q);
	}
}