select_exchange_debit_transfers.c (5687B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 exchangedb/select_exchange_debit_transfers.c 18 * @brief Implementation of the select_exchange_debit_transfers function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/select_exchange_debit_transfers.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #handle_aml_result. 28 */ 29 struct SelectTransferContext 30 { 31 /** 32 * Function to call on each result. 33 */ 34 TALER_EXCHANGEDB_AmlTransferCallback cb; 35 36 /** 37 * Closure for @e cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_EXCHANGEDB_PostgresContext *pg; 45 46 /** 47 * Set to #GNUNET_SYSERR on serious errors. 48 */ 49 enum GNUNET_GenericReturnValue status; 50 }; 51 52 53 /** 54 * Function to be called with the results of a SELECT statement 55 * that has returned @a num_results results. Helper function 56 * for #TALER_EXCHANGEDB_select_exchange_debit_transfers(). 57 * 58 * @param cls closure of type `struct SelectTransferContext *` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 handle_transfer_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct SelectTransferContext *stc = cls; 68 struct TALER_EXCHANGEDB_PostgresContext *pg = stc->pg; 69 70 for (unsigned int i = 0; i<num_results; i++) 71 { 72 char *payto_uri; 73 uint64_t rowid; 74 struct GNUNET_TIME_Absolute execution_time; 75 struct TALER_Amount amount; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_uint64 ("serial_id", 78 &rowid), 79 GNUNET_PQ_result_spec_string ("payto_uri", 80 &payto_uri), 81 GNUNET_PQ_result_spec_absolute_time ("execution_time", 82 &execution_time), 83 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 84 &amount), 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 stc->status = GNUNET_SYSERR; 95 return; 96 } 97 stc->cb (stc->cb_cls, 98 rowid, 99 payto_uri, 100 execution_time, 101 &amount); 102 GNUNET_PQ_cleanup_result (rs); 103 } 104 } 105 106 107 enum GNUNET_DB_QueryStatus 108 TALER_EXCHANGEDB_select_exchange_debit_transfers ( 109 struct TALER_EXCHANGEDB_PostgresContext *pg, 110 const struct TALER_Amount *threshold, 111 uint64_t offset, 112 int64_t limit, 113 const struct TALER_NormalizedPaytoHashP *h_payto, 114 TALER_EXCHANGEDB_AmlTransferCallback cb, 115 void *cb_cls) 116 { 117 struct SelectTransferContext stc = { 118 .pg = pg, 119 .cb = cb, 120 .cb_cls = cb_cls, 121 .status = GNUNET_OK 122 }; 123 uint64_t ulimit = (limit > 0) ? limit : -limit; 124 struct GNUNET_PQ_QueryParam params[] = { 125 GNUNET_PQ_query_param_uint64 (&offset), 126 GNUNET_PQ_query_param_uint64 (&ulimit), 127 TALER_PQ_query_param_amount (pg->conn, 128 threshold), 129 NULL != h_payto 130 ? GNUNET_PQ_query_param_auto_from_type (h_payto) 131 : GNUNET_PQ_query_param_null (), 132 GNUNET_PQ_query_param_end 133 }; 134 enum GNUNET_DB_QueryStatus qs; 135 136 PREPARE (pg, 137 "select_exchange_debit_transfers_inc", 138 "SELECT" 139 " wo.wireout_uuid AS serial_id" 140 ",wt.payto_uri" 141 ",wo.execution_date AS execution_time" 142 ",wo.amount" 143 " FROM wire_out wo" 144 " LEFT JOIN wire_targets wt" 145 " USING (wire_target_h_payto)" 146 " WHERE (wo.wireout_uuid > $1)" 147 " AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )" 148 " AND ( ( (wo.amount).val > ($3::taler_amount).val)" 149 " OR ( ( (wo.amount).val >= ($3::taler_amount).val)" 150 " AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )" 151 " ORDER BY wo.wireout_uuid ASC" 152 " LIMIT $2"); 153 PREPARE (pg, 154 "select_exchange_debit_transfers_dec", 155 "SELECT" 156 " wo.wireout_uuid AS serial_id" 157 ",wt.payto_uri" 158 ",wo.execution_date AS execution_time" 159 ",wo.amount" 160 " FROM wire_out wo" 161 " LEFT JOIN wire_targets wt" 162 " USING (wire_target_h_payto)" 163 " WHERE (wo.wireout_uuid < $1)" 164 " AND ( ($4::BYTEA IS NULL) OR (wt.h_normalized_payto=$4) )" 165 " AND ( ( (wo.amount).val > ($3::taler_amount).val)" 166 " OR ( ( (wo.amount).val >= ($3::taler_amount).val)" 167 " AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )" 168 " ORDER BY wo.wireout_uuid DESC" 169 " LIMIT $2"); 170 qs = GNUNET_PQ_eval_prepared_multi_select ( 171 pg->conn, 172 (limit > 0) 173 ? "select_exchange_debit_transfers_inc" 174 : "select_exchange_debit_transfers_dec", 175 params, 176 &handle_transfer_cb, 177 &stc); 178 if (GNUNET_OK != stc.status) 179 return GNUNET_DB_STATUS_HARD_ERROR; 180 return qs; 181 }