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_not_closed_inconsistency.h"
22 :
23 :
24 : struct ReserveNotClosedInconsistencyContext
25 : {
26 :
27 : /**
28 : * Function to call for each bad sig loss.
29 : */
30 : TALER_AUDITORDB_ReserveNotClosedInconsistencyCallback 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_not_closed_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 ReserveNotClosedInconsistencyContext *`
55 : * @param result the postgres result
56 : * @param num_results the number of results in @a result
57 : */
58 : static void
59 2 : reserve_not_closed_inconsistency_cb (void *cls,
60 : PGresult *result,
61 : unsigned int num_results)
62 : {
63 2 : struct ReserveNotClosedInconsistencyContext *dcc = cls;
64 2 : struct PostgresClosure *pg = dcc->pg;
65 :
66 3 : for (unsigned int i = 0; i < num_results; i++)
67 : {
68 : struct TALER_AUDITORDB_ReserveNotClosedInconsistency dc;
69 1 : struct GNUNET_PQ_ResultSpec rs[] = {
70 1 : GNUNET_PQ_result_spec_uint64 ("row_id",
71 : &dc.row_id),
72 1 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
73 : &dc.reserve_pub),
74 1 : TALER_PQ_RESULT_SPEC_AMOUNT ("balance",
75 : &dc.balance),
76 1 : GNUNET_PQ_result_spec_absolute_time ("expiration_time",
77 : &dc.expiration_time),
78 1 : GNUNET_PQ_result_spec_string ("diagnostic",
79 : &dc.diagnostic),
80 1 : GNUNET_PQ_result_spec_bool ("suppressed",
81 : &dc.suppressed),
82 : GNUNET_PQ_result_spec_end
83 : };
84 : enum GNUNET_GenericReturnValue rval;
85 :
86 1 : if (GNUNET_OK !=
87 1 : GNUNET_PQ_extract_result (result,
88 : rs,
89 : i))
90 : {
91 0 : GNUNET_break (0);
92 0 : dcc->qs = GNUNET_DB_STATUS_HARD_ERROR;
93 0 : return;
94 : }
95 1 : dcc->qs = i + 1;
96 1 : rval = dcc->cb (dcc->cb_cls,
97 : &dc);
98 1 : GNUNET_PQ_cleanup_result (rs);
99 1 : if (GNUNET_OK != rval)
100 0 : break;
101 : }
102 : }
103 :
104 :
105 : enum GNUNET_DB_QueryStatus
106 2 : TAH_PG_get_reserve_not_closed_inconsistency (
107 : void *cls,
108 : int64_t limit,
109 : uint64_t offset,
110 : bool return_suppressed,
111 : TALER_AUDITORDB_ReserveNotClosedInconsistencyCallback cb,
112 : void *cb_cls)
113 : {
114 2 : struct PostgresClosure *pg = cls;
115 2 : uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
116 2 : struct GNUNET_PQ_QueryParam params[] = {
117 2 : GNUNET_PQ_query_param_uint64 (&offset),
118 2 : GNUNET_PQ_query_param_bool (return_suppressed),
119 2 : GNUNET_PQ_query_param_uint64 (&plimit),
120 : GNUNET_PQ_query_param_end
121 : };
122 2 : struct ReserveNotClosedInconsistencyContext dcc = {
123 : .cb = cb,
124 : .cb_cls = cb_cls,
125 : .pg = pg
126 : };
127 : enum GNUNET_DB_QueryStatus qs;
128 :
129 2 : PREPARE (pg,
130 : "auditor_reserve_not_closed_inconsistency_get_desc",
131 : "SELECT"
132 : " row_id"
133 : ",reserve_pub"
134 : ",balance"
135 : ",expiration_time"
136 : ",diagnostic"
137 : ",suppressed"
138 : " FROM auditor_reserve_not_closed_inconsistency"
139 : " WHERE (row_id < $1)"
140 : " AND ($2 OR suppressed is false)"
141 : " ORDER BY row_id DESC"
142 : " LIMIT $3"
143 : );
144 2 : PREPARE (pg,
145 : "auditor_reserve_not_closed_inconsistency_get_asc",
146 : "SELECT"
147 : " row_id"
148 : ",reserve_pub"
149 : ",balance"
150 : ",expiration_time"
151 : ",diagnostic"
152 : ",suppressed"
153 : " FROM auditor_reserve_not_closed_inconsistency"
154 : " WHERE (row_id > $1)"
155 : " AND ($2 OR suppressed is false)"
156 : " ORDER BY row_id ASC"
157 : " LIMIT $3"
158 : );
159 2 : qs = GNUNET_PQ_eval_prepared_multi_select (
160 : pg->conn,
161 : (limit > 0)
162 : ? "auditor_reserve_not_closed_inconsistency_get_asc"
163 : : "auditor_reserve_not_closed_inconsistency_get_desc",
164 : params,
165 : &reserve_not_closed_inconsistency_cb,
166 : &dcc);
167 2 : if (qs > 0)
168 1 : return dcc.qs;
169 1 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs);
170 1 : return qs;
171 : }
|