diff options
Diffstat (limited to 'src/sq/sq.c')
-rw-r--r-- | src/sq/sq.c | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/src/sq/sq.c b/src/sq/sq.c new file mode 100644 index 000000000..089ebf0ff --- /dev/null +++ b/src/sq/sq.c | |||
@@ -0,0 +1,140 @@ | |||
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 | if (SQLITE_OK != | ||
53 | sqlite3_reset (stmt)) | ||
54 | { | ||
55 | GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, | ||
56 | "sq", | ||
57 | _("Failure in sqlite3_reset (!)\n")); | ||
58 | return GNUNET_SYSERR; | ||
59 | } | ||
60 | } | ||
61 | GNUNET_assert (0 != params[i].num_params); | ||
62 | j += params[i].num_params; | ||
63 | } | ||
64 | return GNUNET_OK; | ||
65 | } | ||
66 | |||
67 | |||
68 | /** | ||
69 | * Extract results from a query result according to the given specification. | ||
70 | * | ||
71 | * @param result result to process | ||
72 | * @param[in,out] rs result specification to extract for | ||
73 | * @return | ||
74 | * #GNUNET_OK if all results could be extracted | ||
75 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
76 | */ | ||
77 | int | ||
78 | GNUNET_SQ_extract_result (sqlite3_stmt *result, | ||
79 | struct GNUNET_SQ_ResultSpec *rs) | ||
80 | { | ||
81 | unsigned int j = 0; | ||
82 | |||
83 | for (unsigned int i=0;NULL != rs[i].conv; i++) | ||
84 | { | ||
85 | if (NULL == rs[i].result_size) | ||
86 | rs[i].result_size = &rs[i].dst_size; | ||
87 | if (GNUNET_OK != | ||
88 | rs[i].conv (rs[i].cls, | ||
89 | result, | ||
90 | j, | ||
91 | rs[i].result_size, | ||
92 | rs[i].dst)) | ||
93 | { | ||
94 | for (unsigned int k=0;k<i;k++) | ||
95 | if (NULL != rs[k].cleaner) | ||
96 | rs[k].cleaner (rs[k].cls); | ||
97 | return GNUNET_SYSERR; | ||
98 | } | ||
99 | GNUNET_assert (0 != rs[i].num_params); | ||
100 | j += rs[i].num_params; | ||
101 | } | ||
102 | return GNUNET_OK; | ||
103 | } | ||
104 | |||
105 | |||
106 | /** | ||
107 | * Free all memory that was allocated in @a rs during | ||
108 | * #GNUNET_SQ_extract_result(). | ||
109 | * | ||
110 | * @param rs reult specification to clean up | ||
111 | */ | ||
112 | void | ||
113 | GNUNET_SQ_cleanup_result (struct GNUNET_SQ_ResultSpec *rs) | ||
114 | { | ||
115 | for (unsigned int i=0;NULL != rs[i].conv; i++) | ||
116 | if (NULL != rs[i].cleaner) | ||
117 | rs[i].cleaner (rs[i].cls); | ||
118 | } | ||
119 | |||
120 | |||
121 | /** | ||
122 | * Reset @a stmt and log error. | ||
123 | * | ||
124 | * @param dbh database handle | ||
125 | * @param stmt statement to reset | ||
126 | */ | ||
127 | void | ||
128 | GNUNET_SQ_reset (sqlite3 *dbh, | ||
129 | sqlite3_stmt *stmt) | ||
130 | { | ||
131 | if (SQLITE_OK != | ||
132 | sqlite3_reset (stmt)) | ||
133 | GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, | ||
134 | "sqlite", | ||
135 | _("Failed to reset sqlite statement with error: %s\n"), | ||
136 | sqlite3_errmsg (dbh)); | ||
137 | } | ||
138 | |||
139 | |||
140 | /* end of sq.c */ | ||