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