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