summaryrefslogtreecommitdiff
path: root/lib/dbConn.h
blob: b464e2b8d8806de73b1ee6083a2c30bf3f4d19c0 (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
#ifndef MYGRATE_DBCONN_H
#define MYGRATE_DBCONN_H

#include <dbRecordSet.h>
#include <dbTypes.h>
#include <initializer_list>

namespace MyGrate {
	class DbPrepStmt {
	public:
		virtual ~DbPrepStmt() = default;
		virtual void execute(const std::span<const DbValue>) = 0;
		void
		execute(const std::initializer_list<DbValue> & v)
		{
			return execute(std::span<const DbValue> {v});
		}
		virtual std::size_t rows() const = 0;
		virtual RecordSetPtr recordSet() = 0;
		virtual CursorPtr cursor() = 0;
	};
	using DbPrepStmtPtr = std::unique_ptr<DbPrepStmt>;

	enum class ParamMode { None, DollarNum, QMark };

	class DbConn {
	public:
		virtual ~DbConn() = default;
		virtual void query(const char * const) = 0;
		virtual void query(const char * const, const std::initializer_list<DbValue> &) = 0;

		virtual DbPrepStmtPtr prepare(const char * const, std::size_t nParams) = 0;

		virtual void beginTx() = 0;
		virtual void commitTx() = 0;
		virtual void rollbackTx() = 0;
	};

	class Tx {
	public:
		explicit Tx(DbConn *);
		Tx(const Tx &) = delete;
		Tx(Tx &&) = delete;
		~Tx();

		template<typename C>
		auto
		operator()(C && callback)
		{
			return callback();
		}

	private:
		DbConn * conn;
	};
}

#endif