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 exchangedb/select_purse_deposits_by_purse.c
18 : * @brief Implementation of the select_purse_deposits_by_purse function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_purse_deposits_by_purse.h"
23 : #include "helper.h"
24 :
25 : /**
26 : * Closure for #purse_refund_coin_helper_cb().
27 : */
28 : struct PurseRefundCoinContext
29 : {
30 :
31 : /**
32 : * Callback to call.
33 : */
34 : TALER_EXCHANGEDB_PurseRefundCoinCallback cb;
35 :
36 : /**
37 : * Closure for @e cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Plugin context.
43 : */
44 : struct TALER_EXCHANGEDB_PostgresContext *pg;
45 :
46 : /**
47 : * Status code, set to #GNUNET_SYSERR on hard errors.
48 : */
49 : enum GNUNET_GenericReturnValue status;
50 : };
51 :
52 :
53 : /**
54 : * Helper 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 PurseRefundCoinContext`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 0 : purse_refund_coin_helper_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 0 : struct PurseRefundCoinContext *dsc = cls;
67 0 : struct TALER_EXCHANGEDB_PostgresContext *pg = dsc->pg;
68 :
69 0 : for (unsigned int i = 0; i<num_results; i++)
70 : {
71 : struct TALER_Amount amount_with_fee;
72 : struct TALER_CoinSpendPublicKeyP coin_pub;
73 : struct TALER_DenominationPublicKey denom_pub;
74 : uint64_t rowid;
75 0 : struct GNUNET_PQ_ResultSpec rs[] = {
76 0 : TALER_PQ_result_spec_denom_pub ("denom_pub",
77 : &denom_pub),
78 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
79 : &amount_with_fee),
80 0 : GNUNET_PQ_result_spec_auto_from_type ("coin_pub",
81 : &coin_pub),
82 0 : GNUNET_PQ_result_spec_uint64 ("purse_deposit_serial_id",
83 : &rowid),
84 : GNUNET_PQ_result_spec_end
85 : };
86 : enum GNUNET_GenericReturnValue ret;
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 : dsc->status = GNUNET_SYSERR;
95 0 : return;
96 : }
97 0 : ret = dsc->cb (dsc->cb_cls,
98 : rowid,
99 : &amount_with_fee,
100 : &coin_pub,
101 : &denom_pub);
102 0 : GNUNET_PQ_cleanup_result (rs);
103 0 : if (GNUNET_OK != ret)
104 0 : break;
105 : }
106 : }
107 :
108 :
109 : enum GNUNET_DB_QueryStatus
110 0 : TALER_TALER_EXCHANGEDB_select_purse_deposits_by_purse (
111 : struct TALER_EXCHANGEDB_PostgresContext *pg,
112 : const struct TALER_PurseContractPublicKeyP *purse_pub,
113 : TALER_EXCHANGEDB_PurseRefundCoinCallback cb,
114 : void *cb_cls)
115 : {
116 0 : struct GNUNET_PQ_QueryParam params[] = {
117 0 : GNUNET_PQ_query_param_auto_from_type (purse_pub),
118 : GNUNET_PQ_query_param_end
119 : };
120 0 : struct PurseRefundCoinContext dsc = {
121 : .cb = cb,
122 : .cb_cls = cb_cls,
123 : .pg = pg,
124 : .status = GNUNET_OK
125 : };
126 : enum GNUNET_DB_QueryStatus qs;
127 :
128 0 : PREPARE (pg,
129 : "audit_get_purse_deposits_by_purse",
130 : "SELECT"
131 : " pd.purse_deposit_serial_id"
132 : ",pd.amount_with_fee"
133 : ",pd.coin_pub"
134 : ",denom.denom_pub"
135 : " FROM purse_deposits pd"
136 : " JOIN known_coins kc"
137 : " USING (coin_pub)"
138 : " JOIN denominations denom"
139 : " USING (denominations_serial)"
140 : " WHERE purse_pub=$1;");
141 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
142 : "audit_get_purse_deposits_by_purse",
143 : params,
144 : &purse_refund_coin_helper_cb,
145 : &dsc);
146 0 : if (GNUNET_OK != dsc.status)
147 0 : return GNUNET_DB_STATUS_HARD_ERROR;
148 0 : return qs;
149 : }
|