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