summaryrefslogtreecommitdiff
path: root/lib/input/mysqlConn.cpp
blob: 99c28917ee672dc68844700c2fc85900da7aff67 (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
#include "mysqlConn.h"
#include "mysqlBindings.h"
#include "mysqlStmt.h"
#include <cstddef>
#include <cstring>
#include <dbConn.h>
#include <helpers.h>
#include <memory>
#include <mysql.h>
#include <stdexcept>
#include <vector>

namespace MyGrate::Input {
	MySQLErr::MySQLErr(const std::string & when, MYSQL * c) : std::runtime_error(when + ": " + mysql_error(c)) { }

	MySQLConn::MySQLConn(const char * const host, const char * const user, const char * const pass, unsigned short port,
			const char * const db) :
		st_mysql {}
	{
		mysql_init(this);
		if (!mysql_real_connect(this, host, user, pass, db, port, nullptr, 0)) {
			mysql_close(this);
			throw MySQLErr("Connecting", this);
		}
		verify<MySQLErr>(!mysql_set_character_set(this, "utf8"), "Setting char set", this);
	}

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

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

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

	DbPrepStmtPtr
	MySQLConn::prepare(const char * const q, std::size_t)
	{
		return std::make_unique<MySQLPrepStmt>(q, this);
	}

	void
	MySQLConn::beginTx()
	{
		verify<MySQLErr>(!mysql_autocommit(this, false), "Auto commit off", this);
	}

	void
	MySQLConn::commitTx()
	{
		verify<MySQLErr>(!mysql_commit(this), "Commit", this);
		verify<MySQLErr>(!mysql_autocommit(this, true), "Auto commit on", this);
	}

	void
	MySQLConn::rollbackTx()
	{
		verify<MySQLErr>(!mysql_rollback(this), "Rollback", this);
		verify<MySQLErr>(!mysql_autocommit(this, true), "Auto commit on", this);
	}
}