Line data Source code
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 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 0 : lookup_transfer_summary_cb (void *cls, 65 : PGresult *result, 66 : unsigned int num_results) 67 : { 68 0 : struct LookupTransferSummaryContext *ltdc = cls; 69 : 70 0 : 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 0 : struct GNUNET_PQ_ResultSpec rs[] = { 76 0 : GNUNET_PQ_result_spec_string ("order_id", 77 : &order_id), 78 0 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_value", 79 : &deposit_value), 80 0 : TALER_PQ_result_spec_amount_with_currency ("exchange_deposit_fee", 81 : &deposit_fee), 82 : GNUNET_PQ_result_spec_end 83 : }; 84 : 85 0 : if (GNUNET_OK != 86 0 : GNUNET_PQ_extract_result (result, 87 : rs, 88 : i)) 89 : { 90 0 : GNUNET_break (0); 91 0 : ltdc->qs = GNUNET_DB_STATUS_HARD_ERROR; 92 0 : return; 93 : } 94 0 : ltdc->cb (ltdc->cb_cls, 95 : order_id, 96 : &deposit_value, 97 : &deposit_fee); 98 0 : GNUNET_PQ_cleanup_result (rs); 99 : } 100 0 : ltdc->qs = num_results; 101 : } 102 : 103 : 104 : enum GNUNET_DB_QueryStatus 105 0 : TMH_PG_lookup_transfer_summary (void *cls, 106 : const char *exchange_url, 107 : const struct TALER_WireTransferIdentifierRawP *wtid, 108 : TALER_MERCHANTDB_TransferSummaryCallback cb, 109 : void *cb_cls) 110 : { 111 0 : struct PostgresClosure *pg = cls; 112 0 : struct GNUNET_PQ_QueryParam params[] = { 113 0 : GNUNET_PQ_query_param_string (exchange_url), 114 0 : GNUNET_PQ_query_param_auto_from_type (wtid), 115 : GNUNET_PQ_query_param_end 116 : }; 117 0 : struct LookupTransferSummaryContext ltdc = { 118 : .cb = cb, 119 : .cb_cls = cb_cls, 120 : .pg = pg 121 : }; 122 : enum GNUNET_DB_QueryStatus qs; 123 : 124 0 : check_connection (pg); 125 0 : PREPARE (pg, 126 : "lookup_transfer_summary", 127 : "SELECT" 128 : " mct.order_id" 129 : ",mtc.exchange_deposit_value" 130 : ",mtc.exchange_deposit_fee" 131 : " FROM merchant_transfers mtr" 132 : " JOIN merchant_transfer_to_coin mtc" 133 : " USING (credit_serial)" 134 : " JOIN merchant_deposits dep" 135 : " USING (deposit_serial)" 136 : " JOIN merchant_deposit_confirmations mcon" 137 : " USING (deposit_confirmation_serial)" 138 : " JOIN merchant_contract_terms mct" 139 : " USING (order_serial)" 140 : " WHERE mtr.wtid=$2" 141 : " AND mtr.exchange_url=$1"); 142 : 143 0 : qs = GNUNET_PQ_eval_prepared_multi_select ( 144 : pg->conn, 145 : "lookup_transfer_summary", 146 : params, 147 : &lookup_transfer_summary_cb, 148 : <dc); 149 0 : if (0 >= qs) 150 0 : return qs; 151 0 : return ltdc.qs; 152 : }