select_money_pots.c (5265B)
1 /* 2 This file is part of TALER 3 Copyright (C) 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 src/backenddb/select_money_pots.c 18 * @brief Implementation of the select_money_pots 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 "merchant-database/select_money_pots.h" 26 #include "helper.h" 27 28 29 /** 30 * Context used for TALER_MERCHANTDB_lookup_money_pots(). 31 */ 32 struct LookupMoneyPotsContext 33 { 34 /** 35 * DB context. 36 */ 37 struct TALER_MERCHANTDB_PostgresContext *pg; 38 39 /** 40 * Function to call with the results. 41 */ 42 TALER_MERCHANTDB_MoneyPotsCallback cb; 43 44 /** 45 * Closure for @a cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Did database result extraction fail? 51 */ 52 bool extract_failed; 53 }; 54 55 56 /** 57 * Function to be called with the results of a SELECT statement 58 * that has returned @a num_results results about money pots. 59 * 60 * @param[in,out] cls of type `struct LookupMoneyPotsContext *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 lookup_money_pots_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct LookupMoneyPotsContext *plc = cls; 70 71 for (unsigned int i = 0; i < num_results; i++) 72 { 73 uint64_t money_pot_serial; 74 char *money_pot_name; 75 size_t pot_total_len; 76 struct TALER_Amount *pot_totals; 77 struct GNUNET_PQ_ResultSpec rs[] = { 78 GNUNET_PQ_result_spec_uint64 ("money_pot_serial", 79 &money_pot_serial), 80 GNUNET_PQ_result_spec_string ("money_pot_name", 81 &money_pot_name), 82 TALER_PQ_result_spec_array_amount_with_currency (plc->pg->conn, 83 "pot_totals", 84 &pot_total_len, 85 &pot_totals), 86 GNUNET_PQ_result_spec_end 87 }; 88 89 if (GNUNET_OK != 90 GNUNET_PQ_extract_result (result, 91 rs, 92 i)) 93 { 94 GNUNET_break (0); 95 plc->extract_failed = true; 96 return; 97 } 98 plc->cb (plc->cb_cls, 99 money_pot_serial, 100 money_pot_name, 101 pot_total_len, 102 pot_totals); 103 GNUNET_PQ_cleanup_result (rs); 104 } 105 } 106 107 108 enum GNUNET_DB_QueryStatus 109 TALER_MERCHANTDB_select_money_pots (struct TALER_MERCHANTDB_PostgresContext *pg, 110 const char *instance_id, 111 int64_t limit, 112 uint64_t offset, 113 TALER_MERCHANTDB_MoneyPotsCallback cb, 114 void *cb_cls) 115 { 116 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 117 struct LookupMoneyPotsContext plc = { 118 .pg = pg, 119 .cb = cb, 120 .cb_cls = cb_cls, 121 /* Can be overwritten by the lookup_money_pots_cb */ 122 .extract_failed = false, 123 }; 124 struct GNUNET_PQ_QueryParam params[] = { 125 GNUNET_PQ_query_param_string (instance_id), 126 GNUNET_PQ_query_param_uint64 (&offset), 127 GNUNET_PQ_query_param_uint64 (&plimit), 128 GNUNET_PQ_query_param_end 129 }; 130 enum GNUNET_DB_QueryStatus qs; 131 132 check_connection (pg); 133 PREPARE (pg, 134 "lookup_money_pots_asc", 135 "SELECT" 136 " money_pot_serial" 137 " ,money_pot_name" 138 " ,pot_totals" 139 " FROM merchant_money_pots" 140 " JOIN merchant_instances" 141 " USING (merchant_serial)" 142 " WHERE merchant_instances.merchant_id=$1" 143 " AND money_pot_serial > $2" 144 " ORDER BY money_pot_serial ASC" 145 " LIMIT $3"); 146 PREPARE (pg, 147 "lookup_money_pots_desc", 148 "SELECT" 149 " money_pot_serial" 150 " ,money_pot_name" 151 " ,pot_totals" 152 " FROM merchant_money_pots" 153 " JOIN merchant_instances" 154 " USING (merchant_serial)" 155 " WHERE merchant_instances.merchant_id=$1" 156 " AND money_pot_serial < $2" 157 " ORDER BY money_pot_serial DESC" 158 " LIMIT $3"); 159 qs = GNUNET_PQ_eval_prepared_multi_select ( 160 pg->conn, 161 (limit > 0) 162 ? "lookup_money_pots_asc" 163 : "lookup_money_pots_desc", 164 params, 165 &lookup_money_pots_cb, 166 &plc); 167 /* If there was an error inside lookup_money_pots_cb, return a hard error. */ 168 if (plc.extract_failed) 169 { 170 GNUNET_break (0); 171 return GNUNET_DB_STATUS_HARD_ERROR; 172 } 173 return qs; 174 }