aboutsummaryrefslogtreecommitdiff
path: root/src/my
diff options
context:
space:
mode:
authorChristophe Genevey Metat <genevey.christophe@gmail.com>2016-05-24 16:16:01 +0000
committerChristophe Genevey Metat <genevey.christophe@gmail.com>2016-05-24 16:16:01 +0000
commit52aa7299f2c3a9f9e80b366ca9068a6edf3b9bab (patch)
treee086c7db41ce02182a17e07e1c3d0899c398620c /src/my
parenta0f69c5d40c22904549bb3246c523681d00d72cd (diff)
downloadgnunet-52aa7299f2c3a9f9e80b366ca9068a6edf3b9bab.tar.gz
gnunet-52aa7299f2c3a9f9e80b366ca9068a6edf3b9bab.zip
fix result and query helper
Diffstat (limited to 'src/my')
-rw-r--r--src/my/Makefile.am18
-rw-r--r--src/my/my.c4
-rw-r--r--src/my/my_result_helper.c263
3 files changed, 228 insertions, 57 deletions
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
16libgnunetmy_la_SOURCES = \ 16libgnunetmy_la_SOURCES = \
17 my.c \ 17 my.c \
18 my_query_helper.c 18 my_query_helper.c \
19 my_result_helper.c
20
19libgnunetmy_la_LIBADD = $(MYSQL_LDFLAGS) -lmysqlclient \ 21libgnunetmy_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
22libgnunetmy_la_LDFLAGS = \ 24libgnunetmy_la_LDFLAGS = \
23 $(GN_LIB_LDFLAGS) \ 25 $(GN_LIB_LDFLAGS) \
24 -version-info 0:0:0 26 -version-info 0:0:0
27
28TESTS = \
29 test_my
30
31check_PROGRAMS= \
32 test_my
33
34test_my_SOURCES = \
35 test_my.c
36test_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 *****/
39int 41int
40GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, 42GNUNET_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 */
30static void
31clean_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 */
144static int 123static int
145extracted_fixed_blob (void *cls, 124extract_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,
235static int 214static int
236extract_rsa_public_key (void *cls, 215extract_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 */
295static void
296clean_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 */
577static int
578extract_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 */
630struct GNUNET_MY_ResultSpec
631GNUNET_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 */
656static int
657extract_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 */
709struct GNUNET_MY_ResultSpec
710GNUNET_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