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