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