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