helper.h (4071B)
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 helper.h 18 * @brief shared internal definitions for postgres DB plugin 19 * @author Johannes Casaburi 20 */ 21 #ifndef PG_HELPER_H 22 #define PG_HELPER_H 23 24 #include "donaudb_lib.h" 25 26 27 /** 28 * Type of the "cls" argument given to each of the functions in 29 * our API. 30 */ 31 struct DONAUDB_PostgresContext 32 { 33 34 /** 35 * Our configuration. 36 */ 37 const struct GNUNET_CONFIGURATION_Handle *cfg; 38 39 /** 40 * Which currency should we assume all amounts to be in? 41 */ 42 char *currency; 43 44 /** 45 * Our base URL. 46 */ 47 char *donau_url; 48 49 /** 50 * Postgres connection handle. 51 */ 52 struct GNUNET_PQ_Context *conn; 53 54 /** 55 * Name of the current transaction, for debugging. 56 */ 57 const char *transaction_name; 58 59 /** 60 * Counts how often we have established a fresh @e conn 61 * to the database. Used to re-prepare statements. 62 */ 63 unsigned long long prep_gen; 64 65 }; 66 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 DONAUDB_PostgresContext` 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 struct { \ 80 unsigned long long cnt; \ 81 struct DONAUDB_PostgresContext *pg; \ 82 } preps[2]; /* 2 ctrs for taler-auditor-sync*/ \ 83 unsigned int off = 0; \ 84 \ 85 while ( (NULL != preps[off].pg) && \ 86 (ctx != preps[off].pg) && \ 87 (off < sizeof(preps) / sizeof(*preps)) ) \ 88 off++; \ 89 GNUNET_assert (off < \ 90 sizeof(preps) / sizeof(*preps)); \ 91 if (preps[off].cnt < ctx->prep_gen) \ 92 { \ 93 struct GNUNET_PQ_PreparedStatement ps[] = { \ 94 GNUNET_PQ_make_prepare (name, sql), \ 95 GNUNET_PQ_PREPARED_STATEMENT_END \ 96 }; \ 97 \ 98 if (GNUNET_OK != \ 99 GNUNET_PQ_prepare_statements (pg->conn, \ 100 ps)) \ 101 { \ 102 GNUNET_break (0); \ 103 return GNUNET_DB_STATUS_HARD_ERROR; \ 104 } \ 105 preps[off].pg = ctx; \ 106 preps[off].cnt = pg->prep_gen; \ 107 } \ 108 } while (0) 109 110 111 /** 112 * Wrapper macro to add the currency from the plugin's state 113 * when fetching amounts from the database. 114 * 115 * @param field name of the database field to fetch amount from 116 * @param[out] amountp pointer to amount to set 117 */ 118 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \ 119 amountp) \ 120 TALER_PQ_result_spec_amount ( \ 121 field,ctx->currency,amountp) 122 123 124 #endif