pg_select_aml_measures.c (5918B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 2025 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 exchangedb/pg_select_aml_measures.c 18 * @brief Implementation of the select_aml_measures function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "taler/exchange-database/select_aml_measures.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #handle_aml_result. 28 */ 29 struct LegiMeasureResultContext 30 { 31 /** 32 * Function to call on each result. 33 */ 34 TALER_EXCHANGEDB_LegitimizationMeasureCallback cb; 35 36 /** 37 * Closure for @e cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Plugin context. 43 */ 44 struct TALER_EXCHANGEDB_PostgresContext *pg; 45 46 /** 47 * Set to #GNUNET_SYSERR on serious errors. 48 */ 49 enum GNUNET_GenericReturnValue status; 50 }; 51 52 53 /** 54 * Function to be called with the results of a SELECT statement 55 * that has returned @a num_results results. Helper function 56 * for #TALER_EXCHANGEDB_select_aml_measures(). 57 * 58 * @param cls closure of type `struct LegiMeasureResultContext *` 59 * @param result the postgres result 60 * @param num_results the number of results in @a result 61 */ 62 static void 63 handle_aml_result (void *cls, 64 PGresult *result, 65 unsigned int num_results) 66 { 67 struct LegiMeasureResultContext *ctx = cls; 68 69 for (unsigned int i = 0; i<num_results; i++) 70 { 71 struct TALER_NormalizedPaytoHashP h_payto; 72 uint64_t rowid; 73 struct GNUNET_TIME_Absolute start_time; 74 json_t *jmeasures; 75 bool is_finished; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_uint64 ("legitimization_measure_serial_id", 78 &rowid), 79 GNUNET_PQ_result_spec_auto_from_type ("h_normalized_payto", 80 &h_payto), 81 GNUNET_PQ_result_spec_absolute_time ("start_time", 82 &start_time), 83 TALER_PQ_result_spec_json ("jmeasures", 84 &jmeasures), 85 GNUNET_PQ_result_spec_bool ("is_finished", 86 &is_finished), 87 GNUNET_PQ_result_spec_end 88 }; 89 90 if (GNUNET_OK != 91 GNUNET_PQ_extract_result (result, 92 rs, 93 i)) 94 { 95 GNUNET_break (0); 96 ctx->status = GNUNET_SYSERR; 97 return; 98 } 99 ctx->cb (ctx->cb_cls, 100 &h_payto, 101 start_time, 102 jmeasures, 103 is_finished, 104 rowid); 105 GNUNET_PQ_cleanup_result (rs); 106 } 107 } 108 109 110 enum GNUNET_DB_QueryStatus 111 TALER_EXCHANGEDB_select_aml_measures ( 112 struct TALER_EXCHANGEDB_PostgresContext *pg, 113 const struct TALER_NormalizedPaytoHashP *h_payto, 114 enum TALER_EXCHANGE_YesNoAll active_only, 115 uint64_t offset, 116 int64_t limit, 117 TALER_EXCHANGEDB_LegitimizationMeasureCallback cb, 118 void *cb_cls) 119 { 120 uint64_t ulimit = (limit > 0) ? limit : -limit; 121 struct GNUNET_PQ_QueryParam params[] = { 122 GNUNET_PQ_query_param_bool (NULL == h_payto), 123 NULL == h_payto 124 ? GNUNET_PQ_query_param_null () 125 : GNUNET_PQ_query_param_auto_from_type (h_payto), 126 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == 127 active_only)), 128 GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_NO == 129 active_only)), 130 GNUNET_PQ_query_param_uint64 (&offset), 131 GNUNET_PQ_query_param_uint64 (&ulimit), 132 GNUNET_PQ_query_param_end 133 }; 134 struct LegiMeasureResultContext ctx = { 135 .cb = cb, 136 .cb_cls = cb_cls, 137 .pg = pg, 138 .status = GNUNET_OK 139 }; 140 enum GNUNET_DB_QueryStatus qs; 141 const char *stmt = (limit > 0) 142 ? "select_aml_measures_inc" 143 : "select_aml_measures_dec"; 144 145 PREPARE (pg, 146 "select_aml_measures_inc", 147 "SELECT" 148 " lm.legitimization_measure_serial_id" 149 ",kt.h_normalized_payto" 150 ",lm.jmeasures::TEXT" 151 ",lm.start_time" 152 ",lm.is_finished" 153 " FROM kyc_targets kt" 154 " JOIN legitimization_measures lm" 155 " USING (access_token)" 156 " WHERE (legitimization_measure_serial_id > $5)" 157 " AND ($1 OR (kt.h_normalized_payto = $2))" 158 " AND ($3 OR (lm.is_finished = $4))" 159 " ORDER BY lm.legitimization_measure_serial_id ASC" 160 " LIMIT $6"); 161 PREPARE (pg, 162 "select_aml_measures_dec", 163 "SELECT" 164 " lm.legitimization_measure_serial_id" 165 ",kt.h_normalized_payto" 166 ",lm.jmeasures::TEXT" 167 ",lm.start_time" 168 ",lm.is_finished" 169 " FROM kyc_targets kt" 170 " JOIN legitimization_measures lm" 171 " USING (access_token)" 172 " WHERE (legitimization_measure_serial_id < $5)" 173 " AND ($1 OR (kt.h_normalized_payto = $2))" 174 " AND ($3 OR (lm.is_finished = $4))" 175 " ORDER BY lm.legitimization_measure_serial_id DESC" 176 " LIMIT $6"); 177 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 178 stmt, 179 params, 180 &handle_aml_result, 181 &ctx); 182 if (GNUNET_OK != ctx.status) 183 return GNUNET_DB_STATUS_HARD_ERROR; 184 return qs; 185 }