iterate_reserve_balance_insufficient_inconsistencies.c (6413B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 #include "taler/taler_pq_lib.h" 17 #include "pg_helper.h" 18 #include \ 19 "auditor-database/iterate_reserve_balance_insufficient_inconsistencies.h" 20 21 22 /** 23 * Hard upper bound on the number of records returned by a single 24 * call, regardless of the limit requested by the client. 25 */ 26 #define MAX_RECORDS 50000 27 28 29 /** 30 * Closure for #reserve_balance_insufficient_inconsistency_cb(). 31 */ 32 struct ReserveBalanceInsufficientInconsistencyContext 33 { 34 35 /** 36 * Function to call for each ReserveBalanceInsufficientInconsistency. 37 */ 38 TALER_AUDITORDB_ReserveBalanceInsufficientInconsistencyCallback cb; 39 40 /** 41 * Closure for @e cb 42 */ 43 void *cb_cls; 44 45 /** 46 * Plugin context. 47 */ 48 struct TALER_AUDITORDB_PostgresContext *pg; 49 50 /** 51 * Query status to return. 52 */ 53 enum GNUNET_DB_QueryStatus qs; 54 }; 55 56 57 /** 58 * Helper function for #TALER_AUDITORDB_iterate_reserve_balance_insufficient_inconsistencies(). 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 ReserveBalanceInsufficientInconsistency *` 63 * @param result the postgres result 64 * @param num_results the number of results in @a result 65 */ 66 static void 67 reserve_balance_insufficient_inconsistency_cb (void *cls, 68 PGresult *result, 69 unsigned int num_results) 70 { 71 struct ReserveBalanceInsufficientInconsistencyContext *dcc = cls; 72 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 73 74 for (unsigned int i = 0; i < num_results; i++) 75 { 76 struct TALER_AUDITORDB_ReserveBalanceInsufficientInconsistency dc; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("row_id", 79 &dc.row_id), 80 GNUNET_PQ_result_spec_auto_from_type ("reserve_pub", 81 &dc.reserve_pub), 82 GNUNET_PQ_result_spec_bool ("inconsistency_gain", 83 &dc.inconsistency_gain), 84 TALER_PQ_RESULT_SPEC_AMOUNT ("inconsistency_amount", 85 &dc.inconsistency_amount), 86 GNUNET_PQ_result_spec_bool ("suppressed", 87 &dc.suppressed), 88 GNUNET_PQ_result_spec_end 89 }; 90 enum GNUNET_GenericReturnValue rval; 91 92 if (GNUNET_OK != 93 GNUNET_PQ_extract_result (result, 94 rs, 95 i)) 96 { 97 GNUNET_break (0); 98 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 99 return; 100 } 101 dcc->qs = i + 1; 102 rval = dcc->cb (dcc->cb_cls, 103 &dc); 104 GNUNET_PQ_cleanup_result (rs); 105 if (GNUNET_OK != rval) 106 break; 107 } 108 } 109 110 111 enum GNUNET_DB_QueryStatus 112 TALER_AUDITORDB_iterate_reserve_balance_insufficient_inconsistencies (struct 113 TALER_AUDITORDB_PostgresContext 114 *pg, 115 int64_t 116 limit, 117 uint64_t 118 offset, 119 bool 120 return_suppressed 121 , 122 TALER_AUDITORDB_ReserveBalanceInsufficientInconsistencyCallback 123 cb, 124 void * 125 cb_cls) 126 { 127 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 128 TALER_AUDITORDB_abs_limit (limit)); 129 struct GNUNET_PQ_QueryParam params[] = { 130 GNUNET_PQ_query_param_uint64 (&offset), 131 GNUNET_PQ_query_param_bool (return_suppressed), 132 GNUNET_PQ_query_param_uint64 (&plimit), 133 GNUNET_PQ_query_param_end 134 }; 135 struct ReserveBalanceInsufficientInconsistencyContext dcc = { 136 .cb = cb, 137 .cb_cls = cb_cls, 138 .pg = pg 139 }; 140 enum GNUNET_DB_QueryStatus qs; 141 142 PREPARE (pg, 143 "iterate_reserve_balance_insufficient_inconsistencies_desc", 144 "SELECT" 145 " row_id" 146 ",reserve_pub" 147 ",inconsistency_gain" 148 ",inconsistency_amount" 149 ",suppressed" 150 " FROM auditor_reserve_balance_insufficient_inconsistency" 151 " WHERE (row_id < $1)" 152 " AND ($2 OR NOT suppressed)" 153 " ORDER BY row_id DESC" 154 " LIMIT $3" 155 ); 156 PREPARE (pg, 157 "iterate_reserve_balance_insufficient_inconsistencies_asc", 158 "SELECT" 159 " row_id" 160 ",reserve_pub" 161 ",inconsistency_gain" 162 ",inconsistency_amount" 163 ",suppressed" 164 " FROM auditor_reserve_balance_insufficient_inconsistency" 165 " WHERE (row_id > $1)" 166 " AND ($2 OR NOT suppressed)" 167 " ORDER BY row_id ASC" 168 " LIMIT $3" 169 ); 170 qs = GNUNET_PQ_eval_prepared_multi_select ( 171 pg->conn, 172 (limit > 0) 173 ? 174 "iterate_reserve_balance_insufficient_inconsistencies_asc" 175 : 176 "iterate_reserve_balance_insufficient_inconsistencies_desc", 177 params, 178 &reserve_balance_insufficient_inconsistency_cb, 179 &dcc); 180 if (qs > 0) 181 return dcc.qs; 182 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 183 return qs; 184 }