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 : #include "platform.h"
17 : #include "taler_error_codes.h"
18 : #include "taler_dbevents.h"
19 : #include "taler_pq_lib.h"
20 : #include "pg_helper.h"
21 : #include "pg_get_emergency.h"
22 :
23 : /**
24 : * Closure for #emergency_cb().
25 : */
26 : struct EmergencyContext
27 : {
28 :
29 : /**
30 : * Function to call for each deposit confirmation.
31 : */
32 : TALER_AUDITORDB_EmergencyCallback cb;
33 :
34 : /**
35 : * Closure for @e cb
36 : */
37 : void *cb_cls;
38 :
39 : /**
40 : * Plugin context.
41 : */
42 : struct PostgresClosure *pg;
43 :
44 : /**
45 : * Query status to return.
46 : */
47 : enum GNUNET_DB_QueryStatus qs;
48 : };
49 :
50 :
51 : /**
52 : * Helper function for #TAH_PG_get_emergency().
53 : * To be called with the results of a SELECT statement
54 : * that has returned @a num_results results.
55 : *
56 : * @param cls closure of type `struct Emergency *`
57 : * @param result the postgres result
58 : * @param num_results the number of results in @a result
59 : */
60 : static void
61 7 : emergency_cb (void *cls,
62 : PGresult *result,
63 : unsigned int num_results)
64 : {
65 7 : struct EmergencyContext *dcc = cls;
66 7 : struct PostgresClosure *pg = dcc->pg;
67 :
68 12 : for (unsigned int i = 0; i < num_results; i++)
69 : {
70 : struct TALER_AUDITORDB_Emergency dc;
71 5 : struct GNUNET_PQ_ResultSpec rs[] = {
72 5 : GNUNET_PQ_result_spec_uint64 ("row_id",
73 : &dc.row_id),
74 5 : GNUNET_PQ_result_spec_auto_from_type ("denompub_h",
75 : &dc.denompub_h),
76 5 : TALER_PQ_RESULT_SPEC_AMOUNT ("denom_risk",
77 : &dc.denom_risk),
78 5 : TALER_PQ_RESULT_SPEC_AMOUNT ("denom_loss",
79 : &dc.denom_loss),
80 5 : GNUNET_PQ_result_spec_absolute_time ("deposit_start",
81 : &dc.deposit_start),
82 5 : GNUNET_PQ_result_spec_absolute_time ("deposit_end",
83 : &dc.deposit_end),
84 5 : TALER_PQ_RESULT_SPEC_AMOUNT ("value",
85 : &dc.value),
86 5 : GNUNET_PQ_result_spec_bool ("suppressed",
87 : &dc.suppressed),
88 : GNUNET_PQ_result_spec_end
89 : };
90 : enum GNUNET_GenericReturnValue rval;
91 :
92 5 : if (GNUNET_OK !=
93 5 : GNUNET_PQ_extract_result (result,
94 : rs,
95 : i))
96 : {
97 0 : GNUNET_break (0);
98 0 : dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
99 0 : return;
100 : }
101 5 : dcc->qs = i + 1;
102 5 : rval = dcc->cb (dcc->cb_cls,
103 : &dc);
104 5 : GNUNET_PQ_cleanup_result (rs);
105 5 : if (GNUNET_OK != rval)
106 0 : break;
107 : }
108 : }
109 :
110 :
111 : enum GNUNET_DB_QueryStatus
112 7 : TAH_PG_get_emergency (
113 : void *cls,
114 : int64_t limit,
115 : uint64_t offset,
116 : bool return_suppressed,
117 : TALER_AUDITORDB_EmergencyCallback cb,
118 : void *cb_cls)
119 : {
120 7 : struct PostgresClosure *pg = cls;
121 7 : uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
122 7 : struct GNUNET_PQ_QueryParam params[] = {
123 7 : GNUNET_PQ_query_param_uint64 (&offset),
124 7 : GNUNET_PQ_query_param_bool (return_suppressed),
125 7 : GNUNET_PQ_query_param_uint64 (&plimit),
126 : GNUNET_PQ_query_param_end
127 : };
128 7 : struct EmergencyContext dcc = {
129 : .cb = cb,
130 : .cb_cls = cb_cls,
131 : .pg = pg
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 7 : PREPARE (pg,
136 : "auditor_emergency_get_desc",
137 : "SELECT"
138 : " row_id"
139 : ",denompub_h"
140 : ",denom_risk"
141 : ",denom_loss"
142 : ",deposit_start"
143 : ",deposit_end"
144 : ",value"
145 : ",suppressed"
146 : " FROM auditor_emergency"
147 : " WHERE (row_id < $1)"
148 : " AND ($2 OR NOT suppressed)"
149 : " ORDER BY row_id DESC"
150 : " LIMIT $3"
151 : );
152 7 : PREPARE (pg,
153 : "auditor_emergency_get_asc",
154 : "SELECT"
155 : " row_id"
156 : ",denompub_h"
157 : ",denom_risk"
158 : ",denom_loss"
159 : ",deposit_start"
160 : ",deposit_end"
161 : ",value"
162 : ",suppressed"
163 : " FROM auditor_emergency"
164 : " WHERE (row_id > $1)"
165 : " AND ($2 OR NOT suppressed)"
166 : " ORDER BY row_id ASC"
167 : " LIMIT $3"
168 : );
169 7 : qs = GNUNET_PQ_eval_prepared_multi_select (
170 : pg->conn,
171 : (limit > 0)
172 : ? "auditor_emergency_get_asc"
173 : : "auditor_emergency_get_desc",
174 : params,
175 : &emergency_cb,
176 : &dcc);
177 :
178 7 : if (qs > 0)
179 3 : return dcc.qs;
180 4 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
181 4 : return qs;
182 : }
|