iterate_kycauth_in_inconsistencies.c (5964B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 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 * @file src/auditordb/iterate_kycauth_in_inconsistencies.c 18 * @brief Implementation of the iterate_kycauth_in_inconsistencies function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_error_codes.h" 22 #include "taler/taler_dbevents.h" 23 #include "taler/taler_pq_lib.h" 24 #include "pg_helper.h" 25 #include "auditor-database/iterate_kycauth_in_inconsistencies.h" 26 27 28 /** 29 * Hard upper bound on the number of records returned by a single 30 * call, regardless of the limit requested by the client. 31 */ 32 #define MAX_RECORDS 50000 33 34 35 struct KycauthInInconsistencyContext 36 { 37 38 /** 39 * Function to call for each inconsistency. 40 */ 41 TALER_AUDITORDB_KycauthInInconsistencyCallback cb; 42 43 /** 44 * Closure for @e cb 45 */ 46 void *cb_cls; 47 48 /** 49 * Plugin context. 50 */ 51 struct TALER_AUDITORDB_PostgresContext *pg; 52 53 /** 54 * Query status to return. 55 */ 56 enum GNUNET_DB_QueryStatus qs; 57 }; 58 59 60 /** 61 * Helper function for #TALER_AUDITORDB_iterate_kycauth_in_inconsistencies(). 62 * To be called with the results of a SELECT statement 63 * that has returned @a num_results results. 64 * 65 * @param cls closure of type `struct KycauthInInconsistencyContext *` 66 * @param result the postgres result 67 * @param num_results the number of results in @a result 68 */ 69 static void 70 kycauth_in_inconsistency_cb (void *cls, 71 PGresult *result, 72 unsigned int num_results) 73 { 74 struct KycauthInInconsistencyContext *dcc = cls; 75 struct TALER_AUDITORDB_PostgresContext *pg = dcc->pg; 76 77 for (unsigned int i = 0; i < num_results; i++) 78 { 79 struct TALER_AUDITORDB_KycauthInInconsistency dc; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_uint64 ("row_id", 82 &dc.serial_id), 83 GNUNET_PQ_result_spec_uint64 ("bank_row_id", 84 &dc.bank_row_id), 85 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_exchange_expected", 86 &dc.amount_exchange_expected), 87 TALER_PQ_RESULT_SPEC_AMOUNT ("amount_wired", 88 &dc.amount_wired), 89 GNUNET_PQ_result_spec_auto_from_type ("account_pub", 90 &dc.account_pub), 91 GNUNET_PQ_result_spec_absolute_time ("timestamp", 92 &dc.timestamp), 93 GNUNET_PQ_result_spec_string ("account", 94 &dc.account.full_payto), 95 GNUNET_PQ_result_spec_string ("diagnostic", 96 &dc.diagnostic), 97 GNUNET_PQ_result_spec_bool ("suppressed", 98 &dc.suppressed), 99 GNUNET_PQ_result_spec_end 100 }; 101 enum GNUNET_GenericReturnValue rval; 102 103 if (GNUNET_OK != 104 GNUNET_PQ_extract_result (result, 105 rs, 106 i)) 107 { 108 GNUNET_break (0); 109 dcc->qs = GNUNET_DB_STATUS_HARD_ERROR; 110 return; 111 } 112 113 dcc->qs = i + 1; 114 rval = dcc->cb (dcc->cb_cls, 115 &dc); 116 GNUNET_PQ_cleanup_result (rs); 117 if (GNUNET_OK != rval) 118 break; 119 } 120 } 121 122 123 enum GNUNET_DB_QueryStatus 124 TALER_AUDITORDB_iterate_kycauth_in_inconsistencies ( 125 struct TALER_AUDITORDB_PostgresContext *pg, 126 int64_t limit, 127 uint64_t offset, 128 bool return_suppressed, 129 TALER_AUDITORDB_KycauthInInconsistencyCallback cb, 130 void *cb_cls) 131 { 132 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 133 TALER_AUDITORDB_abs_limit (limit)); 134 struct GNUNET_PQ_QueryParam params[] = { 135 GNUNET_PQ_query_param_uint64 (&offset), 136 GNUNET_PQ_query_param_bool (return_suppressed), 137 GNUNET_PQ_query_param_uint64 (&plimit), 138 GNUNET_PQ_query_param_end 139 }; 140 struct KycauthInInconsistencyContext dcc = { 141 .cb = cb, 142 .cb_cls = cb_cls, 143 .pg = pg 144 }; 145 enum GNUNET_DB_QueryStatus qs; 146 147 PREPARE (pg, 148 "iterate_kycauth_in_inconsistencies_get_desc", 149 "SELECT" 150 " row_id" 151 ",bank_row_id" 152 ",amount_exchange_expected" 153 ",amount_wired" 154 ",account_pub" 155 ",timestamp" 156 ",account" 157 ",diagnostic" 158 ",suppressed" 159 " FROM auditor_kycauth_in_inconsistency" 160 " WHERE (row_id < $1)" 161 " AND ($2 OR suppressed is false)" 162 " ORDER BY row_id DESC" 163 " LIMIT $3" 164 ); 165 PREPARE (pg, 166 "iterate_kycauth_in_inconsistencies_get_asc", 167 "SELECT" 168 " row_id" 169 ",bank_row_id" 170 ",amount_exchange_expected" 171 ",amount_wired" 172 ",account_pub" 173 ",timestamp" 174 ",account" 175 ",diagnostic" 176 ",suppressed" 177 " FROM auditor_kycauth_in_inconsistency" 178 " WHERE (row_id > $1)" 179 " AND ($2 OR suppressed is false)" 180 " ORDER BY row_id ASC" 181 " LIMIT $3" 182 ); 183 qs = GNUNET_PQ_eval_prepared_multi_select ( 184 pg->conn, 185 (limit > 0) 186 ? "iterate_kycauth_in_inconsistencies_get_asc" 187 : "iterate_kycauth_in_inconsistencies_get_desc", 188 params, 189 &kycauth_in_inconsistency_cb, 190 &dcc); 191 if (qs > 0) 192 return dcc.qs; 193 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 194 return qs; 195 }