diff options
Diffstat (limited to 'src/my/my.c')
-rw-r--r-- | src/my/my.c | 270 |
1 files changed, 0 insertions, 270 deletions
diff --git a/src/my/my.c b/src/my/my.c deleted file mode 100644 index b667af4f9..000000000 --- a/src/my/my.c +++ /dev/null | |||
@@ -1,270 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2016, 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your 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 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /** | ||
21 | * @file my/my.c | ||
22 | * @brief library to help with access to a MySQL database | ||
23 | * @author Christophe Genevey | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include <mysql/mysql.h> | ||
28 | #include "gnunet_mysql_compat.h" | ||
29 | #include "gnunet_my_lib.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * Run a prepared SELECT statement. | ||
34 | * | ||
35 | * @param mc mysql context | ||
36 | * @param sh handle to SELECT statement | ||
37 | * @param params parameters to the statement | ||
38 | * @return | ||
39 | #GNUNET_YES if we can prepare all statement | ||
40 | #GNUNET_SYSERR if we can't prepare all statement | ||
41 | */ | ||
42 | int | ||
43 | GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | ||
44 | struct GNUNET_MYSQL_StatementHandle *sh, | ||
45 | struct GNUNET_MY_QueryParam *params) | ||
46 | { | ||
47 | const struct GNUNET_MY_QueryParam *p; | ||
48 | unsigned int num; | ||
49 | MYSQL_STMT *stmt; | ||
50 | |||
51 | num = 0; | ||
52 | for (unsigned int i = 0; NULL != params[i].conv; i++) | ||
53 | num += params[i].num_params; | ||
54 | { | ||
55 | MYSQL_BIND qbind[num]; | ||
56 | unsigned int off; | ||
57 | |||
58 | memset (qbind, | ||
59 | 0, | ||
60 | sizeof(qbind)); | ||
61 | off = 0; | ||
62 | for (unsigned int i = 0; NULL != (p = ¶ms[i])->conv; i++) | ||
63 | { | ||
64 | if (GNUNET_OK != | ||
65 | p->conv (p->conv_cls, | ||
66 | p, | ||
67 | &qbind[off])) | ||
68 | { | ||
69 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
70 | "Conversion for MySQL query failed at offset %u\n", | ||
71 | i); | ||
72 | return GNUNET_SYSERR; | ||
73 | } | ||
74 | off += p->num_params; | ||
75 | } | ||
76 | stmt = GNUNET_MYSQL_statement_get_stmt (sh); | ||
77 | if (mysql_stmt_bind_param (stmt, | ||
78 | qbind)) | ||
79 | { | ||
80 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
81 | "my", | ||
82 | _ ("`%s' failed at %s:%d with error: %s\n"), | ||
83 | "mysql_stmt_bind_param", | ||
84 | __FILE__, __LINE__, | ||
85 | mysql_stmt_error (stmt)); | ||
86 | GNUNET_MYSQL_statements_invalidate (mc); | ||
87 | return GNUNET_SYSERR; | ||
88 | } | ||
89 | |||
90 | if (mysql_stmt_execute (stmt)) | ||
91 | { | ||
92 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
93 | "my", | ||
94 | _ ("`%s' failed at %s:%d with error: %s\n"), | ||
95 | "mysql_stmt_execute", __FILE__, __LINE__, | ||
96 | mysql_stmt_error (stmt)); | ||
97 | GNUNET_MYSQL_statements_invalidate (mc); | ||
98 | return GNUNET_SYSERR; | ||
99 | } | ||
100 | GNUNET_MY_cleanup_query (params, | ||
101 | qbind); | ||
102 | } | ||
103 | return GNUNET_OK; | ||
104 | } | ||
105 | |||
106 | |||
107 | /** | ||
108 | * Free all memory that was allocated in @a qp during | ||
109 | * #GNUNET_MY_exec_prepared(). | ||
110 | * | ||
111 | * @param qp query specification to clean up | ||
112 | * @param qbind array of parameter to clean up | ||
113 | */ | ||
114 | void | ||
115 | GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp, | ||
116 | MYSQL_BIND *qbind) | ||
117 | { | ||
118 | for (unsigned int i = 0; NULL != qp[i].conv; i++) | ||
119 | if (NULL != qp[i].cleaner) | ||
120 | qp[i].cleaner (qp[i].conv_cls, | ||
121 | &qbind[i]); | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Extract results from a query result according to the given | ||
127 | * specification. Always fetches the next row. | ||
128 | * | ||
129 | * @param sh statement that returned results | ||
130 | * @param rs specification to extract for | ||
131 | * @return | ||
132 | * #GNUNET_YES if all results could be extracted | ||
133 | * #GNUNET_NO if there is no more data in the result set | ||
134 | * #GNUNET_SYSERR if a result was invalid | ||
135 | */ | ||
136 | int | ||
137 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | ||
138 | struct GNUNET_MY_ResultSpec *rs) | ||
139 | { | ||
140 | unsigned int num_fields; | ||
141 | int ret; | ||
142 | MYSQL_STMT *stmt; | ||
143 | |||
144 | stmt = GNUNET_MYSQL_statement_get_stmt (sh); | ||
145 | if (NULL == stmt) | ||
146 | { | ||
147 | GNUNET_break (0); | ||
148 | return GNUNET_SYSERR; | ||
149 | } | ||
150 | if (NULL == rs) | ||
151 | { | ||
152 | mysql_stmt_free_result (stmt); | ||
153 | return GNUNET_NO; | ||
154 | } | ||
155 | |||
156 | num_fields = 0; | ||
157 | for (unsigned int i = 0; NULL != rs[i].pre_conv; i++) | ||
158 | num_fields += rs[i].num_fields; | ||
159 | |||
160 | if (mysql_stmt_field_count (stmt) != num_fields) | ||
161 | { | ||
162 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
163 | "Number of fields mismatch between SQL result and result specification\n"); | ||
164 | return GNUNET_SYSERR; | ||
165 | } | ||
166 | |||
167 | { | ||
168 | MYSQL_BIND result[num_fields]; | ||
169 | unsigned int field_off; | ||
170 | |||
171 | memset (result, 0, sizeof(MYSQL_BIND) * num_fields); | ||
172 | field_off = 0; | ||
173 | for (unsigned int i = 0; NULL != rs[i].pre_conv; i++) | ||
174 | { | ||
175 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | ||
176 | |||
177 | if (GNUNET_OK != | ||
178 | rp->pre_conv (rp->conv_cls, | ||
179 | rp, | ||
180 | stmt, | ||
181 | field_off, | ||
182 | &result[field_off])) | ||
183 | |||
184 | { | ||
185 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
186 | "Pre-conversion for MySQL result failed at offset %u\n", | ||
187 | i); | ||
188 | return GNUNET_SYSERR; | ||
189 | } | ||
190 | field_off += rp->num_fields; | ||
191 | } | ||
192 | |||
193 | if (mysql_stmt_bind_result (stmt, result)) | ||
194 | { | ||
195 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
196 | "my", | ||
197 | _ ("%s failed at %s:%d with error: %s\n"), | ||
198 | "mysql_stmt_bind_result", | ||
199 | __FILE__, __LINE__, | ||
200 | mysql_stmt_error (stmt)); | ||
201 | return GNUNET_SYSERR; | ||
202 | } | ||
203 | #if TEST_OPTIMIZATION | ||
204 | (void) mysql_stmt_store_result (stmt); | ||
205 | #endif | ||
206 | ret = mysql_stmt_fetch (stmt); | ||
207 | if (MYSQL_NO_DATA == ret) | ||
208 | { | ||
209 | mysql_stmt_free_result (stmt); | ||
210 | return GNUNET_NO; | ||
211 | } | ||
212 | if (1 == ret) | ||
213 | { | ||
214 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
215 | "my", | ||
216 | _ ("%s failed at %s:%d with error: %s\n"), | ||
217 | "mysql_stmt_fetch", | ||
218 | __FILE__, __LINE__, | ||
219 | mysql_stmt_error (stmt)); | ||
220 | GNUNET_MY_cleanup_result (rs); | ||
221 | mysql_stmt_free_result (stmt); | ||
222 | return GNUNET_SYSERR; | ||
223 | } | ||
224 | field_off = 0; | ||
225 | for (unsigned int i = 0; NULL != rs[i].post_conv; i++) | ||
226 | { | ||
227 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | ||
228 | |||
229 | if (NULL != rp->post_conv) | ||
230 | if (GNUNET_OK != | ||
231 | rp->post_conv (rp->conv_cls, | ||
232 | rp, | ||
233 | stmt, | ||
234 | field_off, | ||
235 | &result[field_off])) | ||
236 | { | ||
237 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
238 | "Post-conversion for MySQL result failed at offset %u\n", | ||
239 | i); | ||
240 | mysql_stmt_free_result (stmt); | ||
241 | for (unsigned int j = 0; j < i; j++) | ||
242 | if (NULL != rs[j].cleaner) | ||
243 | rs[j].cleaner (rs[j].conv_cls, | ||
244 | rs[j].dst); | ||
245 | return GNUNET_SYSERR; | ||
246 | } | ||
247 | field_off += rp->num_fields; | ||
248 | } | ||
249 | } | ||
250 | return GNUNET_OK; | ||
251 | } | ||
252 | |||
253 | |||
254 | /** | ||
255 | * Free all memory that was allocated in @a rs during | ||
256 | * #GNUNET_MY_extract_result(). | ||
257 | * | ||
258 | * @param rs result specification to clean up | ||
259 | */ | ||
260 | void | ||
261 | GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs) | ||
262 | { | ||
263 | for (unsigned int i = 0; NULL != rs[i].post_conv; i++) | ||
264 | if (NULL != rs[i].cleaner) | ||
265 | rs[i].cleaner (rs[i].conv_cls, | ||
266 | &rs[i]); | ||
267 | } | ||
268 | |||
269 | |||
270 | /* end of my.c */ | ||