iterate_all_kyc_attributes.c (5229B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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/iterate_all_kyc_attributes.c 18 * @brief Implementation of the iterate_all_kyc_attributes function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/iterate_all_kyc_attributes.h" 23 #include "helper.h" 24 25 /** 26 * Closure for #get_all_attributes_cb(). 27 */ 28 struct GetAttributesContext 29 { 30 /** 31 * Function to call per result. 32 */ 33 TALER_EXCHANGEDB_AllAttributesCallback cb; 34 35 /** 36 * Closure for @e cb. 37 */ 38 void *cb_cls; 39 40 /** 41 * Plugin context. 42 */ 43 struct TALER_EXCHANGEDB_PostgresContext *pg; 44 45 /** 46 * Flag set to #GNUNET_OK as long as everything is fine. 47 */ 48 enum GNUNET_GenericReturnValue status; 49 50 }; 51 52 /** 53 * Invoke the callback for each result. 54 * 55 * @param cls a `struct GetAttributesContext *` 56 * @param result SQL result 57 * @param num_results number of rows in @a result 58 */ 59 static void 60 get_attributes_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct GetAttributesContext *ctx = cls; 65 66 for (unsigned int i = 0; i < num_results; i++) 67 { 68 uint64_t rowid; 69 struct GNUNET_TIME_Timestamp collection_time; 70 struct GNUNET_TIME_Timestamp expiration_time; 71 struct TALER_NormalizedPaytoHashP h_payto; 72 json_t *properties = NULL; 73 size_t enc_attributes_size; 74 void *enc_attributes; 75 char *provider; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_string ("provider_name", 78 &provider), 79 GNUNET_PQ_result_spec_uint64 ("kyc_attributes_serial_id", 80 &rowid), 81 GNUNET_PQ_result_spec_allow_null ( 82 TALER_PQ_result_spec_json ("jproperties", 83 &properties), 84 NULL), 85 GNUNET_PQ_result_spec_timestamp ("collection_time", 86 &collection_time), 87 GNUNET_PQ_result_spec_timestamp ("expiration_time", 88 &expiration_time), 89 GNUNET_PQ_result_spec_auto_from_type ("h_payto", 90 &h_payto), 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 bool cont; 108 109 cont = ctx->cb (ctx->cb_cls, 110 rowid, 111 &h_payto, 112 provider, 113 collection_time, 114 expiration_time, 115 properties, 116 enc_attributes_size, 117 enc_attributes); 118 GNUNET_PQ_cleanup_result (rs); 119 if (! cont) 120 return; 121 } 122 } 123 } 124 125 126 enum GNUNET_DB_QueryStatus 127 TALER_EXCHANGEDB_iterate_all_kyc_attributes ( 128 struct TALER_EXCHANGEDB_PostgresContext *pg, 129 uint64_t min_row_id, 130 TALER_EXCHANGEDB_AllAttributesCallback cb, 131 void *cb_cls) 132 { 133 struct GNUNET_PQ_QueryParam params[] = { 134 GNUNET_PQ_query_param_uint64 (&min_row_id), 135 GNUNET_PQ_query_param_end 136 }; 137 struct GetAttributesContext ctx = { 138 .cb = cb, 139 .cb_cls = cb_cls, 140 .pg = pg, 141 .status = GNUNET_OK 142 }; 143 enum GNUNET_DB_QueryStatus qs; 144 145 PREPARE (pg, 146 "iterate_all_kyc_attributes", 147 "SELECT " 148 " lp.provider_name" 149 ",ka.h_payto" 150 ",ka.kyc_attributes_serial_id" 151 ",lo.jproperties::TEXT" 152 ",ka.collection_time" 153 ",ka.expiration_time" 154 ",ka.encrypted_attributes" 155 " FROM kyc_attributes ka" 156 " JOIN legitimization_processes lp" 157 " ON (ka.legitimization_serial = lp.legitimization_process_serial_id)" 158 " LEFT JOIN legitimization_outcomes lo" 159 " ON (ka.h_payto = lo.h_payto)" 160 /* **IF** we joined with 'lo', the lo must be active */ 161 " WHERE COALESCE(lo.is_active,TRUE)" 162 " AND kyc_attributes_serial_id > $1" 163 " ORDER BY kyc_attributes_serial_id ASC" 164 ); 165 qs = GNUNET_PQ_eval_prepared_multi_select ( 166 pg->conn, 167 "iterate_all_kyc_attributes", 168 params, 169 &get_attributes_cb, 170 &ctx); 171 if (GNUNET_OK != ctx.status) 172 return GNUNET_DB_STATUS_HARD_ERROR; 173 return qs; 174 }