Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 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 exchangedb/pg_select_aggregation_amounts_for_kyc_check.c
18 : * @brief Implementation of the select_aggregation_amounts_for_kyc_check function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/platform.h"
22 : #include "taler/taler_error_codes.h"
23 : #include "taler/taler_dbevents.h"
24 : #include "taler/taler_pq_lib.h"
25 : #include "pg_select_aggregation_amounts_for_kyc_check.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #get_kyc_amounts_cb().
31 : */
32 : struct KycAmountCheckContext
33 : {
34 : /**
35 : * Function to call per result.
36 : */
37 : TALER_EXCHANGEDB_KycAmountCallback 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 : * Flag set to #GNUNET_OK as long as everything is fine.
51 : */
52 : enum GNUNET_GenericReturnValue status;
53 :
54 : };
55 :
56 :
57 : /**
58 : * Invoke the callback for each result.
59 : *
60 : * @param cls a `struct KycAmountCheckContext *`
61 : * @param result SQL result
62 : * @param num_results number of rows in @a result
63 : */
64 : static void
65 2 : get_kyc_amounts_cb (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 2 : struct KycAmountCheckContext *ctx = cls;
70 2 : struct PostgresClosure *pg = ctx->pg;
71 :
72 2 : for (unsigned int i = 0; i < num_results; i++)
73 : {
74 : struct GNUNET_TIME_Absolute date;
75 : struct TALER_Amount amount;
76 0 : struct GNUNET_PQ_ResultSpec rs[] = {
77 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
78 : &amount),
79 0 : GNUNET_PQ_result_spec_absolute_time ("date",
80 : &date),
81 : GNUNET_PQ_result_spec_end
82 : };
83 : enum GNUNET_GenericReturnValue ret;
84 :
85 0 : if (GNUNET_OK !=
86 0 : GNUNET_PQ_extract_result (result,
87 : rs,
88 : i))
89 : {
90 0 : GNUNET_break (0);
91 0 : ctx->status = GNUNET_SYSERR;
92 0 : return;
93 : }
94 0 : ret = ctx->cb (ctx->cb_cls,
95 : &amount,
96 : date);
97 0 : GNUNET_PQ_cleanup_result (rs);
98 0 : switch (ret)
99 : {
100 0 : case GNUNET_OK:
101 0 : continue;
102 0 : case GNUNET_NO:
103 0 : break;
104 0 : case GNUNET_SYSERR:
105 0 : ctx->status = GNUNET_SYSERR;
106 0 : break;
107 : }
108 0 : break;
109 : }
110 : }
111 :
112 :
113 : enum GNUNET_DB_QueryStatus
114 2 : TEH_PG_select_aggregation_amounts_for_kyc_check (
115 : void *cls,
116 : const struct TALER_NormalizedPaytoHashP *h_payto,
117 : struct GNUNET_TIME_Absolute time_limit,
118 : TALER_EXCHANGEDB_KycAmountCallback kac,
119 : void *kac_cls)
120 : {
121 2 : struct PostgresClosure *pg = cls;
122 2 : struct GNUNET_PQ_QueryParam params[] = {
123 2 : GNUNET_PQ_query_param_auto_from_type (h_payto),
124 2 : GNUNET_PQ_query_param_absolute_time (&time_limit),
125 : GNUNET_PQ_query_param_end
126 : };
127 2 : struct KycAmountCheckContext ctx = {
128 : .cb = kac,
129 : .cb_cls = kac_cls,
130 : .pg = pg,
131 : .status = GNUNET_OK
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 2 : PREPARE (pg,
136 : "select_kyc_relevant_aggregation_events",
137 : "SELECT"
138 : " amount"
139 : ",execution_date AS date"
140 : " FROM wire_out"
141 : " WHERE wire_target_h_payto IN"
142 : " (SELECT wire_target_h_payto"
143 : " FROM wire_targets"
144 : " WHERE h_normalized_payto=$1"
145 : " )"
146 : " AND execution_date >= $2"
147 : " ORDER BY execution_date DESC");
148 :
149 2 : qs = GNUNET_PQ_eval_prepared_multi_select (
150 : pg->conn,
151 : "select_kyc_relevant_aggregation_events",
152 : params,
153 : &get_kyc_amounts_cb,
154 : &ctx);
155 2 : if (GNUNET_OK != ctx.status)
156 0 : return GNUNET_DB_STATUS_HARD_ERROR;
157 2 : return qs;
158 : }
|