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