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