helper.h (5103B)
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 * Counts how often we have established a fresh @e conn 92 * to the database. Used to re-prepare statements. 93 */ 94 unsigned long long prep_gen; 95 96 /** 97 * Number of purses we allow to be opened concurrently 98 * for one year per annual fee payment. 99 */ 100 uint32_t def_purse_limit; 101 102 }; 103 104 105 /** 106 * Prepares SQL statement @a sql under @a name for 107 * connection @a pg once. 108 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 109 * 110 * @param pg a `struct TALER_EXCHANGEDB_PostgresContext` 111 * @param name name to prepare the statement under 112 * @param sql actual SQL text 113 */ 114 #define PREPARE(pg,name,sql) \ 115 do { \ 116 static struct { \ 117 unsigned long long cnt; \ 118 struct TALER_EXCHANGEDB_PostgresContext *pg; \ 119 } preps_[2]; /* 2 ctrs for taler-auditor-sync*/ \ 120 unsigned int off_ = 0; \ 121 \ 122 while ( (NULL != preps_[off_].pg) && \ 123 (pg != preps_[off_].pg) && \ 124 (off_ < sizeof(preps_) / sizeof(*preps_)) ) \ 125 off_++; \ 126 GNUNET_assert (off_ < \ 127 sizeof(preps_) / sizeof(*preps_)); \ 128 if (preps_[off_].cnt < pg->prep_gen) \ 129 { \ 130 struct GNUNET_PQ_PreparedStatement ps[] = { \ 131 GNUNET_PQ_make_prepare (name, sql), \ 132 GNUNET_PQ_PREPARED_STATEMENT_END \ 133 }; \ 134 \ 135 if (GNUNET_OK != \ 136 GNUNET_PQ_prepare_statements (pg->conn, \ 137 ps)) \ 138 { \ 139 GNUNET_break (0); \ 140 return GNUNET_DB_STATUS_HARD_ERROR; \ 141 } \ 142 preps_[off_].pg = pg; \ 143 preps_[off_].cnt = pg->prep_gen; \ 144 } \ 145 } while (0) 146 147 148 /** 149 * Wrapper macro to add the currency from the plugin's state 150 * when fetching amounts from the database. 151 * 152 * @param field name of the database field to fetch amount from 153 * @param[out] amountp pointer to amount to set 154 */ 155 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \ 156 amountp) TALER_PQ_result_spec_amount ( \ 157 field,pg->currency,amountp) 158 159 160 #endif