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