Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023, 2024 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_by_order.c
18 : * @brief Implementation of the iterate_deposits_by_order 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_by_order.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for lookup_deposits_by_order_cb().
28 : */
29 : struct LookupDepositsByOrderContext
30 : {
31 :
32 : /**
33 : * Plugin context.
34 : */
35 : struct TALER_MERCHANTDB_PostgresContext *pg;
36 :
37 : /**
38 : * Function to call with all results.
39 : */
40 : TALER_MERCHANTDB_DepositedCoinsCallback cb;
41 :
42 : /**
43 : * Closure for @e cb.
44 : */
45 : void *cb_cls;
46 :
47 : /**
48 : * Set to the query result.
49 : */
50 : enum GNUNET_DB_QueryStatus qs;
51 : };
52 :
53 :
54 : /**
55 : * Function 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 LookupDepositsByOrderContext *`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 61 : lookup_deposits_by_order_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 61 : struct LookupDepositsByOrderContext *ldoc = cls;
68 :
69 122 : for (unsigned int i = 0; i<num_results; i++)
70 : {
71 : uint64_t deposit_serial;
72 : char *exchange_url;
73 : struct TALER_MerchantWireHashP h_wire;
74 : struct TALER_CoinSpendPublicKeyP coin_pub;
75 : struct GNUNET_TIME_Timestamp deposit_timestamp;
76 : struct TALER_Amount amount_with_fee;
77 : struct TALER_Amount deposit_fee;
78 61 : struct GNUNET_PQ_ResultSpec rs[] = {
79 61 : GNUNET_PQ_result_spec_uint64 ("deposit_serial",
80 : &deposit_serial),
81 61 : GNUNET_PQ_result_spec_string ("exchange_url",
82 : &exchange_url),
83 61 : GNUNET_PQ_result_spec_timestamp ("deposit_timestamp",
84 : &deposit_timestamp),
85 61 : GNUNET_PQ_result_spec_auto_from_type ("h_wire",
86 : &h_wire),
87 61 : TALER_PQ_result_spec_amount_with_currency ("amount_with_fee",
88 : &amount_with_fee),
89 61 : TALER_PQ_result_spec_amount_with_currency ("deposit_fee",
90 : &deposit_fee),
91 61 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
92 : &coin_pub),
93 : GNUNET_PQ_result_spec_end
94 : };
95 :
96 61 : if (GNUNET_OK !=
97 61 : GNUNET_PQ_extract_result (result,
98 : rs,
99 : i))
100 : {
101 0 : GNUNET_break (0);
102 0 : ldoc->qs = GNUNET_DB_STATUS_HARD_ERROR;
103 0 : return;
104 : }
105 61 : ldoc->cb (ldoc->cb_cls,
106 : deposit_serial,
107 : exchange_url,
108 : &h_wire,
109 : deposit_timestamp,
110 : &amount_with_fee,
111 : &deposit_fee,
112 : &coin_pub);
113 61 : GNUNET_PQ_cleanup_result (rs); /* technically useless here */
114 : }
115 61 : ldoc->qs = num_results;
116 : }
117 :
118 :
119 : enum GNUNET_DB_QueryStatus
120 61 : TALER_MERCHANTDB_iterate_deposits_by_order (
121 : struct TALER_MERCHANTDB_PostgresContext *pg,
122 : uint64_t order_serial,
123 : TALER_MERCHANTDB_DepositedCoinsCallback cb,
124 : void *cb_cls)
125 : {
126 61 : struct LookupDepositsByOrderContext ldoc = {
127 : .pg = pg,
128 : .cb = cb,
129 : .cb_cls = cb_cls
130 : };
131 61 : struct GNUNET_PQ_QueryParam params[] = {
132 61 : GNUNET_PQ_query_param_uint64 (&order_serial),
133 : GNUNET_PQ_query_param_end
134 : };
135 : enum GNUNET_DB_QueryStatus qs;
136 :
137 61 : GNUNET_assert (NULL != pg->current_merchant_id);
138 61 : TMH_PQ_prepare_anon (pg,
139 : "SELECT"
140 : " dep.deposit_serial"
141 : ",mcon.exchange_url"
142 : ",acc.h_wire"
143 : ",mcon.deposit_timestamp"
144 : ",dep.amount_with_fee"
145 : ",dep.deposit_fee"
146 : ",dep.coin_pub"
147 : " FROM merchant_deposits dep"
148 : " JOIN merchant_deposit_confirmations mcon"
149 : " USING(deposit_confirmation_serial)"
150 : " JOIN merchant_accounts acc"
151 : " USING (account_serial)"
152 : " WHERE mcon.order_serial=$1");
153 61 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
154 : "",
155 : params,
156 : &lookup_deposits_by_order_cb,
157 : &ldoc);
158 61 : if (qs < 0)
159 0 : return qs;
160 61 : return ldoc.qs;
161 : }
|