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 exchangedb/pg_lookup_aml_history.c
18 : * @brief Implementation of the lookup_aml_history 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_lookup_aml_history.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for callbacks called from #TEH_PG_lookup_aml_history()
31 : */
32 : struct AmlHistoryContext
33 : {
34 :
35 : /**
36 : * Function to call on each result.
37 : */
38 : TALER_EXCHANGEDB_AmlHistoryCallback cb;
39 :
40 : /**
41 : * Closure for @e cb.
42 : */
43 : void *cb_cls;
44 :
45 : /**
46 : * Plugin context.
47 : */
48 : struct PostgresClosure *pg;
49 :
50 : /**
51 : * Set to 'true' if the transaction failed.
52 : */
53 : bool failed;
54 :
55 : };
56 :
57 :
58 : /**
59 : * Function to be called with the results of a SELECT statement
60 : * that has returned @a num_results results.
61 : *
62 : * @param cls closure of type `struct AmlHistoryContext`
63 : * @param result the postgres result
64 : * @param num_results the number of results in @a result
65 : */
66 : static void
67 0 : handle_aml_entry (void *cls,
68 : PGresult *result,
69 : unsigned int num_results)
70 : {
71 0 : struct AmlHistoryContext *ahc = cls;
72 :
73 0 : for (unsigned int i = 0; i < num_results; i++)
74 : {
75 : struct GNUNET_TIME_Timestamp decision_time;
76 : char *justification;
77 : struct TALER_AmlOfficerPublicKeyP decider_pub;
78 : json_t *jproperties;
79 0 : json_t *jnew_rules = NULL;
80 : bool to_investigate;
81 : bool is_active;
82 0 : struct GNUNET_PQ_ResultSpec rs[] = {
83 0 : GNUNET_PQ_result_spec_timestamp ("decision_time",
84 : &decision_time),
85 0 : GNUNET_PQ_result_spec_string ("justification",
86 : &justification),
87 0 : GNUNET_PQ_result_spec_auto_from_type ("decider_pub",
88 : &decider_pub),
89 0 : GNUNET_PQ_result_spec_allow_null (
90 : TALER_PQ_result_spec_json ("jproperties",
91 : &jproperties),
92 : NULL),
93 0 : TALER_PQ_result_spec_json ("jnew_rules",
94 : &jnew_rules),
95 0 : GNUNET_PQ_result_spec_bool ("to_investigate",
96 : &to_investigate),
97 0 : GNUNET_PQ_result_spec_bool ("is_active",
98 : &is_active),
99 : GNUNET_PQ_result_spec_end
100 : };
101 :
102 0 : if (GNUNET_OK !=
103 0 : GNUNET_PQ_extract_result (result,
104 : rs,
105 : i))
106 : {
107 0 : GNUNET_break (0);
108 0 : ahc->failed = true;
109 0 : return;
110 : }
111 0 : ahc->cb (ahc->cb_cls,
112 : decision_time,
113 : justification,
114 : &decider_pub,
115 : jproperties,
116 : jnew_rules,
117 : to_investigate,
118 : is_active);
119 0 : GNUNET_PQ_cleanup_result (rs);
120 : }
121 : }
122 :
123 :
124 : enum GNUNET_DB_QueryStatus
125 0 : TEH_PG_lookup_aml_history (
126 : void *cls,
127 : const struct TALER_NormalizedPaytoHashP *h_payto,
128 : TALER_EXCHANGEDB_AmlHistoryCallback cb,
129 : void *cb_cls)
130 : {
131 0 : struct PostgresClosure *pg = cls;
132 0 : struct AmlHistoryContext ahc = {
133 : .pg = pg,
134 : .cb = cb,
135 : .cb_cls = cb_cls
136 : };
137 0 : struct GNUNET_PQ_QueryParam params[] = {
138 0 : GNUNET_PQ_query_param_auto_from_type (h_payto),
139 : GNUNET_PQ_query_param_end
140 : };
141 : enum GNUNET_DB_QueryStatus qs;
142 :
143 0 : PREPARE (pg,
144 : "lookup_aml_history",
145 : "SELECT"
146 : " lo.decision_time"
147 : ",ah.justification"
148 : ",ah.decider_pub"
149 : ",lo.jproperties"
150 : ",lo.jnew_rules"
151 : ",lo.to_investigate"
152 : ",lo.is_active"
153 : " FROM aml_history ah"
154 : " JOIN legitimization_outcomes lo"
155 : " USING (outcome_serial_id)"
156 : " WHERE ah.h_payto=$1"
157 : " ORDER BY decision_time DESC, outcome_serial_id DESC;");
158 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
159 : pg->conn,
160 : "lookup_aml_history",
161 : params,
162 : &handle_aml_entry,
163 : &ahc);
164 0 : if (qs <= 0)
165 0 : return qs;
166 0 : if (ahc.failed)
167 : {
168 0 : GNUNET_break (0);
169 0 : return GNUNET_DB_STATUS_HARD_ERROR;
170 : }
171 0 : return qs;
172 : }
|