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