diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-05-19 10:05:51 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-05-19 10:05:51 +0000 |
commit | c68f44aa5797f1e765e6119a5b895c1746e8c958 (patch) | |
tree | 68837e17195ca6f4602bfea82a0b1bdb90a29c62 /src/my | |
parent | ab43f423dbc9bf0a4d2824c7a318f781f7889a06 (diff) | |
download | gnunet-c68f44aa5797f1e765e6119a5b895c1746e8c958.tar.gz gnunet-c68f44aa5797f1e765e6119a5b895c1746e8c958.zip |
starting libgnunetmy
Diffstat (limited to 'src/my')
-rw-r--r-- | src/my/Makefile.am | 24 | ||||
-rw-r--r-- | src/my/my.c | 94 | ||||
-rw-r--r-- | src/my/my_query_helper.c | 73 |
3 files changed, 191 insertions, 0 deletions
diff --git a/src/my/Makefile.am b/src/my/Makefile.am new file mode 100644 index 000000000..54e6d5563 --- /dev/null +++ b/src/my/Makefile.am | |||
@@ -0,0 +1,24 @@ | |||
1 | # This Makefile.am is in the public domain | ||
2 | AM_CPPFLAGS = -I$(top_srcdir)/src/include | ||
3 | |||
4 | if MINGW | ||
5 | WINFLAGS = -Wl,--no-undefined -Wl,--export-all-symbols | ||
6 | endif | ||
7 | |||
8 | if USE_COVERAGE | ||
9 | AM_CFLAGS = --coverage | ||
10 | endif | ||
11 | |||
12 | if HAVE_MYSQL | ||
13 | lib_LTLIBRARIES = libgnunetmy.la | ||
14 | endif | ||
15 | |||
16 | libgnunetmy_la_SOURCES = \ | ||
17 | my.c \ | ||
18 | my_query_helper.c | ||
19 | libgnunetmy_la_LIBADD = $(MYSQL_LDFLAGS) -lmysqlclient \ | ||
20 | $(top_builddir)/src/mysql/libgnunetmysql.la \ | ||
21 | $(top_builddir)/src/util/libgnunetutil.la | ||
22 | libgnunetmy_la_LDFLAGS = \ | ||
23 | $(GN_LIB_LDFLAGS) \ | ||
24 | -version-info 0:0:0 | ||
diff --git a/src/my/my.c b/src/my/my.c new file mode 100644 index 000000000..62b1ede09 --- /dev/null +++ b/src/my/my.c | |||
@@ -0,0 +1,94 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file my/my.c | ||
22 | * @brief library to help with access to a MySQL database | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <mysql/mysql.h> | ||
27 | #include "gnunet_my_lib.h" | ||
28 | |||
29 | |||
30 | |||
31 | /** | ||
32 | * Run a prepared SELECT statement. | ||
33 | * | ||
34 | * @param mc mysql context | ||
35 | * @param sh handle to SELECT statment | ||
36 | * @param params parameters to the statement | ||
37 | * @return | ||
38 | */ | ||
39 | int | ||
40 | GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | ||
41 | struct GNUNET_MYSQL_StatementHandle *sh, | ||
42 | const struct GNUNET_MY_QueryParam *params) | ||
43 | { | ||
44 | const struct GNUNET_MY_QueryParam *p; | ||
45 | unsigned int num; | ||
46 | unsigned int i; | ||
47 | MYSQL_STMT *stmt; | ||
48 | |||
49 | num = 0; | ||
50 | for (i=0;NULL != params[i].conv;i++) | ||
51 | num += params[i].num_params; | ||
52 | { | ||
53 | MYSQL_BIND qbind[num]; | ||
54 | unsigned int off; | ||
55 | |||
56 | memset(qbind, 0, sizeof(qbind)); | ||
57 | off = 0; | ||
58 | for (i=0;NULL != (p = ¶ms[i])->conv;i++) | ||
59 | { | ||
60 | if (GNUNET_OK != | ||
61 | p->conv (p->conv_cls, | ||
62 | p, | ||
63 | &qbind[off])) | ||
64 | { | ||
65 | return GNUNET_SYSERR; | ||
66 | } | ||
67 | off += p->num_params; | ||
68 | } | ||
69 | stmt = GNUNET_MYSQL_statement_get_stmt (mc, sh); | ||
70 | if (mysql_stmt_bind_param (stmt, | ||
71 | qbind)) | ||
72 | { | ||
73 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", | ||
74 | _("`%s' failed at %s:%d with error: %s\n"), | ||
75 | "mysql_stmt_bind_param", __FILE__, __LINE__, | ||
76 | mysql_stmt_error (stmt)); | ||
77 | GNUNET_MYSQL_statements_invalidate (mc); | ||
78 | return GNUNET_SYSERR; | ||
79 | } | ||
80 | } | ||
81 | if (mysql_stmt_execute (stmt)) | ||
82 | { | ||
83 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql", | ||
84 | _("`%s' failed at %s:%d with error: %s\n"), | ||
85 | "mysql_stmt_execute", __FILE__, __LINE__, | ||
86 | mysql_stmt_error (stmt)); | ||
87 | GNUNET_MYSQL_statements_invalidate (mc); | ||
88 | return GNUNET_SYSERR; | ||
89 | } | ||
90 | return GNUNET_OK; | ||
91 | } | ||
92 | |||
93 | |||
94 | /* end of my.c */ | ||
diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c new file mode 100644 index 000000000..057c32d9f --- /dev/null +++ b/src/my/my_query_helper.c | |||
@@ -0,0 +1,73 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | ||
19 | */ | ||
20 | /** | ||
21 | * @file my/my_query_helper.c | ||
22 | * @brief library to help with access to a MySQL database | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include <mysql/mysql.h> | ||
27 | #include "gnunet_my_lib.h" | ||
28 | |||
29 | |||
30 | /** | ||
31 | * Function called to convert input argument into SQL parameters. | ||
32 | * | ||
33 | * @param cls closure | ||
34 | * @param pq data about the query | ||
35 | * @param qbind array of parameters to initialize | ||
36 | * @return -1 on error | ||
37 | */ | ||
38 | static int | ||
39 | pq_conv_fixed_size (void *cls, | ||
40 | const struct GNUNET_MY_QueryParam *qp, | ||
41 | MYSQL_BIND *qbind) | ||
42 | { | ||
43 | GNUNET_assert (1 == qp->num_params); | ||
44 | qbind->buffer = (void *) qp->data; | ||
45 | qbind->buffer_length = qp->data_len; | ||
46 | qbind->length = (unsigned long *) &qp->data_len; | ||
47 | return 0; | ||
48 | } | ||
49 | |||
50 | |||
51 | /** | ||
52 | * Generate query parameter for a buffer @a ptr of | ||
53 | * @a ptr_size bytes. | ||
54 | * | ||
55 | * @param ptr pointer to the query parameter to pass | ||
56 | * @oaran ptr_size number of bytes in @a ptr | ||
57 | */ | ||
58 | struct GNUNET_MY_QueryParam | ||
59 | GNUNET_MY_query_param_fixed_size (const void *ptr, | ||
60 | size_t ptr_size) | ||
61 | { | ||
62 | struct GNUNET_MY_QueryParam qp = { | ||
63 | &pq_conv_fixed_size, | ||
64 | NULL, | ||
65 | 1, | ||
66 | ptr, | ||
67 | (unsigned long) ptr_size | ||
68 | }; | ||
69 | return qp; | ||
70 | } | ||
71 | |||
72 | |||
73 | /* end of my_query_helper.c */ | ||