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