aboutsummaryrefslogtreecommitdiff
path: root/src/sq
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-03-12 02:51:36 +0100
committerChristian Grothoff <christian@grothoff.org>2017-03-12 02:51:36 +0100
commita707b513688690a3dad9dc39535900da73a79f28 (patch)
tree0439c267fc54960441ae09f0ca5c62e0c41f2cb7 /src/sq
parent538d7fde8cb1c0d079f01f2290aa3e3e2744beff (diff)
downloadgnunet-a707b513688690a3dad9dc39535900da73a79f28.tar.gz
gnunet-a707b513688690a3dad9dc39535900da73a79f28.zip
converting datacache to sqlite, fixing misc. issues in libgnunetsq
Diffstat (limited to 'src/sq')
-rw-r--r--src/sq/sq.c25
-rw-r--r--src/sq/sq_query_helper.c44
-rw-r--r--src/sq/sq_result_helper.c67
3 files changed, 134 insertions, 2 deletions
diff --git a/src/sq/sq.c b/src/sq/sq.c
index 114de2d88..089ebf0ff 100644
--- a/src/sq/sq.c
+++ b/src/sq/sq.c
@@ -90,7 +90,12 @@ GNUNET_SQ_extract_result (sqlite3_stmt *result,
90 j, 90 j,
91 rs[i].result_size, 91 rs[i].result_size,
92 rs[i].dst)) 92 rs[i].dst))
93 {
94 for (unsigned int k=0;k<i;k++)
95 if (NULL != rs[k].cleaner)
96 rs[k].cleaner (rs[k].cls);
93 return GNUNET_SYSERR; 97 return GNUNET_SYSERR;
98 }
94 GNUNET_assert (0 != rs[i].num_params); 99 GNUNET_assert (0 != rs[i].num_params);
95 j += rs[i].num_params; 100 j += rs[i].num_params;
96 } 101 }
@@ -112,4 +117,24 @@ GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs)
112 rs[i].cleaner (rs[i].cls); 117 rs[i].cleaner (rs[i].cls);
113} 118}
114 119
120
121/**
122 * Reset @a stmt and log error.
123 *
124 * @param dbh database handle
125 * @param stmt statement to reset
126 */
127void
128GNUNET_SQ_reset (sqlite3 *dbh,
129 sqlite3_stmt *stmt)
130{
131 if (SQLITE_OK !=
132 sqlite3_reset (stmt))
133 GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
134 "sqlite",
135 _("Failed to reset sqlite statement with error: %s\n"),
136 sqlite3_errmsg (dbh));
137}
138
139
115/* end of sq.c */ 140/* end of sq.c */
diff --git a/src/sq/sq_query_helper.c b/src/sq/sq_query_helper.c
index 5529c5e6c..a04b4ced4 100644
--- a/src/sq/sq_query_helper.c
+++ b/src/sq/sq_query_helper.c
@@ -235,6 +235,40 @@ GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
235 235
236 236
237/** 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 */
248static int
249bind_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/**
238 * Generate query parameter for an absolute time value. 272 * Generate query parameter for an absolute time value.
239 * The database must store a 64-bit integer. 273 * The database must store a 64-bit integer.
240 * 274 *
@@ -243,7 +277,13 @@ GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
243struct GNUNET_SQ_QueryParam 277struct GNUNET_SQ_QueryParam
244GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x) 278GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
245{ 279{
246 return GNUNET_SQ_query_param_uint64 (&x->abs_value_us); 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;
247} 287}
248 288
249 289
@@ -269,6 +309,8 @@ bind_nbotime (void *cls,
269 struct GNUNET_TIME_Absolute abs; 309 struct GNUNET_TIME_Absolute abs;
270 310
271 abs = GNUNET_TIME_absolute_ntoh (*u); 311 abs = GNUNET_TIME_absolute_ntoh (*u);
312 if (abs.abs_value_us > INT64_MAX)
313 abs.abs_value_us = INT64_MAX;
272 GNUNET_assert (sizeof (uint64_t) == data_len); 314 GNUNET_assert (sizeof (uint64_t) == data_len);
273 if (SQLITE_OK != 315 if (SQLITE_OK !=
274 sqlite3_bind_int64 (stmt, 316 sqlite3_bind_int64 (stmt,
diff --git a/src/sq/sq_result_helper.c b/src/sq/sq_result_helper.c
index eaf606aa4..fad3f3c8d 100644
--- a/src/sq/sq_result_helper.c
+++ b/src/sq/sq_result_helper.c
@@ -46,6 +46,15 @@ extract_var_blob (void *cls,
46 const void *ret; 46 const void *ret;
47 void **rdst = (void **) dst; 47 void **rdst = (void **) dst;
48 48
49 if (SQLITE_NULL ==
50 sqlite3_column_type (result,
51 column))
52 {
53 *rdst = NULL;
54 *dst_size = 0;
55 return GNUNET_YES;
56 }
57
49 if (SQLITE_BLOB != 58 if (SQLITE_BLOB !=
50 sqlite3_column_type (result, 59 sqlite3_column_type (result,
51 column)) 60 column))
@@ -142,6 +151,14 @@ extract_fixed_blob (void *cls,
142 int have; 151 int have;
143 const void *ret; 152 const void *ret;
144 153
154 if ( (0 == *dst_size) &&
155 (SQLITE_NULL ==
156 sqlite3_column_type (result,
157 column)) )
158 {
159 return GNUNET_YES;
160 }
161
145 if (SQLITE_BLOB != 162 if (SQLITE_BLOB !=
146 sqlite3_column_type (result, 163 sqlite3_column_type (result,
147 column)) 164 column))
@@ -459,6 +476,45 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
459 476
460 477
461/** 478/**
479 * Extract absolute time value from a Postgres database @a result at row @a row.
480 *
481 * @param cls closure
482 * @param result where to extract data from
483 * @param column column to extract data from
484 * @param[in,out] dst_size where to store size of result, may be NULL
485 * @param[out] dst where to store the result
486 * @return
487 * #GNUNET_YES if all results could be extracted
488 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
489 */
490static int
491extract_abs_time (void *cls,
492 sqlite3_stmt *result,
493 unsigned int column,
494 size_t *dst_size,
495 void *dst)
496{
497 struct GNUNET_TIME_Absolute *u = dst;
498 struct GNUNET_TIME_Absolute t;
499
500 GNUNET_assert (sizeof (uint64_t) == *dst_size);
501 if (SQLITE_INTEGER !=
502 sqlite3_column_type (result,
503 column))
504 {
505 GNUNET_break (0);
506 return GNUNET_SYSERR;
507 }
508 t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
509 column);
510 if (INT64_MAX == t.abs_value_us)
511 t = GNUNET_TIME_UNIT_FOREVER_ABS;
512 *u = t;
513 return GNUNET_OK;
514}
515
516
517/**
462 * Absolute time expected. 518 * Absolute time expected.
463 * 519 *
464 * @param[out] at where to store the result 520 * @param[out] at where to store the result
@@ -467,7 +523,14 @@ GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig)
467struct GNUNET_SQ_ResultSpec 523struct GNUNET_SQ_ResultSpec
468GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at) 524GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at)
469{ 525{
470 return GNUNET_SQ_result_spec_uint64 (&at->abs_value_us); 526 struct GNUNET_SQ_ResultSpec rs = {
527 .conv = &extract_abs_time,
528 .dst = at,
529 .dst_size = sizeof (struct GNUNET_TIME_Absolute),
530 .num_params = 1
531 };
532
533 return rs;
471} 534}
472 535
473 536
@@ -503,6 +566,8 @@ extract_abs_time_nbo (void *cls,
503 } 566 }
504 t.abs_value_us = (uint64_t) sqlite3_column_int64 (result, 567 t.abs_value_us = (uint64_t) sqlite3_column_int64 (result,
505 column); 568 column);
569 if (INT64_MAX == t.abs_value_us)
570 t = GNUNET_TIME_UNIT_FOREVER_ABS;
506 *u = GNUNET_TIME_absolute_hton (t); 571 *u = GNUNET_TIME_absolute_hton (t);
507 return GNUNET_OK; 572 return GNUNET_OK;
508} 573}