Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024, 2025 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 exchangedb/pg_select_aml_measures.c
18 : * @brief Implementation of the select_aml_measures function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler_error_codes.h"
23 : #include "taler_dbevents.h"
24 : #include "taler_pq_lib.h"
25 : #include "pg_select_aml_measures.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #handle_aml_result.
31 : */
32 : struct LegiMeasureResultContext
33 : {
34 : /**
35 : * Function to call on each result.
36 : */
37 : TALER_EXCHANGEDB_LegitimizationMeasureCallback cb;
38 :
39 : /**
40 : * Closure for @e cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Plugin context.
46 : */
47 : struct PostgresClosure *pg;
48 :
49 : /**
50 : * Set to #GNUNET_SYSERR on serious errors.
51 : */
52 : enum GNUNET_GenericReturnValue status;
53 : };
54 :
55 :
56 : /**
57 : * Function to be called with the results of a SELECT statement
58 : * that has returned @a num_results results. Helper function
59 : * for #TEH_PG_select_aml_measures().
60 : *
61 : * @param cls closure of type `struct LegiMeasureResultContext *`
62 : * @param result the postgres result
63 : * @param num_results the number of results in @a result
64 : */
65 : static void
66 0 : handle_aml_result (void *cls,
67 : PGresult *result,
68 : unsigned int num_results)
69 : {
70 0 : struct LegiMeasureResultContext *ctx = cls;
71 :
72 0 : for (unsigned int i = 0; i<num_results; i++)
73 : {
74 : struct TALER_NormalizedPaytoHashP h_payto;
75 : uint64_t rowid;
76 : struct GNUNET_TIME_Timestamp start_time;
77 0 : json_t *jmeasures = NULL;
78 : bool is_finished;
79 0 : struct GNUNET_PQ_ResultSpec rs[] = {
80 0 : GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id",
81 : &rowid),
82 0 : GNUNET_PQ_result_spec_auto_from_type ("h_normalized_payto",
83 : &h_payto),
84 0 : GNUNET_PQ_result_spec_timestamp ("start_time",
85 : &start_time),
86 0 : TALER_PQ_result_spec_json ("jmeasures",
87 : &jmeasures),
88 0 : GNUNET_PQ_result_spec_bool ("is_finished",
89 : &is_finished),
90 : GNUNET_PQ_result_spec_end
91 : };
92 :
93 0 : if (GNUNET_OK !=
94 0 : GNUNET_PQ_extract_result (result,
95 : rs,
96 : i))
97 : {
98 0 : GNUNET_break (0);
99 0 : ctx->status = GNUNET_SYSERR;
100 0 : return;
101 : }
102 0 : ctx->cb (ctx->cb_cls,
103 : &h_payto,
104 : start_time,
105 : jmeasures,
106 : is_finished,
107 : rowid);
108 0 : GNUNET_PQ_cleanup_result (rs);
109 : }
110 : }
111 :
112 :
113 : enum GNUNET_DB_QueryStatus
114 0 : TEH_PG_select_aml_measures (
115 : void *cls,
116 : const struct TALER_NormalizedPaytoHashP *h_payto,
117 : enum TALER_EXCHANGE_YesNoAll finished_only,
118 : uint64_t offset,
119 : int64_t limit,
120 : TALER_EXCHANGEDB_LegitimizationMeasureCallback cb,
121 : void *cb_cls)
122 : {
123 0 : struct PostgresClosure *pg = cls;
124 0 : uint64_t ulimit = (limit > 0) ? limit : -limit;
125 0 : struct GNUNET_PQ_QueryParam params[] = {
126 0 : GNUNET_PQ_query_param_bool (NULL == h_payto),
127 : NULL == h_payto
128 0 : ? GNUNET_PQ_query_param_null ()
129 0 : : GNUNET_PQ_query_param_auto_from_type (h_payto),
130 0 : GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL ==
131 : finished_only)),
132 0 : GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES ==
133 : finished_only)),
134 0 : GNUNET_PQ_query_param_uint64 (&offset),
135 0 : GNUNET_PQ_query_param_uint64 (&ulimit),
136 : GNUNET_PQ_query_param_end
137 : };
138 0 : struct LegiMeasureResultContext ctx = {
139 : .cb = cb,
140 : .cb_cls = cb_cls,
141 : .pg = pg,
142 : .status = GNUNET_OK
143 : };
144 : enum GNUNET_DB_QueryStatus qs;
145 0 : const char *stmt = (limit > 0)
146 : ? "select_aml_measures_inc"
147 : : "select_aml_measures_dec";
148 :
149 0 : PREPARE (pg,
150 : "select_aml_measures_inc",
151 : "SELECT"
152 : " lm.legitimization_measure_serial_id"
153 : ",kt.h_normalized_payto"
154 : ",lm.jmeasures"
155 : ",lm.start_time"
156 : ",lm.is_finished"
157 : " FROM kyc_targets kt"
158 : " JOIN legitimization_measures lm"
159 : " USING (access_token)"
160 : " WHERE (legitimization_measure_serial_id > $5)"
161 : " AND ($1 OR (kt.h_normalized_payto = $2))"
162 : " AND ($3 OR (lm.is_finished = $4))"
163 : " ORDER BY lm.legitimization_measure_serial_id ASC"
164 : " LIMIT $6");
165 0 : PREPARE (pg,
166 : "select_aml_measures_dec",
167 : "SELECT"
168 : " lm.legitimization_measure_serial_id"
169 : ",kt.h_normalized_payto"
170 : ",lm.jmeasures"
171 : ",lm.start_time"
172 : ",lm.is_finished"
173 : " FROM kyc_targets kt"
174 : " JOIN legitimization_measures lm"
175 : " USING (access_token)"
176 : " WHERE (legitimization_measure_serial_id < $5)"
177 : " AND ($1 OR (kt.h_normalized_payto = $2))"
178 : " AND ($3 OR (lm.is_finished = $4))"
179 : " ORDER BY lm.legitimization_measure_serial_id DESC"
180 : " LIMIT $6");
181 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
182 : stmt,
183 : params,
184 : &handle_aml_result,
185 : &ctx);
186 0 : if (GNUNET_OK != ctx.status)
187 0 : return GNUNET_DB_STATUS_HARD_ERROR;
188 0 : return qs;
189 : }
|