pg_select_pending_deposits.c (5307B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 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 auditordb/pg_select_pending_deposits.c 18 * @brief Implementation of the select_pending_deposits 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_pending_deposits.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #wire_missing_cb(). 31 */ 32 struct WireMissingContext 33 { 34 35 /** 36 * Function to call for each pending deposit. 37 */ 38 TALER_AUDITORDB_WireMissingCallback cb; 39 40 /** 41 * Closure for @e cb 42 */ 43 void *cb_cls; 44 45 /** 46 * Plugin context. 47 */ 48 struct PostgresClosure *pg; 49 50 /** 51 * Query status to return. 52 */ 53 enum GNUNET_DB_QueryStatus qs; 54 }; 55 56 57 /** 58 * Helper function for #TAH_PG_select_purse_expired(). 59 * To be called with the results of a SELECT statement 60 * that has returned @a num_results results. 61 * 62 * @param cls closure of type `struct WireMissingContext *` 63 * @param result the postgres result 64 * @param num_results the number of results in @a result 65 */ 66 static void 67 wire_missing_cb (void *cls, 68 PGresult *result, 69 unsigned int num_results) 70 { 71 struct WireMissingContext *eic = cls; 72 struct PostgresClosure *pg = eic->pg; 73 74 for (unsigned int i = 0; i < num_results; i++) 75 { 76 uint64_t row_id; 77 uint64_t batch_deposit_serial_id; 78 struct TALER_Amount total_amount; 79 struct TALER_FullPaytoHashP wire_target_h_payto; 80 struct GNUNET_TIME_Timestamp deadline; 81 bool suppressed; 82 struct GNUNET_PQ_ResultSpec rs[] = { 83 GNUNET_PQ_result_spec_uint64 ("row_id", 84 &row_id), 85 GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id", 86 &batch_deposit_serial_id), 87 TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount", 88 &total_amount), 89 GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto", 90 &wire_target_h_payto), 91 GNUNET_PQ_result_spec_timestamp ("deadline", 92 &deadline), 93 GNUNET_PQ_result_spec_bool ("suppressed", 94 &suppressed), 95 GNUNET_PQ_result_spec_end 96 }; 97 98 if (GNUNET_OK != 99 GNUNET_PQ_extract_result (result, 100 rs, 101 i)) 102 { 103 GNUNET_break (0); 104 eic->qs = GNUNET_DB_STATUS_HARD_ERROR; 105 return; 106 } 107 eic->cb (eic->cb_cls, 108 row_id, 109 batch_deposit_serial_id, 110 &total_amount, 111 &wire_target_h_payto, 112 deadline, 113 suppressed); 114 } 115 eic->qs = num_results; 116 } 117 118 119 enum GNUNET_DB_QueryStatus 120 TAH_PG_select_pending_deposits ( 121 void *cls, 122 struct GNUNET_TIME_Absolute deadline, 123 int64_t limit, 124 uint64_t offset, 125 bool return_suppressed, 126 TALER_AUDITORDB_WireMissingCallback cb, 127 void *cb_cls) 128 { 129 struct PostgresClosure *pg = cls; 130 uint64_t ulimit = (limit < 0) ? -limit : limit; 131 struct GNUNET_PQ_QueryParam params[] = { 132 GNUNET_PQ_query_param_absolute_time (&deadline), 133 GNUNET_PQ_query_param_uint64 (&offset), 134 GNUNET_PQ_query_param_uint64 (&ulimit), 135 GNUNET_PQ_query_param_bool (return_suppressed), 136 GNUNET_PQ_query_param_end 137 }; 138 struct WireMissingContext eic = { 139 .cb = cb, 140 .cb_cls = cb_cls, 141 .pg = pg 142 }; 143 enum GNUNET_DB_QueryStatus qs; 144 145 PREPARE (pg, 146 "auditor_select_pending_deposits_asc", 147 "SELECT" 148 " row_id" 149 ",total_amount" 150 ",wire_target_h_payto" 151 ",batch_deposit_serial_id" 152 ",deadline" 153 ",suppressed" 154 " FROM auditor_pending_deposits" 155 " WHERE deadline<$1" 156 " AND (row_id > $2)" 157 " AND ($4 OR NOT suppressed)" 158 " ORDER BY row_id ASC" 159 " LIMIT $3;"); 160 PREPARE (pg, 161 "auditor_select_pending_deposits_desc", 162 "SELECT" 163 " row_id" 164 ",total_amount" 165 ",wire_target_h_payto" 166 ",batch_deposit_serial_id" 167 ",deadline" 168 ",suppressed" 169 " FROM auditor_pending_deposits" 170 " WHERE deadline<$1" 171 " AND (row_id < $2)" 172 " AND ($4 OR NOT suppressed)" 173 " ORDER BY row_id DESC" 174 " LIMIT $3;"); 175 qs = GNUNET_PQ_eval_prepared_multi_select ( 176 pg->conn, 177 "auditor_select_pending_deposits", 178 params, 179 &wire_missing_cb, 180 &eic); 181 if (0 > qs) 182 return qs; 183 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != eic.qs); 184 return eic.qs; 185 }