lookup_deposits.c (5147B)
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/lookup_deposits.c 18 * @brief Implementation of the lookup_deposits function for Postgres 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <taler/taler_error_codes.h> 23 #include <taler/taler_dbevents.h> 24 #include <taler/taler_pq_lib.h> 25 #include "merchant-database/lookup_deposits.h" 26 #include "helper.h" 27 28 /** 29 * Closure for #lookup_deposits_cb(). 30 */ 31 struct LookupDepositsContext 32 { 33 /** 34 * Function to call with results. 35 */ 36 TALER_MERCHANTDB_DepositsCallback cb; 37 38 /** 39 * Closure for @e cls. 40 */ 41 void *cb_cls; 42 43 /** 44 * Plugin context. 45 */ 46 struct TALER_MERCHANTDB_PostgresContext *pg; 47 48 /** 49 * Transaction status (set). 50 */ 51 enum GNUNET_DB_QueryStatus qs; 52 }; 53 54 55 /** 56 * Function to be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param[in,out] cls of type `struct LookupDepositsContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 lookup_deposits_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct LookupDepositsContext *ldc = cls; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 struct TALER_CoinSpendPublicKeyP coin_pub; 73 struct TALER_Amount amount_with_fee; 74 struct TALER_Amount deposit_fee; 75 struct TALER_Amount refund_fee; 76 char *exchange_url; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_string ("exchange_url", 79 &exchange_url), 80 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 81 &coin_pub), 82 TALER_PQ_result_spec_amount_with_currency ("amount_with_fee", 83 &amount_with_fee), 84 TALER_PQ_result_spec_amount_with_currency ("deposit_fee", 85 &deposit_fee), 86 TALER_PQ_result_spec_amount_with_currency ("refund_fee", 87 &refund_fee), 88 GNUNET_PQ_result_spec_end 89 }; 90 91 if (GNUNET_OK != 92 GNUNET_PQ_extract_result (result, 93 rs, 94 i)) 95 { 96 GNUNET_break (0); 97 ldc->qs = GNUNET_DB_STATUS_HARD_ERROR; 98 return; 99 } 100 ldc->cb (ldc->cb_cls, 101 exchange_url, 102 &coin_pub, 103 &amount_with_fee, 104 &deposit_fee, 105 &refund_fee); 106 GNUNET_PQ_cleanup_result (rs); 107 } 108 ldc->qs = num_results; 109 } 110 111 112 enum GNUNET_DB_QueryStatus 113 TALER_MERCHANTDB_lookup_deposits ( 114 struct TALER_MERCHANTDB_PostgresContext *pg, 115 const char *instance_id, 116 const struct TALER_PrivateContractHashP *h_contract_terms, 117 TALER_MERCHANTDB_DepositsCallback cb, 118 void *cb_cls) 119 { 120 struct GNUNET_PQ_QueryParam params[] = { 121 GNUNET_PQ_query_param_string (instance_id), 122 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 123 GNUNET_PQ_query_param_end 124 }; 125 struct LookupDepositsContext ldc = { 126 .cb = cb, 127 .cb_cls = cb_cls, 128 .pg = pg 129 }; 130 enum GNUNET_DB_QueryStatus qs; 131 132 /* no preflight check here, run in its own transaction by the caller! */ 133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 134 "Finding deposits for h_contract_terms '%s'\n", 135 GNUNET_h2s (&h_contract_terms->hash)); 136 check_connection (pg); 137 PREPARE (pg, 138 "lookup_deposits", 139 "SELECT" 140 " dcom.exchange_url" 141 ",dep.coin_pub" 142 ",dep.amount_with_fee" 143 ",dep.deposit_fee" 144 ",dep.refund_fee" 145 " FROM merchant_deposits dep" 146 " JOIN merchant_deposit_confirmations dcom" 147 " USING (deposit_confirmation_serial)" 148 " WHERE dcom.order_serial=" 149 " (SELECT order_serial" 150 " FROM merchant_contract_terms" 151 " WHERE h_contract_terms=$2" 152 " AND merchant_serial=" 153 " (SELECT merchant_serial" 154 " FROM merchant_instances" 155 " WHERE merchant_id=$1))"); 156 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 157 "lookup_deposits", 158 params, 159 &lookup_deposits_cb, 160 &ldc); 161 if (qs <= 0) 162 return qs; 163 return ldc.qs; 164 }