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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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"
30 #define STRING_SIZE 50
33 * Run a prepared SELECT statement.
35 * @param mc mysql context
36 * @param sh handle to SELECT statment
37 * @param params parameters to the statement
39 #GNUNET_YES if we can prepare all statement
40 #GNUNET_SYSERR if we can't prepare all statement
43 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
44 struct GNUNET_MYSQL_StatementHandle *sh,
45 const struct GNUNET_MY_QueryParam *params)
47 const struct GNUNET_MY_QueryParam *p;
53 for (i=0;NULL != params[i].conv;i++)
54 num += params[i].num_params;
56 MYSQL_BIND qbind[num];
59 memset(qbind, 0, sizeof(qbind));
61 for (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",
71 GNUNET_MY_cleanup_query (params);
76 stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
77 if (mysql_stmt_bind_param (stmt,
80 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
81 _("`%s' failed at %s:%d with error: %s\n"),
82 "mysql_stmt_bind_param", __FILE__, __LINE__,
83 mysql_stmt_error (stmt));
84 GNUNET_MYSQL_statements_invalidate (mc);
88 if (mysql_stmt_execute (stmt))
90 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
91 _("`%s' failed at %s:%d with error: %s\n"),
92 "mysql_stmt_execute", __FILE__, __LINE__,
93 mysql_stmt_error (stmt));
94 GNUNET_MYSQL_statements_invalidate (mc);
95 GNUNET_MY_cleanup_query (params);
105 * Free all memory that was allocated in @a qp during
106 * #GNUNET_MY_exect_prepared().
108 * @param qp query specification to clean up
111 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp)
115 for (i=0;NULL != qp[i].cleaner;i++)
116 qp[i].cleaner (qp[i].conv_cls,
122 * Extract results from a query result according to the given
123 * specification. Always fetches the next row.
126 * @param sh statement that returned results
127 * @param rs specification to extract for
129 * #GNUNET_YES if all results could be extracted
130 * #GNUNET_NO if there is no more data in the result set
131 * #GNUNET_SYSERR if a result was invalid
134 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
135 struct GNUNET_MY_ResultSpec *rs)
137 unsigned int num_fields;
142 stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
145 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
146 ("`%s' failed at %s:%d with error: %s\n"),
147 "mysql_stmt_bind_result", __FILE__, __LINE__,
148 mysql_stmt_error (stmt));
149 return GNUNET_SYSERR;
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,
180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
181 "Pre-conversion for MySQL result failed at offset %u\n",
183 GNUNET_MY_cleanup_result (rs);
184 return GNUNET_SYSERR;
186 field_off += rp->num_fields;
188 if (mysql_stmt_bind_result (stmt, result))
190 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
192 _("`%s' failed at %s:%d with error: %s\n"),
193 "mysql_stmt_bind_result", __FILE__, __LINE__,
194 mysql_stmt_error (stmt));
195 return GNUNET_SYSERR;
198 ret = mysql_stmt_fetch (stmt);
200 if (MYSQL_NO_DATA == ret)
202 if ((0 != ret ) && (MYSQL_DATA_TRUNCATED != ret))
204 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
206 _("mysql_stmt_fetch failed at %s:%d with error: %s\n"),
208 mysql_stmt_error (stmt));
209 return GNUNET_SYSERR;
212 for (i=0;NULL != rs[i].post_conv;i++)
214 struct GNUNET_MY_ResultSpec *rp = &rs[i];
216 if (NULL != rp->post_conv)
218 rp->post_conv (rp->conv_cls,
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Post-conversion for MySQL result failed at offset %u\n",
227 GNUNET_MY_cleanup_result (rs);
228 return GNUNET_SYSERR;
230 field_off += rp->num_fields;
238 * Free all memory that was allocated in @a rs during
239 * #GNUNET_MY_extract_result().
241 * @param rs reult specification to clean up
244 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs)
248 for (i=0;NULL != rs[i].cleaner;i++)
249 rs[i].cleaner (rs[i].conv_cls,