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 exchangedb/get_global_fees.c
18 : * @brief Implementation of the get_global_fees function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_error_codes.h"
22 : #include "taler/taler_dbevents.h"
23 : #include "taler/taler_pq_lib.h"
24 : #include "exchange-database/get_global_fees.h"
25 : #include "helper.h"
26 :
27 :
28 : /**
29 : * Closure for #global_fees_cb().
30 : */
31 : struct GlobalFeeContext
32 : {
33 : /**
34 : * Function to call for each global fee block.
35 : */
36 : TALER_EXCHANGEDB_GlobalFeeCallback cb;
37 :
38 : /**
39 : * Closure to give to @e rec.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Plugin context.
45 : */
46 : struct TALER_EXCHANGEDB_PostgresContext *pg;
47 :
48 : /**
49 : * Set to #GNUNET_SYSERR on error.
50 : */
51 : enum GNUNET_GenericReturnValue status;
52 : };
53 :
54 :
55 : /**
56 : * Function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results.
58 : *
59 : * @param cls closure
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 41 : global_fees_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 41 : struct GlobalFeeContext *gctx = cls;
69 41 : struct TALER_EXCHANGEDB_PostgresContext *pg = gctx->pg;
70 :
71 87 : for (unsigned int i = 0; i<num_results; i++)
72 : {
73 : struct TALER_GlobalFeeSet fees;
74 : struct GNUNET_TIME_Relative purse_timeout;
75 : struct GNUNET_TIME_Relative history_expiration;
76 : uint32_t purse_account_limit;
77 : struct GNUNET_TIME_Timestamp start_date;
78 : struct GNUNET_TIME_Timestamp end_date;
79 : struct TALER_MasterSignatureP master_sig;
80 46 : struct GNUNET_PQ_ResultSpec rs[] = {
81 46 : GNUNET_PQ_result_spec_timestamp ("start_date",
82 : &start_date),
83 46 : GNUNET_PQ_result_spec_timestamp ("end_date",
84 : &end_date),
85 46 : TALER_PQ_RESULT_SPEC_AMOUNT ("history_fee",
86 : &fees.history),
87 46 : TALER_PQ_RESULT_SPEC_AMOUNT ("account_fee",
88 : &fees.account),
89 46 : TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee",
90 : &fees.purse),
91 46 : GNUNET_PQ_result_spec_relative_time ("purse_timeout",
92 : &purse_timeout),
93 46 : GNUNET_PQ_result_spec_relative_time ("history_expiration",
94 : &history_expiration),
95 46 : GNUNET_PQ_result_spec_uint32 ("purse_account_limit",
96 : &purse_account_limit),
97 46 : GNUNET_PQ_result_spec_auto_from_type ("master_sig",
98 : &master_sig),
99 : GNUNET_PQ_result_spec_end
100 : };
101 46 : if (GNUNET_OK !=
102 46 : GNUNET_PQ_extract_result (result,
103 : rs,
104 : i))
105 : {
106 0 : GNUNET_break (0);
107 0 : gctx->status = GNUNET_SYSERR;
108 0 : break;
109 : }
110 46 : gctx->cb (gctx->cb_cls,
111 : &fees,
112 : purse_timeout,
113 : history_expiration,
114 : purse_account_limit,
115 : start_date,
116 : end_date,
117 : &master_sig);
118 46 : GNUNET_PQ_cleanup_result (rs);
119 : }
120 41 : }
121 :
122 :
123 : enum GNUNET_DB_QueryStatus
124 41 : TALER_TALER_EXCHANGEDB_get_global_fees (struct
125 : TALER_EXCHANGEDB_PostgresContext *pg,
126 : TALER_EXCHANGEDB_GlobalFeeCallback cb,
127 : void *cb_cls)
128 : {
129 : struct GNUNET_TIME_Timestamp date
130 41 : = GNUNET_TIME_absolute_to_timestamp (
131 : GNUNET_TIME_absolute_subtract (
132 : GNUNET_TIME_absolute_get (),
133 : GNUNET_TIME_UNIT_YEARS));
134 41 : struct GNUNET_PQ_QueryParam params[] = {
135 41 : GNUNET_PQ_query_param_timestamp (&date),
136 : GNUNET_PQ_query_param_end
137 : };
138 41 : struct GlobalFeeContext gctx = {
139 : .cb = cb,
140 : .cb_cls = cb_cls,
141 : .pg = pg,
142 : .status = GNUNET_OK
143 : };
144 :
145 41 : PREPARE (pg,
146 : "get_global_fees",
147 : "SELECT "
148 : " start_date"
149 : ",end_date"
150 : ",history_fee"
151 : ",account_fee"
152 : ",purse_fee"
153 : ",purse_timeout"
154 : ",history_expiration"
155 : ",purse_account_limit"
156 : ",master_sig"
157 : " FROM global_fee"
158 : " WHERE start_date >= $1");
159 41 : return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
160 : "get_global_fees",
161 : params,
162 : &global_fees_cb,
163 : &gctx);
164 : }
|