pg_helper.h (4184B)
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 25 #include "auditordb_lib.h" 26 27 /** 28 * Postgres database connection handle. 29 */ 30 struct TALER_AUDITORDB_PostgresContext 31 { 32 33 /** 34 * Postgres connection handle. 35 */ 36 struct GNUNET_PQ_Context *conn; 37 38 /** 39 * Name of the ongoing transaction, used to debug cases where 40 * a transaction is not properly terminated via COMMIT or 41 * ROLLBACK. 42 */ 43 const char *transaction_name; 44 45 /** 46 * Our configuration. 47 */ 48 const struct GNUNET_CONFIGURATION_Handle *cfg; 49 /** 50 * How often have we connected to the DB so far? 51 */ 52 unsigned long long prep_gen; 53 54 /** 55 * Which currency should we assume all amounts to be in? 56 */ 57 char *currency; 58 }; 59 60 61 // FIXME: comment 62 const char * 63 TALER_AUDITORDB_get_deletable_suppressable_table_name ( 64 enum TALER_AUDITORDB_DeletableSuppressableTables table); 65 66 67 /** 68 * Prepares SQL statement @a sql under @a name for 69 * connection @a pg once. 70 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 71 * 72 * @param pg a `struct PostgresClosure` 73 * @param name name to prepare the statement under 74 * @param sql actual SQL text 75 */ 76 #define PREPARE(pg,name,sql) \ 77 do { \ 78 static struct { \ 79 unsigned long long cnt; \ 80 struct TALER_AUDITORDB_PostgresContext *pg; \ 81 } preps_[2]; /* 2 ctrs for taler-auditor-sync*/ \ 82 unsigned int off_ = 0; \ 83 \ 84 while ( (NULL != preps_[off_].pg) && \ 85 (pg != preps_[off_].pg) && \ 86 (off_ < sizeof(preps_) / sizeof(*preps_)) ) \ 87 off_++; \ 88 GNUNET_assert (off_ < \ 89 sizeof(preps_) / sizeof(*preps_)); \ 90 if (preps_[off_].cnt < pg->prep_gen) \ 91 { \ 92 struct GNUNET_PQ_PreparedStatement ps[] = { \ 93 GNUNET_PQ_make_prepare (name, sql), \ 94 GNUNET_PQ_PREPARED_STATEMENT_END \ 95 }; \ 96 \ 97 if (GNUNET_OK != \ 98 GNUNET_PQ_prepare_statements (pg->conn, \ 99 ps)) \ 100 { \ 101 GNUNET_break (0); \ 102 return GNUNET_DB_STATUS_HARD_ERROR; \ 103 } \ 104 preps_[off_].pg = pg; \ 105 preps_[off_].cnt = pg->prep_gen; \ 106 } \ 107 } while (0) 108 109 110 /** 111 * Wrapper macro to add the currency from the plugin's state 112 * when fetching amounts from the database. 113 * 114 * @param field name of the database field to fetch amount from 115 * @param[out] amountp pointer to amount to set 116 */ 117 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \ 118 amountp) \ 119 TALER_PQ_result_spec_amount ( \ 120 field,pg->currency,amountp) 121 122 123 #endif