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