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