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