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