aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/sqlite.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sqlite.c')
-rw-r--r--src/sqlite.c117
1 files changed, 50 insertions, 67 deletions
diff --git a/src/sqlite.c b/src/sqlite.c
index 4c2eded..5cb764a 100644
--- a/src/sqlite.c
+++ b/src/sqlite.c
@@ -6,6 +6,7 @@
*/
#include "sqlite.h"
+
#include "compiler.h"
#include "log.h"
@@ -16,21 +17,20 @@
#include <stdlib.h>
#include <string.h>
-#define sqlite_errno(var, fn) \
- do { \
- log_err("%s: %s\n", fn, sqlite3_errstr(var)); \
- var = -var; \
+#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); \
+#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 sqlite_init(void) {
int ret = 0;
ret = sqlite3_initialize();
@@ -42,13 +42,11 @@ int sqlite_init(void)
return ret;
}
-void sqlite_destroy(void)
-{
+void sqlite_destroy(void) {
sqlite_errno_if(sqlite3_shutdown(), "sqlite3_shutdown");
}
-static int sqlite_open(const char *path, sqlite3 **db, int flags)
-{
+static int sqlite_open(const char* path, sqlite3** db, int flags) {
int ret = 0;
ret = sqlite3_open_v2(path, db, flags, NULL);
@@ -60,25 +58,21 @@ static int sqlite_open(const char *path, sqlite3 **db, int flags)
return ret;
}
-int sqlite_open_rw(const char *path, sqlite3 **db)
-{
+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)
-{
+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)
-{
+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 sqlite_exec(sqlite3* db, const char* stmt, sqlite3_callback callback, void* arg) {
int ret = 0;
ret = sqlite3_exec(db, stmt, callback, arg, NULL);
@@ -90,8 +84,10 @@ int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback, void *
return ret;
}
-int sqlite_exec_log_result(UNUSED void *arg, int numof_columns, char **values, char **column_names)
-{
+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]);
@@ -99,8 +95,7 @@ int sqlite_exec_log_result(UNUSED void *arg, int numof_columns, char **values, c
return 0;
}
-int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result)
-{
+int sqlite_prepare(sqlite3* db, const char* stmt, sqlite3_stmt** result) {
int ret = 0;
ret = sqlite3_prepare_v2(db, stmt, -1, result, NULL);
@@ -112,45 +107,40 @@ int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result)
return ret;
}
-void sqlite_reset(sqlite3_stmt *stmt)
-{
+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)
-{
+void sqlite_finalize(sqlite3_stmt* stmt) {
sqlite_errno_if(sqlite3_finalize(stmt), "sqlite3_finalize");
}
-int sqlite_step(sqlite3_stmt *stmt)
-{
+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;
+ case SQLITE_ROW:
+ return 1;
+ case SQLITE_DONE:
+ return 0;
- default:
- sqlite_errno(ret, "sqlite3_step");
- return ret;
+ default:
+ sqlite_errno(ret, "sqlite3_step");
+ return ret;
}
}
-int sqlite_column_int(sqlite3_stmt *stmt, int index)
-{
+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 sqlite_column_text(sqlite3_stmt* stmt, int index, char** _result) {
int ret = 0;
- const unsigned char *value = sqlite3_column_text(stmt, index);
+ const unsigned char* value = sqlite3_column_text(stmt, index);
if (!value) {
ret = sqlite3_errcode(sqlite3_db_handle(stmt));
if (ret) {
@@ -165,7 +155,7 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result)
ret = sqlite3_column_bytes(stmt, index);
size_t nb = (size_t)ret;
- char *result = calloc(nb + 1, 1);
+ char* result = calloc(nb + 1, 1);
if (!result) {
log_errno("calloc");
return -1;
@@ -176,11 +166,10 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result)
return 0;
}
-int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result)
-{
+int sqlite_column_blob(sqlite3_stmt* stmt, int index, unsigned char** _result) {
int ret = 0;
- const unsigned char *value = sqlite3_column_blob(stmt, index);
+ const unsigned char* value = sqlite3_column_blob(stmt, index);
if (!value) {
ret = sqlite3_errcode(sqlite3_db_handle(stmt));
if (ret) {
@@ -195,7 +184,7 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result)
ret = sqlite3_column_bytes(stmt, index);
size_t nb = (size_t)ret;
- unsigned char *result = malloc(nb);
+ unsigned char* result = malloc(nb);
if (!result) {
log_errno("malloc");
return -1;
@@ -206,8 +195,7 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result)
return 0;
}
-int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value)
-{
+int sqlite_bind_int(sqlite3_stmt* stmt, int index, int value) {
int ret = 0;
ret = sqlite3_bind_int(stmt, index, value);
@@ -219,8 +207,7 @@ int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value)
return ret;
}
-int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value)
-{
+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);
@@ -232,8 +219,7 @@ int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value)
return ret;
}
-int sqlite_bind_blob(sqlite3_stmt *stmt, int index, unsigned char *value, size_t nb)
-{
+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);
@@ -245,16 +231,15 @@ int sqlite_bind_blob(sqlite3_stmt *stmt, int index, unsigned char *value, size_t
return ret;
}
-int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt)
-{
- static const char *const fmt = "BEGIN; %s COMMIT;";
+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);
+ char* full_stmt = malloc(nb);
if (!full_stmt) {
log_errno("malloc");
return -1;
@@ -270,11 +255,10 @@ free:
return ret;
}
-int sqlite_get_user_version(sqlite3 *db, unsigned int *output)
-{
- static const char *const sql = "PRAGMA user_version;";
+int sqlite_get_user_version(sqlite3* db, unsigned int* output) {
+ static const char* const sql = "PRAGMA user_version;";
- sqlite3_stmt *stmt = NULL;
+ sqlite3_stmt* stmt = NULL;
int result = -1, ret = 0;
ret = sqlite_prepare(db, sql, &stmt);
@@ -305,8 +289,7 @@ finalize:
return ret;
}
-int sqlite_set_foreign_keys(sqlite3 *db)
-{
- static const char *const sql = "PRAGMA foreign_keys = ON;";
+int sqlite_set_foreign_keys(sqlite3* db) {
+ static const char* const sql = "PRAGMA foreign_keys = ON;";
return sqlite_exec(db, sql, NULL, NULL);
}