pg_select_exchanges.c (4209B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 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_select_exchanges.c 18 * @brief Implementation of the select_exchanges function for Postgres 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_select_exchanges.h" 26 #include "pg_helper.h" 27 28 /** 29 * Context used for TMH_PG_lookup_exchanges(). 30 */ 31 struct LookupExchangesContext 32 { 33 /** 34 * Function to call with the results. 35 */ 36 TALER_MERCHANTDB_ExchangesCallback cb; 37 38 /** 39 * Closure for @a cb. 40 */ 41 void *cb_cls; 42 43 /** 44 * Did database result extraction fail? 45 */ 46 bool extract_failed; 47 }; 48 49 50 /** 51 * Function to be called with the results of a SELECT statement 52 * that has returned @a num_results results about product exchanges. 53 * 54 * @param[in,out] cls of type `struct LookupExchangesContext *` 55 * @param result the postgres result 56 * @param num_results the number of results in @a result 57 */ 58 static void 59 lookup_exchanges_cb (void *cls, 60 PGresult *result, 61 unsigned int num_results) 62 { 63 struct LookupExchangesContext *plc = cls; 64 65 for (unsigned int i = 0; i < num_results; i++) 66 { 67 char *exchange_url; 68 struct GNUNET_TIME_Absolute next_download; 69 struct GNUNET_TIME_Absolute keys_expiration; 70 uint32_t hc; 71 uint32_t ec; 72 struct GNUNET_PQ_ResultSpec rs[] = { 73 GNUNET_PQ_result_spec_absolute_time ("first_retry", 74 &next_download), 75 GNUNET_PQ_result_spec_absolute_time ("expiration_time", 76 &keys_expiration), 77 GNUNET_PQ_result_spec_string ("exchange_url", 78 &exchange_url), 79 GNUNET_PQ_result_spec_uint32 ("exchange_http_status", 80 &hc), 81 GNUNET_PQ_result_spec_uint32 ("exchange_ec_code", 82 &ec), 83 GNUNET_PQ_result_spec_end 84 }; 85 86 if (GNUNET_OK != 87 GNUNET_PQ_extract_result (result, 88 rs, 89 i)) 90 { 91 GNUNET_break (0); 92 plc->extract_failed = true; 93 return; 94 } 95 plc->cb (plc->cb_cls, 96 exchange_url, 97 next_download, 98 keys_expiration, 99 (unsigned int) hc, 100 (enum TALER_ErrorCode) ec); 101 GNUNET_PQ_cleanup_result (rs); 102 } 103 } 104 105 106 enum GNUNET_DB_QueryStatus 107 TMH_PG_select_exchanges (void *cls, 108 TALER_MERCHANTDB_ExchangesCallback cb, 109 void *cb_cls) 110 { 111 struct PostgresClosure *pg = cls; 112 struct LookupExchangesContext plc = { 113 .cb = cb, 114 .cb_cls = cb_cls, 115 /* Can be overwritten by the lookup_exchanges_cb */ 116 .extract_failed = false, 117 }; 118 struct GNUNET_PQ_QueryParam params[] = { 119 GNUNET_PQ_query_param_end 120 }; 121 enum GNUNET_DB_QueryStatus qs; 122 123 check_connection (pg); 124 PREPARE (pg, 125 "select_exchanges", 126 "SELECT" 127 " exchange_url" 128 " ,first_retry" 129 " ,expiration_time" 130 " ,exchange_http_status" 131 " ,exchange_ec_code" 132 " FROM merchant_exchange_keys"); 133 qs = GNUNET_PQ_eval_prepared_multi_select ( 134 pg->conn, 135 "select_exchanges", 136 params, 137 &lookup_exchanges_cb, 138 &plc); 139 /* If there was an error inside lookup_exchanges_cb, return a hard error. */ 140 if (plc.extract_failed) 141 { 142 GNUNET_break (0); 143 return GNUNET_DB_STATUS_HARD_ERROR; 144 } 145 return qs; 146 }