aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/sqlite.c
blob: f064280f64031bf3441a51c4275c9482742cbff4 (plain) (blame)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "sqlite.h"
#include "compiler.h"
#include "log.h"

#include <sqlite3.h>

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define sqlite_errno(var, fn)                                                                      \
	do {                                                                                       \
		log_err("%s: %s\n", fn, sqlite3_errstr(var));                                      \
		var = -var;                                                                        \
	} while (0)

#define sqlite_errno_if(expr, fn)                                                                  \
	do {                                                                                       \
		int CONCAT(ret, __LINE__) = expr;                                                  \
		if (CONCAT(ret, __LINE__))                                                         \
			sqlite_errno(CONCAT(ret, __LINE__), fn);                                   \
	} while (0)

int sqlite_init(void)
{
	int ret = 0;

	ret = sqlite3_initialize();
	if (ret) {
		sqlite_errno(ret, "sqlite3_initialize");
		return ret;
	}

	return ret;
}

void sqlite_destroy(void)
{
	sqlite_errno_if(sqlite3_shutdown(), "sqlite3_shutdown");
}

static int sqlite_open(const char *path, sqlite3 **db, int flags)
{
	int ret = 0;

	ret = sqlite3_open_v2(path, db, flags, NULL);
	if (ret) {
		sqlite_errno(ret, "sqlite3_open_v2");
		return ret;
	}

	return ret;
}

int sqlite_open_rw(const char *path, sqlite3 **db)
{
	static const int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
	return sqlite_open(path, db, flags);
}

int sqlite_open_ro(const char *path, sqlite3 **db)
{
	static const int flags = SQLITE_OPEN_READONLY;
	return sqlite_open(path, db, flags);
}

void sqlite_close(sqlite3 *db)
{
	sqlite_errno_if(sqlite3_close(db), "sqlite3_close");
}

int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback, void *arg)
{
	int ret = 0;

	ret = sqlite3_exec(db, stmt, callback, arg, NULL);
	if (ret) {
		sqlite_errno(ret, "sqlite3_exec");
		return ret;
	}

	return ret;
}

int sqlite_exec_log_result(UNUSED void *arg, int numof_columns, char **values, char **column_names)
{
	log("Row:\n");
	for (int i = 0; i < numof_columns; ++i) {
		log("\t%s: %s\n", column_names[i], values[i]);
	}
	return 0;
}

int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result)
{
	int ret = 0;

	ret = sqlite3_prepare_v2(db, stmt, -1, result, NULL);
	if (ret) {
		sqlite_errno(ret, "sqlite3_prepare_v2");
		return ret;
	}

	return ret;
}

void sqlite_reset(sqlite3_stmt *stmt)
{
	sqlite_errno_if(sqlite3_reset(stmt), "sqlite3_reset");
	sqlite_errno_if(sqlite3_clear_bindings(stmt), "sqlite3_clear_bindings");
}

void sqlite_finalize(sqlite3_stmt *stmt)
{
	sqlite_errno_if(sqlite3_finalize(stmt), "sqlite3_finalize");
}

int sqlite_step(sqlite3_stmt *stmt)
{
	int ret = 0;

	ret = sqlite3_step(stmt);

	switch (ret) {
	case SQLITE_ROW:
		return 1;
	case SQLITE_DONE:
		return 0;

	default:
		sqlite_errno(ret, "sqlite3_step");
		return ret;
	}
}

int sqlite_column_int(sqlite3_stmt *stmt, int index)
{
	return sqlite3_column_int(stmt, index);
}

int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result)
{
	int ret = 0;

	const unsigned char *value = sqlite3_column_text(stmt, index);
	if (!value) {
		ret = sqlite3_errcode(sqlite3_db_handle(stmt));
		if (ret) {
			sqlite_errno(ret, "sqlite3_column_text");
			return ret;
		}

		*_result = NULL;
		return 0;
	}

	ret = sqlite3_column_bytes(stmt, index);
	size_t nb = (size_t)ret;

	char *result = calloc(nb + 1, 1);
	if (!result) {
		log_errno("calloc");
		return -1;
	}
	memcpy(result, value, nb);

	*_result = result;
	return 0;
}

int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result)
{
	int ret = 0;

	const unsigned char *value = sqlite3_column_blob(stmt, index);
	if (!value) {
		ret = sqlite3_errcode(sqlite3_db_handle(stmt));
		if (ret) {
			sqlite_errno(ret, "sqlite3_column_text");
			return ret;
		}

		*_result = NULL;
		return 0;
	}

	ret = sqlite3_column_bytes(stmt, index);
	size_t nb = (size_t)ret;

	unsigned char *result = malloc(nb);
	if (!result) {
		log_errno("malloc");
		return -1;
	}
	memcpy(result, value, nb);

	*_result = result;
	return 0;
}

int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value)
{
	int ret = 0;

	ret = sqlite3_bind_int(stmt, index, value);
	if (ret) {
		sqlite_errno(ret, "sqlite3_bind_int");
		return ret;
	}

	return ret;
}

int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value)
{
	int ret = 0;

	ret = sqlite3_bind_text(stmt, index, value, -1, SQLITE_STATIC);
	if (ret) {
		sqlite_errno(ret, "sqlite3_bind_text");
		return ret;
	}

	return ret;
}

int sqlite_bind_blob(sqlite3_stmt *stmt, int index, unsigned char *value, size_t nb)
{
	int ret = 0;

	ret = sqlite3_bind_blob64(stmt, index, value, nb, SQLITE_STATIC);
	if (ret) {
		sqlite_errno(ret, "sqlite3_bind_blob64");
		return ret;
	}

	return ret;
}

int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt)
{
	static const char *const fmt = "BEGIN; %s COMMIT;";
	int ret = 0;

	ret = snprintf(NULL, 0, fmt, stmt);
	size_t nb = (size_t)ret + 1;
	ret = 0;

	char *full_stmt = malloc(nb);
	if (!full_stmt) {
		log_errno("malloc");
		return -1;
	}
	snprintf(full_stmt, nb, fmt, stmt);

	ret = sqlite_exec(db, stmt, NULL, NULL);
	goto free;

free:
	free(full_stmt);

	return ret;
}

int sqlite_get_user_version(sqlite3 *db, unsigned int *output)
{
	static const char *const sql = "PRAGMA user_version;";

	sqlite3_stmt *stmt = NULL;
	int result = -1, ret = 0;

	ret = sqlite_prepare(db, sql, &stmt);
	if (ret < 0)
		return ret;
	ret = sqlite_step(stmt);
	if (ret < 0)
		goto finalize;
	if (!ret) {
		ret = -1;
		log_err("Failed to read database version\n");
		goto finalize;
	}

	result = sqlite_column_int(stmt, 0);
	if (result < 0) {
		log_err("Invalid database version: %d\n", result);
		ret = -1;
		goto finalize;
	}
	*output = (unsigned int)result;

	goto finalize;

finalize:
	sqlite_finalize(stmt);

	return ret;
}

int sqlite_set_foreign_keys(sqlite3 *db)
{
	static const char *const sql = "PRAGMA foreign_keys = ON;";
	return sqlite_exec(db, sql, NULL, NULL);
}