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/>.
20 * @brief helper functions for Sqlite3 DB interactions
21 * @author Christian Grothoff
24 #include "gnunet_sq_lib.h"
28 * Execute a prepared statement.
30 * @param db_conn database connection
31 * @param params parameters to the statement
32 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
35 GNUNET_SQ_bind (sqlite3_stmt *stmt,
36 const struct GNUNET_SQ_QueryParam *params)
41 for (unsigned int i=0;NULL != params[i].conv; i++)
44 params[i].conv (params[i].conv_cls,
50 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
52 _("Failure to bind %u-th SQL parameter\n"),
57 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
59 _("Failure in sqlite3_reset (!)\n"));
63 GNUNET_assert (0 != params[i].num_params);
64 j += params[i].num_params;
71 * Extract results from a query result according to the given specification.
73 * @param result result to process
74 * @param[in,out] rs result specification to extract for
76 * #GNUNET_OK if all results could be extracted
77 * #GNUNET_SYSERR if a result was invalid (non-existing field)
80 GNUNET_SQ_extract_result (sqlite3_stmt *result,
81 struct GNUNET_SQ_ResultSpec *rs)
85 for (unsigned int i=0;NULL != rs[i].conv; i++)
87 if (NULL == rs[i].result_size)
88 rs[i].result_size = &rs[i].dst_size;
90 rs[i].conv (rs[i].cls,
96 for (unsigned int k=0;k<i;k++)
97 if (NULL != rs[k].cleaner)
98 rs[k].cleaner (rs[k].cls);
101 GNUNET_assert (0 != rs[i].num_params);
102 j += rs[i].num_params;
109 * Free all memory that was allocated in @a rs during
110 * #GNUNET_SQ_extract_result().
112 * @param rs reult specification to clean up
115 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
117 for (unsigned int i=0;NULL != rs[i].conv; i++)
118 if (NULL != rs[i].cleaner)
119 rs[i].cleaner (rs[i].cls);
124 * Reset @a stmt and log error.
126 * @param dbh database handle
127 * @param stmt statement to reset
130 GNUNET_SQ_reset (sqlite3 *dbh,
134 sqlite3_reset (stmt))
135 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
137 _("Failed to reset sqlite statement with error: %s\n"),
138 sqlite3_errmsg (dbh));