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
102 * to the given specification. If colums are NULL,
103 * the destination is not modified, and #GNUNET_NO is returned4
107 * @param row, the row from the result to extract
108 * @param result specificatio to extract for
110 #GNUNET_YES if all results could be extracted
111 #GNUNET_NO if at least one result was NULL
112 #GNUNET_SYSERR if a result was invalid
115 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
116 struct GNUNET_MY_QueryParam *qp,
117 struct GNUNET_MY_ResultSpec *rs,
128 int had_null = GNUNET_NO;
134 stmt = GNUNET_MYSQL_statement_get_stmt (NULL /* FIXME */, sh);
137 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
138 ("`%s' failed at %s:%d with error: %s\n"),
139 "mysql_stmt_bind_result", __FILE__, __LINE__,
140 mysql_stmt_error (stmt));
141 return GNUNET_SYSERR;
145 num_fields = mysql_stmt_field_count (stmt);
146 res = mysql_stmt_result_metadata (stmt);
147 fields = mysql_fetch_fields (res);
149 int int_data[num_fields];
150 long int long_data[num_fields];
151 short short_data[num_fields];
152 char str_data[STRING_SIZE];
153 int error[num_fields];
155 result = (MYSQL_BIND *)malloc (sizeof (MYSQL_BIND)*num_fields);
158 fprintf(stderr, "Error to allocate output buffers\n");
159 return GNUNET_SYSERR;
162 memset(result, 0, sizeof (MYSQL_BIND) * num_fields);
164 /** INITIALISER LE MYSQL_BIND ****/
166 for(i = 0 ; i< num_fields ;i++)
168 result[i].buffer_type = fields[i].type;
169 result[i].is_null = 0;
170 result[i].error = &error[i];
172 switch (fields[i].type)
174 case MYSQL_TYPE_LONG:
175 result[i].buffer = &(int_data[i]);
176 result[i].buffer_length = sizeof (int_data);
179 case MYSQL_TYPE_LONGLONG:
180 result[i].buffer = &(long_data[i]);
181 result[i].buffer_length = sizeof (long_data);
184 case MYSQL_TYPE_STRING:
185 result[i].buffer = (char *)str_data;
186 result[i].buffer_length = sizeof (str_data);
189 case MYSQL_TYPE_SHORT:
190 result[i].buffer = &(short_data[i]);
191 result[i].buffer_length = sizeof (short_data);
195 fprintf(stderr, "Failed : wrong type : %d!\n", fields[i].type);
199 if (mysql_stmt_bind_result(stmt, result))
201 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
202 _("`%s' failed at %s:%d with error: %s\n"),
203 "mysql_stmt_bind_result", __FILE__, __LINE__,
204 mysql_stmt_error (stmt));
205 return GNUNET_SYSERR;
208 /*** FAILED HERE ***/
209 if (mysql_stmt_fetch (stmt))
211 for(j = 0 ; j < num_fields ;j++)
213 fprintf(stderr, "Error Bind [%d] : %d\n", j, error[j]);
216 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
217 _("`%s' failed at %s:%d with error: %s\n"),
218 "mysql_stmt_fetch", __FILE__, __LINE__,
219 mysql_stmt_error (stmt));
220 return GNUNET_SYSERR;
226 mysql_stmt_fetch (stmt);
228 for (i = 0 ; NULL != rs[i].conv ; i++)
230 struct GNUNET_MY_ResultSpec *spec;
233 ret = spec->conv (spec->conv_cls,
237 if (GNUNET_SYSERR == ret)
239 return GNUNET_SYSERR;
242 if (NULL != spec->result_size)
243 *spec->result_size = spec->dst_size;
247 if (GNUNET_YES == had_null)