1 /* gpgsql.c - SQLite helper functions.
2 * Copyright (C) 2015 g10 Code GmbH
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
31 /* This is a convenience function that combines sqlite3_mprintf and
34 gpgsql_exec_printf (sqlite3 *db,
35 int (*callback)(void*,int,char**,char**), void *cookie,
44 sql2 = sqlite3_vmprintf (sql, ap);
48 log_debug ("tofo db: executing: '%s'\n", sql2);
51 rc = sqlite3_exec (db, sql2, callback, cookie, errmsg);
59 gpgsql_stepx (sqlite3 *db,
61 gpgsql_stepx_callback callback,
68 sqlite3_stmt *stmt = NULL;
72 enum gpgsql_arg_type t;
76 /* Names of the columns. We initialize this lazily to avoid the
77 overhead in case the query doesn't return any results. */
78 const char **azColName = 0;
79 int callback_initialized = 0;
81 const char **azVals = 0;
83 callback_initialized = 0;
89 /* Make sure this statement is associated with the supplied db. */
90 log_assert (db == sqlite3_db_handle (stmt));
98 const char *tail = NULL;
100 rc = sqlite3_prepare_v2 (db, sql, -1, &stmt, &tail);
102 log_fatal ("failed to prepare SQL: %s", sql);
104 /* We can only process a single statement. */
107 while (*tail == ' ' || *tail == ';' || *tail == '\n')
112 ("sqlite3_stepx can only process a single SQL statement."
113 " Second statement starts with: '%s'\n",
125 args = sqlite3_bind_parameter_count (stmt);
129 for (i = 1; i <= args; i ++)
131 t = va_arg (va, enum gpgsql_arg_type);
136 int value = va_arg (va, int);
137 err = sqlite3_bind_int (stmt, i, value);
140 case GPGSQL_ARG_LONG_LONG:
142 long long value = va_arg (va, long long);
143 err = sqlite3_bind_int64 (stmt, i, value);
146 case GPGSQL_ARG_STRING:
148 char *text = va_arg (va, char *);
149 err = sqlite3_bind_text (stmt, i, text, -1, SQLITE_STATIC);
152 case GPGSQL_ARG_BLOB:
154 char *blob = va_arg (va, void *);
155 long long length = va_arg (va, long long);
156 err = sqlite3_bind_blob (stmt, i, blob, length, SQLITE_STATIC);
160 /* Internal error. Likely corruption. */
161 log_fatal ("Bad value for parameter type %d.\n", t);
166 log_fatal ("Error binding parameter %d\n", i);
172 t = va_arg (va, enum gpgsql_arg_type);
173 log_assert (t == GPGSQL_ARG_END);
178 rc = sqlite3_step (stmt);
180 if (rc != SQLITE_ROW)
181 /* No more data (SQLITE_DONE) or an error occurred. */
187 if (! callback_initialized)
189 cols = sqlite3_column_count (stmt);
190 azColName = xmalloc (2 * cols * sizeof (const char *) + 1);
192 for (i = 0; i < cols; i ++)
193 azColName[i] = sqlite3_column_name (stmt, i);
195 callback_initialized = 1;
198 azVals = &azColName[cols];
199 for (i = 0; i < cols; i ++)
201 azVals[i] = sqlite3_column_text (stmt, i);
202 if (! azVals[i] && sqlite3_column_type (stmt, i) != SQLITE_NULL)
210 if (callback (cookie, cols, (char **) azVals, (char **) azColName, stmt))
211 /* A non-zero result means to abort. */
222 rc = sqlite3_reset (stmt);
224 rc = sqlite3_finalize (stmt);
225 if (rc == SQLITE_OK && err)
231 const char *e = sqlite3_errstr (err);
232 size_t l = strlen (e) + 1;
233 *errmsg = sqlite3_malloc (l);
235 log_fatal ("Out of memory.\n");
236 memcpy (*errmsg, e, l);
239 else if (rc != SQLITE_OK && errmsg)
240 /* Error reported by sqlite. */
242 const char * e = sqlite3_errmsg (db);
243 size_t l = strlen (e) + 1;
244 *errmsg = sqlite3_malloc (l);
246 log_fatal ("Out of memory.\n");
247 memcpy (*errmsg, e, l);