reduce loop counters to more practical levels
[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 (NULL == data)
94   {
95     if (SQLITE_OK !=
96         sqlite3_bind_null (stmt,
97                            (int) off))
98       return GNUNET_SYSERR;
99     return GNUNET_OK;
100   }
101   if (SQLITE_OK !=
102       sqlite3_bind_text (stmt,
103                          (int) off,
104                          (const char *) data,
105                          -1,
106                          SQLITE_TRANSIENT))
107     return GNUNET_SYSERR;
108   return GNUNET_OK;
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_SQ_QueryParam
118 GNUNET_SQ_query_param_string (const char *ptr)
119 {
120   struct GNUNET_SQ_QueryParam qp = {
121     .conv = &bind_string,
122     .data = ptr,
123     .num_params = 1
124   };
125   return qp;
126 }
127
128
129 /**
130  * Function called to convert input argument into SQL parameters.
131  *
132  * @param cls closure
133  * @param data pointer to input argument
134  * @param data_len number of bytes in @a data (if applicable)
135  * @param stmt sqlite statement to bind parameters for
136  * @param off offset of the argument to bind in @a stmt, numbered from 1,
137  *            so immediately suitable for passing to `sqlite3_bind`-functions.
138  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
139  */
140 static int
141 bind_rsa_pub (void *cls,
142               const void *data,
143               size_t data_len,
144               sqlite3_stmt *stmt,
145               unsigned int off)
146 {
147   const struct GNUNET_CRYPTO_RsaPublicKey *rsa = data;
148   char *buf;
149   size_t buf_size;
150
151   GNUNET_break (NULL == cls);
152   buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
153                                                   &buf);
154   if (SQLITE_OK !=
155       sqlite3_bind_blob64 (stmt,
156                            (int) off,
157                            buf,
158                            (sqlite3_uint64) buf_size,
159                            SQLITE_TRANSIENT))
160   {
161     GNUNET_free (buf);
162     return GNUNET_SYSERR;
163   }
164   GNUNET_free (buf);
165   return GNUNET_OK;
166 }
167
168
169 /**
170  * Generate query parameter for an RSA public key.  The
171  * database must contain a BLOB type in the respective position.
172  *
173  * @param x the query parameter to pass.
174  */
175 struct GNUNET_SQ_QueryParam
176 GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
177 {
178  struct GNUNET_SQ_QueryParam qp = {
179     .conv = &bind_rsa_pub,
180     .data = x,
181     .num_params = 1
182   };
183  return qp;
184 }
185
186
187 /**
188  * Function called to convert input argument into SQL parameters.
189  *
190  * @param cls closure
191  * @param data pointer to input argument
192  * @param data_len number of bytes in @a data (if applicable)
193  * @param stmt sqlite statement to bind parameters for
194  * @param off offset of the argument to bind in @a stmt, numbered from 1,
195  *            so immediately suitable for passing to `sqlite3_bind`-functions.
196  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
197  */
198 static int
199 bind_rsa_sig (void *cls,
200               const void *data,
201               size_t data_len,
202               sqlite3_stmt *stmt,
203               unsigned int off)
204 {
205   const struct GNUNET_CRYPTO_RsaSignature *sig = data;
206   char *buf;
207   size_t buf_size;
208
209   GNUNET_break (NULL == cls);
210   buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
211                                                  &buf);
212   if (SQLITE_OK !=
213       sqlite3_bind_blob64 (stmt,
214                            (int) off,
215                            buf,
216                            (sqlite3_uint64) buf_size,
217                            SQLITE_TRANSIENT))
218   {
219     GNUNET_free (buf);
220     return GNUNET_SYSERR;
221   }
222   GNUNET_free (buf);
223   return GNUNET_OK;
224 }
225
226
227 /**
228  * Generate query parameter for an RSA signature.  The
229  * database must contain a BLOB type in the respective position.
230  *
231  * @param x the query parameter to pass
232  */
233 struct GNUNET_SQ_QueryParam
234 GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
235 {
236  struct GNUNET_SQ_QueryParam qp = {
237     .conv = &bind_rsa_sig,
238     .data = x,
239     .num_params = 1
240   };
241  return qp;
242 }
243
244
245 /**
246  * Function called to convert input argument into SQL parameters.
247  *
248  * @param cls closure
249  * @param data pointer to input argument
250  * @param data_len number of bytes in @a data (if applicable)
251  * @param stmt sqlite statement to bind parameters for
252  * @param off offset of the argument to bind in @a stmt, numbered from 1,
253  *            so immediately suitable for passing to `sqlite3_bind`-functions.
254  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
255  */
256 static int
257 bind_abstime (void *cls,
258               const void *data,
259               size_t data_len,
260               sqlite3_stmt *stmt,
261               unsigned int off)
262 {
263   const struct GNUNET_TIME_Absolute *u = data;
264   struct GNUNET_TIME_Absolute abs;
265
266   abs = *u;
267   if (abs.abs_value_us > INT64_MAX)
268     abs.abs_value_us = INT64_MAX;
269   GNUNET_assert (sizeof (uint64_t) == data_len);
270   if (SQLITE_OK !=
271       sqlite3_bind_int64 (stmt,
272                           (int) off,
273                           (sqlite3_int64) abs.abs_value_us))
274     return GNUNET_SYSERR;
275   return GNUNET_OK;
276 }
277
278
279 /**
280  * Generate query parameter for an absolute time value.
281  * The database must store a 64-bit integer.
282  *
283  * @param x pointer to the query parameter to pass
284  */
285 struct GNUNET_SQ_QueryParam
286 GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
287 {
288  struct GNUNET_SQ_QueryParam qp = {
289     .conv = &bind_abstime,
290     .data = x,
291     .size = sizeof (struct GNUNET_TIME_Absolute),
292     .num_params = 1
293   };
294   return qp;
295 }
296
297
298 /**
299  * Function called to convert input argument into SQL parameters.
300  *
301  * @param cls closure
302  * @param data pointer to input argument
303  * @param data_len number of bytes in @a data (if applicable)
304  * @param stmt sqlite statement to bind parameters for
305  * @param off offset of the argument to bind in @a stmt, numbered from 1,
306  *            so immediately suitable for passing to `sqlite3_bind`-functions.
307  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
308  */
309 static int
310 bind_nbotime (void *cls,
311               const void *data,
312               size_t data_len,
313               sqlite3_stmt *stmt,
314               unsigned int off)
315 {
316   const struct GNUNET_TIME_AbsoluteNBO *u = data;
317   struct GNUNET_TIME_Absolute abs;
318
319   abs = GNUNET_TIME_absolute_ntoh (*u);
320   if (abs.abs_value_us > INT64_MAX)
321     abs.abs_value_us = INT64_MAX;
322   GNUNET_assert (sizeof (uint64_t) == data_len);
323   if (SQLITE_OK !=
324       sqlite3_bind_int64 (stmt,
325                           (int) off,
326                           (sqlite3_int64) abs.abs_value_us))
327     return GNUNET_SYSERR;
328   return GNUNET_OK;
329 }
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  */
338 struct GNUNET_SQ_QueryParam
339 GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
340 {
341  struct GNUNET_SQ_QueryParam qp = {
342     .conv = &bind_nbotime,
343     .data = x,
344     .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
345     .num_params = 1
346   };
347   return qp;
348 }
349
350
351 /**
352  * Function called to convert input argument into SQL parameters.
353  *
354  * @param cls closure
355  * @param data pointer to input argument
356  * @param data_len number of bytes in @a data (if applicable)
357  * @param stmt sqlite statement to bind parameters for
358  * @param off offset of the argument to bind in @a stmt, numbered from 1,
359  *            so immediately suitable for passing to `sqlite3_bind`-functions.
360  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
361  */
362 static int
363 bind_u16 (void *cls,
364           const void *data,
365           size_t data_len,
366           sqlite3_stmt *stmt,
367           unsigned int off)
368 {
369   const uint16_t *u = data;
370
371   GNUNET_assert (sizeof (uint16_t) == data_len);
372   if (SQLITE_OK !=
373       sqlite3_bind_int (stmt,
374                         (int) off,
375                         (int) *u))
376     return GNUNET_SYSERR;
377   return GNUNET_OK;
378 }
379
380
381 /**
382  * Generate query parameter for an uint16_t in host byte order.
383  *
384  * @param x pointer to the query parameter to pass
385  */
386 struct GNUNET_SQ_QueryParam
387 GNUNET_SQ_query_param_uint16 (const uint16_t *x)
388 {
389  struct GNUNET_SQ_QueryParam qp = {
390     .conv = &bind_u16,
391     .data = x,
392     .size = sizeof (uint16_t),
393     .num_params = 1
394   };
395   return qp;
396 }
397
398
399 /**
400  * Function called to convert input argument into SQL parameters.
401  *
402  * @param cls closure
403  * @param data pointer to input argument
404  * @param data_len number of bytes in @a data (if applicable)
405  * @param stmt sqlite statement to bind parameters for
406  * @param off offset of the argument to bind in @a stmt, numbered from 1,
407  *            so immediately suitable for passing to `sqlite3_bind`-functions.
408  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
409  */
410 static int
411 bind_u32 (void *cls,
412           const void *data,
413           size_t data_len,
414           sqlite3_stmt *stmt,
415           unsigned int off)
416 {
417   const uint32_t *u = data;
418
419   GNUNET_assert (sizeof (uint32_t) == data_len);
420   if (SQLITE_OK !=
421       sqlite3_bind_int64 (stmt,
422                           (int) off,
423                           (sqlite3_int64) *u))
424     return GNUNET_SYSERR;
425   return GNUNET_OK;
426 }
427
428 /**
429  * Generate query parameter for an uint32_t in host byte order.
430  *
431  * @param x pointer to the query parameter to pass
432  */
433 struct GNUNET_SQ_QueryParam
434 GNUNET_SQ_query_param_uint32 (const uint32_t *x)
435 {
436  struct GNUNET_SQ_QueryParam qp = {
437     .conv = &bind_u32,
438     .data = x,
439     .size = sizeof (uint32_t),
440     .num_params = 1
441   };
442   return qp;
443 }
444
445
446 /**
447  * Function called to convert input argument into SQL parameters.
448  *
449  * @param cls closure
450  * @param data pointer to input argument
451  * @param data_len number of bytes in @a data (if applicable)
452  * @param stmt sqlite statement to bind parameters for
453  * @param off offset of the argument to bind in @a stmt, numbered from 1,
454  *            so immediately suitable for passing to `sqlite3_bind`-functions.
455  * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
456  */
457 static int
458 bind_u64 (void *cls,
459           const void *data,
460           size_t data_len,
461           sqlite3_stmt *stmt,
462           unsigned int off)
463 {
464   const uint64_t *u = data;
465
466   GNUNET_assert (sizeof (uint64_t) == data_len);
467   if (SQLITE_OK !=
468       sqlite3_bind_int64 (stmt,
469                           (int) off,
470                           (sqlite3_int64) *u))
471     return GNUNET_SYSERR;
472   return GNUNET_OK;
473 }
474
475
476 /**
477  * Generate query parameter for an uint16_t in host byte order.
478  *
479  * @param x pointer to the query parameter to pass
480  */
481 struct GNUNET_SQ_QueryParam
482 GNUNET_SQ_query_param_uint64 (const uint64_t *x)
483 {
484   struct GNUNET_SQ_QueryParam qp = {
485     .conv = &bind_u64,
486     .data = x,
487     .size = sizeof (uint64_t),
488     .num_params = 1
489   };
490   return qp;
491 }
492
493 /* end of sq_query_helper.c */