diff options
Diffstat (limited to 'src/lib/sq/sq.c')
-rw-r--r-- | src/lib/sq/sq.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/lib/sq/sq.c b/src/lib/sq/sq.c new file mode 100644 index 000000000..96d1d333d --- /dev/null +++ b/src/lib/sq/sq.c | |||
@@ -0,0 +1,131 @@ | |||
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 | ||
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 sq/sq.c | ||
22 | * @brief helper functions for Sqlite3 DB interactions | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_sq_lib.h" | ||
27 | |||
28 | |||
29 | enum GNUNET_GenericReturnValue | ||
30 | GNUNET_SQ_bind (sqlite3_stmt *stmt, | ||
31 | const struct GNUNET_SQ_QueryParam *params) | ||
32 | { | ||
33 | unsigned int j; | ||
34 | |||
35 | j = 1; | ||
36 | for (unsigned int i = 0; NULL != params[i].conv; i++) | ||
37 | { | ||
38 | if (GNUNET_OK != | ||
39 | params[i].conv (params[i].conv_cls, | ||
40 | params[i].data, | ||
41 | params[i].size, | ||
42 | stmt, | ||
43 | j)) | ||
44 | { | ||
45 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
46 | "sq", | ||
47 | _ ("Failure to bind %u-th SQL parameter\n"), | ||
48 | i); | ||
49 | if (SQLITE_OK != | ||
50 | sqlite3_reset (stmt)) | ||
51 | { | ||
52 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
53 | "sq", | ||
54 | _ ("Failure in sqlite3_reset (!)\n")); | ||
55 | return GNUNET_SYSERR; | ||
56 | } | ||
57 | } | ||
58 | GNUNET_assert (0 != params[i].num_params); | ||
59 | j += params[i].num_params; | ||
60 | } | ||
61 | return GNUNET_OK; | ||
62 | } | ||
63 | |||
64 | |||
65 | /** | ||
66 | * Extract results from a query result according to the given specification. | ||
67 | * | ||
68 | * @param result result to process | ||
69 | * @param[in,out] rs result specification to extract for | ||
70 | * @return | ||
71 | * #GNUNET_OK if all results could be extracted | ||
72 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
73 | */ | ||
74 | enum GNUNET_GenericReturnValue | ||
75 | GNUNET_SQ_extract_result (sqlite3_stmt *result, | ||
76 | struct GNUNET_SQ_ResultSpec *rs) | ||
77 | { | ||
78 | unsigned int j = 0; | ||
79 | |||
80 | for (unsigned int i = 0; NULL != rs[i].conv; i++) | ||
81 | { | ||
82 | if (NULL == rs[i].result_size) | ||
83 | rs[i].result_size = &rs[i].dst_size; | ||
84 | if (GNUNET_OK != | ||
85 | rs[i].conv (rs[i].cls, | ||
86 | result, | ||
87 | j, | ||
88 | rs[i].result_size, | ||
89 | rs[i].dst)) | ||
90 | { | ||
91 | for (unsigned int k = 0; k < i; k++) | ||
92 | if (NULL != rs[k].cleaner) | ||
93 | rs[k].cleaner (rs[k].cls); | ||
94 | return GNUNET_SYSERR; | ||
95 | } | ||
96 | GNUNET_assert (0 != rs[i].num_params); | ||
97 | j += rs[i].num_params; | ||
98 | } | ||
99 | return GNUNET_OK; | ||
100 | } | ||
101 | |||
102 | |||
103 | void | ||
104 | GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs) | ||
105 | { | ||
106 | for (unsigned int i = 0; NULL != rs[i].conv; i++) | ||
107 | if (NULL != rs[i].cleaner) | ||
108 | rs[i].cleaner (rs[i].cls); | ||
109 | } | ||
110 | |||
111 | |||
112 | /** | ||
113 | * Reset @a stmt and log error. | ||
114 | * | ||
115 | * @param dbh database handle | ||
116 | * @param stmt statement to reset | ||
117 | */ | ||
118 | void | ||
119 | GNUNET_SQ_reset (sqlite3 *dbh, | ||
120 | sqlite3_stmt *stmt) | ||
121 | { | ||
122 | if (SQLITE_OK != | ||
123 | sqlite3_reset (stmt)) | ||
124 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
125 | "sqlite", | ||
126 | _ ("Failed to reset sqlite statement with error: %s\n"), | ||
127 | sqlite3_errmsg (dbh)); | ||
128 | } | ||
129 | |||
130 | |||
131 | /* end of sq.c */ | ||