pg_iterate_reserve_close_info.c (3621B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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 pg_iterate_reserve_close_info.c 18 * @brief Low-level (statement-level) Postgres database access for the exchange 19 * @author Christian Grothoff 20 */ 21 #include "taler/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_iterate_reserve_close_info.h" 26 #include "pg_helper.h" 27 28 /** 29 * Closure for #iterate_reserve_close_info_cb() 30 */ 31 struct IteratorContext 32 { 33 /** 34 * Function to call with the results. 35 */ 36 TALER_EXCHANGEDB_KycAmountCallback cb; 37 38 /** 39 * Closure to pass to @e cb 40 */ 41 void *cb_cls; 42 43 /** 44 * Plugin context. 45 */ 46 struct PostgresClosure *pg; 47 }; 48 49 50 /** 51 * Helper function for #TEH_PG_iterate_reserve_close_info(). 52 * Calls the callback with each denomination key. 53 * 54 * @param cls a `struct IteratorContext` 55 * @param result db results 56 * @param num_results number of results in @a result 57 */ 58 static void 59 iterate_reserve_close_info_cb (void *cls, 60 PGresult *result, 61 unsigned int num_results) 62 { 63 struct IteratorContext *ic = cls; 64 struct PostgresClosure *pg = ic->pg; 65 66 for (unsigned int i = 0; i<num_results; i++) 67 { 68 struct TALER_Amount amount; 69 struct GNUNET_TIME_Absolute ts; 70 struct GNUNET_PQ_ResultSpec rs[] = { 71 GNUNET_PQ_result_spec_absolute_time ("execution_date", 72 &ts), 73 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 74 &amount), 75 GNUNET_PQ_result_spec_end 76 }; 77 78 if (GNUNET_OK != 79 GNUNET_PQ_extract_result (result, 80 rs, 81 i)) 82 { 83 GNUNET_break (0); 84 return; 85 } 86 ic->cb (ic->cb_cls, 87 &amount, 88 ts); 89 } 90 } 91 92 93 enum GNUNET_DB_QueryStatus 94 TEH_PG_iterate_reserve_close_info ( 95 void *cls, 96 const struct TALER_NormalizedPaytoHashP *h_payto, 97 struct GNUNET_TIME_Absolute time_limit, 98 TALER_EXCHANGEDB_KycAmountCallback kac, 99 void *kac_cls) 100 { 101 struct PostgresClosure *pg = cls; 102 struct GNUNET_PQ_QueryParam params[] = { 103 GNUNET_PQ_query_param_auto_from_type (h_payto), 104 GNUNET_PQ_query_param_absolute_time (&time_limit), 105 GNUNET_PQ_query_param_end 106 }; 107 struct IteratorContext ic = { 108 .cb = kac, 109 .cb_cls = kac_cls, 110 .pg = pg 111 }; 112 113 PREPARE (pg, 114 "iterate_reserve_close_info", 115 "SELECT amount" 116 " ,execution_date" 117 " FROM reserves_close" 118 " WHERE wire_target_h_payto IN (" 119 " SELECT wire_target_h_payto" 120 " FROM wire_targets" 121 " WHERE h_normalized_payto=$1" 122 " )" 123 " AND execution_date >= $2" 124 " ORDER BY execution_date DESC"); 125 return GNUNET_PQ_eval_prepared_multi_select ( 126 pg->conn, 127 "iterate_reserve_close_info", 128 params, 129 &iterate_reserve_close_info_cb, 130 &ic); 131 }