aboutsummaryrefslogtreecommitdiff
path: root/source/20-codegen-compiler/SQLiteHelper.hpp
blob: c24e4762ccc751839ad792bce5f32f49f64c24d0 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#pragma once

#include <fmt/format.h>
#include <sqlite3.h>
#include <cassert>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <tuple>
#include <type_traits>

struct SQLiteDatabase {
	sqlite3* database = nullptr;

	~SQLiteDatabase() {
		// NOTE: calling with NULL is a harmless no-op
		int result = sqlite3_close(database);
		assert(result == SQLITE_OK);
	}

	operator sqlite3*() const { return database; }
	sqlite3** operator&() { return &database; }
};

struct SQLiteStatement {
	sqlite3_stmt* stmt = nullptr;

	SQLiteStatement(const SQLiteStatement&) = delete;
	SQLiteStatement& operator=(const SQLiteStatement&) = delete;

	SQLiteStatement() = default;

	SQLiteStatement(sqlite3* database, std::string_view sql) {
		Initialize(database, sql);
	}

	~SQLiteStatement() {
		// NOTE: calling with NULL is a harmless no-op
		// NOTE: we don't care about the error code, because they are returned if the statement has errored in the most recent execution
		//       but deleting it will succeeed anyways
		sqlite3_finalize(stmt);
	}

	operator sqlite3_stmt*() const { return stmt; }
	sqlite3_stmt** operator&() { return &stmt; }

	void Initialize(sqlite3* database, std::string_view sql) {
		int result = sqlite3_prepare_v2(database, sql.data(), sql.size(), &stmt, nullptr);
		if (result != SQLITE_OK) {
			auto msg = fmt::format(
				"Failed to prepare SQLite3 statement, error message: {}",
				sqlite3_errmsg(sqlite3_db_handle(stmt)));
			throw std::runtime_error(msg);
		}
	}

	bool InitializeLazily(sqlite3* database, std::string_view sql) {
		if (!stmt) {
			Initialize(database, sql);
			return true;
		}
		return false;
	}
};

struct SQLiteRunningStatement {
	sqlite3_stmt* stmt;

	SQLiteRunningStatement(sqlite3_stmt* stmt)
		: stmt{ stmt } {
	}

	SQLiteRunningStatement(const SQLiteStatement& stmt)
		: stmt{ stmt.stmt } {
	}

	~SQLiteRunningStatement() {
		sqlite3_clear_bindings(stmt);
		sqlite3_reset(stmt);
	}

	void BindArgument(int index, int32_t value) {
		sqlite3_bind_int(stmt, index, (int)value);
	}

	void BindArgument(int index, uint32_t value) {
		sqlite3_bind_int(stmt, index, (int)value);
	}

	void BindArgument(int index, int64_t value) {
		sqlite3_bind_int64(stmt, index, value);
	}

	void BindArgument(int index, uint64_t value) {
		sqlite3_bind_int64(stmt, index, (int64_t)value);
	}

	void BindArgument(int index, const char* value) {
		sqlite3_bind_text(stmt, index, value, -1, nullptr);
	}

	void BindArgument(int index, std::string_view value) {
		sqlite3_bind_text(stmt, index, value.data(), value.size(), nullptr);
	}

	void BindArgument(int index, std::nullptr_t) {
		// Noop
	}

	template <typename... Ts>
	void BindArguments(Ts&&... args) {
		// NOTE: SQLite3 argument index starts at 1
		size_t idx = 1;
		auto HandleEachArgument = [this, &idx]<typename T>(T&& arg) {
			BindArgument(idx, std::forward<T>(arg));
			++idx;
		};
		(HandleEachArgument(std::forward<Ts>(args)), ...);
	}

	int Step() {
		return sqlite3_step(stmt);
	}

	void StepAndCheck(int forErr) {
		int err = sqlite3_step(stmt);
		assert(err == forErr);
	}

	int StepAndCheckError() {
		int err = sqlite3_step(stmt);
		if (err != SQLITE_DONE || err != SQLITE_ROW) {
			auto msg = fmt::format(
				"Error {} executing SQLite3 statement, error message: {}",
				sqlite3_errstr(err),
				sqlite3_errmsg(sqlite3_db_handle(stmt)));
			throw std::runtime_error(msg);
		}
		return err;
	}

	void StepUntilDone() {
		while (true) {
			int err = sqlite3_step(stmt);
			// SQLITE_OK is never returned for sqlite3_step() //TODO fact check this
			if (err == SQLITE_DONE) {
				break;
			}
			if (err == SQLITE_ROW) {
				continue;
			}

			auto msg = fmt::format(
				"Error {} executing SQLite3 statement, error message: {}",
				sqlite3_errstr(err),
				sqlite3_errmsg(sqlite3_db_handle(stmt)));
			throw std::runtime_error(msg);
		}
	}

	using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
	using TpFromUnixTimestamp = std::pair<TimePoint, int64_t>;
	using TpFromDateTime = std::pair<TimePoint, const char*>;

	// TODO replace with overloads?
	template <typename T>
	auto ResultColumn(int column) const {
		if constexpr (std::is_enum_v<T>) {
			auto value = sqlite3_column_int64(stmt, column);
			return static_cast<T>(value);
		} else if constexpr (std::is_same_v<T, int> || std::is_same_v<T, bool>) {
			return (T)sqlite3_column_int(stmt, column);
		} else if constexpr (std::is_same_v<T, int64_t>) {
			return (T)sqlite3_column_int64(stmt, column);
		} else if constexpr (std::is_same_v<T, const char*>) {
			return (const char*)sqlite3_column_text(stmt, column);
		} else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, std::string_view>) {
			// SQLite3 uses `unsigned char` to represent UTF-8 code units, on all platforms we care about this is the same as plain `char`
			auto cstr = (const char*)sqlite3_column_text(stmt, column);
			// For std::string_view, this finds size based on null terminator and stores reference to pointer
			// For std::string, this also allocates buffer and copies `cstr` content
			return T(cstr);
		} else if constexpr (std::is_same_v<T, TpFromUnixTimestamp>) {
			auto unixTimestamp = sqlite3_column_int64(stmt, column);
			auto chrono = std::chrono::seconds(unixTimestamp);
			return TimePoint(chrono);
		} else if constexpr (std::is_same_v<T, TpFromDateTime>) {
			// TODO wait for libstdc++ and libc++ implement c++20 std::chrono addition
#ifdef _MSC_VER
			auto datetime = (const char*)sqlite3_column_text(stmt, column);
			if (datetime) {
				std::stringstream ss(datetime);
				TimePoint timepoint;
				ss >> std::chrono::parse("%F %T", timepoint);
				return timepoint;
			} else {
				return TimePoint();
			}
#else
			static_assert(false && sizeof(T), "Unimplemented");
#endif
		} else {
			static_assert(false && sizeof(T), "Unknown type");
		}
	}

	template <typename... Ts>
	auto ResultColumns() {
		// NOTE: SQLite3 column index starts at 0
		// NOTE: ((size_t)-1) + 1 == 0
		size_t idx = -1;
		// NOTE: std::make_tuple() -- variadic template function
		//       std::tuple() -- CTAD constructor
		//       Both of these cause make the comma operator unsequenced, not viable here
		return std::tuple{ (++idx, ResultColumn<Ts>(idx))... };
	}
};