aboutsummaryrefslogtreecommitdiff
path: root/src/my/my_query_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/my/my_query_helper.c')
-rw-r--r--src/my/my_query_helper.c401
1 files changed, 0 insertions, 401 deletions
diff --git a/src/my/my_query_helper.c b/src/my/my_query_helper.c
deleted file mode 100644
index c12970876..000000000
--- a/src/my/my_query_helper.c
+++ /dev/null
@@ -1,401 +0,0 @@
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 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_query_helper.c
22 * @brief library to help with access to a MySQL database
23 * @author Christian Grothoff
24 * @author Christophe Genevey
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 * Function called to clean up memory allocated
34 * by a #GNUNET_MY_QueryConverter.
35 *
36 * @param cls closure
37 * @param qbind array of parameter to clean up
38 */
39static void
40my_clean_query (void *cls,
41 MYSQL_BIND *qbind)
42{
43 (void) cls;
44 GNUNET_free (qbind[0].buffer);
45}
46
47
48/**
49 * Function called to convert input argument into SQL parameters.
50 *
51 * @param cls closure
52 * @param qp data about the query
53 * @param qbind array of parameters to initialize
54 * @return -1 on error
55 */
56static int
57my_conv_fixed_size (void *cls,
58 const struct GNUNET_MY_QueryParam *qp,
59 MYSQL_BIND *qbind)
60{
61 (void) cls;
62 GNUNET_assert (1 == qp->num_params);
63 qbind->buffer = (void *) qp->data;
64 qbind->buffer_length = qp->data_len;
65 qbind->buffer_type = MYSQL_TYPE_BLOB;
66
67 return 1;
68}
69
70
71/**
72 * Generate query parameter for a buffer @a ptr of
73 * @a ptr_size bytes.
74 *
75 * @param ptr pointer to the query parameter to pass
76 * @param ptr_size number of bytes in @a ptr
77 */
78struct GNUNET_MY_QueryParam
79GNUNET_MY_query_param_fixed_size (const void *ptr,
80 size_t ptr_size)
81{
82 struct GNUNET_MY_QueryParam qp = {
83 .conv = &my_conv_fixed_size,
84 .cleaner = NULL,
85 .conv_cls = NULL,
86 .num_params = 1,
87 .data = ptr,
88 .data_len = (unsigned long) ptr_size
89 };
90
91 return qp;
92}
93
94
95/**
96 * Function called to convert input argument into SQL parameters.
97 *
98 * @param cls closure
99 * @param qp data about the query
100 * @param qbind array of parameters to initialize
101 * @return -1 on error
102 */
103static int
104my_conv_string (void *cls,
105 const struct GNUNET_MY_QueryParam *qp,
106 MYSQL_BIND *qbind)
107{
108 (void) cls;
109 GNUNET_assert (1 == qp->num_params);
110 qbind->buffer = (void *) qp->data;
111 qbind->buffer_length = qp->data_len;
112 qbind->buffer_type = MYSQL_TYPE_STRING;
113 return 1;
114}
115
116
117/**
118 * Generate query parameter for a string
119 *
120 * @param ptr pointer to the string query parameter to pass
121 */
122struct GNUNET_MY_QueryParam
123GNUNET_MY_query_param_string (const char *ptr)
124{
125 struct GNUNET_MY_QueryParam qp = {
126 .conv = &my_conv_string,
127 .cleaner = NULL,
128 .conv_cls = NULL,
129 .num_params = 1,
130 .data = ptr,
131 .data_len = strlen (ptr)
132 };
133
134 return qp;
135}
136
137
138/**
139 * Function called to convert input argument into SQL parameters
140 *
141 * @param cls closure
142 * @param qp data about the query
143 * @param qbind array of parameters to initialize
144 * @return -1 on error
145 */
146static int
147my_conv_uint16 (void *cls,
148 const struct GNUNET_MY_QueryParam *qp,
149 MYSQL_BIND *qbind)
150{
151 (void) cls;
152 GNUNET_assert (1 == qp->num_params);
153 qbind->buffer = (void *) qp->data;
154 qbind->buffer_length = sizeof(uint16_t);
155 qbind->buffer_type = MYSQL_TYPE_SHORT;
156 qbind->is_unsigned = 1;
157 return 1;
158}
159
160
161/**
162 * Generate query parameter for an uint16_t in host byte order.
163 *
164 * @param x pointer to the query parameter to pass
165 */
166struct GNUNET_MY_QueryParam
167GNUNET_MY_query_param_uint16 (const uint16_t *x)
168{
169 struct GNUNET_MY_QueryParam res = {
170 .conv = &my_conv_uint16,
171 .cleaner = NULL,
172 .conv_cls = NULL,
173 .num_params = 1,
174 .data = x,
175 .data_len = sizeof(*x)
176 };
177
178 return res;
179}
180
181
182/**
183 * Function called to convert input argument into SQL parameters
184 *
185 * @param cls closure
186 * @param qp data about the query
187 * @param qbind array of parameters to initialize
188 * @return -1 on error
189 */
190static int
191my_conv_uint32 (void *cls,
192 const struct GNUNET_MY_QueryParam *qp,
193 MYSQL_BIND *qbind)
194{
195 (void) cls;
196 GNUNET_assert (1 == qp->num_params);
197 qbind->buffer = (void *) qp->data;
198 qbind->buffer_length = sizeof(uint32_t);
199 qbind->buffer_type = MYSQL_TYPE_LONG;
200 qbind->is_unsigned = 1;
201 return 1;
202}
203
204
205/**
206 * Generate query parameter for an uint32_t in host byte order
207 *
208 * @param x pointer to the query parameter to pass
209 */
210struct GNUNET_MY_QueryParam
211GNUNET_MY_query_param_uint32 (const uint32_t *x)
212{
213 struct GNUNET_MY_QueryParam res = {
214 .conv = &my_conv_uint32,
215 .cleaner = NULL,
216 .conv_cls = NULL,
217 .num_params = 1,
218 .data = x,
219 .data_len = sizeof(*x)
220 };
221
222 return res;
223}
224
225
226/**
227 * Function called to convert input argument into SQL parameters
228 *
229 * @param cls closure
230 * @param qp data about the query
231 * @param qbind array of parameters to initialize
232 * @return -1 on error
233 */
234static int
235my_conv_uint64 (void *cls,
236 const struct GNUNET_MY_QueryParam *qp,
237 MYSQL_BIND *qbind)
238{
239 (void) cls;
240 GNUNET_assert (1 == qp->num_params);
241 qbind->buffer = (void *) qp->data;
242 qbind->buffer_length = sizeof(uint64_t);
243 qbind->buffer_type = MYSQL_TYPE_LONGLONG;
244 qbind->is_unsigned = 1;
245 return 1;
246}
247
248
249/**
250 * Generate query parameter for an uint64_t in host byte order
251 *
252 * @param x pointer to the query parameter to pass
253 */
254struct GNUNET_MY_QueryParam
255GNUNET_MY_query_param_uint64 (const uint64_t *x)
256{
257 struct GNUNET_MY_QueryParam res = {
258 .conv = &my_conv_uint64,
259 .cleaner = NULL,
260 .conv_cls = NULL,
261 .num_params = 1,
262 .data = x,
263 .data_len = sizeof(*x)
264 };
265
266 return res;
267}
268
269
270/**
271 * Function called to convert input argument into SQL parameters
272 *
273 * @param cls closure
274 * @param qp data about the query
275 * @param qbind array of parameters to initialize
276 * @return -1 on error
277 */
278static int
279my_conv_rsa_public_key (void *cls,
280 const struct GNUNET_MY_QueryParam *qp,
281 MYSQL_BIND *qbind)
282{
283 const struct GNUNET_CRYPTO_RsaPublicKey *rsa = qp->data;
284 void *buf;
285 size_t buf_size;
286
287 (void) cls;
288 GNUNET_assert (1 == qp->num_params);
289 buf_size = GNUNET_CRYPTO_rsa_public_key_encode (rsa,
290 &buf);
291 qbind->buffer = buf;
292 qbind->buffer_length = buf_size;
293 qbind->buffer_type = MYSQL_TYPE_BLOB;
294 return 1;
295}
296
297
298/**
299 * Generate query parameter for an RSA public key. The
300 * database must contain a BLOB type in the respective position.
301 *
302 * @param x the query parameter to pass
303 * @return array entry for the query parameters to use
304 */
305struct GNUNET_MY_QueryParam
306GNUNET_MY_query_param_rsa_public_key (const struct
307 GNUNET_CRYPTO_RsaPublicKey *x)
308{
309 struct GNUNET_MY_QueryParam res = {
310 .conv = &my_conv_rsa_public_key,
311 .cleaner = &my_clean_query,
312 .conv_cls = NULL,
313 .num_params = 1,
314 .data = x,
315 .data_len = 0
316 };
317
318 return res;
319}
320
321
322/**
323 * Function called to convert input argument into SQL parameters
324 *
325 *@param cls closure
326 *@param qp data about the query
327 *@param qbind array of parameters to initialize
328 *@return -1 on error
329 */
330static int
331my_conv_rsa_signature (void *cls,
332 const struct GNUNET_MY_QueryParam *qp,
333 MYSQL_BIND *qbind)
334{
335 const struct GNUNET_CRYPTO_RsaSignature *sig = qp->data;
336 void *buf;
337 size_t buf_size;
338
339 (void) cls;
340 GNUNET_assert (1 == qp->num_params);
341 buf_size = GNUNET_CRYPTO_rsa_signature_encode (sig,
342 &buf);
343 qbind->buffer = buf;
344 qbind->buffer_length = buf_size;
345 qbind->buffer_type = MYSQL_TYPE_BLOB;
346 return 1;
347}
348
349
350/**
351 * Generate query parameter for an RSA signature. The
352 * database must contain a BLOB type in the respective position
353 *
354 * @param x the query parameter to pass
355 * @return array entry for the query parameters to use
356 */
357struct GNUNET_MY_QueryParam
358GNUNET_MY_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x)
359{
360 struct GNUNET_MY_QueryParam res = {
361 .conv = &my_conv_rsa_signature,
362 .cleaner = &my_clean_query,
363 .conv_cls = NULL,
364 .num_params = 1,
365 .data = (x),
366 .data_len = 0
367 };
368
369 return res;
370}
371
372
373/**
374 * Generate query parameter for an absolute time value.
375 * The database must store a 64-bit integer.
376 *
377 * @param x pointer to the query parameter to pass
378 * @return array entry for the query parameters to use
379 */
380struct GNUNET_MY_QueryParam
381GNUNET_MY_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x)
382{
383 return GNUNET_MY_query_param_uint64 (&x->abs_value_us);
384}
385
386
387/**
388 * Generate query parameter for an absolute time value.
389 * The database must store a 64-bit integer.
390 *
391 * @param x pointer to the query parameter to pass
392 */
393struct GNUNET_MY_QueryParam
394GNUNET_MY_query_param_absolute_time_nbo (const struct
395 GNUNET_TIME_AbsoluteNBO *x)
396{
397 return GNUNET_MY_query_param_auto_from_type (&x->abs_value_us__);
398}
399
400
401/* end of my_query_helper.c */