Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022 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_refunds.c
18 : * @brief Implementation of the iterate_refunds 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_refunds.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Closure for #lookup_refunds_cb().
28 : */
29 : struct LookupRefundsContext
30 : {
31 : /**
32 : * Function to call for each refund.
33 : */
34 : TALER_MERCHANTDB_RefundCallback rc;
35 :
36 : /**
37 : * Closure for @e rc.
38 : */
39 : void *rc_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 closure of type `struct LookupRefundsContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 105 : lookup_refunds_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 105 : struct LookupRefundsContext *lrc = cls;
67 :
68 116 : for (unsigned int i = 0; i<num_results; i++)
69 : {
70 : struct TALER_CoinSpendPublicKeyP coin_pub;
71 : struct TALER_Amount refund_amount;
72 11 : struct GNUNET_PQ_ResultSpec rs[] = {
73 11 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
74 : &coin_pub),
75 11 : TALER_PQ_result_spec_amount_with_currency ("refund_amount",
76 : &refund_amount),
77 : GNUNET_PQ_result_spec_end
78 : };
79 :
80 11 : if (GNUNET_OK !=
81 11 : GNUNET_PQ_extract_result (result,
82 : rs,
83 : i))
84 : {
85 0 : GNUNET_break (0);
86 0 : lrc->qs = GNUNET_DB_STATUS_HARD_ERROR;
87 0 : return;
88 : }
89 11 : lrc->rc (lrc->rc_cls,
90 : &coin_pub,
91 : &refund_amount);
92 11 : GNUNET_PQ_cleanup_result (rs); /* technically useless here */
93 : }
94 105 : lrc->qs = num_results;
95 : }
96 :
97 :
98 : enum GNUNET_DB_QueryStatus
99 105 : TALER_MERCHANTDB_iterate_refunds (
100 : struct TALER_MERCHANTDB_PostgresContext *pg,
101 : const char *instance_id,
102 : const struct TALER_PrivateContractHashP *h_contract_terms,
103 : TALER_MERCHANTDB_RefundCallback rc,
104 : void *rc_cls)
105 : {
106 105 : struct GNUNET_PQ_QueryParam params[] = {
107 105 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
108 : GNUNET_PQ_query_param_end
109 : };
110 105 : struct LookupRefundsContext lrc = {
111 : .rc = rc,
112 : .rc_cls = rc_cls,
113 : .pg = pg
114 : };
115 : enum GNUNET_DB_QueryStatus qs;
116 :
117 105 : GNUNET_assert (NULL != pg->current_merchant_id);
118 105 : GNUNET_assert (0 == strcmp (instance_id,
119 : pg->current_merchant_id));
120 : /* no preflight check here, run in transaction by caller! */
121 105 : TALER_LOG_DEBUG ("Looking for refund of h_contract_terms %s at `%s'\n",
122 : GNUNET_h2s (&h_contract_terms->hash),
123 : instance_id);
124 105 : TMH_PQ_prepare_anon (pg,
125 : "SELECT"
126 : " coin_pub"
127 : ",refund_amount"
128 : " FROM merchant_refunds"
129 : " WHERE order_serial="
130 : " (SELECT order_serial"
131 : " FROM merchant_contract_terms"
132 : " WHERE h_contract_terms=$1)");
133 105 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
134 : "",
135 : params,
136 : &lookup_refunds_cb,
137 : &lrc);
138 105 : if (0 >= qs)
139 98 : return qs;
140 7 : return lrc.qs;
141 : }
|