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
44 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
45 struct GNUNET_MYSQL_StatementHandle *sh,
46 const struct GNUNET_MY_QueryParam *params)
48 const struct GNUNET_MY_QueryParam *p;
54 for (i=0;NULL != params[i].conv;i++)
55 num += params[i].num_params;
57 MYSQL_BIND qbind[num];
60 memset(qbind, 0, sizeof(qbind));
62 for (i=0;NULL != (p = ¶ms[i])->conv;i++)
73 stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh);
74 if (mysql_stmt_bind_param (stmt,
77 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
78 _("`%s' failed at %s:%d with error: %s\n"),
79 "mysql_stmt_bind_param", __FILE__, __LINE__,
80 mysql_stmt_error (stmt));
81 GNUNET_MYSQL_statements_invalidate (mc);
86 if (mysql_stmt_execute (stmt))
88 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
89 _("`%s' failed at %s:%d with error: %s\n"),
90 "mysql_stmt_execute", __FILE__, __LINE__,
91 mysql_stmt_error (stmt));
92 GNUNET_MYSQL_statements_invalidate (mc);
101 * Extract results from a query result according to the given
102 * specification. Always fetches the next row.
105 * @param sh statement that returned results
106 * @param rs specification to extract for
108 * #GNUNET_YES if all results could be extracted
109 * #GNUNET_NO if there is no more data in the result set
110 * #GNUNET_SYSERR if a result was invalid
113 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
114 struct GNUNET_MY_ResultSpec *rs)
116 unsigned int num_fields;
121 stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
124 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
125 ("`%s' failed at %s:%d with error: %s\n"),
126 "mysql_stmt_bind_result", __FILE__, __LINE__,
127 mysql_stmt_error (stmt));
128 return GNUNET_SYSERR;
132 for (i=0;NULL != rs[i].conv;i++)
133 num_fields += rs[i].num_fields;
135 if (mysql_stmt_field_count (stmt) != num_fields)
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
138 "Number of fields missmatch between SQL result and result specification\n");
139 return GNUNET_SYSERR;
143 MYSQL_BIND result[num_fields];
144 unsigned int field_off;
146 memset (result, 0, sizeof (MYSQL_BIND) * num_fields);
148 for (i=0;NULL != rs[i].conv;i++)
150 struct GNUNET_MY_ResultSpec *rp = &rs[i];
153 rp->pre_conv (rp->cls,
159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
160 "Pre-conversion for MySQL result failed at offset %u\n",
162 GNUNET_MY_cleanup_result (rs);
163 return GNUNET_SYSERR;
165 field_off += rp->num_fields;
167 if (mysql_stmt_bind_result (stmt, result))
169 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
171 _("`%s' failed at %s:%d with error: %s\n"),
172 "mysql_stmt_bind_result", __FILE__, __LINE__,
173 mysql_stmt_error (stmt));
174 return GNUNET_SYSERR;
176 ret = mysql_stmt_fetch (stmt);
177 if (MYSQL_NO_DATA == ret)
181 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
183 _("mysql_stmt_fetch failed at %s:%d with error: %s\n"),
185 mysql_stmt_error (stmt));
186 return GNUNET_SYSERR;
189 for (i=0;NULL != rs[i].conv;i++)
191 struct GNUNET_MY_ResultSpec *rp = &rs[i];
193 if (NULL != rp->post_conv)
195 rp->post_conv (rp->cls,
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Post-conversion for MySQL result failed at offset %u\n",
204 GNUNET_MY_cleanup_result (rs);
205 return GNUNET_SYSERR;
207 field_off += rp->num_fields;
215 * Free all memory that was allocated in @a rs during
216 * #GNUNET_MY_extract_result().
218 * @param rs reult specification to clean up
221 GNUNET_MY_cleanup_result (struct GNUNET_PQ_ResultSpec *rs)
225 for (i=0;NULL != rs[i].conv;i++)
226 rs[i].cleaner (rs[i].cls,