pg_select_wire_out_above_serial_id.c (4383B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 2024 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/pg_select_wire_out_above_serial_id.c 18 * @brief Implementation of the select_wire_out_above_serial_id function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "taler/exchange-database/select_wire_out_above_serial_id.h" 23 #include "helper.h" 24 25 /** 26 * Closure for #wire_out_serial_helper_cb(). 27 */ 28 struct WireOutSerialContext 29 { 30 31 /** 32 * Callback to call. 33 */ 34 TALER_EXCHANGEDB_WireTransferOutCallback 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 * Status code, set to #GNUNET_SYSERR on hard errors. 48 */ 49 int status; 50 }; 51 52 53 /** 54 * Helper function to be called with the results of a SELECT statement 55 * that has returned @a num_results results. 56 * 57 * @param cls closure of type `struct WireOutSerialContext` 58 * @param result the postgres result 59 * @param num_results the number of results in @a result 60 */ 61 static void 62 wire_out_serial_helper_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct WireOutSerialContext *wosc = cls; 67 struct TALER_EXCHANGEDB_PostgresContext *pg = wosc->pg; 68 69 for (unsigned int i = 0; i<num_results; i++) 70 { 71 uint64_t rowid; 72 struct GNUNET_TIME_Timestamp date; 73 struct TALER_WireTransferIdentifierRawP wtid; 74 struct TALER_FullPayto payto_uri; 75 struct TALER_Amount amount; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_uint64 ("wireout_uuid", 78 &rowid), 79 GNUNET_PQ_result_spec_timestamp ("execution_date", 80 &date), 81 GNUNET_PQ_result_spec_auto_from_type ("wtid_raw", 82 &wtid), 83 GNUNET_PQ_result_spec_string ("payto_uri", 84 &payto_uri.full_payto), 85 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 86 &amount), 87 GNUNET_PQ_result_spec_end 88 }; 89 int ret; 90 91 if (GNUNET_OK != 92 GNUNET_PQ_extract_result (result, 93 rs, 94 i)) 95 { 96 GNUNET_break (0); 97 wosc->status = GNUNET_SYSERR; 98 return; 99 } 100 ret = wosc->cb (wosc->cb_cls, 101 rowid, 102 date, 103 &wtid, 104 payto_uri, 105 &amount); 106 GNUNET_PQ_cleanup_result (rs); 107 if (GNUNET_OK != ret) 108 break; 109 } 110 } 111 112 113 enum GNUNET_DB_QueryStatus 114 TALER_EXCHANGEDB_select_wire_out_above_serial_id ( 115 struct TALER_EXCHANGEDB_PostgresContext *pg, 116 uint64_t serial_id, 117 TALER_EXCHANGEDB_WireTransferOutCallback cb, 118 void *cb_cls) 119 { 120 struct GNUNET_PQ_QueryParam params[] = { 121 GNUNET_PQ_query_param_uint64 (&serial_id), 122 GNUNET_PQ_query_param_end 123 }; 124 struct WireOutSerialContext wosc = { 125 .cb = cb, 126 .cb_cls = cb_cls, 127 .pg = pg, 128 .status = GNUNET_OK 129 }; 130 enum GNUNET_DB_QueryStatus qs; 131 132 PREPARE (pg, 133 "select_wire_out_above_serial_id", 134 "SELECT" 135 " wo.wireout_uuid" 136 ",wo.execution_date" 137 ",wo.wtid_raw" 138 ",wt.payto_uri" 139 ",wo.amount" 140 " FROM wire_out wo" 141 " JOIN wire_targets wt" 142 " USING (wire_target_h_payto)" 143 " WHERE wireout_uuid>=$1" 144 " ORDER BY wireout_uuid ASC;"); 145 qs = GNUNET_PQ_eval_prepared_multi_select ( 146 pg->conn, 147 "select_wire_out_above_serial_id", 148 params, 149 &wire_out_serial_helper_cb, 150 &wosc); 151 if (GNUNET_OK != wosc.status) 152 return GNUNET_DB_STATUS_HARD_ERROR; 153 return qs; 154 }