lookup_transfer_details.c (4781B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 src/backenddb/lookup_transfer_details.c 18 * @brief Implementation of the lookup_transfer_details 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_transfer_details.h" 26 #include "helper.h" 27 28 /** 29 * Closure for #lookup_transfer_details_cb(). 30 */ 31 struct LookupTransferDetailsContext 32 { 33 /** 34 * Function to call for each order that was aggregated. 35 */ 36 TALER_MERCHANTDB_TransferDetailsCallback cb; 37 38 /** 39 * Closure for @e cb. 40 */ 41 void *cb_cls; 42 43 /** 44 * Plugin context. 45 */ 46 struct TALER_MERCHANTDB_PostgresContext *pg; 47 48 /** 49 * Transaction result. 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 cls of type `struct LookupTransferDetailsContext *` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 lookup_transfer_details_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct LookupTransferDetailsContext *ltdc = cls; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 uint64_t current_offset; 73 struct TALER_TrackTransferDetails ttd; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_uint64 ("offset_in_exchange_list", 76 ¤t_offset), 77 GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms", 78 &ttd.h_contract_terms), 79 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 80 &ttd.coin_pub), 81 TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value", 82 &ttd.coin_value), 83 TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee", 84 &ttd.coin_fee), 85 GNUNET_PQ_result_spec_end 86 }; 87 88 if (GNUNET_OK != 89 GNUNET_PQ_extract_result (result, 90 rs, 91 i)) 92 { 93 GNUNET_break (0); 94 ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR; 95 return; 96 } 97 ltdc->cb (ltdc->cb_cls, 98 (unsigned int) current_offset, 99 &ttd); 100 GNUNET_PQ_cleanup_result (rs); 101 } 102 ltdc->qs = num_results; 103 } 104 105 106 enum GNUNET_DB_QueryStatus 107 TALER_MERCHANTDB_lookup_transfer_details ( 108 struct TALER_MERCHANTDB_PostgresContext *pg, 109 const char *exchange_url, 110 const struct TALER_WireTransferIdentifierRawP *wtid, 111 TALER_MERCHANTDB_TransferDetailsCallback cb, 112 void *cb_cls) 113 { 114 struct GNUNET_PQ_QueryParam params[] = { 115 GNUNET_PQ_query_param_string (exchange_url), 116 GNUNET_PQ_query_param_auto_from_type (wtid), 117 GNUNET_PQ_query_param_end 118 }; 119 struct LookupTransferDetailsContext ltdc = { 120 .cb = cb, 121 .cb_cls = cb_cls, 122 .pg = pg 123 }; 124 enum GNUNET_DB_QueryStatus qs; 125 126 check_connection (pg); 127 PREPARE (pg, 128 "lookup_transfer_details", 129 "SELECT" 130 " mterm.h_contract_terms" 131 ",mtcoin.offset_in_exchange_list" 132 ",dep.coin_pub" 133 ",mtcoin.exchange_deposit_value" 134 ",mtcoin.exchange_deposit_fee" 135 " FROM merchant_expected_transfer_to_coin mtcoin" 136 " JOIN merchant_deposits dep" 137 " USING (deposit_serial)" 138 " JOIN merchant_deposit_confirmations mcon" 139 " USING (deposit_confirmation_serial)" 140 " JOIN merchant_contract_terms mterm" 141 " USING (order_serial)" 142 " JOIN merchant_expected_transfers met" 143 " USING (expected_credit_serial)" 144 " WHERE met.wtid=$2" 145 " AND met.exchange_url=$1"); 146 147 qs = GNUNET_PQ_eval_prepared_multi_select ( 148 pg->conn, 149 "lookup_transfer_details", 150 params, 151 &lookup_transfer_details_cb, 152 <dc); 153 if (0 >= qs) 154 return qs; 155 return ltdc.qs; 156 }