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