diff options
author | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-06-03 16:08:23 +0000 |
---|---|---|
committer | Christophe Genevey Metat <genevey.christophe@gmail.com> | 2016-06-03 16:08:23 +0000 |
commit | 67dc0efd426a379b818155ed87f3c1a76d9e352f (patch) | |
tree | 80eed7316d10a21a665a51e69846273beeac4947 /src | |
parent | 6a81c1536eab851aa192b39b00801304377a6b86 (diff) | |
download | gnunet-67dc0efd426a379b818155ed87f3c1a76d9e352f.tar.gz gnunet-67dc0efd426a379b818155ed87f3c1a76d9e352f.zip |
start to fix extract result
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_my_lib.h | 2 | ||||
-rw-r--r-- | src/my/my.c | 16 | ||||
-rw-r--r-- | src/my/my_query_helper.c | 11 | ||||
-rw-r--r-- | src/my/my_result_helper.c | 426 | ||||
-rw-r--r-- | src/my/test_my.c | 81 |
5 files changed, 416 insertions, 120 deletions
diff --git a/src/include/gnunet_my_lib.h b/src/include/gnunet_my_lib.h index 1ab139d31..f42276554 100644 --- a/src/include/gnunet_my_lib.h +++ b/src/include/gnunet_my_lib.h | |||
@@ -455,7 +455,7 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
455 | * @param rs reult specification to clean up | 455 | * @param rs reult specification to clean up |
456 | */ | 456 | */ |
457 | void | 457 | void |
458 | GNUNET_MY_cleanup_result (struct GNUNET_PQ_ResultSpec *rs); | 458 | GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs); |
459 | 459 | ||
460 | 460 | ||
461 | #if 0 /* keep Emacsens' auto-indent happy */ | 461 | #if 0 /* keep Emacsens' auto-indent happy */ |
diff --git a/src/my/my.c b/src/my/my.c index 1d78a08cf..d4e72ae7c 100644 --- a/src/my/my.c +++ b/src/my/my.c | |||
@@ -129,7 +129,7 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
129 | } | 129 | } |
130 | 130 | ||
131 | num_fields = 0; | 131 | num_fields = 0; |
132 | for (i=0;NULL != rs[i].conv;i++) | 132 | for (i=0;NULL != rs[i].pre_conv;i++) |
133 | num_fields += rs[i].num_fields; | 133 | num_fields += rs[i].num_fields; |
134 | 134 | ||
135 | if (mysql_stmt_field_count (stmt) != num_fields) | 135 | if (mysql_stmt_field_count (stmt) != num_fields) |
@@ -145,12 +145,12 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
145 | 145 | ||
146 | memset (result, 0, sizeof (MYSQL_BIND) * num_fields); | 146 | memset (result, 0, sizeof (MYSQL_BIND) * num_fields); |
147 | field_off = 0; | 147 | field_off = 0; |
148 | for (i=0;NULL != rs[i].conv;i++) | 148 | for (i=0;NULL != rs[i].pre_conv;i++) |
149 | { | 149 | { |
150 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | 150 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
151 | 151 | ||
152 | if (GNUNET_OK != | 152 | if (GNUNET_OK != |
153 | rp->pre_conv (rp->cls, | 153 | rp->pre_conv (rp->conv_cls, |
154 | rp, | 154 | rp, |
155 | stmt, | 155 | stmt, |
156 | field_off, | 156 | field_off, |
@@ -186,13 +186,13 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
186 | return GNUNET_SYSERR; | 186 | return GNUNET_SYSERR; |
187 | } | 187 | } |
188 | field_off = 0; | 188 | field_off = 0; |
189 | for (i=0;NULL != rs[i].conv;i++) | 189 | for (i=0;NULL != rs[i].post_conv;i++) |
190 | { | 190 | { |
191 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | 191 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
192 | 192 | ||
193 | if (NULL != rp->post_conv) | 193 | if (NULL != rp->post_conv) |
194 | if (GNUNET_OK != | 194 | if (GNUNET_OK != |
195 | rp->post_conv (rp->cls, | 195 | rp->post_conv (rp->conv_cls, |
196 | rp, | 196 | rp, |
197 | stmt, | 197 | stmt, |
198 | field_off, | 198 | field_off, |
@@ -218,12 +218,12 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
218 | * @param rs reult specification to clean up | 218 | * @param rs reult specification to clean up |
219 | */ | 219 | */ |
220 | void | 220 | void |
221 | GNUNET_MY_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) | 221 | GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs) |
222 | { | 222 | { |
223 | unsigned int i; | 223 | unsigned int i; |
224 | 224 | ||
225 | for (i=0;NULL != rs[i].conv;i++) | 225 | for (i=0;NULL != rs[i].cleaner;i++) |
226 | rs[i].cleaner (rs[i].cls, | 226 | rs[i].cleaner (rs[i].conv_cls, |
227 | &rs[i]); | 227 | &rs[i]); |
228 | } | 228 | } |
229 | 229 | ||
diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c index 4ea1b4ffe..6bbbf0b51 100644 --- a/src/my/my_query_helper.c +++ b/src/my/my_query_helper.c | |||
@@ -106,8 +106,9 @@ my_conv_uint16 (void *cls, | |||
106 | if (NULL == u_nbo) | 106 | if (NULL == u_nbo) |
107 | return -1; | 107 | return -1; |
108 | 108 | ||
109 | *u_nbo = htons (*u_hbo); | 109 | // *u_nbo = htons (*u_hbo); |
110 | 110 | *u_nbo = *u_hbo; | |
111 | |||
111 | qbind->buffer = (void *) u_nbo; | 112 | qbind->buffer = (void *) u_nbo; |
112 | qbind->buffer_length = sizeof(uint16_t); | 113 | qbind->buffer_length = sizeof(uint16_t); |
113 | qbind->buffer_type = MYSQL_TYPE_SHORT; | 114 | qbind->buffer_type = MYSQL_TYPE_SHORT; |
@@ -153,7 +154,8 @@ my_conv_uint32 (void *cls, | |||
153 | GNUNET_assert (1 == qp->num_params); | 154 | GNUNET_assert (1 == qp->num_params); |
154 | 155 | ||
155 | u_nbo = GNUNET_new (uint32_t); | 156 | u_nbo = GNUNET_new (uint32_t); |
156 | *u_nbo = htonl (*u_hbo); | 157 | // *u_nbo = htonl (*u_hbo); |
158 | *u_nbo = *u_hbo; | ||
157 | 159 | ||
158 | qbind->buffer = (void *) u_nbo; | 160 | qbind->buffer = (void *) u_nbo; |
159 | qbind->buffer_length = sizeof(uint32_t); | 161 | qbind->buffer_length = sizeof(uint32_t); |
@@ -200,7 +202,8 @@ my_conv_uint64 (void *cls, | |||
200 | GNUNET_assert (1 == qp->num_params); | 202 | GNUNET_assert (1 == qp->num_params); |
201 | 203 | ||
202 | u_nbo = GNUNET_new(uint64_t); | 204 | u_nbo = GNUNET_new(uint64_t); |
203 | *u_nbo = GNUNET_htonll (*u_hbo); | 205 | //*u_nbo = GNUNET_htonll (*u_hbo); |
206 | *u_nbo = *u_hbo; | ||
204 | 207 | ||
205 | qbind->buffer = (void *) u_nbo; | 208 | qbind->buffer = (void *) u_nbo; |
206 | qbind->buffer_length = sizeof (uint64_t); | 209 | qbind->buffer_length = sizeof (uint64_t); |
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c index 108edb62d..eafe434f8 100644 --- a/src/my/my_result_helper.c +++ b/src/my/my_result_helper.c | |||
@@ -33,11 +33,14 @@ | |||
33 | static int | 33 | static int |
34 | pre_extract_varsize_blob (void *cls, | 34 | pre_extract_varsize_blob (void *cls, |
35 | struct GNUNET_MY_ResultSpec *rs, | 35 | struct GNUNET_MY_ResultSpec *rs, |
36 | MYSQL_STMT *stmt, | ||
37 | unsigned int column, | ||
36 | MYSQL_BIND *results) | 38 | MYSQL_BIND *results) |
37 | { | 39 | { |
38 | results[0].buffer = NULL; | 40 | results[0].buffer = 0; |
39 | results[0].buffer_length = 0; | 41 | results[0].buffer_length = 0; |
40 | results[0].length = &rs->mysql_bind_output_length; | 42 | results[0].length = &rs->mysql_bind_output_length; |
43 | |||
41 | return GNUNET_OK; | 44 | return GNUNET_OK; |
42 | } | 45 | } |
43 | 46 | ||
@@ -147,11 +150,15 @@ GNUNET_MY_result_spec_variable_size (void **dst, | |||
147 | static int | 150 | static int |
148 | pre_extract_fixed_blob (void *cls, | 151 | pre_extract_fixed_blob (void *cls, |
149 | struct GNUNET_MY_ResultSpec *rs, | 152 | struct GNUNET_MY_ResultSpec *rs, |
153 | MYSQL_STMT *stmt, | ||
154 | unsigned int column, | ||
150 | MYSQL_BIND *results) | 155 | MYSQL_BIND *results) |
151 | { | 156 | { |
152 | results[0].buffer = rs->dst; | 157 | results[0].buffer = rs->dst; |
153 | results[0].buffer_length = rs->dst_size; | 158 | results[0].buffer_length = rs->dst_size; |
154 | results[0].length = &rs->mysql_bind_output_length; | 159 | results[0].length = &rs->mysql_bind_output_length; |
160 | results[0].buffer_type = MYSQL_TYPE_BLOB; | ||
161 | |||
155 | return GNUNET_OK; | 162 | return GNUNET_OK; |
156 | } | 163 | } |
157 | 164 | ||
@@ -173,6 +180,8 @@ pre_extract_fixed_blob (void *cls, | |||
173 | static int | 180 | static int |
174 | post_extract_fixed_blob (void *cls, | 181 | post_extract_fixed_blob (void *cls, |
175 | struct GNUNET_MY_ResultSpec *rs, | 182 | struct GNUNET_MY_ResultSpec *rs, |
183 | MYSQL_STMT *stmt, | ||
184 | unsigned int column, | ||
176 | MYSQL_BIND *results) | 185 | MYSQL_BIND *results) |
177 | { | 186 | { |
178 | if (rs->dst_size != rs->mysql_bind_output_length) | 187 | if (rs->dst_size != rs->mysql_bind_output_length) |
@@ -219,27 +228,63 @@ GNUNET_MY_result_spec_fixed_size (void *ptr, | |||
219 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 228 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
220 | */ | 229 | */ |
221 | static int | 230 | static int |
222 | extract_rsa_public_key (void *cls, | 231 | pre_extract_rsa_public_key (void *cls, |
223 | struct GNUNET_MY_ResultSpec *rs, | 232 | struct GNUNET_MY_ResultSpec *rs, |
233 | MYSQL_STMT *stmt, | ||
234 | unsigned int column, | ||
235 | MYSQL_BIND *results) | ||
236 | |||
237 | { | ||
238 | results[0].buffer = 0; | ||
239 | results[0].buffer_length = 0; | ||
240 | results[0].length = rs->mysql_bind_output_length; | ||
241 | results[0].buffer_type = MYSQL_TYPE_LONG; | ||
242 | |||
243 | return GNUNET_OK; | ||
244 | } | ||
245 | |||
246 | |||
247 | /** | ||
248 | * Check size of extracted fixed size data from a Mysql database @a | ||
249 | * result at row @a row | ||
250 | * | ||
251 | * @param cls closure | ||
252 | * @param result where to extract data from | ||
253 | * @param int row to extract data from | ||
254 | * @param fname name (or prefix) of the fields to extract from | ||
255 | * @param[in, out] dst_size where to store size of result, may be NULL | ||
256 | * @param[out] dst where to store the result | ||
257 | * @return | ||
258 | * #GNUNET_OK if all results could be extracted | ||
259 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
260 | */ | ||
261 | static int | ||
262 | post_extract_rsa_public_key (void *cls, | ||
263 | struct GNUNET_MY_ResultSpec *rs, | ||
264 | MYSQL_STMT *stmt, | ||
265 | unsigned int column, | ||
224 | MYSQL_BIND *results) | 266 | MYSQL_BIND *results) |
225 | 267 | ||
226 | { | 268 | { |
227 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; | 269 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; |
228 | 270 | ||
229 | size_t len; | 271 | size_t size; |
230 | const char *res; | 272 | char *res; |
231 | 273 | ||
232 | if (results->is_null) | 274 | results[0].buffer = res; |
275 | results[0].buffer_length = size; | ||
276 | |||
277 | if (0 != | ||
278 | mysql_stmt_fetch_column (stmt, | ||
279 | results, | ||
280 | column, | ||
281 | 0)) | ||
233 | { | 282 | { |
234 | return GNUNET_SYSERR; | 283 | return GNUNET_SYSERR; |
235 | } | 284 | } |
236 | 285 | ||
237 | len = results->buffer_length; | ||
238 | res = results->buffer; | ||
239 | |||
240 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, | 286 | *pk = GNUNET_CRYPTO_rsa_public_key_decode (res, |
241 | len); | 287 | size); |
242 | |||
243 | if (NULL == *pk) | 288 | if (NULL == *pk) |
244 | { | 289 | { |
245 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 290 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -247,9 +292,35 @@ extract_rsa_public_key (void *cls, | |||
247 | return GNUNET_SYSERR; | 292 | return GNUNET_SYSERR; |
248 | } | 293 | } |
249 | 294 | ||
295 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
296 | return GNUNET_SYSERR; | ||
297 | |||
250 | return GNUNET_OK; | 298 | return GNUNET_OK; |
251 | } | 299 | } |
252 | 300 | ||
301 | |||
302 | /** | ||
303 | * Function called to clean up memory allocated | ||
304 | * by a #GNUNET_MY_ResultConverter. | ||
305 | * | ||
306 | * @param cls closure | ||
307 | * @param rd result data to clean up | ||
308 | */ | ||
309 | static void | ||
310 | clean_rsa_public_key (void *cls, | ||
311 | struct GNUNET_MY_ResultSpec *rs) | ||
312 | { | ||
313 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rs->dst; | ||
314 | |||
315 | if (NULL != *pk) | ||
316 | { | ||
317 | GNUNET_CRYPTO_rsa_public_key_free (*pk); | ||
318 | *pk = NULL; | ||
319 | *rs->result_size = 0; | ||
320 | } | ||
321 | } | ||
322 | |||
323 | |||
253 | /** | 324 | /** |
254 | * RSA public key expected | 325 | * RSA public key expected |
255 | * | 326 | * |
@@ -261,12 +332,12 @@ struct GNUNET_MY_ResultSpec | |||
261 | GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | 332 | GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) |
262 | { | 333 | { |
263 | struct GNUNET_MY_ResultSpec res = { | 334 | struct GNUNET_MY_ResultSpec res = { |
264 | &extract_rsa_public_key, | 335 | .pre_conv = &pre_extract_rsa_public_key, |
265 | NULL, | 336 | .post_conv = &post_extract_rsa_public_key, |
266 | (void *) rsa, | 337 | .cleaner = &clean_rsa_public_key, |
267 | 0, | 338 | .dst = (void *) rsa, |
268 | NULL, | 339 | .dst_size = 0, |
269 | 1 | 340 | .num_fields = 1 |
270 | }; | 341 | }; |
271 | 342 | ||
272 | return res; | 343 | return res; |
@@ -286,24 +357,57 @@ GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa) | |||
286 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 357 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
287 | */ | 358 | */ |
288 | static int | 359 | static int |
289 | extract_rsa_signature (void *cls, | 360 | pre_extract_rsa_signature (void *cls, |
290 | struct GNUNET_MY_ResultSpec *rs, | 361 | struct GNUNET_MY_ResultSpec *rs, |
362 | MYSQL_STMT *stmt, | ||
363 | unsigned int column, | ||
364 | MYSQL_BIND *results) | ||
365 | { | ||
366 | results[0].buffer = 0; | ||
367 | results[0].buffer_length = 0; | ||
368 | results[0].length = &rs->mysql_bind_output_length; | ||
369 | results[0].buffer_type = MYSQL_TYPE_LONG; | ||
370 | |||
371 | return GNUNET_OK; | ||
372 | } | ||
373 | /** | ||
374 | * Extract data from a Mysql database @a result at row @a row. | ||
375 | * | ||
376 | * @param cls closure | ||
377 | * @param result where to extract data from | ||
378 | * @param int row to extract data from | ||
379 | * @param fname name (or prefix) of the fields to extract from | ||
380 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
381 | * @param[out] dst where to store the result | ||
382 | * @return | ||
383 | * #GNUNET_OK if all results could be extracted | ||
384 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
385 | */ | ||
386 | static int | ||
387 | post_extract_rsa_signature (void *cls, | ||
388 | struct GNUNET_MY_ResultSpec *rs, | ||
389 | MYSQL_STMT *stmt, | ||
390 | unsigned int column, | ||
291 | MYSQL_BIND *results) | 391 | MYSQL_BIND *results) |
292 | { | 392 | { |
293 | struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst; | 393 | struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst; |
294 | size_t len; | 394 | size_t size; |
295 | const char *res; | 395 | const char *res; |
296 | 396 | ||
297 | if (results->is_null) | 397 | results[0].buffer = res; |
398 | results[0].buffer_length = size; | ||
399 | |||
400 | if (0 != | ||
401 | mysql_stmt_fetch_column (stmt, | ||
402 | results, | ||
403 | column, | ||
404 | 0)) | ||
298 | { | 405 | { |
299 | return GNUNET_SYSERR; | 406 | return GNUNET_SYSERR; |
300 | } | 407 | } |
301 | 408 | ||
302 | len = results->buffer_length; | ||
303 | res = results->buffer; | ||
304 | |||
305 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, | 409 | *sig = GNUNET_CRYPTO_rsa_signature_decode (res, |
306 | len); | 410 | size); |
307 | 411 | ||
308 | if (NULL != *sig) | 412 | if (NULL != *sig) |
309 | { | 413 | { |
@@ -315,6 +419,29 @@ extract_rsa_signature (void *cls, | |||
315 | return GNUNET_OK; | 419 | return GNUNET_OK; |
316 | } | 420 | } |
317 | 421 | ||
422 | |||
423 | /** | ||
424 | * Function called to clean up memory allocated | ||
425 | * by a #GNUNET_MY_ResultConverter. | ||
426 | * | ||
427 | * @param cls closure | ||
428 | * @param rd result data to clean up | ||
429 | */ | ||
430 | static void | ||
431 | clean_rsa_signature (void *cls, | ||
432 | struct GNUNET_MY_ResultSpec *rs) | ||
433 | { | ||
434 | struct GNUNET_CRYPTO_RsaSignature **sig = rs->dst; | ||
435 | |||
436 | if (NULL != *sig) | ||
437 | { | ||
438 | GNUNET_CRYPTO_rsa_signature_free (*sig); | ||
439 | *sig = NULL; | ||
440 | rs->result_size = 0; | ||
441 | } | ||
442 | } | ||
443 | |||
444 | |||
318 | /** | 445 | /** |
319 | * RSA signature expected. | 446 | * RSA signature expected. |
320 | * | 447 | * |
@@ -326,12 +453,12 @@ GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | |||
326 | { | 453 | { |
327 | struct GNUNET_MY_ResultSpec res = | 454 | struct GNUNET_MY_ResultSpec res = |
328 | { | 455 | { |
329 | &extract_rsa_signature, | 456 | .pre_conv = &pre_extract_rsa_signature, |
330 | NULL, | 457 | .post_conv = &post_extract_rsa_signature, |
331 | (void *)sig, | 458 | .cleaner = &clean_rsa_signature, |
332 | 0, | 459 | .dst = (void *)sig, |
333 | NULL, | 460 | .dst_size = 0, |
334 | 1 | 461 | .num_fields = 1 |
335 | }; | 462 | }; |
336 | return res; | 463 | return res; |
337 | } | 464 | } |
@@ -350,10 +477,67 @@ GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig) | |||
350 | * #GNUNET_SYSERR if a result was invalid (non existing field or NULL) | 477 | * #GNUNET_SYSERR if a result was invalid (non existing field or NULL) |
351 | */ | 478 | */ |
352 | static int | 479 | static int |
353 | extract_string (void * cls, | 480 | pre_extract_string (void * cls, |
354 | struct GNUNET_MY_ResultSpec *rs, | 481 | struct GNUNET_MY_ResultSpec *rs, |
482 | MYSQL_STMT *stmt, | ||
483 | unsigned int column, | ||
355 | MYSQL_BIND *results) | 484 | MYSQL_BIND *results) |
356 | { | 485 | { |
486 | results[0].buffer = (char *)rs->dst; | ||
487 | results[0].buffer_length = rs->dst_size; | ||
488 | results[0].length = &rs->mysql_bind_output_length; | ||
489 | /* | ||
490 | char **str = rs->dst; | ||
491 | size_t len; | ||
492 | const char *res; | ||
493 | |||
494 | *str = NULL; | ||
495 | |||
496 | if (results->is_null) | ||
497 | { | ||
498 | return GNUNET_SYSERR; | ||
499 | } | ||
500 | |||
501 | len = results->buffer_length; | ||
502 | res = results->buffer; | ||
503 | |||
504 | *str = GNUNET_strndup (res, | ||
505 | len); | ||
506 | |||
507 | if (NULL == *str) | ||
508 | { | ||
509 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
510 | "Results contains bogus value (fail to decode)\n"); | ||
511 | return GNUNET_SYSERR; | ||
512 | } | ||
513 | */ return GNUNET_OK; | ||
514 | } | ||
515 | |||
516 | |||
517 | /** | ||
518 | * Check size of extracted fixed size data from a Mysql database @a | ||
519 | * | ||
520 | * @param cls closure | ||
521 | * @param result where to extract data from | ||
522 | * @param int row to extract data from | ||
523 | * @param fname name (or prefix) of the fields to extract from | ||
524 | * @param[in, out] dst_size where to store size of result, may be NULL | ||
525 | * @param[out] dst where to store the result | ||
526 | * @return | ||
527 | * #GNUNET_OK if all results could be extracted | ||
528 | * #GNUNET_SYSERR if a result was invalid (non existing field or NULL) | ||
529 | */ | ||
530 | static int | ||
531 | post_extract_string (void * cls, | ||
532 | struct GNUNET_MY_ResultSpec *rs, | ||
533 | MYSQL_STMT *stmt, | ||
534 | unsigned int column, | ||
535 | MYSQL_BIND *results) | ||
536 | { | ||
537 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
538 | return GNUNET_SYSERR; | ||
539 | return GNUNET_OK; | ||
540 | /* | ||
357 | char **str = rs->dst; | 541 | char **str = rs->dst; |
358 | size_t len; | 542 | size_t len; |
359 | const char *res; | 543 | const char *res; |
@@ -378,6 +562,7 @@ extract_string (void * cls, | |||
378 | return GNUNET_SYSERR; | 562 | return GNUNET_SYSERR; |
379 | } | 563 | } |
380 | return GNUNET_OK; | 564 | return GNUNET_OK; |
565 | */ | ||
381 | } | 566 | } |
382 | 567 | ||
383 | 568 | ||
@@ -391,12 +576,11 @@ struct GNUNET_MY_ResultSpec | |||
391 | GNUNET_MY_result_spec_string (char **dst) | 576 | GNUNET_MY_result_spec_string (char **dst) |
392 | { | 577 | { |
393 | struct GNUNET_MY_ResultSpec res = { | 578 | struct GNUNET_MY_ResultSpec res = { |
394 | &extract_string, | 579 | .pre_conv = &pre_extract_string, |
395 | NULL, | 580 | .post_conv = &post_extract_string, |
396 | (void *) dst, | 581 | .dst = (void *) dst, |
397 | 0, | 582 | .dst_size = 0, |
398 | NULL, | 583 | .num_fields = 1 |
399 | 1 | ||
400 | }; | 584 | }; |
401 | return res; | 585 | return res; |
402 | } | 586 | } |
@@ -445,28 +629,43 @@ GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at) | |||
445 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 629 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
446 | */ | 630 | */ |
447 | static int | 631 | static int |
448 | extract_uint16 (void *cls, | 632 | pre_extract_uint16 (void *cls, |
449 | struct GNUNET_MY_ResultSpec *rs, | 633 | struct GNUNET_MY_ResultSpec *rs, |
634 | MYSQL_STMT *stmt, | ||
635 | unsigned int column, | ||
450 | MYSQL_BIND *results) | 636 | MYSQL_BIND *results) |
451 | { | 637 | { |
452 | uint16_t *udst = rs->dst; | 638 | results[0].buffer = (char *)rs->dst; |
453 | const uint16_t *res; | 639 | results[0].buffer_length = rs->dst_size; |
454 | 640 | results[0].length = &rs->mysql_bind_output_length; | |
455 | if(results->is_null) | 641 | results[0].buffer_type = MYSQL_TYPE_SHORT; |
456 | { | ||
457 | return GNUNET_SYSERR; | ||
458 | } | ||
459 | 642 | ||
460 | GNUNET_assert (NULL != rs->dst); | 643 | return GNUNET_OK; |
461 | if (sizeof (uint16_t) != rs->dst_size) | 644 | } |
462 | { | ||
463 | GNUNET_break (0); | ||
464 | return GNUNET_SYSERR; | ||
465 | } | ||
466 | 645 | ||
467 | res = (uint16_t *)results->buffer; | ||
468 | *udst = ntohs (*res); | ||
469 | 646 | ||
647 | /** | ||
648 | * Check size of extracted fixed size data from a Mysql datbase. | ||
649 | * | ||
650 | * @param cls closure | ||
651 | * @param result where to extract data from | ||
652 | * @param int row to extract data from | ||
653 | * @param fname name (or prefix) of the fields to extract from | ||
654 | * @param[in,out] dst_size where to store size of result, may be NULL | ||
655 | * @param[out] dst where to store the result | ||
656 | * @return | ||
657 | * #GNUNET_YES if all results could be extracted | ||
658 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
659 | */ | ||
660 | static int | ||
661 | post_extract_uint16 (void *cls, | ||
662 | struct GNUNET_MY_ResultSpec *rs, | ||
663 | MYSQL_STMT *stmt, | ||
664 | unsigned int column, | ||
665 | MYSQL_BIND *results) | ||
666 | { | ||
667 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
668 | return GNUNET_SYSERR; | ||
470 | return GNUNET_OK; | 669 | return GNUNET_OK; |
471 | } | 670 | } |
472 | 671 | ||
@@ -481,12 +680,11 @@ struct GNUNET_MY_ResultSpec | |||
481 | GNUNET_MY_result_spec_uint16 (uint16_t *u16) | 680 | GNUNET_MY_result_spec_uint16 (uint16_t *u16) |
482 | { | 681 | { |
483 | struct GNUNET_MY_ResultSpec res = { | 682 | struct GNUNET_MY_ResultSpec res = { |
484 | &extract_uint16, | 683 | .pre_conv = &pre_extract_uint16, |
485 | NULL, | 684 | .post_conv = &post_extract_uint16, |
486 | (void *) u16, | 685 | .dst = (void *) u16, |
487 | sizeof (*u16), | 686 | .dst_size = sizeof (*u16), |
488 | NULL, | 687 | .num_fields = 1 |
489 | 1 | ||
490 | }; | 688 | }; |
491 | return res; | 689 | return res; |
492 | } | 690 | } |
@@ -505,32 +703,47 @@ GNUNET_MY_result_spec_uint16 (uint16_t *u16) | |||
505 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 703 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
506 | */ | 704 | */ |
507 | static int | 705 | static int |
508 | extract_uint32 (void *cls, | 706 | pre_extract_uint32 (void *cls, |
509 | struct GNUNET_MY_ResultSpec *rs, | 707 | struct GNUNET_MY_ResultSpec *rs, |
708 | MYSQL_STMT *stmt, | ||
709 | unsigned int column, | ||
510 | MYSQL_BIND *results) | 710 | MYSQL_BIND *results) |
511 | { | 711 | { |
512 | uint32_t *udst = rs->dst; | 712 | results[0].buffer = (int *)rs->dst; |
513 | const uint32_t *res; | 713 | results[0].buffer_length = rs->dst_size; |
514 | 714 | results[0].length = &rs->mysql_bind_output_length; | |
515 | if(results->is_null) | 715 | results[0].buffer_type = MYSQL_TYPE_LONG; |
516 | { | ||
517 | return GNUNET_SYSERR; | ||
518 | } | ||
519 | |||
520 | GNUNET_assert (NULL != rs->dst); | ||
521 | if (sizeof (uint32_t) != rs->dst_size) | ||
522 | { | ||
523 | GNUNET_break (0); | ||
524 | return GNUNET_SYSERR; | ||
525 | } | ||
526 | 716 | ||
527 | res = (uint32_t *)results->buffer; | 717 | return GNUNET_OK; |
718 | } | ||
528 | 719 | ||
529 | *udst = ntohl (*res); | ||
530 | 720 | ||
721 | /** | ||
722 | * Extrac data from a MYSQL database @a result at row @a row | ||
723 | * | ||
724 | * @param cls closure | ||
725 | * @param result where to extract data from | ||
726 | * @param int row to extract data from | ||
727 | * @param fname name (or prefix) of the fields to extract from | ||
728 | * @param[in, out] dst_size where to store size of result, may be NULL | ||
729 | * @param[out] dst where to store the result | ||
730 | * @return | ||
731 | * #GNUNET_OK if all results could be extracted | ||
732 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
733 | */ | ||
734 | static int | ||
735 | post_extract_uint32 (void *cls, | ||
736 | struct GNUNET_MY_ResultSpec *rs, | ||
737 | MYSQL_STMT * stmt, | ||
738 | unsigned int column, | ||
739 | MYSQL_BIND *results) | ||
740 | { | ||
741 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
742 | return GNUNET_SYSERR; | ||
531 | return GNUNET_OK; | 743 | return GNUNET_OK; |
532 | } | 744 | } |
533 | 745 | ||
746 | |||
534 | /** | 747 | /** |
535 | * uint32_t expected | 748 | * uint32_t expected |
536 | * | 749 | * |
@@ -541,16 +754,16 @@ struct GNUNET_MY_ResultSpec | |||
541 | GNUNET_MY_result_spec_uint32 (uint32_t *u32) | 754 | GNUNET_MY_result_spec_uint32 (uint32_t *u32) |
542 | { | 755 | { |
543 | struct GNUNET_MY_ResultSpec res = { | 756 | struct GNUNET_MY_ResultSpec res = { |
544 | &extract_uint32, | 757 | .pre_conv = &pre_extract_uint32, |
545 | NULL, | 758 | .post_conv = &post_extract_uint32, |
546 | (void *) u32, | 759 | .dst = (void *) u32, |
547 | sizeof (*u32), | 760 | .dst_size = sizeof (*u32), |
548 | NULL, | 761 | .num_fields = 1 |
549 | 1 | ||
550 | }; | 762 | }; |
551 | return res; | 763 | return res; |
552 | } | 764 | } |
553 | 765 | ||
766 | |||
554 | /** | 767 | /** |
555 | * Extract data from a MYSQL database @a result at row @a row | 768 | * Extract data from a MYSQL database @a result at row @a row |
556 | * | 769 | * |
@@ -565,26 +778,44 @@ GNUNET_MY_result_spec_uint32 (uint32_t *u32) | |||
565 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | 778 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) |
566 | */ | 779 | */ |
567 | static int | 780 | static int |
568 | extract_uint64 (void *cls, | 781 | pre_extract_uint64 (void *cls, |
569 | struct GNUNET_MY_ResultSpec *rs, | 782 | struct GNUNET_MY_ResultSpec *rs, |
783 | MYSQL_STMT *stmt, | ||
784 | unsigned int column, | ||
570 | MYSQL_BIND *results) | 785 | MYSQL_BIND *results) |
571 | { | 786 | { |
572 | uint64_t *udst = rs->dst; | 787 | results[0].buffer = rs->dst; |
573 | const uint64_t *res; | 788 | results[0].buffer_length = rs->dst_size; |
789 | results[0].length = &rs->mysql_bind_output_length; | ||
790 | results[0].buffer_type = MYSQL_TYPE_LONGLONG; | ||
574 | 791 | ||
575 | results[0].buffer = &rs->dst; | 792 | return GNUNET_OK; |
576 | results[0].buffer_length = 42; | 793 | } |
577 | 794 | ||
578 | GNUNET_assert (NULL != rs->dst); | ||
579 | if (sizeof (uint64_t) != rs->dst_size) | ||
580 | { | ||
581 | GNUNET_break (0); | ||
582 | return GNUNET_SYSERR; | ||
583 | } | ||
584 | 795 | ||
585 | res = (uint64_t *)results->buffer; | ||
586 | *udst = GNUNET_ntohll (*res); | ||
587 | 796 | ||
797 | /** | ||
798 | * Check size of extracted fixe size data from a Mysql database | ||
799 | * | ||
800 | * @param cls closure | ||
801 | * @param result where to extract data from | ||
802 | * @param int row to extract data from | ||
803 | * @param fname name (or prefix) of the fields to extract from | ||
804 | * @param[in, out] dst_size where to store size of result, may be null | ||
805 | * @param[out] dst where to store the result | ||
806 | * @return | ||
807 | * #GNUNET_OK if all results could be extracted | ||
808 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
809 | */ | ||
810 | static int | ||
811 | post_extract_uint64 (void *cls, | ||
812 | struct GNUNET_MY_ResultSpec *rs, | ||
813 | MYSQL_STMT *stmt, | ||
814 | unsigned int column, | ||
815 | MYSQL_BIND *results) | ||
816 | { | ||
817 | if (rs->dst_size != rs->mysql_bind_output_length) | ||
818 | return GNUNET_SYSERR; | ||
588 | return GNUNET_OK; | 819 | return GNUNET_OK; |
589 | } | 820 | } |
590 | 821 | ||
@@ -599,7 +830,8 @@ struct GNUNET_MY_ResultSpec | |||
599 | GNUNET_MY_result_spec_uint64 (uint64_t *u64) | 830 | GNUNET_MY_result_spec_uint64 (uint64_t *u64) |
600 | { | 831 | { |
601 | struct GNUNET_MY_ResultSpec res = { | 832 | struct GNUNET_MY_ResultSpec res = { |
602 | .pre_conv = &extract_uint64, | 833 | .pre_conv = &pre_extract_uint64, |
834 | .post_conv = &post_extract_uint64, | ||
603 | .dst = (void *) u64, | 835 | .dst = (void *) u64, |
604 | .dst_size = sizeof (*u64), | 836 | .dst_size = sizeof (*u64), |
605 | .num_fields = 1 | 837 | .num_fields = 1 |
diff --git a/src/my/test_my.c b/src/my/test_my.c index c63a069bf..c9529cbaf 100644 --- a/src/my/test_my.c +++ b/src/my/test_my.c | |||
@@ -75,7 +75,7 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
75 | u32 = 32; | 75 | u32 = 32; |
76 | u64 = 64; | 76 | u64 = 64; |
77 | 77 | ||
78 | statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, | 78 | /* statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, |
79 | "INSERT INTO test_my (" | 79 | "INSERT INTO test_my (" |
80 | " pub" | 80 | " pub" |
81 | ",sig" | 81 | ",sig" |
@@ -107,6 +107,25 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
107 | GNUNET_MY_query_param_uint64 (&u64), | 107 | GNUNET_MY_query_param_uint64 (&u64), |
108 | GNUNET_MY_query_param_end | 108 | GNUNET_MY_query_param_end |
109 | }; | 109 | }; |
110 | */ | ||
111 | statements_handle_insert = GNUNET_MYSQL_statement_prepare (context, | ||
112 | "INSERT INTO test_my2 (" | ||
113 | " abs_time" | ||
114 | ",forever" | ||
115 | ",u16" | ||
116 | ",u32" | ||
117 | ",u64" | ||
118 | ") VALUES " | ||
119 | "( ?, ?, ?, ?, ?)"); | ||
120 | |||
121 | struct GNUNET_MY_QueryParam params_insert[] = { | ||
122 | GNUNET_MY_query_param_absolute_time (&abs_time), | ||
123 | GNUNET_MY_query_param_absolute_time (&forever), | ||
124 | GNUNET_MY_query_param_uint16 (&u16), | ||
125 | GNUNET_MY_query_param_uint32 (&u32), | ||
126 | GNUNET_MY_query_param_uint64 (&u64), | ||
127 | GNUNET_MY_query_param_end | ||
128 | }; | ||
110 | 129 | ||
111 | if (GNUNET_OK != GNUNET_MY_exec_prepared(context, | 130 | if (GNUNET_OK != GNUNET_MY_exec_prepared(context, |
112 | statements_handle_insert, | 131 | statements_handle_insert, |
@@ -118,7 +137,7 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
118 | 137 | ||
119 | 138 | ||
120 | 139 | ||
121 | statements_handle_select = GNUNET_MYSQL_statement_prepare (context, | 140 | /* statements_handle_select = GNUNET_MYSQL_statement_prepare (context, |
122 | "SELECT" | 141 | "SELECT" |
123 | " pub" | 142 | " pub" |
124 | ",sig" | 143 | ",sig" |
@@ -133,6 +152,16 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
133 | " ORDER BY abs_time DESC " | 152 | " ORDER BY abs_time DESC " |
134 | " LIMIT 1;"); | 153 | " LIMIT 1;"); |
135 | 154 | ||
155 | */ | ||
156 | statements_handle_select = GNUNET_MYSQL_statement_prepare (context, | ||
157 | "SELECT" | ||
158 | " abs_time" | ||
159 | ",forever" | ||
160 | ",u16" | ||
161 | ",u32" | ||
162 | ",u64" | ||
163 | " FROM test_my2"); | ||
164 | |||
136 | if (NULL == statements_handle_select) | 165 | if (NULL == statements_handle_select) |
137 | { | 166 | { |
138 | fprintf(stderr, "Failed to prepared statement SELECT\n"); | 167 | fprintf(stderr, "Failed to prepared statement SELECT\n"); |
@@ -151,7 +180,7 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
151 | return 1; | 180 | return 1; |
152 | } | 181 | } |
153 | 182 | ||
154 | 183 | /* | |
155 | struct GNUNET_MY_ResultSpec results_select[] = { | 184 | struct GNUNET_MY_ResultSpec results_select[] = { |
156 | GNUNET_MY_result_spec_rsa_public_key (&pub2), | 185 | GNUNET_MY_result_spec_rsa_public_key (&pub2), |
157 | GNUNET_MY_result_spec_rsa_signature (&sig2), | 186 | GNUNET_MY_result_spec_rsa_signature (&sig2), |
@@ -164,11 +193,26 @@ run_queries (struct GNUNET_MYSQL_Context *context) | |||
164 | GNUNET_MY_result_spec_uint64 (&u642), | 193 | GNUNET_MY_result_spec_uint64 (&u642), |
165 | GNUNET_MY_result_spec_end | 194 | GNUNET_MY_result_spec_end |
166 | }; | 195 | }; |
196 | */ | ||
197 | struct GNUNET_MY_ResultSpec results_select[] = { | ||
198 | GNUNET_MY_result_spec_absolute_time (&abs_time2), | ||
199 | GNUNET_MY_result_spec_absolute_time (&forever2), | ||
200 | GNUNET_MY_result_spec_uint16 (&u162), | ||
201 | GNUNET_MY_result_spec_uint32 (&u322), | ||
202 | GNUNET_MY_result_spec_uint64 (&u642), | ||
203 | GNUNET_MY_result_spec_end | ||
204 | }; | ||
167 | 205 | ||
168 | ret = GNUNET_MY_extract_result (statements_handle_select, | 206 | ret = GNUNET_MY_extract_result (statements_handle_select, |
169 | NULL, | 207 | results_select); |
170 | results_select, | 208 | |
171 | 0); | 209 | GNUNET_break (abs_time.abs_value_us == abs_time2.abs_value_us); |
210 | GNUNET_break (forever.abs_value_us == forever2.abs_value_us); | ||
211 | |||
212 | GNUNET_break (16 == u162); | ||
213 | GNUNET_break (32 == u322); | ||
214 | GNUNET_break (64 == u642); | ||
215 | |||
172 | if (GNUNET_OK != ret) | 216 | if (GNUNET_OK != ret) |
173 | { | 217 | { |
174 | fprintf(stderr, "Failed to extract result\n"); | 218 | fprintf(stderr, "Failed to extract result\n"); |
@@ -212,7 +256,7 @@ main (int argc, const char * const argv[]) | |||
212 | return 77; | 256 | return 77; |
213 | } | 257 | } |
214 | 258 | ||
215 | if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, | 259 | /* if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, |
216 | "CREATE TABLE test_my(" | 260 | "CREATE TABLE test_my(" |
217 | "pub INT NOT NULL" | 261 | "pub INT NOT NULL" |
218 | ", sig INT NOT NULL" | 262 | ", sig INT NOT NULL" |
@@ -232,11 +276,28 @@ main (int argc, const char * const argv[]) | |||
232 | 276 | ||
233 | return 1; | 277 | return 1; |
234 | } | 278 | } |
279 | */ | ||
280 | if (GNUNET_OK != GNUNET_MYSQL_statement_run (context, | ||
281 | "CREATE TABLE test_my2(" | ||
282 | " abs_time BIGINT NOT NULL" | ||
283 | ", forever BIGINT NOT NULL" | ||
284 | ", u16 SMALLINT NOT NULL" | ||
285 | ", u32 INT NOT NULL" | ||
286 | ", u64 BIGINT NOT NULL" | ||
287 | ")")) | ||
288 | { | ||
289 | fprintf (stderr, | ||
290 | "Failed to create table \n"); | ||
291 | GNUNET_MYSQL_statements_invalidate (context); | ||
292 | GNUNET_MYSQL_context_destroy (context); | ||
293 | |||
294 | return 1; | ||
295 | } | ||
235 | 296 | ||
236 | ret = run_queries (context); | 297 | ret = run_queries (context); |
237 | 298 | /* | |
238 | /* if(GNUNET_OK != GNUNET_MYSQL_statement_run (context, | 299 | if(GNUNET_OK != GNUNET_MYSQL_statement_run (context, |
239 | "DROP TABLE test_my")) | 300 | "DROP TABLE test_my2")) |
240 | { | 301 | { |
241 | fprintf (stderr, "Failed to drop table test_my\n"); | 302 | fprintf (stderr, "Failed to drop table test_my\n"); |
242 | GNUNET_MYSQL_statements_invalidate (context); | 303 | GNUNET_MYSQL_statements_invalidate (context); |