Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023, 2025 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_transfer_summaries.c
18 : * @brief Implementation of the iterate_transfer_summaries 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_transfer_summaries.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #lookup_transfer_summary_cb().
28 : */
29 : struct LookupTransferSummaryContext
30 : {
31 : /**
32 : * Function to call for each order that was aggregated.
33 : */
34 : TALER_MERCHANTDB_TransferSummaryCallback cb;
35 :
36 : /**
37 : * Closure for @e cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_MERCHANTDB_PostgresContext *pg;
45 :
46 : /**
47 : * Transaction result.
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 cls of type `struct LookupTransferSummaryContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 1 : lookup_transfer_summary_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 1 : struct LookupTransferSummaryContext *ltdc = cls;
67 :
68 2 : for (unsigned int i = 0; i<num_results; i++)
69 : {
70 : char *order_id;
71 : struct TALER_Amount deposit_value;
72 : struct TALER_Amount deposit_fee;
73 1 : struct GNUNET_PQ_ResultSpec rs[] = {
74 1 : GNUNET_PQ_result_spec_string ("order_id",
75 : &order_id),
76 1 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value",
77 : &deposit_value),
78 1 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee",
79 : &deposit_fee),
80 : GNUNET_PQ_result_spec_end
81 : };
82 :
83 1 : if (GNUNET_OK !=
84 1 : GNUNET_PQ_extract_result (result,
85 : rs,
86 : i))
87 : {
88 0 : GNUNET_break (0);
89 0 : ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR;
90 0 : return;
91 : }
92 1 : ltdc->cb (ltdc->cb_cls,
93 : order_id,
94 : &deposit_value,
95 : &deposit_fee);
96 1 : GNUNET_PQ_cleanup_result (rs);
97 : }
98 1 : ltdc->qs = num_results;
99 : }
100 :
101 :
102 : enum GNUNET_DB_QueryStatus
103 1 : TALER_MERCHANTDB_iterate_transfer_summaries (
104 : struct TALER_MERCHANTDB_PostgresContext *pg,
105 : const char *exchange_url,
106 : const struct TALER_WireTransferIdentifierRawP *wtid,
107 : TALER_MERCHANTDB_TransferSummaryCallback cb,
108 : void *cb_cls)
109 : {
110 1 : struct GNUNET_PQ_QueryParam params[] = {
111 1 : GNUNET_PQ_query_param_string (exchange_url),
112 1 : GNUNET_PQ_query_param_auto_from_type (wtid),
113 : GNUNET_PQ_query_param_end
114 : };
115 1 : struct LookupTransferSummaryContext ltdc = {
116 : .cb = cb,
117 : .cb_cls = cb_cls,
118 : .pg = pg
119 : };
120 : enum GNUNET_DB_QueryStatus qs;
121 :
122 1 : GNUNET_assert (NULL != pg->current_merchant_id);
123 1 : TMH_PQ_prepare_anon (pg,
124 : "SELECT"
125 : " mct.order_id"
126 : ",mtc.exchange_deposit_value"
127 : ",mtc.exchange_deposit_fee"
128 : " FROM merchant_expected_transfers met"
129 : " JOIN merchant_expected_transfer_to_coin mtc"
130 : " USING (expected_credit_serial)"
131 : " JOIN merchant_deposits dep"
132 : " USING (deposit_serial)"
133 : " JOIN merchant_deposit_confirmations mcon"
134 : " USING (deposit_confirmation_serial)"
135 : " JOIN merchant_contract_terms mct"
136 : " USING (order_serial)"
137 : " WHERE met.wtid=$2"
138 : " AND met.exchange_url=$1");
139 :
140 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
141 : pg->conn,
142 : "",
143 : params,
144 : &lookup_transfer_summary_cb,
145 : <dc);
146 1 : if (0 >= qs)
147 0 : return qs;
148 1 : return ltdc.qs;
149 : }
|