get_wire_format_inconsistency.c (5121B)
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_wire_format_inconsistency.h" 19 20 21 struct WireFormatInconsistencyContext 22 { 23 24 /** 25 * Function to call for each bad sig loss. 26 */ 27 TALER_AUDITORDB_WireFormatInconsistencyCallback 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_wire_format_inconsistency(). 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 WireFormatInconsistencyContext *` 52 * @param result the postgres result 53 * @param num_results the number of results in @a result 54 */ 55 static void 56 wire_format_inconsistency_cb (void *cls, 57 PGresult *result, 58 unsigned int num_results) 59 { 60 struct WireFormatInconsistencyContext *dcc = cls; 61 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 62 63 for (unsigned int i = 0; i < num_results; i++) 64 { 65 struct TALER_AUDITORDB_WireFormatInconsistency dc; 66 struct GNUNET_PQ_ResultSpec rs[] = { 67 GNUNET_PQ_result_spec_uint64 ("row_id", 68 &dc.row_id), 69 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 70 &dc.amount), 71 GNUNET_PQ_result_spec_uint64 ("wire_offset", 72 &dc.wire_offset), 73 GNUNET_PQ_result_spec_string ("diagnostic", 74 &dc.diagnostic), 75 GNUNET_PQ_result_spec_bool ("suppressed", 76 &dc.suppressed), 77 GNUNET_PQ_result_spec_end 78 }; 79 enum GNUNET_GenericReturnValue rval; 80 81 if (GNUNET_OK != 82 GNUNET_PQ_extract_result (result, 83 rs, 84 i)) 85 { 86 GNUNET_break (0); 87 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 88 return; 89 } 90 dcc->qs = i + 1; 91 rval = dcc->cb (dcc->cb_cls, 92 &dc); 93 GNUNET_PQ_cleanup_result (rs); 94 if (GNUNET_OK != rval) 95 break; 96 } 97 } 98 99 100 enum GNUNET_DB_QueryStatus 101 TALER_AUDITORDB_get_wire_format_inconsistency (struct 102 TALER_AUDITORDB_PostgresContext * 103 pg, 104 int64_t limit, 105 uint64_t offset, 106 bool return_suppressed, 107 TALER_AUDITORDB_WireFormatInconsistencyCallback 108 cb, 109 void *cb_cls) 110 { 111 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 112 struct GNUNET_PQ_QueryParam params[] = { 113 GNUNET_PQ_query_param_uint64 (&offset), 114 GNUNET_PQ_query_param_bool (return_suppressed), 115 GNUNET_PQ_query_param_uint64 (&plimit), 116 GNUNET_PQ_query_param_end 117 }; 118 struct WireFormatInconsistencyContext dcc = { 119 .cb = cb, 120 .cb_cls = cb_cls, 121 .pg = pg 122 }; 123 enum GNUNET_DB_QueryStatus qs; 124 125 PREPARE (pg, 126 "auditor_wire_format_inconsistency_get_desc", 127 "SELECT" 128 " row_id," 129 " amount," 130 " wire_offset," 131 " diagnostic," 132 " suppressed" 133 " FROM auditor_wire_format_inconsistency" 134 " WHERE (row_id < $1)" 135 " AND ($2 OR NOT suppressed)" 136 " ORDER BY row_id DESC" 137 " LIMIT $3" 138 ); 139 PREPARE (pg, 140 "auditor_wire_format_inconsistency_get_asc", 141 "SELECT" 142 " row_id," 143 " amount," 144 " wire_offset," 145 " diagnostic," 146 " suppressed" 147 " FROM auditor_wire_format_inconsistency" 148 " WHERE (row_id > $1)" 149 " AND ($2 OR NOT suppressed)" 150 " ORDER BY row_id ASC" 151 " LIMIT $3" 152 ); 153 qs = GNUNET_PQ_eval_prepared_multi_select ( 154 pg->conn, 155 (limit > 0) 156 ? "auditor_wire_format_inconsistency_get_asc" 157 : "auditor_wire_format_inconsistency_get_desc", 158 params, 159 &wire_format_inconsistency_cb, 160 &dcc); 161 if (qs > 0) 162 return dcc.qs; 163 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 164 return qs; 165 }