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