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 src/backenddb/iterate_deposits.c
18 : * @brief Implementation of the iterate_deposits function for Postgres
19 : * @author Iván Ávalos
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_deposits.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #lookup_deposits_cb().
28 : */
29 : struct LookupDepositsContext
30 : {
31 : /**
32 : * Function to call with results.
33 : */
34 : TALER_MERCHANTDB_DepositsCallback cb;
35 :
36 : /**
37 : * Closure for @e cls.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_MERCHANTDB_PostgresContext *pg;
45 :
46 : /**
47 : * Transaction status (set).
48 : */
49 : enum GNUNET_DB_QueryStatus qs;
50 : };
51 :
52 :
53 : /**
54 : * Function to be called with the results of a SELECT statement
55 : * that has returned @a num_results results.
56 : *
57 : * @param[in,out] cls of type `struct LookupDepositsContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 100 : lookup_deposits_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 100 : struct LookupDepositsContext *ldc = cls;
67 :
68 155 : for (unsigned int i = 0; i<num_results; i++)
69 : {
70 : struct TALER_CoinSpendPublicKeyP coin_pub;
71 : struct TALER_Amount amount_with_fee;
72 : struct TALER_Amount deposit_fee;
73 : struct TALER_Amount refund_fee;
74 : struct TALER_Amount wire_fee;
75 : char *exchange_url;
76 55 : struct GNUNET_PQ_ResultSpec rs[] = {
77 55 : GNUNET_PQ_result_spec_string ("exchange_url",
78 : &exchange_url),
79 55 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
80 : &coin_pub),
81 55 : TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
82 : &amount_with_fee),
83 55 : TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
84 : &deposit_fee),
85 55 : TALER_PQ_result_spec_amount_with_currency ("refund_fee",
86 : &refund_fee),
87 55 : TALER_PQ_result_spec_amount_with_currency ("wire_fee",
88 : &wire_fee),
89 : GNUNET_PQ_result_spec_end
90 : };
91 :
92 55 : if (GNUNET_OK !=
93 55 : GNUNET_PQ_extract_result (result,
94 : rs,
95 : i))
96 : {
97 0 : GNUNET_break (0);
98 0 : ldc->qs = GNUNET_DB_STATUS_HARD_ERROR;
99 0 : return;
100 : }
101 55 : ldc->cb (ldc->cb_cls,
102 : exchange_url,
103 : &coin_pub,
104 : &amount_with_fee,
105 : &deposit_fee,
106 : &refund_fee,
107 : &wire_fee);
108 55 : GNUNET_PQ_cleanup_result (rs);
109 : }
110 100 : ldc->qs = num_results;
111 : }
112 :
113 :
114 : enum GNUNET_DB_QueryStatus
115 100 : TALER_MERCHANTDB_iterate_deposits (
116 : struct TALER_MERCHANTDB_PostgresContext *pg,
117 : const char *instance_id,
118 : const struct TALER_PrivateContractHashP *h_contract_terms,
119 : TALER_MERCHANTDB_DepositsCallback cb,
120 : void *cb_cls)
121 : {
122 100 : struct GNUNET_PQ_QueryParam params[] = {
123 100 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
124 : GNUNET_PQ_query_param_end
125 : };
126 100 : struct LookupDepositsContext ldc = {
127 : .cb = cb,
128 : .cb_cls = cb_cls,
129 : .pg = pg
130 : };
131 : enum GNUNET_DB_QueryStatus qs;
132 :
133 100 : GNUNET_assert (NULL != pg->current_merchant_id);
134 100 : GNUNET_assert (0 == strcmp (instance_id,
135 : pg->current_merchant_id));
136 : /* no preflight check here, run in its own transaction by the caller! */
137 100 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 : "Finding deposits for h_contract_terms '%s'\n",
139 : GNUNET_h2s (&h_contract_terms->hash));
140 100 : TMH_PQ_prepare_anon (pg,
141 : "SELECT"
142 : " dcom.exchange_url"
143 : ",dcom.wire_fee"
144 : ",dep.coin_pub"
145 : ",dep.amount_with_fee"
146 : ",dep.deposit_fee"
147 : ",dep.refund_fee"
148 : " FROM merchant_deposits dep"
149 : " JOIN merchant_deposit_confirmations dcom"
150 : " USING (deposit_confirmation_serial)"
151 : " WHERE dcom.order_serial="
152 : " (SELECT order_serial"
153 : " FROM merchant_contract_terms"
154 : " WHERE h_contract_terms=$1)");
155 100 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
156 : "",
157 : params,
158 : &lookup_deposits_cb,
159 : &ldc);
160 100 : if (qs <= 0)
161 52 : return qs;
162 48 : return ldc.qs;
163 : }
|