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 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.
17 * @brief helper functions for Sqlite3 DB interactions
18 * @author Christian Grothoff
21 #include "gnunet_sq_lib.h"
25 * Execute a prepared statement.
27 * @param db_conn database connection
28 * @param params parameters to the statement
29 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
32 GNUNET_SQ_bind (sqlite3_stmt *stmt,
33 const struct GNUNET_SQ_QueryParam *params)
38 for (unsigned int i=0;NULL != params[i].conv; i++)
41 params[i].conv (params[i].conv_cls,
47 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
49 _("Failure to bind %u-th SQL parameter\n"),
54 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
56 _("Failure in sqlite3_reset (!)\n"));
60 GNUNET_assert (0 != params[i].num_params);
61 j += params[i].num_params;
68 * Extract results from a query result according to the given specification.
70 * @param result result to process
71 * @param[in,out] rs result specification to extract for
73 * #GNUNET_OK if all results could be extracted
74 * #GNUNET_SYSERR if a result was invalid (non-existing field)
77 GNUNET_SQ_extract_result (sqlite3_stmt *result,
78 struct GNUNET_SQ_ResultSpec *rs)
82 for (unsigned int i=0;NULL != rs[i].conv; i++)
84 if (NULL == rs[i].result_size)
85 rs[i].result_size = &rs[i].dst_size;
87 rs[i].conv (rs[i].cls,
93 for (unsigned int k=0;k<i;k++)
94 if (NULL != rs[k].cleaner)
95 rs[k].cleaner (rs[k].cls);
98 GNUNET_assert (0 != rs[i].num_params);
99 j += rs[i].num_params;
106 * Free all memory that was allocated in @a rs during
107 * #GNUNET_SQ_extract_result().
109 * @param rs reult specification to clean up
112 GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
114 for (unsigned int i=0;NULL != rs[i].conv; i++)
115 if (NULL != rs[i].cleaner)
116 rs[i].cleaner (rs[i].cls);
121 * Reset @a stmt and log error.
123 * @param dbh database handle
124 * @param stmt statement to reset
127 GNUNET_SQ_reset (sqlite3 *dbh,
131 sqlite3_reset (stmt))
132 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134 _("Failed to reset sqlite statement with error: %s\n"),
135 sqlite3_errmsg (dbh));