pg_lookup_deposits.c (5109B)
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 backenddb/pg_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 "pg_lookup_deposits.h" 26 #include "pg_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 PostgresClosure *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 TMH_PG_lookup_deposits ( 114 void *cls, 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 PostgresClosure *pg = cls; 121 struct GNUNET_PQ_QueryParam params[] = { 122 GNUNET_PQ_query_param_string (instance_id), 123 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 124 GNUNET_PQ_query_param_end 125 }; 126 struct LookupDepositsContext ldc = { 127 .cb = cb, 128 .cb_cls = cb_cls, 129 .pg = pg 130 }; 131 enum GNUNET_DB_QueryStatus qs; 132 133 /* no preflight check here, run in its own transaction by the caller! */ 134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 135 "Finding deposits for h_contract_terms '%s'\n", 136 GNUNET_h2s (&h_contract_terms->hash)); 137 check_connection (pg); 138 PREPARE (pg, 139 "lookup_deposits", 140 "SELECT" 141 " dcom.exchange_url" 142 ",dep.coin_pub" 143 ",dep.amount_with_fee" 144 ",dep.deposit_fee" 145 ",dep.refund_fee" 146 " FROM merchant_deposits dep" 147 " JOIN merchant_deposit_confirmations dcom" 148 " USING (deposit_confirmation_serial)" 149 " WHERE dcom.order_serial=" 150 " (SELECT order_serial" 151 " FROM merchant_contract_terms" 152 " WHERE h_contract_terms=$2" 153 " AND merchant_serial=" 154 " (SELECT merchant_serial" 155 " FROM merchant_instances" 156 " WHERE merchant_id=$1))"); 157 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 158 "lookup_deposits", 159 params, 160 &lookup_deposits_cb, 161 &ldc); 162 if (qs <= 0) 163 return qs; 164 return ldc.qs; 165 }