diff options
-rw-r--r-- | src/include/gnunet_my_lib.h | 86 | ||||
-rw-r--r-- | src/my/Makefile.am | 18 | ||||
-rw-r--r-- | src/my/my.c | 4 | ||||
-rw-r--r-- | src/my/my_result_helper.c | 263 |
4 files changed, 314 insertions, 57 deletions
diff --git a/src/include/gnunet_my_lib.h b/src/include/gnunet_my_lib.h index 2a7df1ed6..6931ddcbd 100644 --- a/src/include/gnunet_my_lib.h +++ b/src/include/gnunet_my_lib.h | |||
@@ -299,10 +299,96 @@ GNUNET_MY_query_param_uint64 (const uint64_t *x); | |||
299 | */ | 299 | */ |
300 | #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst))) | 300 | #define GNUNET_MY_result_spec_auto_from_type(dst) GNUNET_MY_result_spec_fixed_size ((dst), sizeof (*(dst))) |
301 | 301 | ||
302 | |||
302 | /** | 303 | /** |
303 | * FIXME. | 304 | * FIXME. |
304 | * | 305 | * |
305 | */ | 306 | */ |
307 | |||
308 | /** | ||
309 | * Variable-size result expected | ||
310 | * | ||
311 | * @param[out] dst where to store the result, allocated | ||
312 | * @param[out] sptr where to store the size of @a dst | ||
313 | * @return array entru for the result specification to use | ||
314 | */ | ||
315 | struct GNUNET_MY_ResultSpec | ||
316 | GNUNET_MY_result_spec_variable_size (void **dst, | ||
317 | size_t *ptr_size); | ||
318 | /** | ||
319 | * RSA public key expected | ||
320 | * | ||
321 | * @param name name of the field in the table | ||
322 | * @param[out] rsa where to store the result | ||
323 | * @return array entry for the result specification to use | ||
324 | */ | ||
325 | struct GNUNET_MY_ResultSpec | ||
326 | GNUNET_MY_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa); | ||
327 | |||
328 | /** | ||
329 | * RSA signature expected. | ||
330 | * | ||
331 | * @param[out] sig where to store the result; | ||
332 | * @return array entry for the result specification to use | ||
333 | */ | ||
334 | struct GNUNET_MY_ResultSpec | ||
335 | GNUNET_MY_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig); | ||
336 | |||
337 | /** | ||
338 | * 0- terminated string exprected. | ||
339 | * | ||
340 | * @param[out] dst where to store the result, allocated | ||
341 | * @return array entry for the result specification to use | ||
342 | */ | ||
343 | struct GNUNET_MY_ResultSpec | ||
344 | GNUNET_MY_result_spec_string (char **dst); | ||
345 | |||
346 | /** | ||
347 | * Absolute time expected | ||
348 | * | ||
349 | * @param name name of the field in the table | ||
350 | * @param[out] at where to store the result | ||
351 | * @return array entry for the result specification to use | ||
352 | */ | ||
353 | struct GNUNET_MY_ResultSpec | ||
354 | GNUNET_MY_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at); | ||
355 | |||
356 | /** | ||
357 | * Absolute time in network byte order expected | ||
358 | * | ||
359 | * @param[out] at where to store the result | ||
360 | * @return array entry for the result specification to use | ||
361 | */ | ||
362 | struct GNUNET_MY_ResultSpec | ||
363 | GNUNET_MY_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at); | ||
364 | |||
365 | /** | ||
366 | * uint16_t expected | ||
367 | * | ||
368 | * @param[out] u16 where to store the result | ||
369 | * @return array entry for the result specification to use | ||
370 | */ | ||
371 | struct GNUNET_MY_ResultSpec | ||
372 | GNUNET_MY_result_spec_uint16 (uint16_t *u16); | ||
373 | |||
374 | /** | ||
375 | * uint32_t expected | ||
376 | * | ||
377 | * @param[out] u32 where to store the result | ||
378 | * @return array entry for the result specification to use | ||
379 | */ | ||
380 | struct GNUNET_MY_ResultSpec | ||
381 | GNUNET_MY_result_spec_uint32 (uint32_t *u32); | ||
382 | |||
383 | /** | ||
384 | * uint64_t expected. | ||
385 | * | ||
386 | * @param[out] u64 where to store the result | ||
387 | * @return array entry for the result specification to use | ||
388 | */ | ||
389 | struct GNUNET_MY_ResultSpec | ||
390 | GNUNET_MY_result_spec_uint64 (uint64_t *u64); | ||
391 | |||
306 | int | 392 | int |
307 | GNUNET_MY_extract_result (MYSQL_BIND * result, | 393 | GNUNET_MY_extract_result (MYSQL_BIND * result, |
308 | struct GNUNET_MY_QueryParam *qp, | 394 | struct GNUNET_MY_QueryParam *qp, |
diff --git a/src/my/Makefile.am b/src/my/Makefile.am index 54e6d5563..7c0bc404e 100644 --- a/src/my/Makefile.am +++ b/src/my/Makefile.am | |||
@@ -15,10 +15,26 @@ endif | |||
15 | 15 | ||
16 | libgnunetmy_la_SOURCES = \ | 16 | libgnunetmy_la_SOURCES = \ |
17 | my.c \ | 17 | my.c \ |
18 | my_query_helper.c | 18 | my_query_helper.c \ |
19 | my_result_helper.c | ||
20 | |||
19 | libgnunetmy_la_LIBADD = $(MYSQL_LDFLAGS) -lmysqlclient \ | 21 | libgnunetmy_la_LIBADD = $(MYSQL_LDFLAGS) -lmysqlclient \ |
20 | $(top_builddir)/src/mysql/libgnunetmysql.la \ | 22 | $(top_builddir)/src/mysql/libgnunetmysql.la \ |
21 | $(top_builddir)/src/util/libgnunetutil.la | 23 | $(top_builddir)/src/util/libgnunetutil.la |
22 | libgnunetmy_la_LDFLAGS = \ | 24 | libgnunetmy_la_LDFLAGS = \ |
23 | $(GN_LIB_LDFLAGS) \ | 25 | $(GN_LIB_LDFLAGS) \ |
24 | -version-info 0:0:0 | 26 | -version-info 0:0:0 |
27 | |||
28 | TESTS = \ | ||
29 | test_my | ||
30 | |||
31 | check_PROGRAMS= \ | ||
32 | test_my | ||
33 | |||
34 | test_my_SOURCES = \ | ||
35 | test_my.c | ||
36 | test_my_LDADD = \ | ||
37 | libgnunetmy.la \ | ||
38 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
39 | -lmysqlclient $(XLIB) | ||
40 | |||
diff --git a/src/my/my.c b/src/my/my.c index 09d00163e..cc4f21cb1 100644 --- a/src/my/my.c +++ b/src/my/my.c | |||
@@ -34,8 +34,10 @@ | |||
34 | * @param mc mysql context | 34 | * @param mc mysql context |
35 | * @param sh handle to SELECT statment | 35 | * @param sh handle to SELECT statment |
36 | * @param params parameters to the statement | 36 | * @param params parameters to the statement |
37 | * @return | 37 | * @return mysql result |
38 | */ | 38 | */ |
39 | |||
40 | /***** FIXE THIS FUNCTION *****/ | ||
39 | int | 41 | int |
40 | GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | 42 | GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, |
41 | struct GNUNET_MYSQL_StatementHandle *sh, | 43 | struct GNUNET_MYSQL_StatementHandle *sh, |
diff --git a/src/my/my_result_helper.c b/src/my/my_result_helper.c index 009fe2f11..f58c8e150 100644 --- a/src/my/my_result_helper.c +++ b/src/my/my_result_helper.c | |||
@@ -18,28 +18,7 @@ | |||
18 | 18 | ||
19 | #include "platform.h" | 19 | #include "platform.h" |
20 | #include "gnunet_util_lib.h" | 20 | #include "gnunet_util_lib.h" |
21 | #include "gnunet_pq_lib.h" | 21 | #include "gnunet_my_lib.h" |
22 | |||
23 | /** | ||
24 | * Function called to clean up memory allocated | ||
25 | * by a #GNUNET_MY_ResultConverter. | ||
26 | * | ||
27 | * @param cls closure | ||
28 | * @param rd result data to clean up | ||
29 | */ | ||
30 | static void | ||
31 | clean_varsize_blob (void *cls, | ||
32 | void *rs) | ||
33 | { | ||
34 | void **dst = rd; | ||
35 | |||
36 | if (NULL != *dst) | ||
37 | { | ||
38 | GNUNET_free (*dst); | ||
39 | *dst = NULL; | ||
40 | } | ||
41 | } | ||
42 | |||
43 | 22 | ||
44 | /** | 23 | /** |
45 | * extract data from a Mysql database @a result at row @a row | 24 | * extract data from a Mysql database @a result at row @a row |
@@ -66,15 +45,15 @@ extract_varsize_blob (void *cls, | |||
66 | void *idst; | 45 | void *idst; |
67 | size_t len; | 46 | size_t len; |
68 | 47 | ||
69 | MYSQL_ROW * rows; | 48 | MYSQL_ROW rows; |
70 | MYSQL_FIELD * field; | 49 | MYSQL_FIELD *field; |
71 | 50 | ||
72 | rows = mysql_fetch_row (result); | 51 | rows = mysql_fetch_row (result); |
73 | 52 | ||
74 | field = mysql_fetch_field (result); | 53 | field = mysql_fetch_field (result); |
75 | 54 | ||
76 | //If it's the correct field | 55 | //If it's the correct field |
77 | if (field != fname) | 56 | if (field->name != fname) |
78 | { | 57 | { |
79 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 58 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
80 | "Field '%s' does not exist in result", | 59 | "Field '%s' does not exist in result", |
@@ -122,7 +101,7 @@ GNUNET_MY_result_spec_variable_size (void **dst, | |||
122 | (void *)(dst), | 101 | (void *)(dst), |
123 | 0, | 102 | 0, |
124 | ptr_size | 103 | ptr_size |
125 | } | 104 | }; |
126 | 105 | ||
127 | return res; | 106 | return res; |
128 | } | 107 | } |
@@ -142,7 +121,7 @@ GNUNET_MY_result_spec_variable_size (void **dst, | |||
142 | * | 121 | * |
143 | */ | 122 | */ |
144 | static int | 123 | static int |
145 | extracted_fixed_blob (void *cls, | 124 | extract_fixed_blob (void *cls, |
146 | MYSQL_RES * result, | 125 | MYSQL_RES * result, |
147 | int row, | 126 | int row, |
148 | const char * fname, | 127 | const char * fname, |
@@ -152,7 +131,7 @@ extracted_fixed_blob (void *cls, | |||
152 | size_t len; | 131 | size_t len; |
153 | const char *res; | 132 | const char *res; |
154 | 133 | ||
155 | MYSQL_ROW * rows; | 134 | MYSQL_ROW rows; |
156 | MYSQL_FIELD * field; | 135 | MYSQL_FIELD * field; |
157 | 136 | ||
158 | rows = mysql_fetch_row (result); | 137 | rows = mysql_fetch_row (result); |
@@ -160,7 +139,7 @@ extracted_fixed_blob (void *cls, | |||
160 | field = mysql_fetch_field (result); | 139 | field = mysql_fetch_field (result); |
161 | 140 | ||
162 | //If it's the correct field | 141 | //If it's the correct field |
163 | if (field != fname) | 142 | if (field->name != fname) |
164 | { | 143 | { |
165 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 144 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
166 | "Field '%s' does not exist in result", | 145 | "Field '%s' does not exist in result", |
@@ -235,7 +214,7 @@ GNUNET_MY_result_spec_fixed_size (void *ptr, | |||
235 | static int | 214 | static int |
236 | extract_rsa_public_key (void *cls, | 215 | extract_rsa_public_key (void *cls, |
237 | MYSQL_RES *result, | 216 | MYSQL_RES *result, |
238 | int rown, | 217 | int row, |
239 | const char *fname, | 218 | const char *fname, |
240 | size_t *dst_size, | 219 | size_t *dst_size, |
241 | void *dst) | 220 | void *dst) |
@@ -244,7 +223,7 @@ extract_rsa_public_key (void *cls, | |||
244 | size_t len; | 223 | size_t len; |
245 | const char *res; | 224 | const char *res; |
246 | 225 | ||
247 | MYSQL_ROW * rows; | 226 | MYSQL_ROW rows; |
248 | MYSQL_FIELD * field; | 227 | MYSQL_FIELD * field; |
249 | 228 | ||
250 | *pk = NULL; | 229 | *pk = NULL; |
@@ -254,7 +233,7 @@ extract_rsa_public_key (void *cls, | |||
254 | field = mysql_fetch_field (result); | 233 | field = mysql_fetch_field (result); |
255 | 234 | ||
256 | //If it's the correct field | 235 | //If it's the correct field |
257 | if (field != fname) | 236 | if (field->name != fname) |
258 | { | 237 | { |
259 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 238 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
260 | "Field '%s' does not exist in result", | 239 | "Field '%s' does not exist in result", |
@@ -286,26 +265,6 @@ extract_rsa_public_key (void *cls, | |||
286 | } | 265 | } |
287 | 266 | ||
288 | /** | 267 | /** |
289 | * Function called to clean up memory allocated | ||
290 | * by a #GNUNET_MY_ResultConverter | ||
291 | * | ||
292 | * @param cls closure | ||
293 | * @param rd result data to clean up | ||
294 | */ | ||
295 | static void | ||
296 | clean_rsa_public_key (void *cls, | ||
297 | void *rd) | ||
298 | { | ||
299 | struct GNUNET_CRYPTO_RsaPublicKey **pk = rd; | ||
300 | if (NULL != *pk) | ||
301 | { | ||
302 | GNUNET_CRYPTO_rsa_public_key_free (*pk); | ||
303 | *pk = NULL; | ||
304 | } | ||
305 | } | ||
306 | |||
307 | |||
308 | /** | ||
309 | * RSA public key expected | 268 | * RSA public key expected |
310 | * | 269 | * |
311 | * @param name name of the field in the table | 270 | * @param name name of the field in the table |
@@ -351,7 +310,7 @@ extract_rsa_signature (void *cls, | |||
351 | const char *res; | 310 | const char *res; |
352 | 311 | ||
353 | 312 | ||
354 | MYSQL_ROW * rows; | 313 | MYSQL_ROW rows; |
355 | MYSQL_FIELD * field; | 314 | MYSQL_FIELD * field; |
356 | 315 | ||
357 | *sig = NULL; | 316 | *sig = NULL; |
@@ -361,7 +320,7 @@ extract_rsa_signature (void *cls, | |||
361 | field = mysql_fetch_field (result); | 320 | field = mysql_fetch_field (result); |
362 | 321 | ||
363 | //If it's the correct field | 322 | //If it's the correct field |
364 | if (field == fname) | 323 | if (field->name == fname) |
365 | { | 324 | { |
366 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 325 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
367 | "Field '%s' does not exist in result", | 326 | "Field '%s' does not exist in result", |
@@ -437,7 +396,7 @@ extract_string (void * cls, | |||
437 | size_t len; | 396 | size_t len; |
438 | const char *res; | 397 | const char *res; |
439 | 398 | ||
440 | MYSQL_ROW * rows; | 399 | MYSQL_ROW rows; |
441 | MYSQL_FIELD * field; | 400 | MYSQL_FIELD * field; |
442 | 401 | ||
443 | *str = NULL; | 402 | *str = NULL; |
@@ -544,6 +503,43 @@ extract_uint16 (void *cls, | |||
544 | void *dst) | 503 | void *dst) |
545 | { | 504 | { |
546 | //TO COMPLETE | 505 | //TO COMPLETE |
506 | uint16_t *udst = dst; | ||
507 | const uint16_t *res; | ||
508 | |||
509 | MYSQL_ROW rows; | ||
510 | MYSQL_FIELD * field; | ||
511 | |||
512 | rows = mysql_fetch_row (result); | ||
513 | |||
514 | field = mysql_fetch_field (result); | ||
515 | |||
516 | //If it's the correct field | ||
517 | if (field->name == fname) | ||
518 | { | ||
519 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
520 | "Field '%s' does not exist in result", | ||
521 | fname); | ||
522 | return GNUNET_SYSERR; | ||
523 | } | ||
524 | |||
525 | |||
526 | if (rows[row] == NULL) | ||
527 | { | ||
528 | return GNUNET_SYSERR; | ||
529 | } | ||
530 | |||
531 | GNUNET_assert (NULL != dst); | ||
532 | |||
533 | if (sizeof (uint16_t) != *dst_size) | ||
534 | { | ||
535 | GNUNET_break (0); | ||
536 | return GNUNET_SYSERR; | ||
537 | } | ||
538 | |||
539 | res = (uint16_t) rows[row]; | ||
540 | *udst = ntohs (*res); | ||
541 | |||
542 | return GNUNET_OK; | ||
547 | } | 543 | } |
548 | 544 | ||
549 | /** | 545 | /** |
@@ -566,4 +562,161 @@ GNUNET_MY_result_spec_uint16 (uint16_t *u16) | |||
566 | } | 562 | } |
567 | 563 | ||
568 | /** | 564 | /** |
565 | * Extrac data from a MYSQL database @a result at row @a row | ||
566 | * | ||
567 | * @param cls closure | ||
568 | * @param result where to extract data from | ||
569 | * @param int row to extract data from | ||
570 | * @param fname name (or prefix) of the fields to extract from | ||
571 | * @param[in, out] dst_size where to store size of result, may be NULL | ||
572 | * @param[out] dst where to store the result | ||
573 | * @return | ||
574 | * #GNUNET_OK if all results could be extracted | ||
575 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
576 | */ | ||
577 | static int | ||
578 | extract_uint32 (void *cls, | ||
579 | MYSQL_RES * result, | ||
580 | int row, | ||
581 | const char *fname, | ||
582 | size_t *dst_size, | ||
583 | void *dst) | ||
584 | { | ||
585 | uint32_t *udst = dst; | ||
586 | const uint32_t *res; | ||
587 | |||
588 | MYSQL_ROW rows; | ||
589 | MYSQL_FIELD * field; | ||
590 | |||
591 | rows = mysql_fetch_row (result); | ||
592 | |||
593 | field = mysql_fetch_field (result); | ||
594 | |||
595 | //If it's the correct field | ||
596 | if (field->name == fname) | ||
597 | { | ||
598 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
599 | "Field '%s' does not exist in result", | ||
600 | fname); | ||
601 | return GNUNET_SYSERR; | ||
602 | } | ||
603 | |||
604 | |||
605 | if (rows[row] == NULL) | ||
606 | { | ||
607 | return GNUNET_SYSERR; | ||
608 | } | ||
609 | |||
610 | GNUNET_assert (NULL != dst); | ||
611 | |||
612 | if (sizeof (uint32_t) != *dst_size) | ||
613 | { | ||
614 | GNUNET_break (0); | ||
615 | return GNUNET_SYSERR; | ||
616 | } | ||
617 | |||
618 | res = (uint32_t) rows[row]; | ||
619 | |||
620 | *udst = ntohl (*res); | ||
621 | return GNUNET_OK; | ||
622 | } | ||
623 | |||
624 | /** | ||
625 | * uint32_t expected | ||
626 | * | ||
627 | * @param[out] u32 where to store the result | ||
628 | * @return array entry for the result specification to use | ||
629 | */ | ||
630 | struct GNUNET_MY_ResultSpec | ||
631 | GNUNET_MY_result_spec_uint32 (uint32_t *u32) | ||
632 | { | ||
633 | struct GNUNET_MY_ResultSpec res = { | ||
634 | &extract_uint32, | ||
635 | NULL, | ||
636 | (void *) u32, | ||
637 | sizeof (*u32), | ||
638 | NULL | ||
639 | }; | ||
640 | return res; | ||
641 | } | ||
642 | |||
643 | /** | ||
644 | * Extract data from a MYSQL database @a result at row @a row | ||
645 | * | ||
646 | * @param cls closure | ||
647 | * @param result where to extract data from | ||
648 | * @param int row to extract data from | ||
649 | * @param fname name (or prefix) of the fields to extract from | ||
650 | * @param[in, out] dst_size where to store size of result, may be null | ||
651 | * @param[out] dst where to store the result | ||
652 | * @return | ||
653 | * #GNUNET_OK if all results could be extracted | ||
654 | * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) | ||
655 | */ | ||
656 | static int | ||
657 | extract_uint64 (void *cls, | ||
658 | MYSQL_RES * result, | ||
659 | int row, | ||
660 | const char *fname, | ||
661 | size_t *dst_size, | ||
662 | void *dst) | ||
663 | { | ||
664 | uint64_t *udst = dst; | ||
665 | const uint64_t *res; | ||
666 | |||
667 | MYSQL_ROW rows; | ||
668 | MYSQL_FIELD * field; | ||
669 | |||
670 | rows = mysql_fetch_row (result); | ||
671 | |||
672 | field = mysql_fetch_field (result); | ||
673 | |||
674 | //If it's the correct field | ||
675 | if (field->name == fname) | ||
676 | { | ||
677 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
678 | "Field '%s' does not exist in result", | ||
679 | fname); | ||
680 | return GNUNET_SYSERR; | ||
681 | } | ||
682 | |||
683 | |||
684 | if (rows[row] == NULL) | ||
685 | { | ||
686 | return GNUNET_SYSERR; | ||
687 | } | ||
688 | |||
689 | GNUNET_assert (NULL != dst); | ||
690 | if (sizeof (uint64_t) != *dst_size) | ||
691 | { | ||
692 | GNUNET_break (0); | ||
693 | return GNUNET_SYSERR; | ||
694 | } | ||
695 | |||
696 | res = (uint64_t) rows[row]; | ||
697 | *udst = GNUNET_ntohll (*res); | ||
698 | |||
699 | return GNUNET_OK; | ||
700 | } | ||
701 | |||
702 | |||
703 | /** | ||
704 | * uint64_t expected. | ||
705 | * | ||
706 | * @param[out] u64 where to store the result | ||
707 | * @return array entry for the result specification to use | ||
708 | */ | ||
709 | struct GNUNET_MY_ResultSpec | ||
710 | GNUNET_MY_result_spec_uint64 (uint64_t *u64) | ||
711 | { | ||
712 | struct GNUNET_MY_ResultSpec res = { | ||
713 | &extract_uint64, | ||
714 | NULL, | ||
715 | (void *) u64, | ||
716 | sizeof (*u64), | ||
717 | NULL | ||
718 | }; | ||
719 | return res; | ||
720 | } | ||
721 | |||
569 | /* end of pq_result_helper.c */ \ No newline at end of file | 722 | /* end of pq_result_helper.c */ \ No newline at end of file |