iterate_row_inconsistencies.c (4845B)
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 17 #include "taler/taler_pq_lib.h" 18 #include "pg_helper.h" 19 #include "auditor-database/iterate_row_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 #deposit_confirmation_cb(). 31 */ 32 struct RowInconsistencyContext 33 { 34 35 /** 36 * Function to call for each deposit confirmation. 37 */ 38 TALER_AUDITORDB_RowInconsistencyCallback 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_row_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 DepositConfirmationContext *` 63 * @param result the postgres result 64 * @param num_results the number of results in @a result 65 */ 66 static void 67 row_inconsistency_cb (void *cls, 68 PGresult *result, 69 unsigned int num_results) 70 { 71 struct RowInconsistencyContext *dcc = cls; 72 73 for (unsigned int i = 0; i < num_results; i++) 74 { 75 struct TALER_AUDITORDB_RowInconsistency dc; 76 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("row_id", &dc.row_id), 79 GNUNET_PQ_result_spec_uint64 ("problem_row_id", &dc.problem_row_id), 80 81 GNUNET_PQ_result_spec_string ("row_table", &dc.row_table), 82 GNUNET_PQ_result_spec_string ("diagnostic", &dc.diagnostic), 83 GNUNET_PQ_result_spec_bool ("suppressed", &dc.suppressed), 84 85 86 GNUNET_PQ_result_spec_end 87 }; 88 enum GNUNET_GenericReturnValue rval; 89 90 if (GNUNET_OK != 91 GNUNET_PQ_extract_result (result, 92 rs, 93 i)) 94 { 95 GNUNET_break (0); 96 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 97 return; 98 } 99 100 dcc->qs = i + 1; 101 102 103 rval = dcc->cb (dcc->cb_cls, 104 dc.row_id, 105 &dc); 106 GNUNET_PQ_cleanup_result (rs); 107 if (GNUNET_OK != rval) 108 break; 109 } 110 } 111 112 113 enum GNUNET_DB_QueryStatus 114 TALER_AUDITORDB_iterate_row_inconsistencies ( 115 struct TALER_AUDITORDB_PostgresContext *pg, 116 int64_t limit, 117 uint64_t offset, 118 bool return_suppressed, // maybe not needed 119 TALER_AUDITORDB_RowInconsistencyCallback cb, 120 void *cb_cls) 121 { 122 123 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 124 TALER_AUDITORDB_abs_limit (limit)); 125 struct GNUNET_PQ_QueryParam params[] = { 126 GNUNET_PQ_query_param_uint64 (&offset), 127 GNUNET_PQ_query_param_bool (return_suppressed), 128 GNUNET_PQ_query_param_uint64 (&plimit), 129 GNUNET_PQ_query_param_end 130 }; 131 struct RowInconsistencyContext dcc = { 132 .cb = cb, 133 .cb_cls = cb_cls, 134 .pg = pg 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 139 PREPARE (pg, 140 "iterate_row_inconsistencies_inconsistency_select_desc", 141 "SELECT" 142 " row_id" 143 ",problem_row_id" 144 ",row_table" 145 ",diagnostic" 146 ",suppressed" 147 " FROM auditor_row_inconsistency" 148 " WHERE (row_id < $1)" 149 " AND ($2 OR suppressed is false)" 150 " ORDER BY row_id DESC" 151 " LIMIT $3" 152 ); 153 PREPARE (pg, 154 "iterate_row_inconsistencies_inconsistency_select_asc", 155 "SELECT" 156 " row_id" 157 ",problem_row_id" 158 ",row_table" 159 ",diagnostic" 160 ",suppressed" 161 " FROM auditor_row_inconsistency" 162 " WHERE (row_id > $1)" 163 " AND ($2 OR suppressed is false)" 164 " ORDER BY row_id ASC" 165 " LIMIT $3" 166 ); 167 qs = GNUNET_PQ_eval_prepared_multi_select ( 168 pg->conn, 169 (limit > 0) 170 ? "iterate_row_inconsistencies_inconsistency_select_asc" 171 : "iterate_row_inconsistencies_inconsistency_select_desc", 172 params, 173 &row_inconsistency_cb, 174 &dcc); 175 176 if (qs > 0) 177 return dcc.qs; 178 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 179 return qs; 180 }