helper.h (3276B)
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 28 /** 29 * Type of the "cls" argument given to each of the functions in 30 * our API. 31 */ 32 struct TALER_MERCHANTDB_PostgresContext 33 { 34 35 /** 36 * Postgres connection handle. 37 */ 38 struct GNUNET_PQ_Context *conn; 39 40 /** 41 * Directory with SQL statements to run to create tables. 42 */ 43 char *sql_dir; 44 45 /** 46 * Underlying configuration. 47 */ 48 const struct GNUNET_CONFIGURATION_Handle *cfg; 49 50 /** 51 * Name of the currently active transaction, NULL if none is active. 52 */ 53 const char *transaction_name; 54 55 /** 56 * How many times have we connected to the DB. 57 */ 58 uint64_t prep_gen; 59 60 }; 61 62 63 /** 64 * Prepares SQL statement @a sql under @a name for 65 * connection @a pg once. 66 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 67 * 68 * @param pg a `struct TALER_MERCHANTDB_PostgresContext` 69 * @param name name to prepare the statement under 70 * @param sql actual SQL text 71 */ 72 #define PREPARE(pg,name,sql) \ 73 do { \ 74 static unsigned long long gen; \ 75 \ 76 if (gen < pg->prep_gen) \ 77 { \ 78 struct GNUNET_PQ_PreparedStatement ps[] = { \ 79 GNUNET_PQ_make_prepare (name, sql), \ 80 GNUNET_PQ_PREPARED_STATEMENT_END \ 81 }; \ 82 \ 83 if (GNUNET_OK != \ 84 GNUNET_PQ_prepare_statements (pg->conn, \ 85 ps)) \ 86 { \ 87 GNUNET_break (0); \ 88 return GNUNET_DB_STATUS_HARD_ERROR; \ 89 } \ 90 gen = pg->prep_gen; \ 91 } \ 92 } while (0) 93 94 95 /** 96 * Check that the database connection is still up and automatically reconnects 97 * unless we are already inside of a transaction. 98 * 99 * @param pg connection to check 100 */ 101 void 102 TALER_MERCHANTDB_check_connection (struct TALER_MERCHANTDB_PostgresContext *pg); 103 104 105 #endif