helper.h (5109B)
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 pg_helper.h 18 * @brief shared internal definitions for postgres DB plugin 19 * @author Christian Grothoff 20 */ 21 #ifndef PG_HELPER_H 22 #define PG_HELPER_H 23 24 #include "taler/taler_dbevents.h" /* UNNECESSARY? */ 25 #include "taler/taler_error_codes.h" /* UNNECESSARY? */ 26 #include "exchangedb_lib.h" 27 #include "taler/taler_json_lib.h" 28 #include "taler/taler_pq_lib.h" 29 #include "taler/taler_util.h" 30 31 32 /** 33 * Context for a Postgres database connection for the exchange. 34 */ 35 struct TALER_EXCHANGEDB_PostgresContext 36 { 37 38 /** 39 * Our configuration. 40 */ 41 const struct GNUNET_CONFIGURATION_Handle *cfg; 42 43 /** 44 * Directory with SQL statements to run to create tables. 45 */ 46 char *sql_dir; 47 48 /** 49 * How long are AML programs allowed to run? 50 */ 51 struct GNUNET_TIME_Relative max_aml_program_runtime; 52 53 /** 54 * After how long should idle reserves be closed? 55 */ 56 struct GNUNET_TIME_Relative idle_reserve_expiration_time; 57 58 /** 59 * After how long should reserves that have seen withdraw operations 60 * be garbage collected? 61 */ 62 struct GNUNET_TIME_Relative legal_reserve_expiration_time; 63 64 /** 65 * What delay should we introduce before ready transactions 66 * are actually aggregated? 67 */ 68 struct GNUNET_TIME_Relative aggregator_shift; 69 70 /** 71 * Which currency should we assume all amounts to be in? 72 */ 73 char *currency; 74 75 /** 76 * Our base URL. 77 */ 78 char *exchange_url; 79 80 /** 81 * Postgres connection handle. 82 */ 83 struct GNUNET_PQ_Context *conn; 84 85 /** 86 * Name of the current transaction, for debugging. 87 */ 88 const char *transaction_name; 89 90 /** 91 * Number of purses we allow to be opened concurrently 92 * for one year per annual fee payment. 93 */ 94 uint32_t def_purse_limit; 95 96 }; 97 98 99 /** 100 * Counts how often we have established a fresh @e conn 101 * to the database. Used to re-prepare statements. 102 */ 103 extern unsigned long long TEH_PG_prep_gen_; 104 105 106 /** 107 * Prepares SQL statement @a sql under @a name for 108 * connection @a pg once. 109 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 110 * 111 * @param pg a `struct TALER_EXCHANGEDB_PostgresContext` 112 * @param name name to prepare the statement under 113 * @param sql actual SQL text 114 */ 115 #define PREPARE(pg,name,sql) \ 116 do { \ 117 static struct { \ 118 unsigned long long cnt; \ 119 struct TALER_EXCHANGEDB_PostgresContext *pg; \ 120 } preps_[2]; /* 2 ctrs for taler-auditor-sync*/ \ 121 unsigned int off_ = 0; \ 122 \ 123 while ( (NULL != preps_[off_].pg) && \ 124 (pg != preps_[off_].pg) && \ 125 (off_ < sizeof(preps_) / sizeof(*preps_)) ) \ 126 off_++; \ 127 GNUNET_assert (off_ < \ 128 sizeof(preps_) / sizeof(*preps_)); \ 129 if (preps_[off_].cnt < TEH_PG_prep_gen_) \ 130 { \ 131 struct GNUNET_PQ_PreparedStatement ps[] = { \ 132 GNUNET_PQ_make_prepare (name, sql), \ 133 GNUNET_PQ_PREPARED_STATEMENT_END \ 134 }; \ 135 \ 136 if (GNUNET_OK != \ 137 GNUNET_PQ_prepare_statements (pg->conn, \ 138 ps)) \ 139 { \ 140 GNUNET_break (0); \ 141 return GNUNET_DB_STATUS_HARD_ERROR; \ 142 } \ 143 preps_[off_].pg = pg; \ 144 preps_[off_].cnt = TEH_PG_prep_gen_; \ 145 } \ 146 } while (0) 147 148 149 /** 150 * Wrapper macro to add the currency from the plugin's state 151 * when fetching amounts from the database. 152 * 153 * @param field name of the database field to fetch amount from 154 * @param[out] amountp pointer to amount to set 155 */ 156 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \ 157 amountp) TALER_PQ_result_spec_amount ( \ 158 field,pg->currency,amountp) 159 160 161 #endif