iterate_auditor_aml_holds.c (5568B)
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_auditor_aml_holds.c 18 * @brief Implementation of the iterate_auditor_aml_holds function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "auditor-database/iterate_auditor_aml_holds.h" 23 #include "pg_helper.h" 24 25 26 /** 27 * Closure for #auditor_aml_holds_cb(). 28 */ 29 struct AuditorAmlHoldsContext 30 { 31 /** 32 * Function to call for each hold. 33 */ 34 TALER_AUDITORDB_AuditorAmlHoldCallback cb; 35 36 /** 37 * Closure for @e cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_AUDITORDB_PostgresContext *pg; 45 46 /** 47 * Query status to return. 48 */ 49 enum GNUNET_DB_QueryStatus qs; 50 }; 51 52 53 /** 54 * Helper function for #TALER_AUDITORDB_iterate_auditor_aml_holds(). 55 * To be called with the results of a SELECT statement 56 * that has returned @a num_results results. 57 * 58 * @param cls closure of type `struct AuditorAmlHoldsContext *` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 auditor_aml_holds_cb (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct AuditorAmlHoldsContext *ahc = cls; 68 struct TALER_AUDITORDB_PostgresContext *pg = ahc->pg; 69 70 for (unsigned int i = 0; i < num_results; i++) 71 { 72 struct TALER_AUDITORDB_AmlHold ah; 73 struct GNUNET_PQ_ResultSpec rs[] = { 74 GNUNET_PQ_result_spec_uint64 ("row_id", 75 &ah.row_id), 76 GNUNET_PQ_result_spec_auto_from_type ("wtid", 77 &ah.wtid), 78 GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto", 79 &ah.wire_target_h_payto), 80 GNUNET_PQ_result_spec_string ("account", 81 &ah.account.full_payto), 82 TALER_PQ_RESULT_SPEC_AMOUNT ("amount", 83 &ah.amount), 84 GNUNET_PQ_result_spec_uint32 ("deferral_reason", 85 &ah.deferral_reason), 86 GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id", 87 &ah.legitimization_measure_serial_id), 88 GNUNET_PQ_result_spec_absolute_time ("creation_date", 89 &ah.creation_date), 90 GNUNET_PQ_result_spec_bool ("suppressed", 91 &ah.suppressed), 92 GNUNET_PQ_result_spec_end 93 }; 94 enum GNUNET_GenericReturnValue rval; 95 96 if (GNUNET_OK != 97 GNUNET_PQ_extract_result (result, 98 rs, 99 i)) 100 { 101 GNUNET_break (0); 102 ahc->qs = GNUNET_DB_STATUS_HARD_ERROR; 103 return; 104 } 105 ahc->qs = i + 1; 106 rval = ahc->cb (ahc->cb_cls, 107 &ah); 108 GNUNET_PQ_cleanup_result (rs); 109 if (GNUNET_OK != rval) 110 break; 111 } 112 } 113 114 115 enum GNUNET_DB_QueryStatus 116 TALER_AUDITORDB_iterate_auditor_aml_holds ( 117 struct TALER_AUDITORDB_PostgresContext *pg, 118 int64_t limit, 119 uint64_t offset, 120 bool return_suppressed, 121 TALER_AUDITORDB_AuditorAmlHoldCallback cb, 122 TALER_AUDITORDB_AUDITOR_AML_HOLD_RESULT_CLOSURE *cb_cls) 123 { 124 uint64_t plimit = (uint64_t) ((limit < 0) ? -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 AuditorAmlHoldsContext ahc = { 132 .cb = cb, 133 .cb_cls = cb_cls, 134 .pg = pg 135 }; 136 enum GNUNET_DB_QueryStatus qs; 137 138 PREPARE (pg, 139 "iterate_auditor_aml_holds_desc", 140 "SELECT" 141 " row_id" 142 ",wtid" 143 ",wire_target_h_payto" 144 ",account" 145 ",amount" 146 ",deferral_reason" 147 ",legitimization_measure_serial_id" 148 ",creation_date" 149 ",suppressed" 150 " FROM auditor_aml_holds" 151 " WHERE (row_id < $1)" 152 " AND ($2 OR NOT suppressed)" 153 " ORDER BY row_id DESC" 154 " LIMIT $3"); 155 PREPARE (pg, 156 "iterate_auditor_aml_holds_asc", 157 "SELECT" 158 " row_id" 159 ",wtid" 160 ",wire_target_h_payto" 161 ",account" 162 ",amount" 163 ",deferral_reason" 164 ",legitimization_measure_serial_id" 165 ",creation_date" 166 ",suppressed" 167 " FROM auditor_aml_holds" 168 " WHERE (row_id > $1)" 169 " AND ($2 OR NOT suppressed)" 170 " ORDER BY row_id ASC" 171 " LIMIT $3"); 172 qs = GNUNET_PQ_eval_prepared_multi_select ( 173 pg->conn, 174 (limit > 0) 175 ? "iterate_auditor_aml_holds_asc" 176 : "iterate_auditor_aml_holds_desc", 177 params, 178 &auditor_aml_holds_cb, 179 &ahc); 180 if (qs > 0) 181 return ahc.qs; 182 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != qs); 183 return qs; 184 }