pg_select_open_transfers.c (4618B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 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 backenddb/pg_select_open_transfers.c 18 * @brief Implementation of the select_open_transfers function for Postgres 19 * @author Christian Grothoff 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_select_open_transfers.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Context used for open_transfers_cb(). 31 */ 32 struct SelectOpenTransfersContext 33 { 34 /** 35 * Postgres context. 36 */ 37 struct PostgresClosure *pg; 38 39 /** 40 * Function to call with the results. 41 */ 42 TALER_MERCHANTDB_OpenTransferCallback cb; 43 44 /** 45 * Closure for @a cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Internal result. 51 */ 52 enum GNUNET_DB_QueryStatus qs; 53 }; 54 55 56 /** 57 * Function to be called with the results of a SELECT statement 58 * that has returned @a num_results results about rewards. 59 * 60 * @param[in,out] cls of type `struct SelectOpenTransfersContext *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 open_transfers_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct SelectOpenTransfersContext *plc = cls; 70 71 for (unsigned int i = 0; i < num_results; i++) 72 { 73 uint64_t rowid; 74 char *instance_id; 75 char *exchange_url; 76 struct TALER_FullPayto payto_uri; 77 struct TALER_WireTransferIdentifierRawP wtid; 78 struct GNUNET_TIME_Absolute retry_time; 79 struct GNUNET_PQ_ResultSpec rs[] = { 80 GNUNET_PQ_result_spec_uint64 ("expected_credit_serial", 81 &rowid), 82 GNUNET_PQ_result_spec_string ("instance_id", 83 &instance_id), 84 GNUNET_PQ_result_spec_string ("exchange_url", 85 &exchange_url), 86 GNUNET_PQ_result_spec_string ("payto_uri", 87 &payto_uri.full_payto), 88 GNUNET_PQ_result_spec_auto_from_type ("wtid", 89 &wtid), 90 GNUNET_PQ_result_spec_absolute_time ("retry_time", 91 &retry_time), 92 GNUNET_PQ_result_spec_end 93 }; 94 95 if (GNUNET_OK != 96 GNUNET_PQ_extract_result (result, 97 rs, 98 i)) 99 { 100 GNUNET_break (0); 101 plc->qs = GNUNET_DB_STATUS_HARD_ERROR; 102 return; 103 } 104 plc->cb (plc->cb_cls, 105 rowid, 106 instance_id, 107 exchange_url, 108 payto_uri, 109 &wtid, 110 retry_time); 111 GNUNET_PQ_cleanup_result (rs); 112 } 113 } 114 115 116 enum GNUNET_DB_QueryStatus 117 TMH_PG_select_open_transfers (void *cls, 118 uint64_t limit, 119 TALER_MERCHANTDB_OpenTransferCallback cb, 120 void *cb_cls) 121 { 122 struct PostgresClosure *pg = cls; 123 struct SelectOpenTransfersContext plc = { 124 .pg = pg, 125 .cb = cb, 126 .cb_cls = cb_cls 127 }; 128 struct GNUNET_PQ_QueryParam params[] = { 129 GNUNET_PQ_query_param_uint64 (&limit), 130 GNUNET_PQ_query_param_end 131 }; 132 enum GNUNET_DB_QueryStatus qs; 133 134 PREPARE (pg, 135 "select_open_transfers", 136 "SELECT" 137 " met.expected_credit_serial" 138 ",mi.merchant_id AS instance_id" 139 ",met.exchange_url" 140 ",ma.payto_uri" 141 ",met.wtid" 142 ",met.retry_time" 143 " FROM merchant_expected_transfers met" 144 " JOIN merchant_accounts ma" 145 " USING (account_serial)" 146 " JOIN merchant_instances mi" 147 " ON (ma.merchant_serial=mi.merchant_serial)" 148 " WHERE retry_needed" 149 " ORDER BY retry_time ASC" 150 " LIMIT $1;"); 151 qs = GNUNET_PQ_eval_prepared_multi_select ( 152 pg->conn, 153 "select_open_transfers", 154 params, 155 &open_transfers_cb, 156 &plc); 157 if (0 != plc.qs) 158 return plc.qs; 159 return qs; 160 }