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