helper.h (4512B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 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/helper.h 18 * @brief shared internal definitions for postgres logic 19 * @author Christian Grothoff 20 */ 21 #ifndef HELPER_H 22 #define HELPER_H 23 24 #include <gnunet/gnunet_util_lib.h> 25 #include <gnunet/gnunet_db_lib.h> 26 #include <gnunet/gnunet_time_lib.h> 27 #include <taler/taler_util.h> 28 29 /** 30 * Type of the "cls" argument given to each of the functions in 31 * our API. 32 */ 33 struct TALER_MERCHANTDB_PostgresContext 34 { 35 36 /** 37 * Postgres connection handle. 38 */ 39 struct GNUNET_PQ_Context *conn; 40 41 /** 42 * Underlying configuration. 43 */ 44 const struct GNUNET_CONFIGURATION_Handle *cfg; 45 46 /** 47 * Name of the currently active transaction, NULL if none is active. 48 */ 49 const char *transaction_name; 50 51 /** 52 * Instance id ("merchant_id") that the search_path is currently 53 * pointing at, or NULL if no per-instance schema is selected. 54 * Owned by this struct; set by TALER_MERCHANTDB_set_instance(). 55 */ 56 char *current_merchant_id; 57 58 /** 59 * merchant_serial corresponding to @e current_merchant_id, or 0 60 * if no per-instance schema is selected. Used as the suffix in 61 * per-instance prepared-statement names. 62 */ 63 uint64_t current_merchant_serial; 64 65 /** 66 * Public key of the currently selected instance. Populated by 67 * TALER_MERCHANTDB_set_instance() together with @e current_merchant_id 68 * and @e current_merchant_serial. Used by call sites that emit 69 * cross-process events (e.g. order-pay notifications) which carry the 70 * instance public key in their payload. 71 */ 72 struct TALER_MerchantPublicKeyP current_merchant_pub; 73 74 }; 75 76 77 /** 78 * How many times have we connected to the DB. 79 */ 80 extern uint64_t TMH_PG_prep_gen_; 81 82 83 /** 84 * Prepares SQL statement @a sql under @a name for 85 * connection @a pg once. 86 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 87 * 88 * @param pg a `struct TALER_MERCHANTDB_PostgresContext` 89 * @param name name to prepare the statement under 90 * @param sql actual SQL text 91 */ 92 #define PREPARE(pg,name,sql) \ 93 do { \ 94 static unsigned long long gen; \ 95 \ 96 if (gen < TMH_PG_prep_gen_) \ 97 { \ 98 struct GNUNET_PQ_PreparedStatement ps[] = { \ 99 GNUNET_PQ_make_prepare (name, sql), \ 100 GNUNET_PQ_PREPARED_STATEMENT_END \ 101 }; \ 102 \ 103 if (GNUNET_OK != \ 104 GNUNET_PQ_prepare_statements (pg->conn, \ 105 ps)) \ 106 { \ 107 GNUNET_break (0); \ 108 return GNUNET_DB_STATUS_HARD_ERROR; \ 109 } \ 110 gen = TMH_PG_prep_gen_; \ 111 } \ 112 } while (0) 113 114 115 /** 116 * Prepares SQL statement @a sql under no name ("") for 117 * connection @a pg. Useful for prepared statements that 118 * should not be cached. 119 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 120 * 121 * @param pg a `struct TALER_MERCHANTDB_PostgresContext` 122 * @param sql actual SQL text 123 */ 124 #define TMH_PQ_prepare_anon(pg,sql) \ 125 do { \ 126 if (GNUNET_OK != \ 127 GNUNET_PQ_prepare_anon (pg->conn, \ 128 sql)) { \ 129 GNUNET_break (0); \ 130 return GNUNET_DB_STATUS_HARD_ERROR; \ 131 } } while (0) 132 133 134 #endif