trying again to fix test_service timeout on v6 failure
[oweals/gnunet.git] / src / my / my.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 Inria & GNUnet e.V.
4
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.
9
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.
14
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.
19 */
20 /**
21  * @file my/my.c
22  * @brief library to help with access to a MySQL database
23  * @author Christophe Genevey
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29
30
31 /**
32  * Run a prepared SELECT statement.
33  *
34  * @param mc mysql context
35  * @param sh handle to SELECT statment
36  * @param params parameters to the statement
37  * @return
38       #GNUNET_YES if we can prepare all statement
39       #GNUNET_SYSERR if we can't prepare all statement
40  */
41 int
42 GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc,
43                          struct GNUNET_MYSQL_StatementHandle *sh,
44                          struct GNUNET_MY_QueryParam *params)
45 {
46   const struct GNUNET_MY_QueryParam *p;
47   unsigned int num;
48   unsigned int i;
49   MYSQL_STMT *stmt;
50
51   num = 0;
52   for (i=0;NULL != params[i].conv;i++)
53     num += params[i].num_params;
54   {
55     MYSQL_BIND qbind[num];
56     unsigned int off;
57
58     memset (qbind, 0, sizeof(qbind));
59     off = 0;
60     for (i=0;NULL != (p = &params[i])->conv;i++)
61     {
62       if (GNUNET_OK !=
63           p->conv (p->conv_cls,
64                    p,
65                    &qbind[off]))
66       {
67         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
68                     "Conversion for MySQL query failed at offset %u\n",
69                     i);
70         return GNUNET_SYSERR;
71       }
72       off += p->num_params;
73     }
74     stmt = GNUNET_MYSQL_statement_get_stmt (sh);
75     if (mysql_stmt_bind_param (stmt,
76                                qbind))
77     {
78       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
79                        "my",
80                        _("`%s' failed at %s:%d with error: %s\n"),
81                        "mysql_stmt_bind_param",
82                        __FILE__, __LINE__,
83                        mysql_stmt_error (stmt));
84       GNUNET_MYSQL_statements_invalidate (mc);
85       return GNUNET_SYSERR;
86     }
87
88     if (mysql_stmt_execute (stmt))
89     {
90       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
91                        "my",
92                        _("`%s' failed at %s:%d with error: %s\n"),
93                        "mysql_stmt_execute", __FILE__, __LINE__,
94                        mysql_stmt_error (stmt));
95       GNUNET_MYSQL_statements_invalidate (mc);
96       return GNUNET_SYSERR;
97     }
98     GNUNET_MY_cleanup_query (params,
99                              qbind);
100   }
101   return GNUNET_OK;
102 }
103
104
105 /**
106  * Free all memory that was allocated in @a qp during
107  * #GNUNET_MY_exec_prepared().
108  *
109  * @param qp query specification to clean up
110  * @param qbind array of parameter to clean up
111  */
112 void
113 GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp,
114                          MYSQL_BIND *qbind)
115 {
116   unsigned int i;
117
118   for (i=0; NULL != qp[i].conv ;i++)
119     if (NULL != qp[i].cleaner)
120       qp[i].cleaner (qp[i].conv_cls,
121                      &qbind[i]);
122 }
123
124
125 /**
126  * Extract results from a query result according to the given
127  * specification.  Always fetches the next row.
128  *
129  * @param sh statement that returned results
130  * @param rs specification to extract for
131  * @return
132  *  #GNUNET_YES if all results could be extracted
133  *  #GNUNET_NO if there is no more data in the result set
134  *  #GNUNET_SYSERR if a result was invalid
135  */
136 int
137 GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh,
138                           struct GNUNET_MY_ResultSpec *rs)
139 {
140   unsigned int num_fields;
141   unsigned int i;
142   int ret;
143   MYSQL_STMT *stmt;
144
145   stmt = GNUNET_MYSQL_statement_get_stmt (sh);
146   if (NULL == stmt)
147   {
148     GNUNET_break (0);
149     return GNUNET_SYSERR;
150   }
151   if (NULL == rs)
152   {
153     mysql_stmt_free_result (stmt);
154     return GNUNET_NO;
155   }
156
157   num_fields = 0;
158   for (i=0;NULL != rs[i].pre_conv;i++)
159     num_fields += rs[i].num_fields;
160
161   if (mysql_stmt_field_count (stmt) != num_fields)
162   {
163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
164                 "Number of fields missmatch between SQL result and result specification\n");
165     return GNUNET_SYSERR;
166   }
167
168   {
169     MYSQL_BIND result[num_fields];
170     unsigned int field_off;
171
172     memset (result, 0, sizeof (MYSQL_BIND) * num_fields);
173     field_off = 0;
174     for (i=0;NULL != rs[i].pre_conv;i++)
175     {
176       struct GNUNET_MY_ResultSpec *rp = &rs[i];
177
178       if (GNUNET_OK !=
179           rp->pre_conv (rp->conv_cls,
180                         rp,
181                         stmt,
182                         field_off,
183                         &result[field_off]))
184
185       {
186         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187                     "Pre-conversion for MySQL result failed at offset %u\n",
188                     i);
189         return GNUNET_SYSERR;
190       }
191       field_off += rp->num_fields;
192     }
193
194     if (mysql_stmt_bind_result (stmt, result))
195     {
196       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
197                        "my",
198                        _("%s failed at %s:%d with error: %s\n"),
199                        "mysql_stmt_bind_result",
200                        __FILE__, __LINE__,
201                        mysql_stmt_error (stmt));
202       return GNUNET_SYSERR;
203     }
204 #if TEST_OPTIMIZATION
205     (void) mysql_stmt_store_result (stmt);
206 #endif
207     ret = mysql_stmt_fetch (stmt);
208     if (MYSQL_NO_DATA == ret)
209     {
210       mysql_stmt_free_result (stmt);
211       return GNUNET_NO;
212     }
213     if (1 == ret)
214     {
215       GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR,
216                        "my",
217                        _("%s failed at %s:%d with error: %s\n"),
218                        "mysql_stmt_fetch",
219                        __FILE__, __LINE__,
220                        mysql_stmt_error (stmt));
221       GNUNET_MY_cleanup_result (rs);
222       mysql_stmt_free_result (stmt);
223       return GNUNET_SYSERR;
224     }
225     field_off = 0;
226     for (i=0;NULL != rs[i].post_conv;i++)
227     {
228       struct GNUNET_MY_ResultSpec *rp = &rs[i];
229
230       if (NULL != rp->post_conv)
231         if (GNUNET_OK !=
232             rp->post_conv (rp->conv_cls,
233                            rp,
234                            stmt,
235                            field_off,
236                            &result[field_off]))
237         {
238           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
239                       "Post-conversion for MySQL result failed at offset %u\n",
240                       i);
241           mysql_stmt_free_result (stmt);
242           GNUNET_MY_cleanup_result (rs);
243           return GNUNET_SYSERR;
244         }
245       field_off += rp->num_fields;
246     }
247   }
248   return GNUNET_OK;
249 }
250
251
252 /**
253  * Free all memory that was allocated in @a rs during
254  * #GNUNET_MY_extract_result().
255  *
256  * @param rs result specification to clean up
257  */
258 void
259 GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs)
260 {
261   unsigned int i;
262
263   for (i=0;NULL != rs[i].post_conv;i++)
264     if (NULL != rs[i].cleaner)
265       rs[i].cleaner (rs[i].conv_cls,
266                      &rs[i]);
267 }
268
269
270 /* end of my.c */