iterate_kycauth_in_above_serial_id_by_account.c (5319B)
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/iterate_kycauth_in_above_serial_id_by_account.c 18 * @brief Implementation of the iterate_kycauth_in_above_serial_id_by_account function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include \ 23 "exchange-database/iterate_kycauth_in_above_serial_id_by_account.h" 24 #include "helper.h" 25 26 27 /** 28 * Closure for #kycauth_in_serial_helper_cb(). 29 */ 30 struct KycauthInSerialContext 31 { 32 33 /** 34 * Callback to call. 35 */ 36 TALER_EXCHANGEDB_KycauthInCallback cb; 37 38 /** 39 * Closure for @e cb. 40 */ 41 void *cb_cls; 42 43 /** 44 * Plugin context. 45 */ 46 struct TALER_EXCHANGEDB_PostgresContext *pg; 47 48 /** 49 * Status code, set to #GNUNET_SYSERR on hard errors. 50 */ 51 enum GNUNET_GenericReturnValue status; 52 }; 53 54 55 /** 56 * Helper function to be called with the results of a SELECT statement 57 * that has returned @a num_results results. 58 * 59 * @param cls closure of type `struct KycauthInSerialContext` 60 * @param result the postgres result 61 * @param num_results the number of results in @a result 62 */ 63 static void 64 kycauth_in_serial_helper_cb (void *cls, 65 PGresult *result, 66 unsigned int num_results) 67 { 68 struct KycauthInSerialContext *kisc = cls; 69 struct TALER_EXCHANGEDB_PostgresContext *pg = kisc->pg; 70 71 for (unsigned int i = 0; i<num_results; i++) 72 { 73 union TALER_AccountPublicKeyP account_pub; 74 struct TALER_Amount credit; 75 struct TALER_FullPayto sender_account_details = { NULL }; 76 struct GNUNET_TIME_Timestamp execution_date; 77 uint64_t rowid; 78 uint64_t wire_reference; 79 bool no_sender_account_details; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_auto_from_type ("account_pub", 82 &account_pub), 83 GNUNET_PQ_result_spec_uint64 ("wire_reference", 84 &wire_reference), 85 TALER_PQ_RESULT_SPEC_AMOUNT ("credit", 86 &credit), 87 GNUNET_PQ_result_spec_timestamp ("execution_date", 88 &execution_date), 89 /* the exchange only creates the wire_targets row the first time it 90 sees the account, so we must not drop a transfer just because 91 that row is missing */ 92 GNUNET_PQ_result_spec_allow_null ( 93 GNUNET_PQ_result_spec_string ("sender_account_details", 94 &sender_account_details.full_payto), 95 &no_sender_account_details), 96 GNUNET_PQ_result_spec_uint64 ("kycauth_in_serial_id", 97 &rowid), 98 GNUNET_PQ_result_spec_end 99 }; 100 enum GNUNET_GenericReturnValue ret; 101 102 if (GNUNET_OK != 103 GNUNET_PQ_extract_result (result, 104 rs, 105 i)) 106 { 107 GNUNET_break (0); 108 kisc->status = GNUNET_SYSERR; 109 return; 110 } 111 ret = kisc->cb (kisc->cb_cls, 112 rowid, 113 &account_pub, 114 &credit, 115 sender_account_details, 116 wire_reference, 117 execution_date); 118 GNUNET_PQ_cleanup_result (rs); 119 if (GNUNET_OK != ret) 120 break; 121 } 122 } 123 124 125 enum GNUNET_DB_QueryStatus 126 TALER_EXCHANGEDB_iterate_kycauth_in_above_serial_id_by_account ( 127 struct TALER_EXCHANGEDB_PostgresContext *pg, 128 const char *account_name, 129 uint64_t serial_id, 130 TALER_EXCHANGEDB_KycauthInCallback cb, 131 void *cb_cls) 132 { 133 struct GNUNET_PQ_QueryParam params[] = { 134 GNUNET_PQ_query_param_uint64 (&serial_id), 135 GNUNET_PQ_query_param_string (account_name), 136 GNUNET_PQ_query_param_end 137 }; 138 struct KycauthInSerialContext kisc = { 139 .cb = cb, 140 .cb_cls = cb_cls, 141 .pg = pg, 142 .status = GNUNET_OK 143 }; 144 enum GNUNET_DB_QueryStatus qs; 145 146 PREPARE (pg, 147 "iterate_kycauth_in_above_serial_id_by_account", 148 "SELECT" 149 " account_pub" 150 ",wire_reference" 151 ",credit" 152 ",execution_date" 153 ",wt.payto_uri AS sender_account_details" 154 ",kycauth_in_serial_id" 155 " FROM kycauths_in" 156 " LEFT JOIN wire_targets wt" 157 " ON (wire_source_h_payto = wire_target_h_payto)" 158 " WHERE kycauth_in_serial_id>=$1" 159 " AND exchange_account_section=$2" 160 " ORDER BY kycauth_in_serial_id ASC;"); 161 qs = GNUNET_PQ_eval_prepared_multi_select ( 162 pg->conn, 163 "iterate_kycauth_in_above_serial_id_by_account", 164 params, 165 &kycauth_in_serial_helper_cb, 166 &kisc); 167 if (GNUNET_OK != kisc.status) 168 return GNUNET_DB_STATUS_HARD_ERROR; 169 return qs; 170 }