Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 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_transfer_details_by_order.c
18 : * @brief Implementation of the lookup_transfer_details_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_transfer_details_by_order.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Closure for lookup_transfer_details_by_order_cb().
30 : */
31 : struct LookupTransferDetailsByOrderContext
32 : {
33 :
34 : /**
35 : * Plugin context.
36 : */
37 : struct PostgresClosure *pg;
38 :
39 : /**
40 : * Function to call with all results.
41 : */
42 : TALER_MERCHANTDB_OrderTransferDetailsCallback 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 LookupTransferDetailsByOrderContext *`
61 : * @param result the postgres result
62 : * @param num_results the number of results in @a result
63 : */
64 : static void
65 34 : lookup_transfer_details_by_order_cb (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 34 : struct LookupTransferDetailsByOrderContext *ltdo = cls;
70 :
71 47 : for (unsigned int i = 0; i<num_results; i++)
72 : {
73 : struct TALER_WireTransferIdentifierRawP wtid;
74 : char *exchange_url;
75 : uint64_t deposit_serial;
76 : struct GNUNET_TIME_Timestamp execution_time;
77 : struct TALER_Amount deposit_value;
78 : struct TALER_Amount deposit_fee;
79 : uint8_t transfer_confirmed;
80 13 : struct GNUNET_PQ_ResultSpec rs[] = {
81 13 : GNUNET_PQ_result_spec_uint64 ("deposit_serial",
82 : &deposit_serial),
83 13 : GNUNET_PQ_result_spec_timestamp ("deposit_timestamp",
84 : &execution_time),
85 13 : GNUNET_PQ_result_spec_string ("exchange_url",
86 : &exchange_url),
87 13 : GNUNET_PQ_result_spec_auto_from_type ("wtid",
88 : &wtid),
89 13 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
90 : &deposit_value),
91 13 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
92 : &deposit_fee),
93 13 : GNUNET_PQ_result_spec_auto_from_type ("transfer_confirmed",
94 : &transfer_confirmed),
95 : GNUNET_PQ_result_spec_end
96 : };
97 :
98 13 : if (GNUNET_OK !=
99 13 : GNUNET_PQ_extract_result (result,
100 : rs,
101 : i))
102 : {
103 0 : GNUNET_break (0);
104 0 : ltdo->qs = GNUNET_DB_STATUS_HARD_ERROR;
105 0 : return;
106 : }
107 13 : ltdo->cb (ltdo->cb_cls,
108 : &wtid,
109 : exchange_url,
110 : execution_time,
111 : &deposit_value,
112 : &deposit_fee,
113 : (0 != transfer_confirmed));
114 13 : GNUNET_PQ_cleanup_result (rs); /* technically useless here */
115 : }
116 34 : ltdo->qs = num_results;
117 : }
118 :
119 :
120 : enum GNUNET_DB_QueryStatus
121 34 : TMH_PG_lookup_transfer_details_by_order (void *cls,
122 : uint64_t order_serial,
123 : TALER_MERCHANTDB_OrderTransferDetailsCallback cb,
124 : void *cb_cls)
125 : {
126 34 : struct PostgresClosure *pg = cls;
127 34 : struct LookupTransferDetailsByOrderContext ltdo = {
128 : .pg = pg,
129 : .cb = cb,
130 : .cb_cls = cb_cls
131 : };
132 34 : struct GNUNET_PQ_QueryParam params[] = {
133 34 : GNUNET_PQ_query_param_uint64 (&order_serial),
134 : GNUNET_PQ_query_param_end
135 : };
136 : enum GNUNET_DB_QueryStatus qs;
137 :
138 34 : check_connection (pg);
139 34 : PREPARE (pg,
140 : "lookup_transfer_details_by_order",
141 : "SELECT"
142 : " md.deposit_serial"
143 : ",mcon.exchange_url"
144 : ",mt.wtid"
145 : ",mtc.exchange_deposit_value"
146 : ",mtc.exchange_deposit_fee"
147 : ",mcon.deposit_timestamp"
148 : ",mt.confirmed AS transfer_confirmed"
149 : " FROM merchant_transfer_to_coin mtc"
150 : " JOIN merchant_deposits md"
151 : " USING (deposit_serial)"
152 : " JOIN merchant_deposit_confirmations mcon"
153 : " USING (deposit_confirmation_serial)"
154 : " JOIN merchant_transfers mt"
155 : " USING (credit_serial)"
156 : " JOIN merchant_accounts acc"
157 : " ON (acc.account_serial = mt.account_serial)"
158 : /* Check that all this is for the same instance */
159 : " JOIN merchant_contract_terms contracts"
160 : " USING (merchant_serial, order_serial)"
161 : " WHERE mcon.order_serial=$1");
162 :
163 34 : qs = GNUNET_PQ_eval_prepared_multi_select (
164 : pg->conn,
165 : "lookup_transfer_details_by_order",
166 : params,
167 : &lookup_transfer_details_by_order_cb,
168 : <do);
169 34 : if (qs < 0)
170 0 : return qs;
171 34 : return ltdo.qs;
172 : }
|