pg_helper.h (4003B)
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 pg_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 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 * Our configuration. 34 */ 35 const struct GNUNET_CONFIGURATION_Handle *cfg; 36 37 /** 38 * Which currency should we assume all amounts to be in? 39 */ 40 char *currency; 41 42 /** 43 * Our base URL. 44 */ 45 char *donau_url; 46 47 /** 48 * Postgres connection handle. 49 */ 50 struct GNUNET_PQ_Context *conn; 51 52 /** 53 * Name of the current transaction, for debugging. 54 */ 55 const char *transaction_name; 56 57 /** 58 * Counts how often we have established a fresh @e conn 59 * to the database. Used to re-prepare statements. 60 */ 61 unsigned long long prep_gen; 62 63 }; 64 65 66 /** 67 * Prepares SQL statement @a sql under @a name for 68 * connection @a pg once. 69 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 70 * 71 * @param pg a `struct PostgresClosure` 72 * @param name name to prepare the statement under 73 * @param sql actual SQL text 74 */ 75 #define PREPARE(pg,name,sql) \ 76 do { \ 77 static struct { \ 78 unsigned long long cnt; \ 79 struct PostgresClosure *pg; \ 80 } preps[2]; /* 2 ctrs for taler-auditor-sync*/ \ 81 unsigned int off = 0; \ 82 \ 83 while ( (NULL != preps[off].pg) && \ 84 (pg != preps[off].pg) && \ 85 (off < sizeof(preps) / sizeof(*preps)) ) \ 86 off++; \ 87 GNUNET_assert (off < \ 88 sizeof(preps) / sizeof(*preps)); \ 89 if (preps[off].cnt < pg->prep_gen) \ 90 { \ 91 struct GNUNET_PQ_PreparedStatement ps[] = { \ 92 GNUNET_PQ_make_prepare (name, sql), \ 93 GNUNET_PQ_PREPARED_STATEMENT_END \ 94 }; \ 95 \ 96 if (GNUNET_OK != \ 97 GNUNET_PQ_prepare_statements (pg->conn, \ 98 ps)) \ 99 { \ 100 GNUNET_break (0); \ 101 return GNUNET_DB_STATUS_HARD_ERROR; \ 102 } \ 103 preps[off].pg = pg; \ 104 preps[off].cnt = pg->prep_gen; \ 105 } \ 106 } while (0) 107 108 109 /** 110 * Wrapper macro to add the currency from the plugin's state 111 * when fetching amounts from the database. 112 * 113 * @param field name of the database field to fetch amount from 114 * @param[out] amountp pointer to amount to set 115 */ 116 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \ 117 amountp) TALER_PQ_result_spec_amount ( \ 118 field,pg->currency,amountp) 119 120 121 #endif