anastasis-db_pg.h (3941B)
1 /* 2 This file is part of Anastasis 3 Copyright (C) 2020-2022 Anastasis SARL 4 5 Anastasis is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Anastasis 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Anastasis; see the file COPYING.GPL. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file stasis/anastasis-db_pg.h 18 * @brief internal shared state for the Anastasis postgres database 19 * @author Christian Grothoff 20 */ 21 #ifndef ANASTASIS_DB_PG_H 22 #define ANASTASIS_DB_PG_H 23 24 #include "platform.h" 25 #include "anastasis_util_lib.h" 26 #include "anastasis_database_lib.h" 27 #include <gnunet/gnunet_util_lib.h> 28 #include <gnunet/gnunet_db_lib.h> 29 #include <gnunet/gnunet_pq_lib.h> 30 #include <taler/taler_pq_lib.h> 31 32 /** 33 * How long do we keep transient accounts open (those that have 34 * not been paid at all, but are awaiting payment). This puts 35 * a cap on how long users have to make a payment after a payment 36 * request was generated. 37 */ 38 #define TRANSIENT_LIFETIME GNUNET_TIME_UNIT_WEEKS 39 40 /** 41 * How often do we re-try if we run into a DB serialization error? 42 */ 43 #define MAX_RETRIES 3 44 45 46 /** 47 * Shared database state for the Anastasis postgres backend. 48 */ 49 struct PostgresClosure 50 { 51 52 /** 53 * Postgres connection handle. 54 */ 55 struct GNUNET_PQ_Context *conn; 56 57 /** 58 * Underlying configuration. 59 */ 60 const struct GNUNET_CONFIGURATION_Handle *cfg; 61 62 /** 63 * Name of the currently active transaction, NULL if none is active. 64 */ 65 const char *transaction_name; 66 67 /** 68 * Currency of the provider's own bank account, from [anastasis] CURRENCY. 69 * This is *not* the currency the service is priced in: since amounts in 70 * the database carry their own currency and a provider may price in 71 * several at once, this is only used to interpret IBAN wire transfers, 72 * which are made into one bank account and are therefore in one currency. 73 */ 74 char *currency; 75 76 }; 77 78 79 /** 80 * Prepared statements have been initialized in this edition. 81 */ 82 extern uint64_t ANASTASIS_DB_prep_gen_; 83 84 /** 85 * Global database state, initialized by ANASTASIS_DB_init(). 86 */ 87 extern struct PostgresClosure *pg; 88 89 /** 90 * Prepares SQL statement @a sql under @a name for 91 * connection @a pg once. 92 * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure. 93 * 94 * @param name name to prepare the statement under 95 * @param sql actual SQL text 96 */ 97 #define PREPARE(name,sql) \ 98 do { \ 99 static unsigned long long gen; \ 100 \ 101 if (gen < ANASTASIS_DB_prep_gen_) \ 102 { \ 103 struct GNUNET_PQ_PreparedStatement ps[] = { \ 104 GNUNET_PQ_make_prepare (name, sql), \ 105 GNUNET_PQ_PREPARED_STATEMENT_END \ 106 }; \ 107 \ 108 if (GNUNET_OK != \ 109 GNUNET_PQ_prepare_statements (pg->conn, \ 110 ps)) \ 111 { \ 112 GNUNET_break (0); \ 113 return GNUNET_DB_STATUS_HARD_ERROR; \ 114 } \ 115 gen = ANASTASIS_DB_prep_gen_; \ 116 } \ 117 } while (0) 118 119 120 #endif