pg_select_all_donau_instances.c (5765B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 2025 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_all_donau_instances.c 18 * @brief Implementation of the select_donau_instance function for Postgres 19 * @author Bohdan Potuzhnyi 20 * @author Vlada Svirsh 21 * @author Christian Grothoff 22 */ 23 #include "platform.h" 24 #include <taler/taler_error_codes.h> 25 #include <taler/taler_dbevents.h> 26 #include <taler/taler_pq_lib.h> 27 #include "pg_select_all_donau_instances.h" 28 #include "taler_merchant_donau.h" 29 #include "pg_helper.h" 30 31 /** 32 * Context for select_donau_instances(). 33 */ 34 struct SelectDonauInstanceContext 35 { 36 /** 37 * Function to call with the results. 38 */ 39 TALER_MERCHANTDB_DonauInstanceCallback cb; 40 41 /** 42 * Closure for @e cb. 43 */ 44 void *cb_cls; 45 46 /** 47 * Did database result extraction fail? 48 */ 49 bool extract_failed; 50 }; 51 52 53 /** 54 * Function to be called with the results of a SELECT statement 55 * that has returned @a num_results results about donau instances. 56 * 57 * @param[in, out] cls of type `struct SelectDonauInstanceContext *` 58 * @param result the postgres result 59 * @param num_results the number of results in @a result 60 */ 61 static void 62 select_donau_instance_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct SelectDonauInstanceContext *sdc = cls; 67 68 for (unsigned int i = 0; i < num_results; i++) 69 { 70 uint64_t donau_instance_serial; 71 char *donau_url; 72 char *charity_name; 73 struct DONAU_CharityPublicKeyP charity_pub_key; 74 uint64_t charity_id; 75 struct TALER_Amount charity_max_per_year; 76 struct TALER_Amount charity_receipts_to_date; 77 int64_t current_year; 78 json_t *donau_keys_json = NULL; 79 struct GNUNET_PQ_ResultSpec rs[] = { 80 GNUNET_PQ_result_spec_uint64 ("donau_instances_serial", 81 &donau_instance_serial), 82 GNUNET_PQ_result_spec_string ("donau_url", 83 &donau_url), 84 GNUNET_PQ_result_spec_string ("charity_name", 85 &charity_name), 86 GNUNET_PQ_result_spec_auto_from_type ("charity_pub_key", 87 &charity_pub_key), 88 GNUNET_PQ_result_spec_uint64 ("charity_id", 89 &charity_id), 90 TALER_PQ_result_spec_amount_with_currency ("charity_max_per_year", 91 &charity_max_per_year), 92 TALER_PQ_result_spec_amount_with_currency ("charity_receipts_to_date", 93 &charity_receipts_to_date), 94 GNUNET_PQ_result_spec_int64 ("current_year", 95 ¤t_year), 96 GNUNET_PQ_result_spec_allow_null ( 97 TALER_PQ_result_spec_json ("keys_json", 98 &donau_keys_json), 99 NULL), 100 GNUNET_PQ_result_spec_end 101 }; 102 103 if (GNUNET_OK != 104 GNUNET_PQ_extract_result (result, 105 rs, 106 i)) 107 { 108 GNUNET_break (0); 109 sdc->extract_failed = true; 110 return; 111 } 112 sdc->cb (sdc->cb_cls, 113 donau_instance_serial, 114 donau_url, 115 charity_name, 116 &charity_pub_key, 117 charity_id, 118 &charity_max_per_year, 119 &charity_receipts_to_date, 120 current_year, 121 donau_keys_json); 122 GNUNET_PQ_cleanup_result (rs); 123 } 124 } 125 126 127 enum GNUNET_DB_QueryStatus 128 TMH_PG_select_all_donau_instances (void *cls, 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_end 141 }; 142 enum GNUNET_DB_QueryStatus qs; 143 144 check_connection (pg); 145 PREPARE (pg, 146 "select_all_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 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 163 "select_all_donau_instances", 164 params, 165 &select_donau_instance_cb, 166 &sdc); 167 168 /* If there was an error inside select_donau_instance_cb, return a hard error. */ 169 if (sdc.extract_failed) 170 return GNUNET_DB_STATUS_HARD_ERROR; 171 172 return qs; 173 }