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