2 This file is part of GNUnet
3 Copyright (C) 2016, 2018 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 library to help with access to a MySQL database
23 * @author Christophe Genevey
24 * @author Christian Grothoff
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
32 * Run a prepared SELECT statement.
34 * @param mc mysql context
35 * @param sh handle to SELECT statment
36 * @param params parameters to the statement
38 #GNUNET_YES if we can prepare all statement
39 #GNUNET_SYSERR if we can't prepare all statement
42 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
43 struct GNUNET_MYSQL_StatementHandle *sh,
44 struct GNUNET_MY_QueryParam *params)
46 const struct GNUNET_MY_QueryParam *p;
51 for (unsigned int i = 0; NULL != params[i].conv; i++)
52 num += params[i].num_params;
54 MYSQL_BIND qbind[num];
61 for (unsigned int i = 0; NULL != (p = ¶ms[i])->conv; i++)
68 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
69 "Conversion for MySQL query failed at offset %u\n",
75 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
76 if (mysql_stmt_bind_param (stmt,
79 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
81 _ ("`%s' failed at %s:%d with error: %s\n"),
82 "mysql_stmt_bind_param",
84 mysql_stmt_error (stmt));
85 GNUNET_MYSQL_statements_invalidate (mc);
89 if (mysql_stmt_execute (stmt))
91 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
93 _ ("`%s' failed at %s:%d with error: %s\n"),
94 "mysql_stmt_execute", __FILE__, __LINE__,
95 mysql_stmt_error (stmt));
96 GNUNET_MYSQL_statements_invalidate (mc);
99 GNUNET_MY_cleanup_query (params,
107 * Free all memory that was allocated in @a qp during
108 * #GNUNET_MY_exec_prepared().
110 * @param qp query specification to clean up
111 * @param qbind array of parameter to clean up
114 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
117 for (unsigned int i = 0; NULL != qp[i].conv; i++)
118 if (NULL != qp[i].cleaner)
119 qp[i].cleaner (qp[i].conv_cls,
125 * Extract results from a query result according to the given
126 * specification. Always fetches the next row.
128 * @param sh statement that returned results
129 * @param rs specification to extract for
131 * #GNUNET_YES if all results could be extracted
132 * #GNUNET_NO if there is no more data in the result set
133 * #GNUNET_SYSERR if a result was invalid
136 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
137 struct GNUNET_MY_ResultSpec *rs)
139 unsigned int num_fields;
143 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
147 return GNUNET_SYSERR;
151 mysql_stmt_free_result (stmt);
156 for (unsigned int i = 0; NULL != rs[i].pre_conv; i++)
157 num_fields += rs[i].num_fields;
159 if (mysql_stmt_field_count (stmt) != num_fields)
161 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
162 "Number of fields mismatch between SQL result and result specification\n");
163 return GNUNET_SYSERR;
167 MYSQL_BIND result[num_fields];
168 unsigned int field_off;
170 memset (result, 0, sizeof(MYSQL_BIND) * num_fields);
172 for (unsigned int i = 0; NULL != rs[i].pre_conv; i++)
174 struct GNUNET_MY_ResultSpec *rp = &rs[i];
177 rp->pre_conv (rp->conv_cls,
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Pre-conversion for MySQL result failed at offset %u\n",
187 return GNUNET_SYSERR;
189 field_off += rp->num_fields;
192 if (mysql_stmt_bind_result (stmt, result))
194 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
196 _ ("%s failed at %s:%d with error: %s\n"),
197 "mysql_stmt_bind_result",
199 mysql_stmt_error (stmt));
200 return GNUNET_SYSERR;
202 #if TEST_OPTIMIZATION
203 (void) mysql_stmt_store_result (stmt);
205 ret = mysql_stmt_fetch (stmt);
206 if (MYSQL_NO_DATA == ret)
208 mysql_stmt_free_result (stmt);
213 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
215 _ ("%s failed at %s:%d with error: %s\n"),
218 mysql_stmt_error (stmt));
219 GNUNET_MY_cleanup_result (rs);
220 mysql_stmt_free_result (stmt);
221 return GNUNET_SYSERR;
224 for (unsigned int i = 0; NULL != rs[i].post_conv; i++)
226 struct GNUNET_MY_ResultSpec *rp = &rs[i];
228 if (NULL != rp->post_conv)
230 rp->post_conv (rp->conv_cls,
236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
237 "Post-conversion for MySQL result failed at offset %u\n",
239 mysql_stmt_free_result (stmt);
240 for (unsigned int j = 0; j < i; j++)
241 if (NULL != rs[j].cleaner)
242 rs[j].cleaner (rs[j].conv_cls,
244 return GNUNET_SYSERR;
246 field_off += rp->num_fields;
254 * Free all memory that was allocated in @a rs during
255 * #GNUNET_MY_extract_result().
257 * @param rs result specification to clean up
260 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs)
262 for (unsigned int i = 0; NULL != rs[i].post_conv; i++)
263 if (NULL != rs[i].cleaner)
264 rs[i].cleaner (rs[i].conv_cls,