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