merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

pg_helper.h (4484B)


      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 Christian Grothoff
     20  */
     21 #ifndef PG_HELPER_H
     22 #define PG_HELPER_H
     23 
     24 #include <gnunet/gnunet_db_lib.h>
     25 
     26 /**
     27  * Type of the "cls" argument given to each of the functions in
     28  * our API.
     29  */
     30 struct PostgresClosure
     31 {
     32 
     33   /**
     34    * Postgres connection handle.
     35    */
     36   struct GNUNET_PQ_Context *conn;
     37 
     38   /**
     39    * Directory with SQL statements to run to create tables.
     40    */
     41   char *sql_dir;
     42 
     43   /**
     44    * Underlying configuration.
     45    */
     46   const struct GNUNET_CONFIGURATION_Handle *cfg;
     47 
     48   /**
     49    * Name of the currently active transaction, NULL if none is active.
     50    */
     51   const char *transaction_name;
     52 
     53   /**
     54    * How many times have we connected to the DB.
     55    */
     56   uint64_t prep_gen;
     57 
     58 };
     59 
     60 
     61 /**
     62  * Prepares SQL statement @a sql under @a name for
     63  * connection @a pg once.
     64  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
     65  *
     66  * @param pg a `struct PostgresClosure`
     67  * @param name name to prepare the statement under
     68  * @param sql actual SQL text
     69  */
     70 #define PREPARE(pg,name,sql)                      \
     71   do {                                            \
     72     static unsigned long long gen;                \
     73                                                   \
     74     if (gen < pg->prep_gen)                       \
     75     {                                             \
     76       struct GNUNET_PQ_PreparedStatement ps[] = { \
     77         GNUNET_PQ_make_prepare (name, sql),       \
     78         GNUNET_PQ_PREPARED_STATEMENT_END          \
     79       };                                          \
     80                                                   \
     81       if (GNUNET_OK !=                            \
     82           GNUNET_PQ_prepare_statements (pg->conn, \
     83                                         ps))      \
     84       {                                           \
     85         GNUNET_break (0);                         \
     86         return GNUNET_DB_STATUS_HARD_ERROR;       \
     87       }                                           \
     88       gen = pg->prep_gen;                         \
     89     }                                             \
     90   } while (0)
     91 
     92 
     93 /**
     94  * Check that the database connection is still up
     95  * and automatically reconnects unless we are
     96  * already inside of a transaction.
     97  *
     98  * @param pg connection to check
     99  */
    100 void
    101 check_connection (struct PostgresClosure *pg);
    102 
    103 
    104 /**
    105  * Do a pre-flight check that we are not in an uncommitted transaction.
    106  * If we are, die.
    107  * Does not return anything, as we will continue regardless of the outcome.
    108  *
    109  * @param cls the `struct PostgresClosure` with the plugin-specific state
    110  */
    111 void
    112 postgres_preflight (void *cls);
    113 
    114 /**
    115  * Start a transaction.
    116  *
    117  * @param cls the `struct PostgresClosure` with the plugin-specific state
    118  * @param name unique name identifying the transaction (for debugging),
    119  *             must point to a constant
    120  * @return #GNUNET_OK on success
    121  */
    122 enum GNUNET_GenericReturnValue
    123 TMH_PG_start (void *cls,
    124               const char *name);
    125 
    126 
    127 /**
    128  * Start a transaction in 'read committed' mode.
    129  *
    130  * @param cls the `struct PostgresClosure` with the plugin-specific state
    131  * @param name unique name identifying the transaction (for debugging),
    132  *             must point to a constant
    133  * @return #GNUNET_OK on success
    134  */
    135 enum GNUNET_GenericReturnValue
    136 TMH_PG_start_read_committed (void *cls,
    137                              const char *name);
    138 
    139 /**
    140  * Roll back the current transaction of a database connection.
    141  *
    142  * @param cls the `struct PostgresClosure` with the plugin-specific state
    143  */
    144 void
    145 TMH_PG_rollback (void *cls);
    146 
    147 
    148 /**
    149  * Commit the current transaction of a database connection.
    150  *
    151  * @param cls the `struct PostgresClosure` with the plugin-specific state
    152  * @return transaction status code
    153  */
    154 enum GNUNET_DB_QueryStatus
    155 TMH_PG_commit (void *cls);
    156 
    157 
    158 #endif