aboutsummaryrefslogtreecommitdiff
path: root/src/sq/sq_query_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sq/sq_query_helper.c')
-rw-r--r--src/sq/sq_query_helper.c314
1 files changed, 314 insertions, 0 deletions
diff --git a/src/sq/sq_query_helper.c b/src/sq/sq_query_helper.c
index 613a0c746..5529c5e6c 100644
--- a/src/sq/sq_query_helper.c
+++ b/src/sq/sq_query_helper.c
@@ -18,10 +18,40 @@
18 * @brief helper functions for queries 18 * @brief helper functions for queries
19 * @author Christian Grothoff 19 * @author Christian Grothoff
20 */ 20 */
21#include "platform.h"
21#include "gnunet_sq_lib.h" 22#include "gnunet_sq_lib.h"
22 23
23 24
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 */
36static int
37bind_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/**
25 * Generate query parameter for a buffer @a ptr of 55 * Generate query parameter for a buffer @a ptr of
26 * @a ptr_size bytes. 56 * @a ptr_size bytes.
27 * 57 *
@@ -32,6 +62,42 @@ struct GNUNET_SQ_QueryParam
32GNUNET_SQ_query_param_fixed_size (const void *ptr, 62GNUNET_SQ_query_param_fixed_size (const void *ptr,
33 size_t ptr_size) 63 size_t ptr_size)
34{ 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 */
86static int
87bind_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;
35} 101}
36 102
37 103
@@ -43,6 +109,52 @@ GNUNET_SQ_query_param_fixed_size (const void *ptr,
43struct GNUNET_SQ_QueryParam 109struct GNUNET_SQ_QueryParam
44GNUNET_SQ_query_param_string (const char *ptr) 110GNUNET_SQ_query_param_string (const char *ptr)
45{ 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 */
132static int
133bind_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;
46} 158}
47 159
48 160
@@ -55,6 +167,52 @@ GNUNET_SQ_query_param_string (const char *ptr)
55struct GNUNET_SQ_QueryParam 167struct GNUNET_SQ_QueryParam
56GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x) 168GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x)
57{ 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 */
190static int
191bind_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;
58} 216}
59 217
60 218
@@ -67,6 +225,12 @@ GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x
67struct GNUNET_SQ_QueryParam 225struct GNUNET_SQ_QueryParam
68GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x) 226GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
69{ 227{
228 struct GNUNET_SQ_QueryParam qp = {
229 .conv = &bind_rsa_sig,
230 .data = x,
231 .num_params = 1
232 };
233 return qp;
70} 234}
71 235
72 236
@@ -79,6 +243,39 @@ GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
79struct GNUNET_SQ_QueryParam 243struct GNUNET_SQ_QueryParam
80GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) 244GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
81{ 245{
246 return GNUNET_SQ_query_param_uint64 (&x->abs_value_us);
247}
248
249
250/**
251 * Function called to convert input argument into SQL parameters.
252 *
253 * @param cls closure
254 * @param data pointer to input argument
255 * @param data_len number of bytes in @a data (if applicable)
256 * @param stmt sqlite statement to bind parameters for
257 * @param off offset of the argument to bind in @a stmt, numbered from 1,
258 * so immediately suitable for passing to `sqlite3_bind`-functions.
259 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
260 */
261static int
262bind_nbotime (void *cls,
263 const void *data,
264 size_t data_len,
265 sqlite3_stmt *stmt,
266 unsigned int off)
267{
268 const struct GNUNET_TIME_AbsoluteNBO *u = data;
269 struct GNUNET_TIME_Absolute abs;
270
271 abs = GNUNET_TIME_absolute_ntoh (*u);
272 GNUNET_assert (sizeof (uint64_t) == data_len);
273 if (SQLITE_OK !=
274 sqlite3_bind_int64 (stmt,
275 (int) off,
276 (sqlite3_int64) abs.abs_value_us))
277 return GNUNET_SYSERR;
278 return GNUNET_OK;
82} 279}
83 280
84 281
@@ -91,6 +288,43 @@ GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
91struct GNUNET_SQ_QueryParam 288struct GNUNET_SQ_QueryParam
92GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x) 289GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x)
93{ 290{
291 struct GNUNET_SQ_QueryParam qp = {
292 .conv = &bind_nbotime,
293 .data = x,
294 .size = sizeof (struct GNUNET_TIME_AbsoluteNBO),
295 .num_params = 1
296 };
297 return qp;
298}
299
300
301/**
302 * Function called to convert input argument into SQL parameters.
303 *
304 * @param cls closure
305 * @param data pointer to input argument
306 * @param data_len number of bytes in @a data (if applicable)
307 * @param stmt sqlite statement to bind parameters for
308 * @param off offset of the argument to bind in @a stmt, numbered from 1,
309 * so immediately suitable for passing to `sqlite3_bind`-functions.
310 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
311 */
312static int
313bind_u16 (void *cls,
314 const void *data,
315 size_t data_len,
316 sqlite3_stmt *stmt,
317 unsigned int off)
318{
319 const uint16_t *u = data;
320
321 GNUNET_assert (sizeof (uint16_t) == data_len);
322 if (SQLITE_OK !=
323 sqlite3_bind_int (stmt,
324 (int) off,
325 (int) *u))
326 return GNUNET_SYSERR;
327 return GNUNET_OK;
94} 328}
95 329
96 330
@@ -102,10 +336,46 @@ GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x
102struct GNUNET_SQ_QueryParam 336struct GNUNET_SQ_QueryParam
103GNUNET_SQ_query_param_uint16 (const uint16_t *x) 337GNUNET_SQ_query_param_uint16 (const uint16_t *x)
104{ 338{
339 struct GNUNET_SQ_QueryParam qp = {
340 .conv = &bind_u16,
341 .data = x,
342 .size = sizeof (uint16_t),
343 .num_params = 1
344 };
345 return qp;
105} 346}
106 347
107 348
108/** 349/**
350 * Function called to convert input argument into SQL parameters.
351 *
352 * @param cls closure
353 * @param data pointer to input argument
354 * @param data_len number of bytes in @a data (if applicable)
355 * @param stmt sqlite statement to bind parameters for
356 * @param off offset of the argument to bind in @a stmt, numbered from 1,
357 * so immediately suitable for passing to `sqlite3_bind`-functions.
358 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
359 */
360static int
361bind_u32 (void *cls,
362 const void *data,
363 size_t data_len,
364 sqlite3_stmt *stmt,
365 unsigned int off)
366{
367 const uint32_t *u = data;
368
369 GNUNET_assert (sizeof (uint32_t) == data_len);
370 if (SQLITE_OK !=
371 sqlite3_bind_int64 (stmt,
372 (int) off,
373 (sqlite3_int64) *u))
374 return GNUNET_SYSERR;
375 return GNUNET_OK;
376}
377
378/**
109 * Generate query parameter for an uint32_t in host byte order. 379 * Generate query parameter for an uint32_t in host byte order.
110 * 380 *
111 * @param x pointer to the query parameter to pass 381 * @param x pointer to the query parameter to pass
@@ -113,6 +383,43 @@ GNUNET_SQ_query_param_uint16 (const uint16_t *x)
113struct GNUNET_SQ_QueryParam 383struct GNUNET_SQ_QueryParam
114GNUNET_SQ_query_param_uint32 (const uint32_t *x) 384GNUNET_SQ_query_param_uint32 (const uint32_t *x)
115{ 385{
386 struct GNUNET_SQ_QueryParam qp = {
387 .conv = &bind_u32,
388 .data = x,
389 .size = sizeof (uint32_t),
390 .num_params = 1
391 };
392 return qp;
393}
394
395
396/**
397 * Function called to convert input argument into SQL parameters.
398 *
399 * @param cls closure
400 * @param data pointer to input argument
401 * @param data_len number of bytes in @a data (if applicable)
402 * @param stmt sqlite statement to bind parameters for
403 * @param off offset of the argument to bind in @a stmt, numbered from 1,
404 * so immediately suitable for passing to `sqlite3_bind`-functions.
405 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
406 */
407static int
408bind_u64 (void *cls,
409 const void *data,
410 size_t data_len,
411 sqlite3_stmt *stmt,
412 unsigned int off)
413{
414 const uint64_t *u = data;
415
416 GNUNET_assert (sizeof (uint64_t) == data_len);
417 if (SQLITE_OK !=
418 sqlite3_bind_int64 (stmt,
419 (int) off,
420 (sqlite3_int64) *u))
421 return GNUNET_SYSERR;
422 return GNUNET_OK;
116} 423}
117 424
118 425
@@ -124,6 +431,13 @@ GNUNET_SQ_query_param_uint32 (const uint32_t *x)
124struct GNUNET_SQ_QueryParam 431struct GNUNET_SQ_QueryParam
125GNUNET_SQ_query_param_uint64 (const uint64_t *x) 432GNUNET_SQ_query_param_uint64 (const uint64_t *x)
126{ 433{
434 struct GNUNET_SQ_QueryParam qp = {
435 .conv = &bind_u64,
436 .data = x,
437 .size = sizeof (uint64_t),
438 .num_params = 1
439 };
440 return qp;
127} 441}
128 442
129/* end of sq_query_helper.c */ 443/* end of sq_query_helper.c */