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.c
18 : * @brief Implementation of the lookup_transfer_details 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.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Closure for #lookup_transfer_details_cb().
30 : */
31 : struct LookupTransferDetailsContext
32 : {
33 : /**
34 : * Function to call for each order that was aggregated.
35 : */
36 : TALER_MERCHANTDB_TransferDetailsCallback cb;
37 :
38 : /**
39 : * Closure for @e cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Plugin context.
45 : */
46 : struct PostgresClosure *pg;
47 :
48 : /**
49 : * Transaction result.
50 : */
51 : enum GNUNET_DB_QueryStatus qs;
52 : };
53 :
54 :
55 : /**
56 : * Function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results.
58 : *
59 : * @param cls of type `struct LookupTransferDetailsContext *`
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 0 : lookup_transfer_details_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 0 : struct LookupTransferDetailsContext *ltdc = cls;
69 :
70 0 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : uint64_t current_offset;
73 : struct TALER_TrackTransferDetails ttd;
74 0 : struct GNUNET_PQ_ResultSpec rs[] = {
75 0 : GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list",
76 : ¤t_offset),
77 0 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
78 : &ttd.h_contract_terms),
79 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
80 : &ttd.coin_pub),
81 0 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
82 : &ttd.coin_value),
83 0 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
84 : &ttd.coin_fee),
85 : GNUNET_PQ_result_spec_end
86 : };
87 :
88 0 : if (GNUNET_OK !=
89 0 : GNUNET_PQ_extract_result (result,
90 : rs,
91 : i))
92 : {
93 0 : GNUNET_break (0);
94 0 : ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
95 0 : return;
96 : }
97 0 : ltdc->cb (ltdc->cb_cls,
98 : (unsigned int) current_offset,
99 : &ttd);
100 0 : GNUNET_PQ_cleanup_result (rs);
101 : }
102 0 : ltdc->qs = num_results;
103 : }
104 :
105 : enum GNUNET_DB_QueryStatus
106 0 : TMH_PG_lookup_transfer_details (void *cls,
107 : const char *exchange_url,
108 : const struct TALER_WireTransferIdentifierRawP *wtid,
109 : TALER_MERCHANTDB_TransferDetailsCallback cb,
110 : void *cb_cls)
111 : {
112 0 : struct PostgresClosure *pg = cls;
113 0 : struct GNUNET_PQ_QueryParam params[] = {
114 0 : GNUNET_PQ_query_param_string (exchange_url),
115 0 : GNUNET_PQ_query_param_auto_from_type (wtid),
116 : GNUNET_PQ_query_param_end
117 : };
118 0 : struct LookupTransferDetailsContext ltdc = {
119 : .cb = cb,
120 : .cb_cls = cb_cls,
121 : .pg = pg
122 : };
123 : enum GNUNET_DB_QueryStatus qs;
124 :
125 0 : check_connection (pg);
126 0 : PREPARE (pg,
127 : "lookup_transfer_details",
128 : "SELECT"
129 : " mterm.h_contract_terms"
130 : ",mtcoin.offset_in_exchange_list"
131 : ",dep.coin_pub"
132 : ",mtcoin.exchange_deposit_value"
133 : ",mtcoin.exchange_deposit_fee"
134 : " FROM merchant_transfer_to_coin mtcoin"
135 : " JOIN merchant_deposits dep"
136 : " USING (deposit_serial)"
137 : " JOIN merchant_deposit_confirmations mcon"
138 : " USING (deposit_confirmation_serial)"
139 : " JOIN merchant_contract_terms mterm"
140 : " USING (order_serial)"
141 : " JOIN merchant_transfers mtr"
142 : " USING (credit_serial)"
143 : " WHERE mtr.wtid=$2"
144 : " AND mtr.exchange_url=$1");
145 :
146 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
147 : pg->conn,
148 : "lookup_transfer_details",
149 : params,
150 : &lookup_transfer_details_cb,
151 : <dc);
152 0 : if (0 >= qs)
153 0 : return qs;
154 0 : return ltdc.qs;
155 : }
|