lookup_deposits_by_order.c (5287B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023, 2024 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_by_order.c 18 * @brief Implementation of the lookup_deposits_by_order 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_by_order.h" 26 #include "helper.h" 27 28 /** 29 * Closure for lookup_deposits_by_order_cb(). 30 */ 31 struct LookupDepositsByOrderContext 32 { 33 34 /** 35 * Plugin context. 36 */ 37 struct TALER_MERCHANTDB_PostgresContext *pg; 38 39 /** 40 * Function to call with all results. 41 */ 42 TALER_MERCHANTDB_DepositedCoinsCallback cb; 43 44 /** 45 * Closure for @e cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Set to the query result. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 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 LookupDepositsByOrderContext *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 lookup_deposits_by_order_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct LookupDepositsByOrderContext *ldoc = cls; 70 71 for (unsigned int i = 0; i<num_results; i++) 72 { 73 uint64_t deposit_serial; 74 char *exchange_url; 75 struct TALER_MerchantWireHashP h_wire; 76 struct TALER_CoinSpendPublicKeyP coin_pub; 77 struct GNUNET_TIME_Timestamp deposit_timestamp; 78 struct TALER_Amount amount_with_fee; 79 struct TALER_Amount deposit_fee; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_uint64 ("deposit_serial", 82 &deposit_serial), 83 GNUNET_PQ_result_spec_string ("exchange_url", 84 &exchange_url), 85 GNUNET_PQ_result_spec_timestamp ("deposit_timestamp", 86 &deposit_timestamp), 87 GNUNET_PQ_result_spec_auto_from_type ("h_wire", 88 &h_wire), 89 TALER_PQ_result_spec_amount_with_currency ("amount_with_fee", 90 &amount_with_fee), 91 TALER_PQ_result_spec_amount_with_currency ("deposit_fee", 92 &deposit_fee), 93 GNUNET_PQ_result_spec_auto_from_type ("coin_pub", 94 &coin_pub), 95 GNUNET_PQ_result_spec_end 96 }; 97 98 if (GNUNET_OK != 99 GNUNET_PQ_extract_result (result, 100 rs, 101 i)) 102 { 103 GNUNET_break (0); 104 ldoc->qs = GNUNET_DB_STATUS_HARD_ERROR; 105 return; 106 } 107 ldoc->cb (ldoc->cb_cls, 108 deposit_serial, 109 exchange_url, 110 &h_wire, 111 deposit_timestamp, 112 &amount_with_fee, 113 &deposit_fee, 114 &coin_pub); 115 GNUNET_PQ_cleanup_result (rs); /* technically useless here */ 116 } 117 ldoc->qs = num_results; 118 } 119 120 121 enum GNUNET_DB_QueryStatus 122 TALER_MERCHANTDB_lookup_deposits_by_order (struct TALER_MERCHANTDB_PostgresContext *pg, 123 uint64_t order_serial, 124 TALER_MERCHANTDB_DepositedCoinsCallback cb, 125 void *cb_cls) 126 { 127 struct LookupDepositsByOrderContext ldoc = { 128 .pg = pg, 129 .cb = cb, 130 .cb_cls = cb_cls 131 }; 132 struct GNUNET_PQ_QueryParam params[] = { 133 GNUNET_PQ_query_param_uint64 (&order_serial), 134 GNUNET_PQ_query_param_end 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 check_connection (pg); 139 PREPARE (pg, 140 "lookup_deposits_by_order", 141 "SELECT" 142 " dep.deposit_serial" 143 ",mcon.exchange_url" 144 ",acc.h_wire" 145 ",mcon.deposit_timestamp" 146 ",dep.amount_with_fee" 147 ",dep.deposit_fee" 148 ",dep.coin_pub" 149 " FROM merchant_deposits dep" 150 " JOIN merchant_deposit_confirmations mcon" 151 " USING(deposit_confirmation_serial)" 152 " JOIN merchant_accounts acc" 153 " USING (account_serial)" 154 " WHERE mcon.order_serial=$1"); 155 156 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 157 "lookup_deposits_by_order", 158 params, 159 &lookup_deposits_by_order_cb, 160 &ldoc); 161 162 if (qs < 0) 163 return qs; 164 return ldoc.qs; 165 }