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