iterate_kyc_references.c (4386B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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 iterate_kyc_references.c 18 * @brief Low-level (statement-level) Postgres database access for the exchange 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "exchange-database/iterate_kyc_references.h" 23 #include "helper.h" 24 25 26 /** 27 * Closure for #iterate_kyc_reference_cb() 28 */ 29 struct IteratorContext 30 { 31 /** 32 * Function to call with the results. 33 */ 34 TALER_EXCHANGEDB_LegitimizationProcessCallback cb; 35 36 /** 37 * Closure to pass to @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 if a row could not be read. 48 */ 49 enum GNUNET_GenericReturnValue status; 50 }; 51 52 53 /** 54 * Helper function for #TALER_EXCHANGEDB_iterate_kyc_references(). 55 * Calls the callback with each denomination key. 56 * 57 * @param cls a `struct IteratorContext` 58 * @param result db results 59 * @param num_results number of results in @a result 60 */ 61 static void 62 iterate_kyc_reference_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct IteratorContext *ic = cls; 67 68 for (unsigned int i = 0; i<num_results; i++) 69 { 70 char *kyc_provider_name_name; 71 char *provider_user_id = NULL; 72 char *legitimization_id = NULL; 73 bool no_user_id; 74 bool no_legitimization_id; 75 /* Both provider-side identifiers are NULL until the provider hands 76 them out -- and stay NULL for a process the exchange created for an 77 AML program that failed before one was started. Reading them as 78 non-NULL used to abort the whole iteration on the first such row, 79 silently truncating the caller's list. */ 80 struct GNUNET_PQ_ResultSpec rs[] = { 81 GNUNET_PQ_result_spec_string ("provider_name", 82 &kyc_provider_name_name), 83 GNUNET_PQ_result_spec_allow_null ( 84 GNUNET_PQ_result_spec_string ("provider_user_id", 85 &provider_user_id), 86 &no_user_id), 87 GNUNET_PQ_result_spec_allow_null ( 88 GNUNET_PQ_result_spec_string ("provider_legitimization_id", 89 &legitimization_id), 90 &no_legitimization_id), 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 ic->status = GNUNET_SYSERR; 101 return; 102 } 103 ic->cb (ic->cb_cls, 104 kyc_provider_name_name, 105 no_user_id ? NULL : provider_user_id, 106 no_legitimization_id ? NULL : legitimization_id); 107 GNUNET_PQ_cleanup_result (rs); 108 } 109 } 110 111 112 enum GNUNET_DB_QueryStatus 113 TALER_EXCHANGEDB_iterate_kyc_references ( 114 struct TALER_EXCHANGEDB_PostgresContext *pg, 115 const struct TALER_NormalizedPaytoHashP *h_payto, 116 TALER_EXCHANGEDB_LegitimizationProcessCallback lpc, 117 void *lpc_cls) 118 { 119 struct GNUNET_PQ_QueryParam params[] = { 120 GNUNET_PQ_query_param_auto_from_type (h_payto), 121 GNUNET_PQ_query_param_end 122 }; 123 struct IteratorContext ic = { 124 .cb = lpc, 125 .cb_cls = lpc_cls, 126 .pg = pg, 127 .status = GNUNET_OK 128 }; 129 enum GNUNET_DB_QueryStatus qs; 130 131 PREPARE (pg, 132 "iterate_kyc_references", 133 "SELECT " 134 " provider_name" 135 ",provider_user_id" 136 ",provider_legitimization_id" 137 " FROM legitimization_processes" 138 " WHERE h_payto=$1;"); 139 qs = GNUNET_PQ_eval_prepared_multi_select ( 140 pg->conn, 141 "iterate_kyc_references", 142 params, 143 &iterate_kyc_reference_cb, 144 &ic); 145 if (GNUNET_OK != ic.status) 146 return GNUNET_DB_STATUS_HARD_ERROR; 147 return qs; 148 }