1 #define _POSIX_C_SOURCE 200809L
9 #define SQLERROR(x) fprintf(stderr, "%s %d: %s\n", __func__, __LINE__, (x))
11 sqlite3_stmt *zpm_dbqueryv(struct zpm *zpm, char *query, va_list args) {
17 if (!zpm || zpm->error) {
28 sql = sqlite3_vmprintf(query, args);
35 rv = sqlite3_prepare_v2(db, sql, strlen(sql), &st, NULL);
36 if (rv != SQLITE_OK) {
37 SQLERROR(sqlite3_errmsg(db));
45 sqlite3_stmt *zpm_dbquery(struct zpm *zpm, char *query, ...) {
49 if (!zpm || zpm->error) {
58 va_start(args, query);
59 st = zpm_dbqueryv(zpm, query, args);
65 void zpm_db_run(struct zpm *zpm, char *query, ...) {
70 va_start(args, query);
71 st = zpm_dbqueryv(zpm, query, args);
74 rv = sqlite3_step(st);
76 if (rv != SQLITE_DONE) {
78 zpm_seterror(zpm, "db error: %s", sqlite3_errstr(rv));
85 char *zpm_db_string(struct zpm *zpm, char *query, ...) {
91 va_start(args, query);
92 st = zpm_dbqueryv(zpm, query, args);
95 rv = sqlite3_step(st);
97 if (rv == SQLITE_ROW) {
98 result = (char *)sqlite3_column_text(st, 0);
100 result = strdup(result);
104 sqlite3_finalize(st);
108 int zpm_db_int(struct zpm *zpm, char *query, ...) {
114 va_start(args, query);
115 st = zpm_dbqueryv(zpm, query, args);
118 rv = sqlite3_step(st);
120 if (rv == SQLITE_ROW) {
121 result = sqlite3_column_int(st, 0);
123 /* TODO set error if it's not SQLITE_ROW */
125 sqlite3_finalize(st);