helper.h (4846B)
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 * Directory with SQL statements to run to create tables. 43 */ 44 char *sql_dir; 45 46 /** 47 * Underlying configuration. 48 */ 49 const struct GNUNET_CONFIGURATION_Handle *cfg; 50 51 /** 52 * Name of the currently active transaction, NULL if none is active. 53 */ 54 const char *transaction_name; 55 56 /** 57 * Instance id ("merchant_id") that the search_path is currently 58 * pointing at, or NULL if no per-instance schema is selected. 59 * Owned by this struct; set by TALER_MERCHANTDB_set_instance(). 60 */ 61 char *current_merchant_id; 62 63 /** 64 * merchant_serial corresponding to @e current_merchant_id, or 0 65 * if no per-instance schema is selected. Used as the suffix in 66 * per-instance prepared-statement names. 67 */ 68 uint64_t current_merchant_serial; 69 70 /** 71 * Public key of the currently selected instance. Populated by 72 * TALER_MERCHANTDB_set_instance() together with @e current_merchant_id 73 * and @e current_merchant_serial. Used by call sites that emit 74 * cross-process events (e.g. order-pay notifications) which carry the 75 * instance public key in their payload. 76 */ 77 struct TALER_MerchantPublicKeyP current_merchant_pub; 78 79 /** 80 * How many times have we connected to the DB. 81 */ 82 uint64_t prep_gen; 83 84 }; 85 86 87 /** 88 * Prepares SQL statement @a sql under @a name for 89 * connection @a pg once. 90 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 91 * 92 * @param pg a `struct TALER_MERCHANTDB_PostgresContext` 93 * @param name name to prepare the statement under 94 * @param sql actual SQL text 95 */ 96 #define PREPARE(pg,name,sql) \ 97 do { \ 98 static unsigned long long gen; \ 99 \ 100 if (gen < pg->prep_gen) \ 101 { \ 102 struct GNUNET_PQ_PreparedStatement ps[] = { \ 103 GNUNET_PQ_make_prepare (name, sql), \ 104 GNUNET_PQ_PREPARED_STATEMENT_END \ 105 }; \ 106 \ 107 if (GNUNET_OK != \ 108 GNUNET_PQ_prepare_statements (pg->conn, \ 109 ps)) \ 110 { \ 111 GNUNET_break (0); \ 112 return GNUNET_DB_STATUS_HARD_ERROR; \ 113 } \ 114 gen = pg->prep_gen; \ 115 } \ 116 } while (0) 117 118 119 /** 120 * Prepares SQL statement @a sql under no name ("") for 121 * connection @a pg. Useful for prepared statements that 122 * should not be cached. 123 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 124 * 125 * @param pg a `struct TALER_MERCHANTDB_PostgresContext` 126 * @param sql actual SQL text 127 */ 128 #define TMH_PQ_prepare_anon(pg,sql) \ 129 do { \ 130 if (GNUNET_OK != \ 131 GNUNET_PQ_prepare_anon (pg->conn, \ 132 sql)) { \ 133 GNUNET_break (0); \ 134 return GNUNET_DB_STATUS_HARD_ERROR; \ 135 } } while (0) 136 137 138 /** 139 * Check that the database connection is still up and automatically reconnects 140 * unless we are already inside of a transaction. 141 * 142 * @param pg connection to check 143 */ 144 void 145 TALER_MERCHANTDB_check_connection ( 146 struct TALER_MERCHANTDB_PostgresContext *pg); 147 148 149 #endif