pg_select_donau_instances.c (5825B)
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 backenddb/pg_select_donau_instances.c 18 * @brief Implementation of the select_donau_instance function for Postgres 19 * @author Bohdan Potuzhnyi 20 * @author Vlada Svirsh 21 */ 22 #include "platform.h" 23 #include <taler/taler_error_codes.h> 24 #include <taler/taler_dbevents.h> 25 #include <taler/taler_pq_lib.h> 26 #include "pg_select_donau_instances.h" 27 #include "taler_merchant_donau.h" 28 #include "pg_helper.h" 29 30 /** 31 * Context for select_donau_instances(). 32 */ 33 struct SelectDonauInstanceContext 34 { 35 /** 36 * Function to call with the results. 37 */ 38 TALER_MERCHANTDB_DonauInstanceCallback cb; 39 40 /** 41 * Closure for @e cb. 42 */ 43 void *cb_cls; 44 45 /** 46 * Did database result extraction fail? 47 */ 48 bool extract_failed; 49 }; 50 51 52 /** 53 * Function to be called with the results of a SELECT statement 54 * that has returned @a num_results results about donau instances. 55 * 56 * @param[in, out] cls of type `struct SelectDonauInstanceContext *` 57 * @param result the postgres result 58 * @param num_results the number of results in @a result 59 */ 60 static void 61 select_donau_instance_cb (void *cls, 62 PGresult *result, 63 unsigned int num_results) 64 { 65 struct SelectDonauInstanceContext *sdc = cls; 66 67 for (unsigned int i = 0; i < num_results; i++) 68 { 69 uint64_t donau_instance_serial; 70 char *donau_url; 71 char *charity_name; 72 struct DONAU_CharityPublicKeyP charity_pub_key; 73 uint64_t charity_id; 74 struct TALER_Amount charity_max_per_year; 75 struct TALER_Amount charity_receipts_to_date; 76 int64_t current_year; 77 json_t *donau_keys_json = NULL; 78 struct GNUNET_PQ_ResultSpec rs[] = { 79 GNUNET_PQ_result_spec_uint64 ("donau_instances_serial", 80 &donau_instance_serial), 81 GNUNET_PQ_result_spec_string ("donau_url", 82 &donau_url), 83 GNUNET_PQ_result_spec_string ("charity_name", 84 &charity_name), 85 GNUNET_PQ_result_spec_auto_from_type ("charity_pub_key", 86 &charity_pub_key), 87 GNUNET_PQ_result_spec_uint64 ("charity_id", 88 &charity_id), 89 TALER_PQ_result_spec_amount_with_currency ("charity_max_per_year", 90 &charity_max_per_year), 91 TALER_PQ_result_spec_amount_with_currency ("charity_receipts_to_date", 92 &charity_receipts_to_date), 93 GNUNET_PQ_result_spec_int64 ("current_year", 94 ¤t_year), 95 GNUNET_PQ_result_spec_allow_null ( 96 TALER_PQ_result_spec_json ("keys_json", 97 &donau_keys_json), 98 NULL), 99 GNUNET_PQ_result_spec_end 100 }; 101 102 if (GNUNET_OK != 103 GNUNET_PQ_extract_result (result, 104 rs, 105 i)) 106 { 107 GNUNET_break (0); 108 sdc->extract_failed = true; 109 return; 110 } 111 sdc->cb (sdc->cb_cls, 112 donau_instance_serial, 113 donau_url, 114 charity_name, 115 &charity_pub_key, 116 charity_id, 117 &charity_max_per_year, 118 &charity_receipts_to_date, 119 current_year, 120 donau_keys_json); 121 GNUNET_PQ_cleanup_result (rs); 122 } 123 } 124 125 126 enum GNUNET_DB_QueryStatus 127 TMH_PG_select_donau_instances (void *cls, 128 const char *id, 129 TALER_MERCHANTDB_DonauInstanceCallback cb, 130 void *cb_cls) 131 { 132 struct PostgresClosure *pg = cls; 133 struct SelectDonauInstanceContext sdc = { 134 .cb = cb, 135 .cb_cls = cb_cls, 136 /* Can be overwritten by the select_donau_instance_cb */ 137 .extract_failed = false, 138 }; 139 struct GNUNET_PQ_QueryParam params[] = { 140 GNUNET_PQ_query_param_string (id), 141 GNUNET_PQ_query_param_end 142 }; 143 enum GNUNET_DB_QueryStatus qs; 144 145 check_connection (pg); 146 PREPARE (pg, 147 "select_donau_instances", 148 "SELECT" 149 " di.donau_instances_serial" 150 ",di.donau_url" 151 ",di.charity_name" 152 ",mi.merchant_pub AS charity_pub_key" 153 ",di.charity_id" 154 ",di.charity_max_per_year" 155 ",di.charity_receipts_to_date" 156 ",di.current_year" 157 ",dk.keys_json::TEXT" 158 " FROM merchant_donau_instances di" 159 " LEFT JOIN merchant_donau_keys dk" 160 " ON di.donau_url = dk.donau_url" 161 " JOIN merchant_instances mi" 162 " ON di.merchant_instance_serial = mi.merchant_serial" 163 " WHERE mi.merchant_id = $1"); 164 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 165 "select_donau_instances", 166 params, 167 &select_donau_instance_cb, 168 &sdc); 169 170 /* If there was an error inside select_donau_instance_cb, return a hard error. */ 171 if (sdc.extract_failed) 172 return GNUNET_DB_STATUS_HARD_ERROR; 173 174 return qs; 175 }