Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-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 exchangedb/select_refunds_by_coin.c
18 : * @brief Implementation of the select_refunds_by_coin function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_refunds_by_coin.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #get_refunds_cb().
28 : */
29 : struct SelectRefundContext
30 : {
31 : /**
32 : * Function to call on each result.
33 : */
34 : TALER_EXCHANGEDB_RefundCoinCallback cb;
35 :
36 : /**
37 : * Closure for @a cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_EXCHANGEDB_PostgresContext *pg;
45 :
46 : /**
47 : * Set to #GNUNET_SYSERR on error.
48 : */
49 : enum GNUNET_GenericReturnValue status;
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 SelectRefundContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 17 : get_refunds_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 17 : struct SelectRefundContext *srctx = cls;
67 17 : struct TALER_EXCHANGEDB_PostgresContext *pg = srctx->pg;
68 :
69 17 : for (unsigned int i = 0; i<num_results; i++)
70 : {
71 : struct TALER_Amount amount_with_fee;
72 0 : struct GNUNET_PQ_ResultSpec rs[] = {
73 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
74 : &amount_with_fee),
75 : GNUNET_PQ_result_spec_end
76 : };
77 :
78 0 : if (GNUNET_OK !=
79 0 : GNUNET_PQ_extract_result (result,
80 : rs,
81 : i))
82 : {
83 0 : GNUNET_break (0);
84 0 : srctx->status = GNUNET_SYSERR;
85 0 : return;
86 : }
87 0 : if (GNUNET_OK !=
88 0 : srctx->cb (srctx->cb_cls,
89 : &amount_with_fee))
90 0 : return;
91 : }
92 : }
93 :
94 :
95 : enum GNUNET_DB_QueryStatus
96 17 : TALER_EXCHANGEDB_select_refunds_by_coin (
97 : struct TALER_EXCHANGEDB_PostgresContext *pg,
98 : const struct TALER_CoinSpendPublicKeyP *coin_pub,
99 : const struct TALER_MerchantPublicKeyP *merchant_pub,
100 : const struct TALER_PrivateContractHashP *h_contract,
101 : TALER_EXCHANGEDB_RefundCoinCallback cb,
102 : void *cb_cls)
103 : {
104 : enum GNUNET_DB_QueryStatus qs;
105 17 : struct GNUNET_PQ_QueryParam params[] = {
106 17 : GNUNET_PQ_query_param_auto_from_type (coin_pub),
107 17 : GNUNET_PQ_query_param_auto_from_type (merchant_pub),
108 17 : GNUNET_PQ_query_param_auto_from_type (h_contract),
109 : GNUNET_PQ_query_param_end
110 : };
111 17 : struct SelectRefundContext srctx = {
112 : .cb = cb,
113 : .cb_cls = cb_cls,
114 : .pg = pg,
115 : .status = GNUNET_OK
116 : };
117 17 : const char *query = "get_refunds_by_coin_and_contract";
118 :
119 17 : PREPARE (pg,
120 : query,
121 : "SELECT"
122 : " ref.amount_with_fee"
123 : " FROM refunds ref"
124 : " JOIN coin_deposits cdep"
125 : " USING (coin_pub,batch_deposit_serial_id)"
126 : " JOIN batch_deposits bdep"
127 : " ON (ref.batch_deposit_serial_id = bdep.batch_deposit_serial_id)"
128 : " WHERE ref.coin_pub=$1"
129 : " AND bdep.merchant_pub=$2"
130 : " AND bdep.h_contract_terms=$3;");
131 17 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
132 : query,
133 : params,
134 : &get_refunds_cb,
135 : &srctx);
136 17 : if (GNUNET_SYSERR == srctx.status)
137 0 : return GNUNET_DB_STATUS_HARD_ERROR;
138 17 : return qs;
139 : }
|