pg_select_aml_attributes.c (6201B)
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 /** 17 * @file exchangedb/pg_select_aml_attributes.c 18 * @brief Implementation of the select_aml_attributes 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_attributes.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Closure for #handle_aml_result. 31 */ 32 struct AmlAttributeResultContext 33 { 34 /** 35 * Function to call on each result. 36 */ 37 TALER_EXCHANGEDB_AmlAttributeCallback 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_attributes(). 60 * 61 * @param cls closure of type `struct AmlAttributeResultContext *` 62 * @param result the postgres result 63 * @param num_results the number of results in @a result 64 */ 65 static void 66 handle_aml_attributes (void *cls, 67 PGresult *result, 68 unsigned int num_results) 69 { 70 struct AmlAttributeResultContext *ctx = cls; 71 72 for (unsigned int i = 0; i<num_results; i++) 73 { 74 uint64_t rowid; 75 struct GNUNET_TIME_Timestamp collection_time; 76 char *officer_name = NULL; 77 bool by_aml_officer; 78 size_t enc_attributes_size; 79 void *enc_attributes; 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id", 82 &rowid), 83 GNUNET_PQ_result_spec_timestamp ("collection_time", 84 &collection_time), 85 GNUNET_PQ_result_spec_bool ("by_aml_officer", 86 &by_aml_officer), 87 GNUNET_PQ_result_spec_allow_null ( 88 GNUNET_PQ_result_spec_string ("decider_name", 89 &officer_name), 90 NULL), 91 GNUNET_PQ_result_spec_variable_size ("encrypted_attributes", 92 &enc_attributes, 93 &enc_attributes_size), 94 GNUNET_PQ_result_spec_end 95 }; 96 97 if (GNUNET_OK != 98 GNUNET_PQ_extract_result (result, 99 rs, 100 i)) 101 { 102 GNUNET_break (0); 103 ctx->status = GNUNET_SYSERR; 104 return; 105 } 106 107 ctx->cb (ctx->cb_cls, 108 rowid, 109 collection_time, 110 by_aml_officer, 111 officer_name, 112 enc_attributes_size, 113 enc_attributes); 114 GNUNET_PQ_cleanup_result (rs); 115 } 116 } 117 118 119 enum GNUNET_DB_QueryStatus 120 TEH_PG_select_aml_attributes ( 121 void *cls, 122 const struct TALER_NormalizedPaytoHashP *h_payto, 123 uint64_t offset, 124 int64_t limit, 125 TALER_EXCHANGEDB_AmlAttributeCallback cb, 126 void *cb_cls) 127 { 128 struct PostgresClosure *pg = cls; 129 uint64_t ulimit = (limit > 0) ? limit : -limit; 130 struct GNUNET_PQ_QueryParam params[] = { 131 GNUNET_PQ_query_param_auto_from_type (h_payto), 132 GNUNET_PQ_query_param_uint64 (&offset), 133 GNUNET_PQ_query_param_uint64 (&ulimit), 134 GNUNET_PQ_query_param_end 135 }; 136 struct AmlAttributeResultContext ctx = { 137 .cb = cb, 138 .cb_cls = cb_cls, 139 .pg = pg, 140 .status = GNUNET_OK 141 }; 142 enum GNUNET_DB_QueryStatus qs; 143 const char *stmt = (limit > 0) 144 ? "select_aml_attributes_inc" 145 : "select_aml_attributes_dec"; 146 147 PREPARE (pg, 148 "select_aml_attributes_inc", 149 "SELECT" 150 " ka.kyc_attributes_serial_id" 151 ",ka.collection_time" 152 ",ka.by_aml_officer" 153 ",astaff.decider_name" 154 ",ka.encrypted_attributes" 155 " FROM kyc_attributes ka" 156 " LEFT JOIN legitimization_processes lp" 157 " ON (ka.by_aml_officer AND" 158 " (ka.legitimization_serial = lp.legitimization_process_serial_id))" 159 " LEFT JOIN aml_staff astaff" 160 " ON (ka.by_aml_officer AND" 161 " (DECODE(lp.provider_user_id, 'base64') = astaff.decider_pub))" 162 " WHERE ka.h_payto=$1" 163 " AND ka.kyc_attributes_serial_id > $2" 164 " ORDER BY ka.kyc_attributes_serial_id ASC" 165 " LIMIT $3"); 166 PREPARE (pg, 167 "select_aml_attributes_dec", 168 "SELECT" 169 " ka.kyc_attributes_serial_id" 170 ",ka.collection_time" 171 ",ka.by_aml_officer" 172 ",astaff.decider_name" 173 ",ka.encrypted_attributes" 174 " FROM kyc_attributes ka" 175 " LEFT JOIN legitimization_processes lp" 176 " ON (ka.by_aml_officer AND" 177 " (ka.legitimization_serial = lp.legitimization_process_serial_id))" 178 " LEFT JOIN aml_staff astaff" 179 " ON (ka.by_aml_officer AND" 180 " (DECODE(lp.provider_user_id, 'base64') = astaff.decider_pub))" 181 " WHERE ka.h_payto=$1" 182 " AND ka.kyc_attributes_serial_id < $2" 183 " ORDER BY ka.kyc_attributes_serial_id DESC" 184 " LIMIT $3"); 185 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 186 stmt, 187 params, 188 &handle_aml_attributes, 189 &ctx); 190 if (GNUNET_OK != ctx.status) 191 return GNUNET_DB_STATUS_HARD_ERROR; 192 return qs; 193 }