2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @brief helper functions for Sqlite3 DB interactions
23 * @author Christian Grothoff
26 #include "gnunet_sq_lib.h"
30 * Execute a prepared statement.
32 * @param db_conn database connection
33 * @param params parameters to the statement
34 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
37 GNUNET_SQ_bind (sqlite3_stmt *stmt,
38 const struct GNUNET_SQ_QueryParam *params)
43 for (unsigned int i=0;NULL != params[i].conv; i++)
46 params[i].conv (params[i].conv_cls,
52 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
54 _("Failure to bind %u-th SQL parameter\n"),
59 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
61 _("Failure in sqlite3_reset (!)\n"));
65 GNUNET_assert (0 != params[i].num_params);
66 j += params[i].num_params;
73 * Extract results from a query result according to the given specification.
75 * @param result result to process
76 * @param[in,out] rs result specification to extract for
78 * #GNUNET_OK if all results could be extracted
79 * #GNUNET_SYSERR if a result was invalid (non-existing field)
82 GNUNET_SQ_extract_result (sqlite3_stmt *result,
83 struct GNUNET_SQ_ResultSpec *rs)
87 for (unsigned int i=0;NULL != rs[i].conv; i++)
89 if (NULL == rs[i].result_size)
90 rs[i].result_size = &rs[i].dst_size;
92 rs[i].conv (rs[i].cls,
98 for (unsigned int k=0;k<i;k++)
99 if (NULL != rs[k].cleaner)
100 rs[k].cleaner (rs[k].cls);
101 return GNUNET_SYSERR;
103 GNUNET_assert (0 != rs[i].num_params);
104 j += rs[i].num_params;
111 * Free all memory that was allocated in @a rs during
112 * #GNUNET_SQ_extract_result().
114 * @param rs reult specification to clean up
117 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
119 for (unsigned int i=0;NULL != rs[i].conv; i++)
120 if (NULL != rs[i].cleaner)
121 rs[i].cleaner (rs[i].cls);
126 * Reset @a stmt and log error.
128 * @param dbh database handle
129 * @param stmt statement to reset
132 GNUNET_SQ_reset (sqlite3 *dbh,
136 sqlite3_reset (stmt))
137 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
139 _("Failed to reset sqlite statement with error: %s\n"),
140 sqlite3_errmsg (dbh));