2 This file is part of GNUnet
3 Copyright (C) 2016 Inria & 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.
17 * @brief library to help with access to a MySQL database
18 * @author Christophe Genevey
19 * @author Christian Grothoff
22 #include <mysql/mysql.h>
23 #include "gnunet_my_lib.h"
27 * Run a prepared SELECT statement.
29 * @param mc mysql context
30 * @param sh handle to SELECT statment
31 * @param params parameters to the statement
33 #GNUNET_YES if we can prepare all statement
34 #GNUNET_SYSERR if we can't prepare all statement
37 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
38 struct GNUNET_MYSQL_StatementHandle *sh,
39 struct GNUNET_MY_QueryParam *params)
41 const struct GNUNET_MY_QueryParam *p;
47 for (i=0;NULL != params[i].conv;i++)
48 num += params[i].num_params;
50 MYSQL_BIND qbind[num];
53 memset (qbind, 0, sizeof(qbind));
55 for (i=0;NULL != (p = ¶ms[i])->conv;i++)
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63 "Conversion for MySQL query failed at offset %u\n",
69 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
70 if (mysql_stmt_bind_param (stmt,
73 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
75 _("`%s' failed at %s:%d with error: %s\n"),
76 "mysql_stmt_bind_param",
78 mysql_stmt_error (stmt));
79 GNUNET_MYSQL_statements_invalidate (mc);
83 if (mysql_stmt_execute (stmt))
85 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
87 _("`%s' failed at %s:%d with error: %s\n"),
88 "mysql_stmt_execute", __FILE__, __LINE__,
89 mysql_stmt_error (stmt));
90 GNUNET_MYSQL_statements_invalidate (mc);
93 GNUNET_MY_cleanup_query (params,
101 * Free all memory that was allocated in @a qp during
102 * #GNUNET_MY_exec_prepared().
104 * @param qp query specification to clean up
105 * @param qbind array of parameter to clean up
108 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
113 for (i=0; NULL != qp[i].conv ;i++)
114 if (NULL != qp[i].cleaner)
115 qp[i].cleaner (qp[i].conv_cls,
121 * Extract results from a query result according to the given
122 * specification. Always fetches the next row.
124 * @param sh statement that returned results
125 * @param rs specification to extract for
127 * #GNUNET_YES if all results could be extracted
128 * #GNUNET_NO if there is no more data in the result set
129 * #GNUNET_SYSERR if a result was invalid
132 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
133 struct GNUNET_MY_ResultSpec *rs)
135 unsigned int num_fields;
140 stmt = GNUNET_MYSQL_statement_get_stmt (sh);
144 return GNUNET_SYSERR;
148 mysql_stmt_free_result (stmt);
153 for (i=0;NULL != rs[i].pre_conv;i++)
154 num_fields += rs[i].num_fields;
156 if (mysql_stmt_field_count (stmt) != num_fields)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Number of fields missmatch between SQL result and result specification\n");
160 return GNUNET_SYSERR;
164 MYSQL_BIND result[num_fields];
165 unsigned int field_off;
167 memset (result, 0, sizeof (MYSQL_BIND) * num_fields);
169 for (i=0;NULL != rs[i].pre_conv;i++)
171 struct GNUNET_MY_ResultSpec *rp = &rs[i];
174 rp->pre_conv (rp->conv_cls,
181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 "Pre-conversion for MySQL result failed at offset %u\n",
184 return GNUNET_SYSERR;
186 field_off += rp->num_fields;
189 if (mysql_stmt_bind_result (stmt, result))
191 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
193 _("%s failed at %s:%d with error: %s\n"),
194 "mysql_stmt_bind_result",
196 mysql_stmt_error (stmt));
197 return GNUNET_SYSERR;
199 #if TEST_OPTIMIZATION
200 (void) mysql_stmt_store_result (stmt);
202 ret = mysql_stmt_fetch (stmt);
203 if (MYSQL_NO_DATA == ret)
205 mysql_stmt_free_result (stmt);
210 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
212 _("%s failed at %s:%d with error: %s\n"),
215 mysql_stmt_error (stmt));
216 GNUNET_MY_cleanup_result (rs);
217 mysql_stmt_free_result (stmt);
218 return GNUNET_SYSERR;
221 for (i=0;NULL != rs[i].post_conv;i++)
223 struct GNUNET_MY_ResultSpec *rp = &rs[i];
225 if (NULL != rp->post_conv)
227 rp->post_conv (rp->conv_cls,
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
234 "Post-conversion for MySQL result failed at offset %u\n",
236 mysql_stmt_free_result (stmt);
237 GNUNET_MY_cleanup_result (rs);
238 return GNUNET_SYSERR;
240 field_off += rp->num_fields;
248 * Free all memory that was allocated in @a rs during
249 * #GNUNET_MY_extract_result().
251 * @param rs result specification to clean up
254 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs)
258 for (i=0;NULL != rs[i].post_conv;i++)
259 if (NULL != rs[i].cleaner)
260 rs[i].cleaner (rs[i].conv_cls,