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 select_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/select_historic_reserve_revenue.h"
23 : #include "pg_helper.h"
24 :
25 :
26 : /**
27 : * Closure for #historic_reserve_revenue_cb().
28 : */
29 : struct HistoricReserveRevenueContext
30 : {
31 : /**
32 : * Function to call for each result.
33 : */
34 : TALER_AUDITORDB_HistoricReserveRevenueDataCallback 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 : * Number of results processed.
48 : */
49 : enum GNUNET_DB_QueryStatus qs;
50 : };
51 :
52 :
53 : /**
54 : * Helper function for #TALER_AUDITORDB_select_historic_reserve_revenue().
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 HistoricRevenueContext *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 1 : historic_reserve_revenue_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 1 : struct HistoricReserveRevenueContext *hrc = cls;
68 1 : struct TALER_AUDITORDB_PostgresContext *pg = hrc->pg;
69 :
70 3 : for (unsigned int i = 0; i < num_results; i++)
71 : {
72 : uint64_t rowid;
73 : struct GNUNET_TIME_Timestamp start_date;
74 : struct GNUNET_TIME_Timestamp end_date;
75 : struct TALER_Amount reserve_profits;
76 2 : struct GNUNET_PQ_ResultSpec rs[] = {
77 2 : GNUNET_PQ_result_spec_uint64 ("row_id",
78 : &rowid),
79 2 : GNUNET_PQ_result_spec_timestamp ("start_date",
80 : &start_date),
81 2 : GNUNET_PQ_result_spec_timestamp ("end_date",
82 : &end_date),
83 2 : TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_profits",
84 : &reserve_profits),
85 : GNUNET_PQ_result_spec_end
86 : };
87 :
88 2 : if (GNUNET_OK !=
89 2 : GNUNET_PQ_extract_result (result,
90 : rs,
91 : i))
92 : {
93 0 : GNUNET_break (0);
94 0 : hrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
95 0 : return;
96 : }
97 2 : hrc->qs = i + 1;
98 2 : if (GNUNET_OK !=
99 2 : hrc->cb (hrc->cb_cls,
100 : rowid,
101 : start_date,
102 : end_date,
103 : &reserve_profits))
104 0 : break;
105 : }
106 : }
107 :
108 :
109 : enum GNUNET_DB_QueryStatus
110 1 : TALER_AUDITORDB_select_historic_reserve_revenue (
111 : struct TALER_AUDITORDB_PostgresContext *pg,
112 : int64_t limit,
113 : uint64_t offset,
114 : TALER_AUDITORDB_HistoricReserveRevenueDataCallback cb,
115 : void *cb_cls)
116 : {
117 1 : uint64_t ulimit = (limit > 0) ? limit : -limit;
118 1 : struct GNUNET_PQ_QueryParam params[] = {
119 1 : GNUNET_PQ_query_param_uint64 (&offset),
120 1 : GNUNET_PQ_query_param_uint64 (&ulimit),
121 : GNUNET_PQ_query_param_end
122 : };
123 : enum GNUNET_DB_QueryStatus qs;
124 1 : struct HistoricReserveRevenueContext hrc = {
125 : .cb = cb,
126 : .cb_cls = cb_cls,
127 : .pg = pg
128 : };
129 :
130 1 : PREPARE (pg,
131 : "auditor_historic_reserve_summary_select_inc",
132 : "SELECT"
133 : " row_id"
134 : ",start_date"
135 : ",end_date"
136 : ",reserve_profits"
137 : " FROM auditor_historic_reserve_summary"
138 : " WHERE row_id > $1"
139 : " ORDER BY row_id ASC"
140 : " LIMIT $2");
141 1 : PREPARE (pg,
142 : "auditor_historic_reserve_summary_select_dec",
143 : "SELECT"
144 : " row_id"
145 : ",start_date"
146 : ",end_date"
147 : ",reserve_profits"
148 : " FROM auditor_historic_reserve_summary"
149 : " WHERE row_id < $1"
150 : " ORDER BY row_id DESC"
151 : " LIMIT $2");
152 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
153 : pg->conn,
154 : limit > 0
155 : ? "auditor_historic_reserve_summary_select_inc"
156 : : "auditor_historic_reserve_summary_select_dec",
157 : params,
158 : &historic_reserve_revenue_cb,
159 : &hrc);
160 1 : if (0 >= qs)
161 0 : return qs;
162 1 : return hrc.qs;
163 : }
|