Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 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 : /**
17 : * @file iterate_historic_reserve_revenue.c
18 : * @brief Low-level (statement-level) Postgres database access for the exchange
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "auditor-database/iterate_historic_reserve_revenue.h"
23 : #include "pg_helper.h"
24 :
25 :
26 : /**
27 : * Hard upper bound on the number of records returned by a single
28 : * call, regardless of the limit requested by the client.
29 : */
30 : #define MAX_RECORDS 50000
31 :
32 :
33 : /**
34 : * Closure for #historic_reserve_revenue_cb().
35 : */
36 : struct HistoricReserveRevenueContext
37 : {
38 : /**
39 : * Function to call for each result.
40 : */
41 : TALER_AUDITORDB_HistoricReserveRevenueDataCallback cb;
42 :
43 : /**
44 : * Closure for @e cb.
45 : */
46 : void *cb_cls;
47 :
48 : /**
49 : * Plugin context.
50 : */
51 : struct TALER_AUDITORDB_PostgresContext *pg;
52 :
53 : /**
54 : * Number of results processed.
55 : */
56 : enum GNUNET_DB_QueryStatus qs;
57 : };
58 :
59 :
60 : /**
61 : * Helper function for #TALER_AUDITORDB_iterate_historic_reserve_revenue().
62 : * To be called with the results of a SELECT statement
63 : * that has returned @a num_results results.
64 : *
65 : * @param cls closure of type `struct HistoricRevenueContext *`
66 : * @param result the postgres result
67 : * @param num_results the number of results in @a result
68 : */
69 : static void
70 1 : historic_reserve_revenue_cb (void *cls,
71 : PGresult *result,
72 : unsigned int num_results)
73 : {
74 1 : struct HistoricReserveRevenueContext *hrc = cls;
75 1 : struct TALER_AUDITORDB_PostgresContext *pg = hrc->pg;
76 :
77 3 : for (unsigned int i = 0; i < num_results; i++)
78 : {
79 : uint64_t rowid;
80 : struct GNUNET_TIME_Timestamp start_date;
81 : struct GNUNET_TIME_Timestamp end_date;
82 : struct TALER_Amount reserve_profits;
83 2 : struct GNUNET_PQ_ResultSpec rs[] = {
84 2 : GNUNET_PQ_result_spec_uint64 ("row_id",
85 : &rowid),
86 2 : GNUNET_PQ_result_spec_timestamp ("start_date",
87 : &start_date),
88 2 : GNUNET_PQ_result_spec_timestamp ("end_date",
89 : &end_date),
90 2 : TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_profits",
91 : &reserve_profits),
92 : GNUNET_PQ_result_spec_end
93 : };
94 :
95 2 : if (GNUNET_OK !=
96 2 : GNUNET_PQ_extract_result (result,
97 : rs,
98 : i))
99 : {
100 0 : GNUNET_break (0);
101 0 : hrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
102 0 : return;
103 : }
104 2 : hrc->qs = i + 1;
105 2 : if (GNUNET_OK !=
106 2 : hrc->cb (hrc->cb_cls,
107 : rowid,
108 : start_date,
109 : end_date,
110 : &reserve_profits))
111 0 : break;
112 : }
113 : }
114 :
115 :
116 : enum GNUNET_DB_QueryStatus
117 1 : TALER_AUDITORDB_iterate_historic_reserve_revenue (
118 : struct TALER_AUDITORDB_PostgresContext *pg,
119 : int64_t limit,
120 : uint64_t offset,
121 : TALER_AUDITORDB_HistoricReserveRevenueDataCallback cb,
122 : void *cb_cls)
123 : {
124 1 : uint64_t ulimit = GNUNET_MIN ((uint64_t) MAX_RECORDS,
125 : TALER_AUDITORDB_abs_limit (limit));
126 1 : struct GNUNET_PQ_QueryParam params[] = {
127 1 : GNUNET_PQ_query_param_uint64 (&offset),
128 1 : GNUNET_PQ_query_param_uint64 (&ulimit),
129 : GNUNET_PQ_query_param_end
130 : };
131 : enum GNUNET_DB_QueryStatus qs;
132 1 : struct HistoricReserveRevenueContext hrc = {
133 : .cb = cb,
134 : .cb_cls = cb_cls,
135 : .pg = pg
136 : };
137 :
138 1 : PREPARE (pg,
139 : "iterate_historic_reserve_revenue_summary_select_inc",
140 : "SELECT"
141 : " row_id"
142 : ",start_date"
143 : ",end_date"
144 : ",reserve_profits"
145 : " FROM auditor_historic_reserve_summary"
146 : " WHERE row_id > $1"
147 : " ORDER BY row_id ASC"
148 : " LIMIT $2");
149 1 : PREPARE (pg,
150 : "iterate_historic_reserve_revenue_summary_select_dec",
151 : "SELECT"
152 : " row_id"
153 : ",start_date"
154 : ",end_date"
155 : ",reserve_profits"
156 : " FROM auditor_historic_reserve_summary"
157 : " WHERE row_id < $1"
158 : " ORDER BY row_id DESC"
159 : " LIMIT $2");
160 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
161 : pg->conn,
162 : limit > 0
163 : ? "iterate_historic_reserve_revenue_summary_select_inc"
164 : : "iterate_historic_reserve_revenue_summary_select_dec",
165 : params,
166 : &historic_reserve_revenue_cb,
167 : &hrc);
168 1 : if (0 >= qs)
169 0 : return qs;
170 1 : return hrc.qs;
171 : }
|