diff options
Diffstat (limited to 'src/sq/sq.c')
-rw-r--r-- | src/sq/sq.c | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/sq/sq.c b/src/sq/sq.c new file mode 100644 index 000000000..dc4416761 --- /dev/null +++ b/src/sq/sq.c | |||
@@ -0,0 +1,108 @@ | |||
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 sq/sq.c | ||
18 | * @brief helper functions for Sqlite3 DB interactions | ||
19 | * @author Christian Grothoff | ||
20 | */ | ||
21 | #include "platform.h" | ||
22 | #include "gnunet_sq_lib.h" | ||
23 | |||
24 | |||
25 | /** | ||
26 | * Execute a prepared statement. | ||
27 | * | ||
28 | * @param db_conn database connection | ||
29 | * @param params parameters to the statement | ||
30 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on error | ||
31 | */ | ||
32 | int | ||
33 | GNUNET_SQ_bind (sqlite3_stmt *stmt, | ||
34 | const struct GNUNET_SQ_QueryParam *params) | ||
35 | { | ||
36 | unsigned int j; | ||
37 | |||
38 | j = 1; | ||
39 | for (unsigned int i=0;NULL != params[i].conv; i++) | ||
40 | { | ||
41 | if (GNUNET_OK != | ||
42 | params[i].conv (params[i].conv_cls, | ||
43 | params[i].data, | ||
44 | params[i].size, | ||
45 | stmt, | ||
46 | j)) | ||
47 | { | ||
48 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
49 | "sq", | ||
50 | _("Failure to bind %u-th SQL parameter\n"), | ||
51 | i); | ||
52 | return GNUNET_SYSERR; | ||
53 | } | ||
54 | GNUNET_assert (0 != params[i].num_params); | ||
55 | j += params[i].num_params; | ||
56 | } | ||
57 | return GNUNET_OK; | ||
58 | } | ||
59 | |||
60 | |||
61 | /** | ||
62 | * Extract results from a query result according to the given specification. | ||
63 | * | ||
64 | * @param result result to process | ||
65 | * @param[in,out] rs result specification to extract for | ||
66 | * @return | ||
67 | * #GNUNET_OK if all results could be extracted | ||
68 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
69 | */ | ||
70 | int | ||
71 | GNUNET_SQ_extract_result (sqlite3_stmt *result, | ||
72 | struct GNUNET_SQ_ResultSpec *rs) | ||
73 | { | ||
74 | unsigned int j = 0; | ||
75 | |||
76 | for (unsigned int i=0;NULL != rs[i].conv; i++) | ||
77 | { | ||
78 | if (NULL == rs[i].result_size) | ||
79 | rs[i].result_size = &rs[i].dst_size; | ||
80 | if (GNUNET_OK != | ||
81 | rs[i].conv (rs[i].cls, | ||
82 | result, | ||
83 | j, | ||
84 | rs[i].result_size, | ||
85 | rs[i].dst)) | ||
86 | return GNUNET_SYSERR; | ||
87 | GNUNET_assert (0 != rs[i].num_params); | ||
88 | j += rs[i].num_params; | ||
89 | } | ||
90 | return GNUNET_OK; | ||
91 | } | ||
92 | |||
93 | |||
94 | /** | ||
95 | * Free all memory that was allocated in @a rs during | ||
96 | * #GNUNET_SQ_extract_result(). | ||
97 | * | ||
98 | * @param rs reult specification to clean up | ||
99 | */ | ||
100 | void | ||
101 | GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs) | ||
102 | { | ||
103 | for (unsigned int i=0;NULL != rs[i].conv; i++) | ||
104 | if (NULL != rs[i].cleaner) | ||
105 | rs[i].cleaner (rs[i].cls); | ||
106 | } | ||
107 | |||
108 | /* end of sq.c */ | ||