select_accounts_by_exchange.c (4122B)
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 src/backenddb/select_accounts_by_exchange.c 18 * @brief Implementation of the select_accounts_by_exchange function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/select_accounts_by_exchange.h" 24 #include "helper.h" 25 26 27 /** 28 * Closure for #parse_accounts. 29 */ 30 struct SelectAccountContext 31 { 32 /** 33 * Function to call on each result. 34 */ 35 TALER_MERCHANTDB_ExchangeAccountCallback cb; 36 37 /** 38 * Closure for @e cb. 39 */ 40 void *cb_cls; 41 42 /** 43 * Set to true on failure. 44 */ 45 bool failed; 46 }; 47 48 /** 49 * Function to be called with the results of a SELECT statement 50 * that has returned @a num_results results about accounts. 51 * 52 * @param cls of type `struct SelectAccountContext *` 53 * @param result the postgres result 54 * @param num_results the number of results in @a result 55 */ 56 static void 57 parse_accounts (void *cls, 58 PGresult *result, 59 unsigned int num_results) 60 { 61 struct SelectAccountContext *ctx = cls; 62 63 for (unsigned int i = 0; i < num_results; i++) 64 { 65 struct TALER_FullPayto payto_uri; 66 char *conversion_url = NULL; 67 json_t *debit_restrictions; 68 json_t *credit_restrictions; 69 struct TALER_MasterSignatureP master_sig; 70 struct GNUNET_PQ_ResultSpec rs[] = { 71 GNUNET_PQ_result_spec_auto_from_type ("master_sig", 72 &master_sig), 73 GNUNET_PQ_result_spec_string ("payto_uri", 74 &payto_uri.full_payto), 75 GNUNET_PQ_result_spec_allow_null ( 76 GNUNET_PQ_result_spec_string ("conversion_url", 77 &conversion_url), 78 NULL), 79 TALER_PQ_result_spec_json ("debit_restrictions", 80 &debit_restrictions), 81 TALER_PQ_result_spec_json ("credit_restrictions", 82 &credit_restrictions), 83 GNUNET_PQ_result_spec_end 84 }; 85 86 if (GNUNET_OK != 87 GNUNET_PQ_extract_result (result, 88 rs, 89 i)) 90 { 91 GNUNET_break (0); 92 ctx->failed = true; 93 return; 94 } 95 ctx->cb (ctx->cb_cls, 96 payto_uri, 97 conversion_url, 98 debit_restrictions, 99 credit_restrictions, 100 &master_sig); 101 GNUNET_PQ_cleanup_result (rs); 102 } 103 } 104 105 106 enum GNUNET_DB_QueryStatus 107 TALER_MERCHANTDB_select_accounts_by_exchange ( 108 struct TALER_MERCHANTDB_PostgresContext *pg, 109 const struct TALER_MasterPublicKeyP *master_pub, 110 TALER_MERCHANTDB_ExchangeAccountCallback cb, 111 void *cb_cls) 112 { 113 struct SelectAccountContext ctx = { 114 .cb = cb, 115 .cb_cls = cb_cls 116 }; 117 struct GNUNET_PQ_QueryParam params[] = { 118 GNUNET_PQ_query_param_auto_from_type (master_pub), 119 GNUNET_PQ_query_param_end 120 }; 121 enum GNUNET_DB_QueryStatus qs; 122 123 check_connection (pg); 124 PREPARE (pg, 125 "select_exchange_accounts", 126 "SELECT" 127 " payto_uri" 128 ",conversion_url" 129 ",debit_restrictions::TEXT" 130 ",credit_restrictions::TEXT" 131 ",master_sig" 132 " FROM merchant_exchange_accounts" 133 " WHERE master_pub=$1;"); 134 qs = GNUNET_PQ_eval_prepared_multi_select ( 135 pg->conn, 136 "select_exchange_accounts", 137 params, 138 &parse_accounts, 139 &ctx); 140 if (ctx.failed) 141 return GNUNET_DB_STATUS_HARD_ERROR; 142 return qs; 143 }