Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 Taler Systems SA
4 :
5 : TALER 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 : TALER 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 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file get_auditor_progress.c
18 : * @brief Implementation of get_auditor_progress function
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_error_codes.h"
22 : #include "taler/taler_pq_lib.h"
23 : #include "auditor-database/get_auditor_progress.h"
24 : #include "pg_helper.h"
25 :
26 :
27 : /**
28 : * Closure for #auditor_progress_cb().
29 : */
30 : struct AuditorProgressContext
31 : {
32 :
33 : /**
34 : * Where to store results.
35 : */
36 : uint64_t **dst;
37 :
38 : /**
39 : * Offset in @e dst.
40 : */
41 : unsigned int off;
42 :
43 : /**
44 : * Length of array at @e dst.
45 : */
46 : unsigned int len;
47 :
48 : /**
49 : * Plugin context.
50 : */
51 : struct TALER_AUDITORDB_PostgresContext *pg;
52 :
53 : /**
54 : * Set to true on failure.
55 : */
56 : bool failure;
57 : };
58 :
59 :
60 : /**
61 : * Helper function for #TALER_AUDITORDB_get_auditor_progress().
62 : * To be called with the results of a SELECT statement
63 : * that has returned @a num_results results.
64 : *
65 : * @param cls closure of type `struct AuditorProgressContext *`
66 : * @param result the postgres result
67 : * @param num_results the number of results in @a result
68 : */
69 : static void
70 33 : auditor_progress_cb (void *cls,
71 : PGresult *result,
72 : unsigned int num_results)
73 : {
74 33 : struct AuditorProgressContext *ctx = cls;
75 :
76 33 : if (num_results > ctx->len)
77 : {
78 0 : GNUNET_break (0);
79 0 : ctx->failure = true;
80 0 : return;
81 : }
82 194 : for (unsigned int i = 0; i < num_results; i++)
83 : {
84 161 : bool is_missing = false;
85 161 : struct GNUNET_PQ_ResultSpec rs[] = {
86 161 : GNUNET_PQ_result_spec_allow_null (
87 : GNUNET_PQ_result_spec_uint64 ("progress_offset",
88 161 : ctx->dst[i]),
89 : &is_missing),
90 : GNUNET_PQ_result_spec_end
91 : };
92 :
93 161 : if (GNUNET_OK !=
94 161 : GNUNET_PQ_extract_result (result,
95 : rs,
96 : i))
97 : {
98 0 : GNUNET_break (0);
99 0 : ctx->failure = true;
100 0 : return;
101 : }
102 161 : if (is_missing)
103 155 : *ctx->dst[i] = 0;
104 161 : ctx->off++;
105 : }
106 : }
107 :
108 :
109 : enum GNUNET_DB_QueryStatus
110 33 : TALER_AUDITORDB_get_auditor_progress (
111 : struct TALER_AUDITORDB_PostgresContext *pg,
112 : const char *progress_key,
113 : uint64_t *progress_offset,
114 : ...)
115 : {
116 33 : unsigned int cnt = 1;
117 : va_list ap;
118 :
119 33 : va_start (ap,
120 : progress_offset);
121 161 : while (NULL != va_arg (ap,
122 : const char *))
123 : {
124 128 : cnt++;
125 128 : (void) va_arg (ap,
126 : uint64_t *);
127 : }
128 33 : va_end (ap);
129 33 : {
130 33 : const char *keys[cnt];
131 33 : uint64_t *dsts[cnt];
132 33 : unsigned int off = 1;
133 33 : struct GNUNET_PQ_QueryParam params[] = {
134 33 : GNUNET_PQ_query_param_array_ptrs_string (cnt,
135 : keys,
136 : pg->conn),
137 : GNUNET_PQ_query_param_end
138 : };
139 33 : struct AuditorProgressContext ctx = {
140 : .dst = dsts,
141 : .len = cnt,
142 : .pg = pg
143 : };
144 : enum GNUNET_DB_QueryStatus qs;
145 :
146 33 : keys[0] = progress_key;
147 33 : dsts[0] = progress_offset;
148 33 : va_start (ap,
149 : progress_offset);
150 161 : while (off < cnt)
151 : {
152 128 : keys[off] = va_arg (ap,
153 : const char *);
154 128 : dsts[off] = va_arg (ap,
155 : uint64_t *);
156 128 : off++;
157 : }
158 33 : GNUNET_assert (NULL == va_arg (ap,
159 : const char *));
160 33 : va_end (ap);
161 :
162 33 : PREPARE (pg,
163 : "get_auditor_progress",
164 : "SELECT"
165 : " auditor_do_get_auditor_progress AS progress_offset"
166 : " FROM auditor_do_get_auditor_progress "
167 : "($1);");
168 33 : ctx.off = 0;
169 33 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
170 : "get_auditor_progress",
171 : params,
172 : &auditor_progress_cb,
173 : &ctx);
174 33 : GNUNET_PQ_cleanup_query_params_closures (params);
175 33 : if (ctx.failure)
176 0 : return GNUNET_DB_STATUS_HARD_ERROR;
177 33 : if (qs < 0)
178 0 : return qs;
179 33 : GNUNET_assert (ctx.off == cnt);
180 33 : return qs;
181 : }
182 : }
|