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_wire_fees.c
18 : * @brief Implementation of the get_wire_fees function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/get_wire_fees.h"
23 : #include "helper.h"
24 :
25 : /**
26 : * Closure for #get_wire_fees_cb().
27 : */
28 : struct GetWireFeesContext
29 : {
30 : /**
31 : * Function to call per result.
32 : */
33 : TALER_EXCHANGEDB_WireFeeCallback cb;
34 :
35 : /**
36 : * Closure for @e cb.
37 : */
38 : void *cb_cls;
39 :
40 : /**
41 : * Plugin context.
42 : */
43 : struct TALER_EXCHANGEDB_PostgresContext *pg;
44 :
45 : /**
46 : * Flag set to #GNUNET_OK as long as everything is fine.
47 : */
48 : enum GNUNET_GenericReturnValue status;
49 :
50 : };
51 :
52 :
53 : /**
54 : * Invoke the callback for each result.
55 : *
56 : * @param cls a `struct GetWireFeesContext *`
57 : * @param result SQL result
58 : * @param num_results number of rows in @a result
59 : */
60 : static void
61 19 : get_wire_fees_cb (void *cls,
62 : PGresult *result,
63 : unsigned int num_results)
64 : {
65 19 : struct GetWireFeesContext *ctx = cls;
66 19 : struct TALER_EXCHANGEDB_PostgresContext *pg = ctx->pg;
67 :
68 57 : for (unsigned int i = 0; i < num_results; i++)
69 : {
70 : struct TALER_MasterSignatureP master_sig;
71 : struct TALER_WireFeeSet fees;
72 : struct GNUNET_TIME_Timestamp start_date;
73 : struct GNUNET_TIME_Timestamp end_date;
74 38 : struct GNUNET_PQ_ResultSpec rs[] = {
75 38 : TALER_PQ_RESULT_SPEC_AMOUNT ("wire_fee",
76 : &fees.wire),
77 38 : TALER_PQ_RESULT_SPEC_AMOUNT ("closing_fee",
78 : &fees.closing),
79 38 : GNUNET_PQ_result_spec_timestamp ("start_date",
80 : &start_date),
81 38 : GNUNET_PQ_result_spec_timestamp ("end_date",
82 : &end_date),
83 38 : GNUNET_PQ_result_spec_auto_from_type ("master_sig",
84 : &master_sig),
85 : GNUNET_PQ_result_spec_end
86 : };
87 :
88 38 : if (GNUNET_OK !=
89 38 : GNUNET_PQ_extract_result (result,
90 : rs,
91 : i))
92 : {
93 0 : GNUNET_break (0);
94 0 : ctx->status = GNUNET_SYSERR;
95 0 : return;
96 : }
97 38 : ctx->cb (ctx->cb_cls,
98 : &fees,
99 : start_date,
100 : end_date,
101 : &master_sig);
102 38 : GNUNET_PQ_cleanup_result (rs);
103 : }
104 : }
105 :
106 :
107 : enum GNUNET_DB_QueryStatus
108 19 : TALER_TALER_EXCHANGEDB_get_wire_fees (struct TALER_EXCHANGEDB_PostgresContext *
109 : pg,
110 : const char *wire_method,
111 : TALER_EXCHANGEDB_WireFeeCallback cb,
112 : void *cb_cls)
113 : {
114 19 : struct GNUNET_PQ_QueryParam params[] = {
115 19 : GNUNET_PQ_query_param_string (wire_method),
116 : GNUNET_PQ_query_param_end
117 : };
118 19 : struct GetWireFeesContext ctx = {
119 : .cb = cb,
120 : .cb_cls = cb_cls,
121 : .pg = pg,
122 : .status = GNUNET_OK
123 : };
124 : enum GNUNET_DB_QueryStatus qs;
125 :
126 19 : PREPARE (pg,
127 : "get_wire_fees",
128 : "SELECT"
129 : " wire_fee"
130 : ",closing_fee"
131 : ",start_date"
132 : ",end_date"
133 : ",master_sig"
134 : " FROM wire_fee"
135 : " WHERE wire_method=$1");
136 19 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
137 : "get_wire_fees",
138 : params,
139 : &get_wire_fees_cb,
140 : &ctx);
141 19 : if (GNUNET_OK != ctx.status)
142 0 : return GNUNET_DB_STATUS_HARD_ERROR;
143 19 : return qs;
144 : }
|