-Merge branch 'master' of ssh://gnunet.org/gnunet into gsoc2018/rest_api
[oweals/gnunet.git] / src / sq / sq_query_helper.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2017 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 sq/sq_query_helper.c
20  * @brief helper functions for queries
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_sq_lib.h"
25
26
27 /**
28  * Function called to convert input argument into SQL parameters.
29  *
30  * @param cls closure
31  * @param data pointer to input argument
32  * @param data_len number of bytes in @a data (if applicable)
33  * @param stmt sqlite statement to bind parameters for
34  * @param off offset of the argument to bind in @a stmt, numbered from 1,
35  *            so immediately suitable for passing to `sqlite3_bind`-functions.
36  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
37  */
38 static int
39 bind_fixed_blob (void *cls,
40                  const void *data,
41                  size_t data_len,
42                  sqlite3_stmt *stmt,
43                  unsigned int off)
44 {
45   if (SQLITE_OK !=
46       sqlite3_bind_blob64 (stmt,
47                            (int) off,
48                            data,
49                            (sqlite3_uint64) data_len,
50                            SQLITE_TRANSIENT))
51     return GNUNET_SYSERR;
52   return GNUNET_OK;
53 }
54
55
56 /**
57  * Generate query parameter for a buffer @a ptr of
58  * @a ptr_size bytes.
59  *
60  * @param ptr pointer to the query parameter to pass
61  * @oaran ptr_size number of bytes in @a ptr
62  */
63 struct GNUNET_SQ_QueryParam
64 GNUNET_SQ_query_param_fixed_size (const void *ptr,
65                                   size_t ptr_size)
66 {
67   struct GNUNET_SQ_QueryParam qp = {
68     .conv = &bind_fixed_blob,
69     .data = ptr,
70     .size = ptr_size,
71     .num_params = 1
72   };
73   return qp;
74 }
75
76
77 /**
78  * Function called to convert input argument into SQL parameters.
79  *
80  * @param cls closure
81  * @param data pointer to input argument
82  * @param data_len number of bytes in @a data (if applicable)
83  * @param stmt sqlite statement to bind parameters for
84  * @param off offset of the argument to bind in @a stmt, numbered from 1,
85  *            so immediately suitable for passing to `sqlite3_bind`-functions.
86  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
87  */
88 static int
89 bind_string (void *cls,
90              const void *data,
91              size_t data_len,
92              sqlite3_stmt *stmt,
93              unsigned int off)
94 {
95   if (NULL == data)
96   {
97     if (SQLITE_OK !=
98         sqlite3_bind_null (stmt,
99                            (int) off))
100       return GNUNET_SYSERR;
101     return GNUNET_OK;
102   }
103   if (SQLITE_OK !=
104       sqlite3_bind_text (stmt,
105                          (int) off,
106                          (const char *) data,
107                          -1,
108                          SQLITE_TRANSIENT))
109     return GNUNET_SYSERR;
110   return GNUNET_OK;
111 }
112
113
114 /**
115  * Generate query parameter for a string.
116  *
117  * @param ptr pointer to the string query parameter to pass
118  */
119 struct GNUNET_SQ_QueryParam
120 GNUNET_SQ_query_param_string (const char *ptr)
121 {
122   struct GNUNET_SQ_QueryParam qp = {
123     .conv = &bind_string,
124     .data = ptr,
125     .num_params = 1
126   };
127   return qp;
128 }
129
130
131 /**
132  * Function called to convert input argument into SQL parameters.
133  *
134  * @param cls closure
135  * @param data pointer to input argument
136  * @param data_len number of bytes in @a data (if applicable)
137  * @param stmt sqlite statement to bind parameters for
138  * @param off offset of the argument to bind in @a stmt, numbered from 1,
139  *            so immediately suitable for passing to `sqlite3_bind`-functions.
140  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
141  */
142 static int
143 bind_rsa_pub (void *cls,
144               const void *data,
145               size_t data_len,
146               sqlite3_stmt *stmt,
147               unsigned int off)
148 {
149   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
150   char *buf;
151   size_t buf_size;
152
153   GNUNET_break (NULL == cls);
154   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
155                                                   &buf);
156   if (SQLITE_OK !=
157       sqlite3_bind_blob64 (stmt,
158                            (int) off,
159                            buf,
160                            (sqlite3_uint64) buf_size,
161                            SQLITE_TRANSIENT))
162   {
163     GNUNET_free (buf);
164     return GNUNET_SYSERR;
165   }
166   GNUNET_free (buf);
167   return GNUNET_OK;
168 }
169
170
171 /**
172  * Generate query parameter for an RSA public key.  The
173  * database must contain a BLOB type in the respective position.
174  *
175  * @param x the query parameter to pass.
176  */
177 struct GNUNET_SQ_QueryParam
178 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
179 {
180  struct GNUNET_SQ_QueryParam qp = {
181     .conv = &bind_rsa_pub,
182     .data = x,
183     .num_params = 1
184   };
185  return qp;
186 }
187
188
189 /**
190  * Function called to convert input argument into SQL parameters.
191  *
192  * @param cls closure
193  * @param data pointer to input argument
194  * @param data_len number of bytes in @a data (if applicable)
195  * @param stmt sqlite statement to bind parameters for
196  * @param off offset of the argument to bind in @a stmt, numbered from 1,
197  *            so immediately suitable for passing to `sqlite3_bind`-functions.
198  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
199  */
200 static int
201 bind_rsa_sig (void *cls,
202               const void *data,
203               size_t data_len,
204               sqlite3_stmt *stmt,
205               unsigned int off)
206 {
207   const struct GNUNET_CRYPTO_RsaSignature *sig = data;
208   char *buf;
209   size_t buf_size;
210
211   GNUNET_break (NULL == cls);
212   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
213                                                  &buf);
214   if (SQLITE_OK !=
215       sqlite3_bind_blob64 (stmt,
216                            (int) off,
217                            buf,
218                            (sqlite3_uint64) buf_size,
219                            SQLITE_TRANSIENT))
220   {
221     GNUNET_free (buf);
222     return GNUNET_SYSERR;
223   }
224   GNUNET_free (buf);
225   return GNUNET_OK;
226 }
227
228
229 /**
230  * Generate query parameter for an RSA signature.  The
231  * database must contain a BLOB type in the respective position.
232  *
233  * @param x the query parameter to pass
234  */
235 struct GNUNET_SQ_QueryParam
236 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
237 {
238  struct GNUNET_SQ_QueryParam qp = {
239     .conv = &bind_rsa_sig,
240     .data = x,
241     .num_params = 1
242   };
243  return qp;
244 }
245
246
247 /**
248  * Function called to convert input argument into SQL parameters.
249  *
250  * @param cls closure
251  * @param data pointer to input argument
252  * @param data_len number of bytes in @a data (if applicable)
253  * @param stmt sqlite statement to bind parameters for
254  * @param off offset of the argument to bind in @a stmt, numbered from 1,
255  *            so immediately suitable for passing to `sqlite3_bind`-functions.
256  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
257  */
258 static int
259 bind_abstime (void *cls,
260               const void *data,
261               size_t data_len,
262               sqlite3_stmt *stmt,
263               unsigned int off)
264 {
265   const struct GNUNET_TIME_Absolute *u = data;
266   struct GNUNET_TIME_Absolute abs;
267
268   abs = *u;
269   if (abs.abs_value_us > INT64_MAX)
270     abs.abs_value_us = INT64_MAX;
271   GNUNET_assert (sizeof (uint64_t) == data_len);
272   if (SQLITE_OK !=
273       sqlite3_bind_int64 (stmt,
274                           (int) off,
275                           (sqlite3_int64) abs.abs_value_us))
276     return GNUNET_SYSERR;
277   return GNUNET_OK;
278 }
279
280
281 /**
282  * Generate query parameter for an absolute time value.
283  * The database must store a 64-bit integer.
284  *
285  * @param x pointer to the query parameter to pass
286  */
287 struct GNUNET_SQ_QueryParam
288 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
289 {
290  struct GNUNET_SQ_QueryParam qp = {
291     .conv = &bind_abstime,
292     .data = x,
293     .size = sizeof (struct GNUNET_TIME_Absolute),
294     .num_params = 1
295   };
296   return qp;
297 }
298
299
300 /**
301  * Function called to convert input argument into SQL parameters.
302  *
303  * @param cls closure
304  * @param data pointer to input argument
305  * @param data_len number of bytes in @a data (if applicable)
306  * @param stmt sqlite statement to bind parameters for
307  * @param off offset of the argument to bind in @a stmt, numbered from 1,
308  *            so immediately suitable for passing to `sqlite3_bind`-functions.
309  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
310  */
311 static int
312 bind_nbotime (void *cls,
313               const void *data,
314               size_t data_len,
315               sqlite3_stmt *stmt,
316               unsigned int off)
317 {
318   const struct GNUNET_TIME_AbsoluteNBO *u = data;
319   struct GNUNET_TIME_Absolute abs;
320
321   abs = GNUNET_TIME_absolute_ntoh (*u);
322   if (abs.abs_value_us > INT64_MAX)
323     abs.abs_value_us = INT64_MAX;
324   GNUNET_assert (sizeof (uint64_t) == data_len);
325   if (SQLITE_OK !=
326       sqlite3_bind_int64 (stmt,
327                           (int) off,
328                           (sqlite3_int64) abs.abs_value_us))
329     return GNUNET_SYSERR;
330   return GNUNET_OK;
331 }
332
333
334 /**
335  * Generate query parameter for an absolute time value.
336  * The database must store a 64-bit integer.
337  *
338  * @param x pointer to the query parameter to pass
339  */
340 struct GNUNET_SQ_QueryParam
341 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
342 {
343  struct GNUNET_SQ_QueryParam qp = {
344     .conv = &bind_nbotime,
345     .data = x,
346     .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
347     .num_params = 1
348   };
349   return qp;
350 }
351
352
353 /**
354  * Function called to convert input argument into SQL parameters.
355  *
356  * @param cls closure
357  * @param data pointer to input argument
358  * @param data_len number of bytes in @a data (if applicable)
359  * @param stmt sqlite statement to bind parameters for
360  * @param off offset of the argument to bind in @a stmt, numbered from 1,
361  *            so immediately suitable for passing to `sqlite3_bind`-functions.
362  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
363  */
364 static int
365 bind_u16 (void *cls,
366           const void *data,
367           size_t data_len,
368           sqlite3_stmt *stmt,
369           unsigned int off)
370 {
371   const uint16_t *u = data;
372
373   GNUNET_assert (sizeof (uint16_t) == data_len);
374   if (SQLITE_OK !=
375       sqlite3_bind_int (stmt,
376                         (int) off,
377                         (int) *u))
378     return GNUNET_SYSERR;
379   return GNUNET_OK;
380 }
381
382
383 /**
384  * Generate query parameter for an uint16_t in host byte order.
385  *
386  * @param x pointer to the query parameter to pass
387  */
388 struct GNUNET_SQ_QueryParam
389 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
390 {
391  struct GNUNET_SQ_QueryParam qp = {
392     .conv = &bind_u16,
393     .data = x,
394     .size = sizeof (uint16_t),
395     .num_params = 1
396   };
397   return qp;
398 }
399
400
401 /**
402  * Function called to convert input argument into SQL parameters.
403  *
404  * @param cls closure
405  * @param data pointer to input argument
406  * @param data_len number of bytes in @a data (if applicable)
407  * @param stmt sqlite statement to bind parameters for
408  * @param off offset of the argument to bind in @a stmt, numbered from 1,
409  *            so immediately suitable for passing to `sqlite3_bind`-functions.
410  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
411  */
412 static int
413 bind_u32 (void *cls,
414           const void *data,
415           size_t data_len,
416           sqlite3_stmt *stmt,
417           unsigned int off)
418 {
419   const uint32_t *u = data;
420
421   GNUNET_assert (sizeof (uint32_t) == data_len);
422   if (SQLITE_OK !=
423       sqlite3_bind_int64 (stmt,
424                           (int) off,
425                           (sqlite3_int64) *u))
426     return GNUNET_SYSERR;
427   return GNUNET_OK;
428 }
429
430 /**
431  * Generate query parameter for an uint32_t in host byte order.
432  *
433  * @param x pointer to the query parameter to pass
434  */
435 struct GNUNET_SQ_QueryParam
436 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
437 {
438  struct GNUNET_SQ_QueryParam qp = {
439     .conv = &bind_u32,
440     .data = x,
441     .size = sizeof (uint32_t),
442     .num_params = 1
443   };
444   return qp;
445 }
446
447
448 /**
449  * Function called to convert input argument into SQL parameters.
450  *
451  * @param cls closure
452  * @param data pointer to input argument
453  * @param data_len number of bytes in @a data (if applicable)
454  * @param stmt sqlite statement to bind parameters for
455  * @param off offset of the argument to bind in @a stmt, numbered from 1,
456  *            so immediately suitable for passing to `sqlite3_bind`-functions.
457  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
458  */
459 static int
460 bind_u64 (void *cls,
461           const void *data,
462           size_t data_len,
463           sqlite3_stmt *stmt,
464           unsigned int off)
465 {
466   const uint64_t *u = data;
467
468   GNUNET_assert (sizeof (uint64_t) == data_len);
469   if (SQLITE_OK !=
470       sqlite3_bind_int64 (stmt,
471                           (int) off,
472                           (sqlite3_int64) *u))
473     return GNUNET_SYSERR;
474   return GNUNET_OK;
475 }
476
477
478 /**
479  * Generate query parameter for an uint16_t in host byte order.
480  *
481  * @param x pointer to the query parameter to pass
482  */
483 struct GNUNET_SQ_QueryParam
484 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
485 {
486   struct GNUNET_SQ_QueryParam qp = {
487     .conv = &bind_u64,
488     .data = x,
489     .size = sizeof (uint64_t),
490     .num_params = 1
491   };
492   return qp;
493 }
494
495 /* end of sq_query_helper.c */