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 under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
18 * @brief helper functions for Sqlite3 DB interactions
19 * @author Christian Grothoff
22 #include "gnunet_sq_lib.h"
26 * Execute a prepared statement.
28 * @param db_conn database connection
29 * @param params parameters to the statement
30 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
33 GNUNET_SQ_bind (sqlite3_stmt *stmt,
34 const struct GNUNET_SQ_QueryParam *params)
39 for (unsigned int i=0;NULL != params[i].conv; i++)
42 params[i].conv (params[i].conv_cls,
48 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
50 _("Failure to bind %u-th SQL parameter\n"),
55 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
57 _("Failure in sqlite3_reset (!)\n"));
61 GNUNET_assert (0 != params[i].num_params);
62 j += params[i].num_params;
69 * Extract results from a query result according to the given specification.
71 * @param result result to process
72 * @param[in,out] rs result specification to extract for
74 * #GNUNET_OK if all results could be extracted
75 * #GNUNET_SYSERR if a result was invalid (non-existing field)
78 GNUNET_SQ_extract_result (sqlite3_stmt *result,
79 struct GNUNET_SQ_ResultSpec *rs)
83 for (unsigned int i=0;NULL != rs[i].conv; i++)
85 if (NULL == rs[i].result_size)
86 rs[i].result_size = &rs[i].dst_size;
88 rs[i].conv (rs[i].cls,
94 for (unsigned int k=0;k<i;k++)
95 if (NULL != rs[k].cleaner)
96 rs[k].cleaner (rs[k].cls);
99 GNUNET_assert (0 != rs[i].num_params);
100 j += rs[i].num_params;
107 * Free all memory that was allocated in @a rs during
108 * #GNUNET_SQ_extract_result().
110 * @param rs reult specification to clean up
113 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
115 for (unsigned int i=0;NULL != rs[i].conv; i++)
116 if (NULL != rs[i].cleaner)
117 rs[i].cleaner (rs[i].cls);
122 * Reset @a stmt and log error.
124 * @param dbh database handle
125 * @param stmt statement to reset
128 GNUNET_SQ_reset (sqlite3 *dbh,
132 sqlite3_reset (stmt))
133 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
135 _("Failed to reset sqlite statement with error: %s\n"),
136 sqlite3_errmsg (dbh));