pg_helper.h (2935B)
1 /* 2 This file is part of Challenger 3 (C) 2023 Taler Systems SA 4 5 Challenger is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Lesser General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Challenger is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of ANASTASISABILITY 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 Challenger; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file challengerdb/pg_helper.h 18 * @brief database helper definitions 19 * @author Christian Grothoff 20 */ 21 #ifndef PG_HELPER_H 22 #define PG_HELPER_H 23 24 25 /** 26 * Type of the "cls" argument given to each of the functions in 27 * our API. 28 */ 29 struct PostgresClosure 30 { 31 32 /** 33 * Postgres connection handle. 34 */ 35 struct GNUNET_PQ_Context *conn; 36 37 /** 38 * Directory with SQL statements to run to create tables. 39 */ 40 char *sql_dir; 41 42 /** 43 * Underlying configuration. 44 */ 45 const struct GNUNET_CONFIGURATION_Handle *cfg; 46 47 /** 48 * Name of the currently active transaction, NULL if none is active. 49 */ 50 const char *transaction_name; 51 52 /** 53 * How often did we (re)establish @a conn so far? 54 */ 55 uint64_t prep_gen; 56 57 }; 58 59 60 /** 61 * Check that the database connection is still up. 62 * 63 * @param cls a `struct PostgresClosure` with connection to check 64 */ 65 void 66 CH_PG_check_connection (void *cls); 67 68 /** 69 * Prepares SQL statement @a sql under @a name for 70 * connection @a pg once. 71 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 72 * 73 * @param pg a `struct PostgresClosure` 74 * @param name name to prepare the statement under 75 * @param sql actual SQL text 76 */ 77 #define PREPARE(pg,name,sql) \ 78 do { \ 79 static unsigned long long gen; \ 80 \ 81 if (gen < pg->prep_gen) \ 82 { \ 83 struct GNUNET_PQ_PreparedStatement ps[] = { \ 84 GNUNET_PQ_make_prepare (name, sql), \ 85 GNUNET_PQ_PREPARED_STATEMENT_END \ 86 }; \ 87 \ 88 if (GNUNET_OK != \ 89 GNUNET_PQ_prepare_statements (pg->conn, \ 90 ps)) \ 91 { \ 92 GNUNET_break (0); \ 93 return GNUNET_DB_STATUS_HARD_ERROR; \ 94 } \ 95 gen = pg->prep_gen; \ 96 } \ 97 } while (0) 98 99 #endif