diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:53:00 +0200 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2023-10-18 13:53:00 +0200 |
commit | 00407f6a36880fda570604cf4e64858bb08a025b (patch) | |
tree | 99f4fbdac3dca9e3f44ef1181503182d1fb8a11d /src/pq/pq.c | |
parent | 5b2527d8d81f92e00a0835e3039a0a2eee0ee566 (diff) | |
download | gnunet-00407f6a36880fda570604cf4e64858bb08a025b.tar.gz gnunet-00407f6a36880fda570604cf4e64858bb08a025b.zip |
BUILD: Move pq/sq to lib
Diffstat (limited to 'src/pq/pq.c')
-rw-r--r-- | src/pq/pq.c | 207 |
1 files changed, 0 insertions, 207 deletions
diff --git a/src/pq/pq.c b/src/pq/pq.c deleted file mode 100644 index 65e051bfc..000000000 --- a/src/pq/pq.c +++ /dev/null | |||
@@ -1,207 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2014, 2015, 2016, 2017, 2019, 2020 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 pq/pq.c | ||
22 | * @brief helper functions for libpq (PostGres) interactions | ||
23 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
24 | * @author Florian Dold | ||
25 | * @author Christian Grothoff | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_pq_lib.h" | ||
29 | #include "pq.h" | ||
30 | |||
31 | |||
32 | PGresult * | ||
33 | GNUNET_PQ_exec_prepared (struct GNUNET_PQ_Context *db, | ||
34 | const char *name, | ||
35 | const struct GNUNET_PQ_QueryParam *params) | ||
36 | { | ||
37 | unsigned int len; | ||
38 | |||
39 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
40 | "Running prepared statement `%s' on %p\n", | ||
41 | name, | ||
42 | db); | ||
43 | /* count the number of parameters */ | ||
44 | len = 0; | ||
45 | for (unsigned int i = 0; 0 != params[i].num_params; i++) | ||
46 | len += params[i].num_params; | ||
47 | |||
48 | /* new scope to allow stack allocation without alloca */ | ||
49 | { | ||
50 | /* Scratch buffer for temporary storage */ | ||
51 | void *scratch[GNUNET_NZL (len)]; | ||
52 | /* Parameter array we are building for the query */ | ||
53 | void *param_values[GNUNET_NZL (len)]; | ||
54 | int param_lengths[GNUNET_NZL (len)]; | ||
55 | int param_formats[GNUNET_NZL (len)]; | ||
56 | unsigned int off; | ||
57 | /* How many entries in the scratch buffer are in use? */ | ||
58 | unsigned int soff; | ||
59 | PGresult *res; | ||
60 | int ret; | ||
61 | ConnStatusType status; | ||
62 | |||
63 | off = 0; | ||
64 | soff = 0; | ||
65 | for (unsigned int i = 0; 0 != params[i].num_params; i++) | ||
66 | { | ||
67 | const struct GNUNET_PQ_QueryParam *x = ¶ms[i]; | ||
68 | |||
69 | ret = x->conv (x->conv_cls, | ||
70 | x->data, | ||
71 | x->size, | ||
72 | ¶m_values[off], | ||
73 | ¶m_lengths[off], | ||
74 | ¶m_formats[off], | ||
75 | x->num_params, | ||
76 | &scratch[soff], | ||
77 | len - soff); | ||
78 | if (ret < 0) | ||
79 | { | ||
80 | for (off = 0; off < soff; off++) | ||
81 | GNUNET_free (scratch[off]); | ||
82 | return NULL; | ||
83 | } | ||
84 | soff += ret; | ||
85 | off += x->num_params; | ||
86 | } | ||
87 | GNUNET_assert (off == len); | ||
88 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
89 | "pq", | ||
90 | "Executing prepared SQL statement `%s'\n", | ||
91 | name); | ||
92 | res = PQexecPrepared (db->conn, | ||
93 | name, | ||
94 | len, | ||
95 | (const char **) param_values, | ||
96 | param_lengths, | ||
97 | param_formats, | ||
98 | 1); | ||
99 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
100 | "pq", | ||
101 | "Execution of prepared SQL statement `%s' finished (%s)\n", | ||
102 | name, | ||
103 | PQresStatus (PQresultStatus (res))); | ||
104 | if ( (PGRES_COMMAND_OK != PQresultStatus (res)) && | ||
105 | (CONNECTION_OK != (status = PQstatus (db->conn))) ) | ||
106 | { | ||
107 | GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, | ||
108 | "pq", | ||
109 | "Database disconnected on SQL statement `%s' (reconnecting)\n", | ||
110 | name); | ||
111 | GNUNET_PQ_reconnect (db); | ||
112 | res = NULL; | ||
113 | } | ||
114 | |||
115 | for (off = 0; off < soff; off++) | ||
116 | GNUNET_free (scratch[off]); | ||
117 | return res; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | |||
122 | void | ||
123 | GNUNET_PQ_cleanup_query_params_closures ( | ||
124 | const struct GNUNET_PQ_QueryParam *params) | ||
125 | { | ||
126 | for (unsigned int i = 0; 0 != params[i].num_params; i++) | ||
127 | { | ||
128 | const struct GNUNET_PQ_QueryParam *x = ¶ms[i]; | ||
129 | |||
130 | if ((NULL != x->conv_cls) && | ||
131 | (NULL != x->conv_cls_cleanup)) | ||
132 | x->conv_cls_cleanup (x->conv_cls); | ||
133 | } | ||
134 | |||
135 | } | ||
136 | |||
137 | |||
138 | void | ||
139 | GNUNET_PQ_cleanup_result (struct GNUNET_PQ_ResultSpec *rs) | ||
140 | { | ||
141 | for (unsigned int i = 0; NULL != rs[i].conv; i++) | ||
142 | if (NULL != rs[i].cleaner) | ||
143 | rs[i].cleaner (rs[i].cls, | ||
144 | rs[i].dst); | ||
145 | } | ||
146 | |||
147 | |||
148 | enum GNUNET_GenericReturnValue | ||
149 | GNUNET_PQ_extract_result (PGresult *result, | ||
150 | struct GNUNET_PQ_ResultSpec *rs, | ||
151 | int row) | ||
152 | { | ||
153 | unsigned int i; | ||
154 | |||
155 | if (NULL == result) | ||
156 | return GNUNET_SYSERR; | ||
157 | for (i = 0; NULL != rs[i].conv; i++) | ||
158 | { | ||
159 | struct GNUNET_PQ_ResultSpec *spec; | ||
160 | enum GNUNET_GenericReturnValue ret; | ||
161 | |||
162 | spec = &rs[i]; | ||
163 | ret = spec->conv (spec->cls, | ||
164 | result, | ||
165 | row, | ||
166 | spec->fname, | ||
167 | &spec->dst_size, | ||
168 | spec->dst); | ||
169 | switch (ret) | ||
170 | { | ||
171 | case GNUNET_OK: | ||
172 | /* canonical case, continue below */ | ||
173 | if (NULL != spec->is_null) | ||
174 | *spec->is_null = false; | ||
175 | break; | ||
176 | case GNUNET_NO: | ||
177 | if (spec->is_nullable) | ||
178 | { | ||
179 | if (NULL != spec->is_null) | ||
180 | *spec->is_null = true; | ||
181 | continue; | ||
182 | } | ||
183 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
184 | "NULL field encountered for `%s' where non-NULL was required\n", | ||
185 | spec->fname); | ||
186 | goto cleanup; | ||
187 | case GNUNET_SYSERR: | ||
188 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
189 | "Failed to extract field `%s'\n", | ||
190 | spec->fname); | ||
191 | GNUNET_break (0); | ||
192 | goto cleanup; | ||
193 | } | ||
194 | if (NULL != spec->result_size) | ||
195 | *spec->result_size = spec->dst_size; | ||
196 | } | ||
197 | return GNUNET_OK; | ||
198 | cleanup: | ||
199 | for (unsigned int j = 0; j < i; j++) | ||
200 | if (NULL != rs[j].cleaner) | ||
201 | rs[j].cleaner (rs[j].cls, | ||
202 | rs[j].dst); | ||
203 | return GNUNET_SYSERR; | ||
204 | } | ||
205 | |||
206 | |||
207 | /* end of pq/pq.c */ | ||