Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2026 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 src/auditordb/iterate_auditor_aml_holds.c
18 : * @brief Implementation of the iterate_auditor_aml_holds function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "auditor-database/iterate_auditor_aml_holds.h"
23 : #include "pg_helper.h"
24 :
25 :
26 : /**
27 : * Hard upper bound on the number of records returned by a single
28 : * call, regardless of the limit requested by the client.
29 : */
30 : #define MAX_RECORDS 50000
31 :
32 :
33 : /**
34 : * Closure for #auditor_aml_holds_cb().
35 : */
36 : struct AuditorAmlHoldsContext
37 : {
38 : /**
39 : * Function to call for each hold.
40 : */
41 : TALER_AUDITORDB_AuditorAmlHoldCallback cb;
42 :
43 : /**
44 : * Closure for @e cb.
45 : */
46 : void *cb_cls;
47 :
48 : /**
49 : * Plugin context.
50 : */
51 : struct TALER_AUDITORDB_PostgresContext *pg;
52 :
53 : /**
54 : * Query status to return.
55 : */
56 : enum GNUNET_DB_QueryStatus qs;
57 : };
58 :
59 :
60 : /**
61 : * Helper function for #TALER_AUDITORDB_iterate_auditor_aml_holds().
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 AuditorAmlHoldsContext *`
66 : * @param result the postgres result
67 : * @param num_results the number of results in @a result
68 : */
69 : static void
70 0 : auditor_aml_holds_cb (void *cls,
71 : PGresult *result,
72 : unsigned int num_results)
73 : {
74 0 : struct AuditorAmlHoldsContext *ahc = cls;
75 0 : struct TALER_AUDITORDB_PostgresContext *pg = ahc->pg;
76 :
77 0 : for (unsigned int i = 0; i < num_results; i++)
78 : {
79 : struct TALER_AUDITORDB_AmlHold ah;
80 0 : struct GNUNET_PQ_ResultSpec rs[] = {
81 0 : GNUNET_PQ_result_spec_uint64 ("row_id",
82 : &ah.row_id),
83 0 : GNUNET_PQ_result_spec_auto_from_type ("wtid",
84 : &ah.wtid),
85 0 : GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto",
86 : &ah.wire_target_h_payto),
87 0 : GNUNET_PQ_result_spec_string ("account",
88 : &ah.account.full_payto),
89 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
90 : &ah.amount),
91 0 : GNUNET_PQ_result_spec_uint32 ("deferral_reason",
92 : &ah.deferral_reason),
93 0 : GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id",
94 : &ah.legitimization_measure_serial_id),
95 0 : GNUNET_PQ_result_spec_absolute_time ("creation_date",
96 : &ah.creation_date),
97 0 : GNUNET_PQ_result_spec_bool ("suppressed",
98 : &ah.suppressed),
99 : GNUNET_PQ_result_spec_end
100 : };
101 : enum GNUNET_GenericReturnValue rval;
102 :
103 0 : if (GNUNET_OK !=
104 0 : GNUNET_PQ_extract_result (result,
105 : rs,
106 : i))
107 : {
108 0 : GNUNET_break (0);
109 0 : ahc->qs = GNUNET_DB_STATUS_HARD_ERROR;
110 0 : return;
111 : }
112 0 : ahc->qs = i + 1;
113 0 : rval = ahc->cb (ahc->cb_cls,
114 : &ah);
115 0 : GNUNET_PQ_cleanup_result (rs);
116 0 : if (GNUNET_OK != rval)
117 0 : break;
118 : }
119 : }
120 :
121 :
122 : enum GNUNET_DB_QueryStatus
123 0 : TALER_AUDITORDB_iterate_auditor_aml_holds (
124 : struct TALER_AUDITORDB_PostgresContext *pg,
125 : int64_t limit,
126 : uint64_t offset,
127 : bool return_suppressed,
128 : TALER_AUDITORDB_AuditorAmlHoldCallback cb,
129 : TALER_AUDITORDB_AUDITOR_AML_HOLD_RESULT_CLOSURE *cb_cls)
130 : {
131 0 : uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
132 : TALER_AUDITORDB_abs_limit (limit));
133 0 : struct GNUNET_PQ_QueryParam params[] = {
134 0 : GNUNET_PQ_query_param_uint64 (&offset),
135 0 : GNUNET_PQ_query_param_bool (return_suppressed),
136 0 : GNUNET_PQ_query_param_uint64 (&plimit),
137 : GNUNET_PQ_query_param_end
138 : };
139 0 : struct AuditorAmlHoldsContext ahc = {
140 : .cb = cb,
141 : .cb_cls = cb_cls,
142 : .pg = pg
143 : };
144 : enum GNUNET_DB_QueryStatus qs;
145 :
146 0 : PREPARE (pg,
147 : "iterate_auditor_aml_holds_desc",
148 : "SELECT"
149 : " row_id"
150 : ",wtid"
151 : ",wire_target_h_payto"
152 : ",account"
153 : ",amount"
154 : ",deferral_reason"
155 : ",legitimization_measure_serial_id"
156 : ",creation_date"
157 : ",suppressed"
158 : " FROM auditor_aml_holds"
159 : " WHERE (row_id < $1)"
160 : " AND ($2 OR NOT suppressed)"
161 : " ORDER BY row_id DESC"
162 : " LIMIT $3");
163 0 : PREPARE (pg,
164 : "iterate_auditor_aml_holds_asc",
165 : "SELECT"
166 : " row_id"
167 : ",wtid"
168 : ",wire_target_h_payto"
169 : ",account"
170 : ",amount"
171 : ",deferral_reason"
172 : ",legitimization_measure_serial_id"
173 : ",creation_date"
174 : ",suppressed"
175 : " FROM auditor_aml_holds"
176 : " WHERE (row_id > $1)"
177 : " AND ($2 OR NOT suppressed)"
178 : " ORDER BY row_id ASC"
179 : " LIMIT $3");
180 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
181 : pg->conn,
182 : (limit > 0)
183 : ? "iterate_auditor_aml_holds_asc"
184 : : "iterate_auditor_aml_holds_desc",
185 : params,
186 : &auditor_aml_holds_cb,
187 : &ahc);
188 0 : if (qs > 0)
189 0 : return ahc.qs;
190 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
191 0 : return qs;
192 : }
|