diff options
Diffstat (limited to 'src/my/my.c')
-rw-r--r-- | src/my/my.c | 274 |
1 files changed, 137 insertions, 137 deletions
diff --git a/src/my/my.c b/src/my/my.c index dd2140c31..bee18145d 100644 --- a/src/my/my.c +++ b/src/my/my.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | /** | 20 | /** |
21 | * @file my/my.c | 21 | * @file my/my.c |
22 | * @brief library to help with access to a MySQL database | 22 | * @brief library to help with access to a MySQL database |
@@ -35,69 +35,69 @@ | |||
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 |
38 | #GNUNET_YES if we can prepare all statement | 38 | #GNUNET_YES if we can prepare all statement |
39 | #GNUNET_SYSERR if we can't prepare all statement | 39 | #GNUNET_SYSERR if we can't prepare all statement |
40 | */ | 40 | */ |
41 | int | 41 | int |
42 | GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | 42 | GNUNET_MY_exec_prepared(struct GNUNET_MYSQL_Context *mc, |
43 | struct GNUNET_MYSQL_StatementHandle *sh, | 43 | struct GNUNET_MYSQL_StatementHandle *sh, |
44 | struct GNUNET_MY_QueryParam *params) | 44 | struct GNUNET_MY_QueryParam *params) |
45 | { | 45 | { |
46 | const struct GNUNET_MY_QueryParam *p; | 46 | const struct GNUNET_MY_QueryParam *p; |
47 | unsigned int num; | 47 | unsigned int num; |
48 | MYSQL_STMT *stmt; | 48 | MYSQL_STMT *stmt; |
49 | 49 | ||
50 | num = 0; | 50 | num = 0; |
51 | for (unsigned int i=0;NULL != params[i].conv;i++) | 51 | for (unsigned int i = 0; NULL != params[i].conv; i++) |
52 | num += params[i].num_params; | 52 | num += params[i].num_params; |
53 | { | 53 | { |
54 | MYSQL_BIND qbind[num]; | 54 | MYSQL_BIND qbind[num]; |
55 | unsigned int off; | 55 | unsigned int off; |
56 | 56 | ||
57 | memset (qbind, | 57 | memset(qbind, |
58 | 0, | 58 | 0, |
59 | sizeof(qbind)); | 59 | sizeof(qbind)); |
60 | off = 0; | 60 | off = 0; |
61 | for (unsigned int i=0;NULL != (p = ¶ms[i])->conv;i++) | 61 | for (unsigned int i = 0; NULL != (p = ¶ms[i])->conv; i++) |
62 | { | 62 | { |
63 | if (GNUNET_OK != | 63 | if (GNUNET_OK != |
64 | p->conv (p->conv_cls, | 64 | p->conv(p->conv_cls, |
65 | p, | 65 | p, |
66 | &qbind[off])) | 66 | &qbind[off])) |
67 | { | ||
68 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
69 | "Conversion for MySQL query failed at offset %u\n", | ||
70 | i); | ||
71 | return GNUNET_SYSERR; | ||
72 | } | ||
73 | off += p->num_params; | ||
74 | } | ||
75 | stmt = GNUNET_MYSQL_statement_get_stmt(sh); | ||
76 | if (mysql_stmt_bind_param(stmt, | ||
77 | qbind)) | ||
67 | { | 78 | { |
68 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 79 | GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, |
69 | "Conversion for MySQL query failed at offset %u\n", | 80 | "my", |
70 | i); | 81 | _("`%s' failed at %s:%d with error: %s\n"), |
82 | "mysql_stmt_bind_param", | ||
83 | __FILE__, __LINE__, | ||
84 | mysql_stmt_error(stmt)); | ||
85 | GNUNET_MYSQL_statements_invalidate(mc); | ||
71 | return GNUNET_SYSERR; | 86 | return GNUNET_SYSERR; |
72 | } | 87 | } |
73 | off += p->num_params; | ||
74 | } | ||
75 | stmt = GNUNET_MYSQL_statement_get_stmt (sh); | ||
76 | if (mysql_stmt_bind_param (stmt, | ||
77 | qbind)) | ||
78 | { | ||
79 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
80 | "my", | ||
81 | _("`%s' failed at %s:%d with error: %s\n"), | ||
82 | "mysql_stmt_bind_param", | ||
83 | __FILE__, __LINE__, | ||
84 | mysql_stmt_error (stmt)); | ||
85 | GNUNET_MYSQL_statements_invalidate (mc); | ||
86 | return GNUNET_SYSERR; | ||
87 | } | ||
88 | 88 | ||
89 | if (mysql_stmt_execute (stmt)) | 89 | if (mysql_stmt_execute(stmt)) |
90 | { | 90 | { |
91 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | 91 | GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, |
92 | "my", | 92 | "my", |
93 | _("`%s' failed at %s:%d with error: %s\n"), | 93 | _("`%s' failed at %s:%d with error: %s\n"), |
94 | "mysql_stmt_execute", __FILE__, __LINE__, | 94 | "mysql_stmt_execute", __FILE__, __LINE__, |
95 | mysql_stmt_error (stmt)); | 95 | mysql_stmt_error(stmt)); |
96 | GNUNET_MYSQL_statements_invalidate (mc); | 96 | GNUNET_MYSQL_statements_invalidate(mc); |
97 | return GNUNET_SYSERR; | 97 | return GNUNET_SYSERR; |
98 | } | 98 | } |
99 | GNUNET_MY_cleanup_query (params, | 99 | GNUNET_MY_cleanup_query(params, |
100 | qbind); | 100 | qbind); |
101 | } | 101 | } |
102 | return GNUNET_OK; | 102 | return GNUNET_OK; |
103 | } | 103 | } |
@@ -111,13 +111,13 @@ GNUNET_MY_exec_prepared (struct GNUNET_MYSQL_Context *mc, | |||
111 | * @param qbind array of parameter to clean up | 111 | * @param qbind array of parameter to clean up |
112 | */ | 112 | */ |
113 | void | 113 | void |
114 | GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp, | 114 | GNUNET_MY_cleanup_query(struct GNUNET_MY_QueryParam *qp, |
115 | MYSQL_BIND *qbind) | 115 | MYSQL_BIND *qbind) |
116 | { | 116 | { |
117 | for (unsigned int i=0; NULL != qp[i].conv ;i++) | 117 | for (unsigned int i = 0; NULL != qp[i].conv; i++) |
118 | if (NULL != qp[i].cleaner) | 118 | if (NULL != qp[i].cleaner) |
119 | qp[i].cleaner (qp[i].conv_cls, | 119 | qp[i].cleaner(qp[i].conv_cls, |
120 | &qbind[i]); | 120 | &qbind[i]); |
121 | } | 121 | } |
122 | 122 | ||
123 | 123 | ||
@@ -133,115 +133,115 @@ GNUNET_MY_cleanup_query (struct GNUNET_MY_QueryParam *qp, | |||
133 | * #GNUNET_SYSERR if a result was invalid | 133 | * #GNUNET_SYSERR if a result was invalid |
134 | */ | 134 | */ |
135 | int | 135 | int |
136 | GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | 136 | GNUNET_MY_extract_result(struct GNUNET_MYSQL_StatementHandle *sh, |
137 | struct GNUNET_MY_ResultSpec *rs) | 137 | struct GNUNET_MY_ResultSpec *rs) |
138 | { | 138 | { |
139 | unsigned int num_fields; | 139 | unsigned int num_fields; |
140 | int ret; | 140 | int ret; |
141 | MYSQL_STMT *stmt; | 141 | MYSQL_STMT *stmt; |
142 | 142 | ||
143 | stmt = GNUNET_MYSQL_statement_get_stmt (sh); | 143 | stmt = GNUNET_MYSQL_statement_get_stmt(sh); |
144 | if (NULL == stmt) | 144 | if (NULL == stmt) |
145 | { | 145 | { |
146 | GNUNET_break (0); | 146 | GNUNET_break(0); |
147 | return GNUNET_SYSERR; | 147 | return GNUNET_SYSERR; |
148 | } | 148 | } |
149 | if (NULL == rs) | 149 | if (NULL == rs) |
150 | { | 150 | { |
151 | mysql_stmt_free_result (stmt); | 151 | mysql_stmt_free_result(stmt); |
152 | return GNUNET_NO; | 152 | return GNUNET_NO; |
153 | } | 153 | } |
154 | 154 | ||
155 | num_fields = 0; | 155 | num_fields = 0; |
156 | for (unsigned int i=0;NULL != rs[i].pre_conv;i++) | 156 | for (unsigned int i = 0; NULL != rs[i].pre_conv; i++) |
157 | num_fields += rs[i].num_fields; | 157 | num_fields += rs[i].num_fields; |
158 | 158 | ||
159 | if (mysql_stmt_field_count (stmt) != num_fields) | 159 | if (mysql_stmt_field_count(stmt) != num_fields) |
160 | { | 160 | { |
161 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 161 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, |
162 | "Number of fields mismatch between SQL result and result specification\n"); | 162 | "Number of fields mismatch between SQL result and result specification\n"); |
163 | return GNUNET_SYSERR; | 163 | return GNUNET_SYSERR; |
164 | } | 164 | } |
165 | 165 | ||
166 | { | 166 | { |
167 | MYSQL_BIND result[num_fields]; | 167 | MYSQL_BIND result[num_fields]; |
168 | unsigned int field_off; | 168 | unsigned int field_off; |
169 | 169 | ||
170 | memset (result, 0, sizeof (MYSQL_BIND) * num_fields); | 170 | memset(result, 0, sizeof(MYSQL_BIND) * num_fields); |
171 | field_off = 0; | 171 | field_off = 0; |
172 | for (unsigned int i=0;NULL != rs[i].pre_conv;i++) | 172 | for (unsigned int i = 0; NULL != rs[i].pre_conv; i++) |
173 | { | 173 | { |
174 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | 174 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
175 | 175 | ||
176 | if (GNUNET_OK != | 176 | if (GNUNET_OK != |
177 | rp->pre_conv (rp->conv_cls, | 177 | rp->pre_conv(rp->conv_cls, |
178 | rp, | 178 | rp, |
179 | stmt, | 179 | stmt, |
180 | field_off, | 180 | field_off, |
181 | &result[field_off])) | 181 | &result[field_off])) |
182 | |||
183 | { | ||
184 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, | ||
185 | "Pre-conversion for MySQL result failed at offset %u\n", | ||
186 | i); | ||
187 | return GNUNET_SYSERR; | ||
188 | } | ||
189 | field_off += rp->num_fields; | ||
190 | } | ||
182 | 191 | ||
192 | if (mysql_stmt_bind_result(stmt, result)) | ||
183 | { | 193 | { |
184 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 194 | GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, |
185 | "Pre-conversion for MySQL result failed at offset %u\n", | 195 | "my", |
186 | i); | 196 | _("%s failed at %s:%d with error: %s\n"), |
197 | "mysql_stmt_bind_result", | ||
198 | __FILE__, __LINE__, | ||
199 | mysql_stmt_error(stmt)); | ||
187 | return GNUNET_SYSERR; | 200 | return GNUNET_SYSERR; |
188 | } | 201 | } |
189 | field_off += rp->num_fields; | ||
190 | } | ||
191 | |||
192 | if (mysql_stmt_bind_result (stmt, result)) | ||
193 | { | ||
194 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | ||
195 | "my", | ||
196 | _("%s failed at %s:%d with error: %s\n"), | ||
197 | "mysql_stmt_bind_result", | ||
198 | __FILE__, __LINE__, | ||
199 | mysql_stmt_error (stmt)); | ||
200 | return GNUNET_SYSERR; | ||
201 | } | ||
202 | #if TEST_OPTIMIZATION | 202 | #if TEST_OPTIMIZATION |
203 | (void) mysql_stmt_store_result (stmt); | 203 | (void)mysql_stmt_store_result(stmt); |
204 | #endif | 204 | #endif |
205 | ret = mysql_stmt_fetch (stmt); | 205 | ret = mysql_stmt_fetch(stmt); |
206 | if (MYSQL_NO_DATA == ret) | 206 | if (MYSQL_NO_DATA == ret) |
207 | { | 207 | { |
208 | mysql_stmt_free_result (stmt); | 208 | mysql_stmt_free_result(stmt); |
209 | return GNUNET_NO; | 209 | return GNUNET_NO; |
210 | } | 210 | } |
211 | if (1 == ret) | 211 | if (1 == ret) |
212 | { | 212 | { |
213 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, | 213 | GNUNET_log_from(GNUNET_ERROR_TYPE_ERROR, |
214 | "my", | 214 | "my", |
215 | _("%s failed at %s:%d with error: %s\n"), | 215 | _("%s failed at %s:%d with error: %s\n"), |
216 | "mysql_stmt_fetch", | 216 | "mysql_stmt_fetch", |
217 | __FILE__, __LINE__, | 217 | __FILE__, __LINE__, |
218 | mysql_stmt_error (stmt)); | 218 | mysql_stmt_error(stmt)); |
219 | GNUNET_MY_cleanup_result (rs); | 219 | GNUNET_MY_cleanup_result(rs); |
220 | mysql_stmt_free_result (stmt); | 220 | mysql_stmt_free_result(stmt); |
221 | return GNUNET_SYSERR; | 221 | return GNUNET_SYSERR; |
222 | } | 222 | } |
223 | field_off = 0; | 223 | field_off = 0; |
224 | for (unsigned int i=0;NULL != rs[i].post_conv;i++) | 224 | for (unsigned int i = 0; NULL != rs[i].post_conv; i++) |
225 | { | 225 | { |
226 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; | 226 | struct GNUNET_MY_ResultSpec *rp = &rs[i]; |
227 | 227 | ||
228 | if (NULL != rp->post_conv) | 228 | if (NULL != rp->post_conv) |
229 | if (GNUNET_OK != | 229 | if (GNUNET_OK != |
230 | rp->post_conv (rp->conv_cls, | 230 | rp->post_conv(rp->conv_cls, |
231 | rp, | 231 | rp, |
232 | stmt, | 232 | stmt, |
233 | field_off, | 233 | field_off, |
234 | &result[field_off])) | 234 | &result[field_off])) |
235 | { | 235 | { |
236 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 236 | GNUNET_log(GNUNET_ERROR_TYPE_INFO, |
237 | "Post-conversion for MySQL result failed at offset %u\n", | 237 | "Post-conversion for MySQL result failed at offset %u\n", |
238 | i); | 238 | i); |
239 | mysql_stmt_free_result (stmt); | 239 | mysql_stmt_free_result(stmt); |
240 | GNUNET_MY_cleanup_result (rs); | 240 | GNUNET_MY_cleanup_result(rs); |
241 | return GNUNET_SYSERR; | 241 | return GNUNET_SYSERR; |
242 | } | 242 | } |
243 | field_off += rp->num_fields; | 243 | field_off += rp->num_fields; |
244 | } | 244 | } |
245 | } | 245 | } |
246 | return GNUNET_OK; | 246 | return GNUNET_OK; |
247 | } | 247 | } |
@@ -254,12 +254,12 @@ GNUNET_MY_extract_result (struct GNUNET_MYSQL_StatementHandle *sh, | |||
254 | * @param rs result specification to clean up | 254 | * @param rs result specification to clean up |
255 | */ | 255 | */ |
256 | void | 256 | void |
257 | GNUNET_MY_cleanup_result (struct GNUNET_MY_ResultSpec *rs) | 257 | GNUNET_MY_cleanup_result(struct GNUNET_MY_ResultSpec *rs) |
258 | { | 258 | { |
259 | for (unsigned int i=0;NULL != rs[i].post_conv;i++) | 259 | for (unsigned int i = 0; NULL != rs[i].post_conv; i++) |
260 | if (NULL != rs[i].cleaner) | 260 | if (NULL != rs[i].cleaner) |
261 | rs[i].cleaner (rs[i].conv_cls, | 261 | rs[i].cleaner(rs[i].conv_cls, |
262 | &rs[i]); | 262 | &rs[i]); |
263 | } | 263 | } |
264 | 264 | ||
265 | 265 | ||