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_fee_by_time.c
18 : * @brief Implementation of the get_wire_fee_by_time function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/get_wire_fee_by_time.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #wire_fee_by_time_helper()
28 : */
29 : struct WireFeeLookupContext
30 : {
31 :
32 : /**
33 : * Set to the wire fees. Set to invalid if fees conflict over
34 : * the given time period.
35 : */
36 : struct TALER_WireFeeSet *fees;
37 :
38 : /**
39 : * Plugin context.
40 : */
41 : struct TALER_EXCHANGEDB_PostgresContext *pg;
42 : };
43 :
44 :
45 : /**
46 : * Helper function for #TALER_EXCHANGEDB_get_wire_fee_by_time().
47 : * Calls the callback with the wire fee structure.
48 : *
49 : * @param cls a `struct WireFeeLookupContext`
50 : * @param result db results
51 : * @param num_results number of results in @a result
52 : */
53 : static void
54 47 : wire_fee_by_time_helper (void *cls,
55 : PGresult *result,
56 : unsigned int num_results)
57 : {
58 47 : struct WireFeeLookupContext *wlc = cls;
59 47 : struct TALER_EXCHANGEDB_PostgresContext *pg = wlc->pg;
60 :
61 56 : for (unsigned int i = 0; i<num_results; i++)
62 : {
63 : struct TALER_WireFeeSet fs;
64 10 : struct GNUNET_PQ_ResultSpec rs[] = {
65 10 : TALER_PQ_RESULT_SPEC_AMOUNT ("wire_fee",
66 : &fs.wire),
67 10 : TALER_PQ_RESULT_SPEC_AMOUNT ("closing_fee",
68 : &fs.closing),
69 : GNUNET_PQ_result_spec_end
70 : };
71 :
72 10 : if (GNUNET_OK !=
73 10 : GNUNET_PQ_extract_result (result,
74 : rs,
75 : i))
76 : {
77 0 : GNUNET_break (0);
78 : /* invalidate */
79 0 : memset (wlc->fees,
80 : 0,
81 : sizeof (struct TALER_WireFeeSet));
82 1 : return;
83 : }
84 10 : if (0 == i)
85 : {
86 7 : *wlc->fees = fs;
87 7 : continue;
88 : }
89 3 : if (0 !=
90 3 : TALER_wire_fee_set_cmp (&fs,
91 3 : wlc->fees))
92 : {
93 : /* invalidate */
94 1 : memset (wlc->fees,
95 : 0,
96 : sizeof (struct TALER_WireFeeSet));
97 1 : return;
98 : }
99 : }
100 : }
101 :
102 :
103 : enum GNUNET_DB_QueryStatus
104 47 : TALER_EXCHANGEDB_get_wire_fee_by_time (
105 : struct TALER_EXCHANGEDB_PostgresContext *pg,
106 : const char *wire_method,
107 : struct GNUNET_TIME_Timestamp start_time,
108 : struct GNUNET_TIME_Timestamp end_time,
109 : struct TALER_WireFeeSet *fees)
110 : {
111 47 : struct GNUNET_PQ_QueryParam params[] = {
112 47 : GNUNET_PQ_query_param_string (wire_method),
113 47 : GNUNET_PQ_query_param_timestamp (&start_time),
114 47 : GNUNET_PQ_query_param_timestamp (&end_time),
115 : GNUNET_PQ_query_param_end
116 : };
117 47 : struct WireFeeLookupContext wlc = {
118 : .fees = fees,
119 : .pg = pg
120 : };
121 :
122 47 : PREPARE (pg,
123 : "get_wire_fee_by_time",
124 : "SELECT"
125 : " wire_fee"
126 : ",closing_fee"
127 : " FROM wire_fee"
128 : " WHERE wire_method=$1"
129 : " AND end_date > $2"
130 : " AND start_date < $3;");
131 47 : return GNUNET_PQ_eval_prepared_multi_select (pg->conn,
132 : "get_wire_fee_by_time",
133 : params,
134 : &wire_fee_by_time_helper,
135 : &wlc);
136 : }
|