aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2017-02-27 00:23:34 +0100
committerChristian Grothoff <christian@grothoff.org>2017-02-27 00:23:47 +0100
commitbefd2f9fc787c2ac75790b06e1601f2f65305375 (patch)
tree902a372dc4996a33820abc70505d60ab9bf72c10 /src
parent82242cae3bb6313ee10df20570e4c14627828033 (diff)
downloadgnunet-befd2f9fc787c2ac75790b06e1601f2f65305375.tar.gz
gnunet-befd2f9fc787c2ac75790b06e1601f2f65305375.zip
designing SQ-lib API
Diffstat (limited to 'src')
-rw-r--r--src/include/Makefile.am1
-rw-r--r--src/include/gnunet_mq_lib.h4
-rw-r--r--src/include/gnunet_mysql_lib.h14
-rw-r--r--src/include/gnunet_pq_lib.h6
-rw-r--r--src/include/gnunet_sq_lib.h431
5 files changed, 445 insertions, 11 deletions
diff --git a/src/include/Makefile.am b/src/include/Makefile.am
index bf3ffe482..4fb2577fd 100644
--- a/src/include/Makefile.am
+++ b/src/include/Makefile.am
@@ -114,6 +114,7 @@ gnunetinclude_HEADERS = \
114 gnunet_signatures.h \ 114 gnunet_signatures.h \
115 gnunet_social_service.h \ 115 gnunet_social_service.h \
116 gnunet_speaker_lib.h \ 116 gnunet_speaker_lib.h \
117 gnunet_sq_lib.h \
117 gnunet_statistics_service.h \ 118 gnunet_statistics_service.h \
118 gnunet_strings_lib.h \ 119 gnunet_strings_lib.h \
119 gnunet_testbed_service.h \ 120 gnunet_testbed_service.h \
diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h
index cb4affe1e..b05128ccc 100644
--- a/src/include/gnunet_mq_lib.h
+++ b/src/include/gnunet_mq_lib.h
@@ -32,8 +32,8 @@
32 * 32 *
33 * @{ 33 * @{
34 */ 34 */
35#ifndef GNUNET_MQ_H 35#ifndef GNUNET_MQ_LIB_H
36#define GNUNET_MQ_H 36#define GNUNET_MQ_LIB_H
37 37
38 38
39/** 39/**
diff --git a/src/include/gnunet_mysql_lib.h b/src/include/gnunet_mysql_lib.h
index fc6f42f86..6a1e5b3ac 100644
--- a/src/include/gnunet_mysql_lib.h
+++ b/src/include/gnunet_mysql_lib.h
@@ -61,10 +61,12 @@ struct GNUNET_MYSQL_StatementHandle;
61 * @param cls user-defined argument 61 * @param cls user-defined argument
62 * @param num_values number of elements in values 62 * @param num_values number of elements in values
63 * @param values values returned by MySQL 63 * @param values values returned by MySQL
64 * @return GNUNET_OK to continue iterating, GNUNET_SYSERR to abort 64 * @return #GNUNET_OK to continue iterating, #GNUNET_SYSERR to abort
65 */ 65 */
66typedef int (*GNUNET_MYSQL_DataProcessor) (void *cls, unsigned int num_values, 66typedef int
67 MYSQL_BIND * values); 67(*GNUNET_MYSQL_DataProcessor) (void *cls,
68 unsigned int num_values,
69 MYSQL_BIND * values);
68 70
69 71
70/** 72/**
@@ -102,7 +104,7 @@ GNUNET_MYSQL_statements_invalidate (struct GNUNET_MYSQL_Context *mc);
102/** 104/**
103 * Get internal handle for a prepared statement. This function should rarely 105 * Get internal handle for a prepared statement. This function should rarely
104 * be used, and if, with caution! On failures during the interaction with 106 * be used, and if, with caution! On failures during the interaction with
105 * the handle, you must call 'GNUNET_MYSQL_statements_invalidate'! 107 * the handle, you must call #GNUNET_MYSQL_statements_invalidate()!
106 * 108 *
107 * @param sh prepared statement to introspect 109 * @param sh prepared statement to introspect
108 * @return MySQL statement handle, NULL on error 110 * @return MySQL statement handle, NULL on error
@@ -129,8 +131,8 @@ GNUNET_MYSQL_statement_prepare (struct GNUNET_MYSQL_Context *mc,
129 * 131 *
130 * @param mc mysql context 132 * @param mc mysql context
131 * @param sql SQL statement to run 133 * @param sql SQL statement to run
132 * @return GNUNET_OK on success 134 * @return #GNUNET_OK on success
133 * GNUNET_SYSERR if there was a problem 135 * #GNUNET_SYSERR if there was a problem
134 */ 136 */
135int 137int
136GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc, 138GNUNET_MYSQL_statement_run (struct GNUNET_MYSQL_Context *mc,
diff --git a/src/include/gnunet_pq_lib.h b/src/include/gnunet_pq_lib.h
index 33a3e5425..e8180f286 100644
--- a/src/include/gnunet_pq_lib.h
+++ b/src/include/gnunet_pq_lib.h
@@ -15,11 +15,11 @@
15*/ 15*/
16/** 16/**
17 * @file include/gnunet_pq_lib.h 17 * @file include/gnunet_pq_lib.h
18 * @brief helper functions for DB interactions 18 * @brief helper functions for Postgres DB interactions
19 * @author Christian Grothoff 19 * @author Christian Grothoff
20 */ 20 */
21#ifndef GNUNET_PQ_LIB_H_ 21#ifndef GNUNET_PQ_LIB_H
22#define GNUNET_PQ_LIB_H_ 22#define GNUNET_PQ_LIB_H
23 23
24#include <libpq-fe.h> 24#include <libpq-fe.h>
25#include "gnunet_util_lib.h" 25#include "gnunet_util_lib.h"
diff --git a/src/include/gnunet_sq_lib.h b/src/include/gnunet_sq_lib.h
new file mode 100644
index 000000000..a068650ac
--- /dev/null
+++ b/src/include/gnunet_sq_lib.h
@@ -0,0 +1,431 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2017 GNUnet e.V.
4
5 GNUnet is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3, or (at your option) any later version.
8
9 GNUnet is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along with
14 GNUnet; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
15*/
16/**
17 * @file include/gnunet_sq_lib.h
18 * @brief helper functions for Sqlite3 DB interactions
19 * @author Christian Grothoff
20 */
21#ifndef GNUNET_SQ_LIB_H
22#define GNUNET_SQ_LIB_H
23
24#include <sqlite/sqlite3.h>
25#include "gnunet_util_lib.h"
26
27
28/**
29 * Function called to convert input argument into SQL parameters.
30 *
31 * @param cls closure
32 * @param data pointer to input argument
33 * @param data_len number of bytes in @a data (if applicable)
34 * @param stmt sqlite statement to bind parameters for
35 * @param off offset of the argument to bind in @a stmt, numbered from 1,
36 * so immediately suitable for passing to `sqlite3_bind`-functions.
37 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
38 */
39typedef int
40(*GNUNET_SQ_QueryConverter)(void *cls,
41 const void *data,
42 size_t data_len,
43 sqlite3_stmt *stmt,
44 unsigned int off);
45
46
47/**
48 * @brief Description of a DB query parameter.
49 */
50struct GNUNET_SQ_QueryParam
51{
52
53 /**
54 * Function for how to handle this type of entry.
55 */
56 GNUNET_SQ_QueryConverter conv;
57
58 /**
59 * Closure for @e conv.
60 */
61 void *conv_cls;
62
63 /**
64 * Data or NULL.
65 */
66 const void *data;
67
68 /**
69 * Size of @e data
70 */
71 size_t size;
72
73 /**
74 * Number of parameters eaten by this operation.
75 */
76 unsigned int num_params;
77};
78
79
80/**
81 * End of query parameter specification.
82 */
83#define GNUNET_SQ_query_param_end { NULL, NULL, NULL, 0, 0 }
84
85
86/**
87 * Generate query parameter for a buffer @a ptr of
88 * @a ptr_size bytes.
89 *
90 * @param ptr pointer to the query parameter to pass
91 * @oaran ptr_size number of bytes in @a ptr
92 */
93struct GNUNET_SQ_QueryParam
94GNUNET_SQ_query_param_fixed_size (const void *ptr,
95 size_t ptr_size);
96
97
98
99/**
100 * Generate query parameter for a string.
101 *
102 * @param ptr pointer to the string query parameter to pass
103 */
104struct GNUNET_SQ_QueryParam
105GNUNET_SQ_query_param_string (const char *ptr);
106
107
108/**
109 * Generate fixed-size query parameter with size determined
110 * by variable type.
111 *
112 * @param x pointer to the query parameter to pass.
113 */
114#define GNUNET_SQ_query_param_auto_from_type(x) GNUNET_SQ_query_param_fixed_size ((x), sizeof (*(x)))
115
116
117/**
118 * Generate query parameter for an RSA public key. The
119 * database must contain a BLOB type in the respective position.
120 *
121 * @param x the query parameter to pass.
122 */
123struct GNUNET_SQ_QueryParam
124GNUNET_SQ_query_param_rsa_public_key (const struct GNUNET_CRYPTO_RsaPublicKey *x);
125
126
127/**
128 * Generate query parameter for an RSA signature. The
129 * database must contain a BLOB type in the respective position.
130 *
131 * @param x the query parameter to pass
132 */
133struct GNUNET_SQ_QueryParam
134GNUNET_SQ_query_param_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *x);
135
136
137/**
138 * Generate query parameter for an absolute time value.
139 * The database must store a 64-bit integer.
140 *
141 * @param x pointer to the query parameter to pass
142 */
143struct GNUNET_SQ_QueryParam
144GNUNET_SQ_query_param_absolute_time (const struct GNUNET_TIME_Absolute *x);
145
146
147/**
148 * Generate query parameter for an absolute time value.
149 * The database must store a 64-bit integer.
150 *
151 * @param x pointer to the query parameter to pass
152 */
153struct GNUNET_SQ_QueryParam
154GNUNET_SQ_query_param_absolute_time_nbo (const struct GNUNET_TIME_AbsoluteNBO *x);
155
156
157/**
158 * Generate query parameter for an uint16_t in host byte order.
159 *
160 * @param x pointer to the query parameter to pass
161 */
162struct GNUNET_SQ_QueryParam
163GNUNET_SQ_query_param_uint16 (const uint16_t *x);
164
165
166/**
167 * Generate query parameter for an uint32_t in host byte order.
168 *
169 * @param x pointer to the query parameter to pass
170 */
171struct GNUNET_SQ_QueryParam
172GNUNET_SQ_query_param_uint32 (const uint32_t *x);
173
174
175/**
176 * Generate query parameter for an uint16_t in host byte order.
177 *
178 * @param x pointer to the query parameter to pass
179 */
180struct GNUNET_SQ_QueryParam
181GNUNET_SQ_query_param_uint64 (const uint64_t *x);
182
183
184/**
185 * Extract data from a Postgres database @a result at row @a row.
186 *
187 * @param cls closure
188 * @param result where to extract data from
189 * @param int row to extract data from
190 * @param[in,out] dst_size where to store size of result, may be NULL
191 * @param[out] dst where to store the result
192 * @return
193 * #GNUNET_YES if all results could be extracted
194 * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL)
195 */
196typedef int
197(*GNUNET_SQ_ResultConverter)(void *cls,
198 sqlite3_stmt *result,
199 int row,
200 size_t *dst_size,
201 void *dst);
202
203
204/**
205 * Function called to clean up memory allocated
206 * by a #GNUNET_SQ_ResultConverter.
207 *
208 * @param cls closure
209 * @param rd result data to clean up
210 */
211typedef void
212(*GNUNET_SQ_ResultCleanup)(void *cls,
213 void *rd);
214
215
216/**
217 * @brief Description of a DB result cell.
218 */
219struct GNUNET_SQ_ResultSpec
220{
221
222 /**
223 * What is the format of the result?
224 */
225 GNUNET_SQ_ResultConverter conv;
226
227 /**
228 * Function to clean up result data, NULL if cleanup is
229 * not necessary.
230 */
231 GNUNET_SQ_ResultCleanup cleaner;
232
233 /**
234 * Closure for @e conv and @e cleaner.
235 */
236 void *cls;
237
238 /**
239 * Destination for the data.
240 */
241 void *dst;
242
243 /**
244 * Allowed size for the data, 0 for variable-size
245 * (in this case, the type of @e dst is a `void **`
246 * and we need to allocate a buffer of the right size).
247 */
248 size_t dst_size;
249
250 /**
251 * Where to store actual size of the result.
252 */
253 size_t *result_size;
254
255};
256
257
258/**
259 * End of result parameter specification.
260 *
261 * @return array last entry for the result specification to use
262 */
263#define GNUNET_SQ_result_spec_end { NULL, NULL, NULL, NULL, 0, NULL }
264
265
266/**
267 * Variable-size result expected.
268 *
269 * @param[out] dst where to store the result, allocated
270 * @param[out] sptr where to store the size of @a dst
271 * @return array entry for the result specification to use
272 */
273struct GNUNET_SQ_ResultSpec
274GNUNET_SQ_result_spec_variable_size (void **dst,
275 size_t *sptr);
276
277
278/**
279 * Fixed-size result expected.
280 *
281 * @param[out] dst where to store the result
282 * @param dst_size number of bytes in @a dst
283 * @return array entry for the result specification to use
284 */
285struct GNUNET_SQ_ResultSpec
286GNUNET_SQ_result_spec_fixed_size (void *dst,
287 size_t dst_size);
288
289
290/**
291 * We expect a fixed-size result, with size determined by the type of `* dst`
292 *
293 * @param dst point to where to store the result, type fits expected result size
294 * @return array entry for the result specification to use
295 */
296#define GNUNET_SQ_result_spec_auto_from_type(dst) GNUNET_SQ_result_spec_fixed_size (name, (dst), sizeof (*(dst)))
297
298
299/**
300 * Variable-size result expected.
301 *
302 * @param[out] dst where to store the result, allocated
303 * @param[out] sptr where to store the size of @a dst
304 * @return array entry for the result specification to use
305 */
306struct GNUNET_SQ_ResultSpec
307GNUNET_SQ_result_spec_variable_size (void **dst,
308 size_t *sptr);
309
310
311/**
312 * 0-terminated string expected.
313 *
314 * @param[out] dst where to store the result, allocated
315 * @return array entry for the result specification to use
316 */
317struct GNUNET_SQ_ResultSpec
318GNUNET_SQ_result_spec_string (char **dst);
319
320
321/**
322 * RSA public key expected.
323 *
324 * @param[out] rsa where to store the result
325 * @return array entry for the result specification to use
326 */
327struct GNUNET_SQ_ResultSpec
328GNUNET_SQ_result_spec_rsa_public_key (struct GNUNET_CRYPTO_RsaPublicKey **rsa);
329
330
331/**
332 * RSA signature expected.
333 *
334 * @param[out] sig where to store the result;
335 * @return array entry for the result specification to use
336 */
337struct GNUNET_SQ_ResultSpec
338GNUNET_SQ_result_spec_rsa_signature (struct GNUNET_CRYPTO_RsaSignature **sig);
339
340
341/**
342 * Absolute time expected.
343 *
344 * @param[out] at where to store the result
345 * @return array entry for the result specification to use
346 */
347struct GNUNET_SQ_ResultSpec
348GNUNET_SQ_result_spec_absolute_time (struct GNUNET_TIME_Absolute *at);
349
350
351/**
352 * Absolute time expected.
353 *
354 * @param[out] at where to store the result
355 * @return array entry for the result specification to use
356 */
357struct GNUNET_SQ_ResultSpec
358GNUNET_SQ_result_spec_absolute_time_nbo (struct GNUNET_TIME_AbsoluteNBO *at);
359
360
361/**
362 * uint16_t expected.
363 *
364 * @param[out] u16 where to store the result
365 * @return array entry for the result specification to use
366 */
367struct GNUNET_SQ_ResultSpec
368GNUNET_SQ_result_spec_uint16 (uint16_t *u16);
369
370
371/**
372 * uint32_t expected.
373 *
374 * @param[out] u32 where to store the result
375 * @return array entry for the result specification to use
376 */
377struct GNUNET_SQ_ResultSpec
378GNUNET_SQ_result_spec_uint32 (uint32_t *u32);
379
380
381/**
382 * uint64_t expected.
383 *
384 * @param[out] u64 where to store the result
385 * @return array entry for the result specification to use
386 */
387struct GNUNET_SQ_ResultSpec
388GNUNET_SQ_result_spec_uint64 (uint64_t *u64);
389
390
391/**
392 * Execute a prepared statement.
393 *
394 * @param db_conn database connection
395 * @param params parameters to the statement
396 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
397 */
398int
399GNUNET_SQ_bind (sqlite3_stmt *stmt,
400 const struct GNUNET_SQ_QueryParam *params);
401
402
403/**
404 * Extract results from a query result according to the given specification.
405 *
406 * @param result result to process
407 * @param[in,out] rs result specification to extract for
408 * @param row row from the result to extract
409 * @return
410 * #GNUNET_YES if all results could be extracted
411 * #GNUNET_SYSERR if a result was invalid (non-existing field)
412 */
413int
414GNUNET_SQ_extract_result (sqlite3_stmt *result,
415 struct GNUNET_SQ_ResultSpec *rs,
416 int row);
417
418
419/**
420 * Free all memory that was allocated in @a rs during
421 * #GNUNET_SQ_extract_result().
422 *
423 * @param rs reult specification to clean up
424 */
425void
426GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs);
427
428
429#endif /* GNUNET_SQ_LIB_H_ */
430
431/* end of include/gnunet_sq_lib.h */