use new connecT API
[oweals/gnunet.git] / src / my / my_query_helper.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2016 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_query_helper.c
22  * @brief library to help with access to a MySQL database
23  * @author Christian Grothoff
24  * @author Christophe Genevey
25  */
26 #include "platform.h"
27 #include <mysql/mysql.h>
28 #include "gnunet_my_lib.h"
29
30
31 /**
32  * Function called to clean up memory allocated
33  * by a #GNUNET_MY_QueryConverter.
34  *
35  * @param cls closure
36  * @param qbind array of parameter to clean up
37  */
38 static void
39 my_clean_query (void *cls,
40                 MYSQL_BIND *qbind)
41 {
42   GNUNET_free (qbind[0].buffer);
43 }
44
45
46 /**
47  * Function called to convert input argument into SQL parameters.
48  *
49  * @param cls closure
50  * @param pq data about the query
51  * @param qbind array of parameters to initialize
52  * @return -1 on error
53  */
54 static int
55 my_conv_fixed_size (void *cls,
56                     const struct GNUNET_MY_QueryParam *qp,
57                     MYSQL_BIND *qbind)
58 {
59   GNUNET_assert (1 == qp->num_params);
60
61   qbind->buffer = (void *) qp->data;
62   qbind->buffer_length = qp->data_len;
63   qbind->buffer_type = MYSQL_TYPE_BLOB;
64
65   return 1;
66 }
67
68
69 /**
70  * Generate query parameter for a buffer @a ptr of
71  * @a ptr_size bytes.
72  *
73  * @param ptr pointer to the query parameter to pass
74  * @param ptr_size number of bytes in @a ptr
75  */
76 struct GNUNET_MY_QueryParam
77 GNUNET_MY_query_param_fixed_size (const void *ptr,
78                                   size_t ptr_size)
79 {
80   struct GNUNET_MY_QueryParam qp = {
81     .conv = &my_conv_fixed_size,
82     .cleaner = NULL,
83     .conv_cls = NULL,
84     .num_params = 1,
85     .data = ptr,
86     .data_len = (unsigned long) ptr_size
87   };
88   return qp;
89 }
90
91
92 /**
93   * Generate query parameter for a string
94   *
95   *@param ptr pointer to the string query parameter to pass
96   */
97 struct GNUNET_MY_QueryParam
98 GNUNET_MY_query_param_string (const char *ptr)
99 {
100   return GNUNET_MY_query_param_fixed_size(ptr,
101                                          strlen(ptr));
102 }
103
104
105 /**
106   * Function called to convert input argument into SQL parameters
107   *
108   *@param cls closure
109   *@param pq data about the query
110  * @param qbind array of parameters to initialize
111   *@return -1 on error
112   */
113 static int
114 my_conv_uint16 (void *cls,
115                 const struct GNUNET_MY_QueryParam * qp,
116                 MYSQL_BIND *qbind)
117 {
118   GNUNET_assert (1 == qp->num_params);
119   qbind->buffer = (void *) qp->data;
120   qbind->buffer_length = sizeof (uint16_t);
121   qbind->buffer_type = MYSQL_TYPE_SHORT;
122   return 1;
123 }
124
125
126 /**
127   * Generate query parameter for an uint16_t in host byte order.
128   *
129   * @param x pointer to the query parameter to pass
130   */
131 struct GNUNET_MY_QueryParam
132 GNUNET_MY_query_param_uint16 (const uint16_t *x)
133 {
134   struct GNUNET_MY_QueryParam res = {
135     .conv = &my_conv_uint16,
136     .cleaner = NULL,
137     .conv_cls = NULL,
138     .num_params = 1,
139     .data = x,
140     .data_len = sizeof (*x)
141   };
142
143   return res;
144 }
145
146
147 /**
148   * Function called to convert input argument into SQL parameters
149   *
150   *@param cls closure
151   *@param pq data about the query
152  * @param qbind array of parameters to initialize
153   *@return -1 on error
154   */
155 static int
156 my_conv_uint32 (void *cls,
157                 const struct GNUNET_MY_QueryParam *qp,
158                 MYSQL_BIND *qbind)
159 {
160   GNUNET_assert (1 == qp->num_params);
161   qbind->buffer = (void *) qp->data;
162   qbind->buffer_length = sizeof(uint32_t);
163   qbind->buffer_type = MYSQL_TYPE_LONG;
164
165   return 1;
166 }
167
168
169 /**
170   * Generate query parameter for an uint32_t in host byte order
171   *
172   *@param x pointer to the query parameter to pass
173   */
174 struct GNUNET_MY_QueryParam
175 GNUNET_MY_query_param_uint32 (const uint32_t *x)
176 {
177   struct GNUNET_MY_QueryParam res = {
178     .conv = &my_conv_uint32,
179     .cleaner = NULL,
180     .conv_cls = NULL,
181     .num_params = 1,
182     .data = x,
183     .data_len = sizeof (*x)
184   };
185
186   return res;
187 }
188
189
190 /**
191   * Function called to convert input argument into SQL parameters
192   *
193   *@param cls closure
194   *@param pq data about the query
195  * @param qbind array of parameters to initialize
196   *@return -1 on error
197   */
198 static int
199 my_conv_uint64 (void *cls,
200                 const struct GNUNET_MY_QueryParam *qp,
201                 MYSQL_BIND * qbind)
202 {
203   GNUNET_assert (1 == qp->num_params);
204   qbind->buffer = (void *) qp->data;
205   qbind->buffer_length = sizeof (uint64_t);
206   qbind->buffer_type = MYSQL_TYPE_LONGLONG;
207   return 1;
208 }
209
210
211 /**
212   * Generate query parameter for an uint64_t in host byte order
213   *
214   *@param x pointer to the query parameter to pass
215   */
216 struct GNUNET_MY_QueryParam
217 GNUNET_MY_query_param_uint64 (const uint64_t *x)
218 {
219   struct GNUNET_MY_QueryParam res = {
220     .conv = &my_conv_uint64,
221     .cleaner = NULL,
222     .conv_cls = NULL,
223     .num_params = 1,
224     .data = x,
225     .data_len = sizeof(*x)
226   };
227
228   return res;
229 }
230
231
232 /**
233   * Function called to convert input argument into SQL parameters
234   *
235   *@param cls closure
236   *@param pq data about the query
237  * @param qbind array of parameters to initialize
238   *@return -1 on error
239   */
240 static int
241 my_conv_rsa_public_key (void *cls,
242                         const struct GNUNET_MY_QueryParam *qp,
243                         MYSQL_BIND * qbind)
244 {
245   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
246   char *buf;
247   size_t buf_size;
248
249   GNUNET_assert(1 == qp->num_params);
250
251   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa, &buf);
252
253   qbind->buffer = (void *) buf;
254   qbind->buffer_length = buf_size;
255   qbind->buffer_type = MYSQL_TYPE_BLOB;
256
257   return 1;
258 }
259
260
261 /**
262   * Generate query parameter for an RSA public key. The
263   * database must contain a BLOB type in the respective position.
264   *
265   * @param x the query parameter to pass
266   * @return array entry for the query parameters to use
267   */
268 struct GNUNET_MY_QueryParam
269 GNUNET_MY_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
270 {
271   struct GNUNET_MY_QueryParam res = {
272     .conv = &my_conv_rsa_public_key,
273     .cleaner = &my_clean_query,
274     .conv_cls = NULL,
275     .num_params = 1,
276     .data = x,
277     .data_len = 0
278   };
279
280   return res;
281 }
282
283
284 /**
285   * Function called to convert input argument into SQL parameters
286   *
287   *@param cls closure
288   *@param pq data about the query
289   *@param qbind array of parameters to initialize
290   *@return -1 on error
291   */
292 static int
293 my_conv_rsa_signature (void *cls,
294                        const struct GNUNET_MY_QueryParam *qp,
295                        MYSQL_BIND *qbind)
296 {
297   const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
298   char *buf;
299   size_t buf_size;
300
301   GNUNET_assert(1 == qp->num_params);
302
303   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
304                                                  &buf);
305   qbind->buffer = (void *) buf;
306   qbind->buffer_length = buf_size;
307   qbind->buffer_type = MYSQL_TYPE_BLOB;
308
309   return 1;
310 }
311
312
313 /**
314   * Generate query parameter for an RSA signature. The
315   * database must contain a BLOB type in the respective position
316   *
317   *@param x the query parameter to pass
318   *@return array entry for the query parameters to use
319   */
320 struct GNUNET_MY_QueryParam
321 GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
322 {
323   struct GNUNET_MY_QueryParam res = {
324     .conv = &my_conv_rsa_signature,
325     .cleaner = &my_clean_query,
326     .conv_cls = NULL,
327     .num_params = 1,
328     .data = (x),
329     .data_len = 0
330   };
331   return res;
332 }
333
334
335 /**
336   * Generate query parameter for an absolute time value.
337   * The database must store a 64-bit integer.
338   *
339   *@param x pointer to the query parameter to pass
340   *@return array entry for the query parameters to use
341   */
342 struct GNUNET_MY_QueryParam
343 GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
344 {
345   return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
346 }
347
348
349 /**
350   * Generate query parameter for an absolute time value.
351   * The database must store a 64-bit integer.
352   *
353   *@param x pointer to the query parameter to pass
354   */
355 struct GNUNET_MY_QueryParam
356 GNUNET_MY_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
357 {
358   return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
359 }
360
361
362 /* end of my_query_helper.c */