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