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