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_denom_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_denom_revenue.h"
23 : #include "pg_helper.h"
24 :
25 :
26 : /**
27 : * Closure for #historic_denom_revenue_cb().
28 : */
29 : struct HistoricDenomRevenueContext
30 : {
31 : /**
32 : * Function to call for each result.
33 : */
34 : TALER_AUDITORDB_HistoricDenominationRevenueDataCallback 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_denom_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_denom_revenue_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 1 : struct HistoricDenomRevenueContext *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 TALER_DenominationHashP denom_pub_hash;
74 : struct GNUNET_TIME_Timestamp revenue_timestamp;
75 : struct TALER_Amount revenue_balance;
76 : struct TALER_Amount loss;
77 2 : struct GNUNET_PQ_ResultSpec rs[] = {
78 2 : GNUNET_PQ_result_spec_uint64 ("row_id",
79 : &rowid),
80 2 : GNUNET_PQ_result_spec_auto_from_type ("denom_pub_hash",
81 : &denom_pub_hash),
82 2 : GNUNET_PQ_result_spec_timestamp ("revenue_timestamp",
83 : &revenue_timestamp),
84 2 : TALER_PQ_RESULT_SPEC_AMOUNT ("revenue_balance",
85 : &revenue_balance),
86 2 : TALER_PQ_RESULT_SPEC_AMOUNT ("loss_balance",
87 : &loss),
88 : GNUNET_PQ_result_spec_end
89 : };
90 :
91 2 : if (GNUNET_OK !=
92 2 : GNUNET_PQ_extract_result (result,
93 : rs,
94 : i))
95 : {
96 0 : GNUNET_break (0);
97 0 : hrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
98 0 : return;
99 : }
100 :
101 2 : hrc->qs = i + 1;
102 2 : if (GNUNET_OK !=
103 2 : hrc->cb (hrc->cb_cls,
104 : rowid,
105 : &denom_pub_hash,
106 : revenue_timestamp,
107 : &revenue_balance,
108 : &loss))
109 0 : break;
110 : }
111 : }
112 :
113 :
114 : enum GNUNET_DB_QueryStatus
115 1 : TALER_AUDITORDB_select_historic_denom_revenue (
116 : struct TALER_AUDITORDB_PostgresContext *pg,
117 : int64_t limit,
118 : uint64_t offset,
119 : TALER_AUDITORDB_HistoricDenominationRevenueDataCallback cb,
120 : void *cb_cls)
121 : {
122 1 : uint64_t ulimit = (limit > 0) ? limit : -limit;
123 1 : struct GNUNET_PQ_QueryParam params[] = {
124 1 : GNUNET_PQ_query_param_uint64 (&offset),
125 1 : GNUNET_PQ_query_param_uint64 (&ulimit),
126 : GNUNET_PQ_query_param_end
127 : };
128 1 : struct HistoricDenomRevenueContext hrc = {
129 : .cb = cb,
130 : .cb_cls = cb_cls,
131 : .pg = pg
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 1 : PREPARE (pg,
136 : "auditor_historic_denomination_revenue_select_inc",
137 : "SELECT"
138 : " row_id"
139 : ",denom_pub_hash"
140 : ",revenue_timestamp"
141 : ",revenue_balance"
142 : ",loss_balance"
143 : " FROM auditor_historic_denomination_revenue"
144 : " WHERE row_id > $1"
145 : " ORDER BY row_id ASC"
146 : " LIMIT $2;");
147 1 : PREPARE (pg,
148 : "auditor_historic_denomination_revenue_select_dec",
149 : "SELECT"
150 : " row_id"
151 : ",denom_pub_hash"
152 : ",revenue_timestamp"
153 : ",revenue_balance"
154 : ",loss_balance"
155 : " FROM auditor_historic_denomination_revenue"
156 : " WHERE row_id < $1"
157 : " ORDER BY row_id DESC"
158 : " LIMIT $2;");
159 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
160 : pg->conn,
161 : limit > 0
162 : ? "auditor_historic_denomination_revenue_select_inc"
163 : : "auditor_historic_denomination_revenue_select_dec",
164 : params,
165 : &historic_denom_revenue_cb,
166 : &hrc);
167 1 : if (qs <= 0)
168 0 : return qs;
169 1 : return hrc.qs;
170 : }
|