merchant

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

helper.h (4492B)


      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 src/backenddb/helper.h
     18  * @brief shared internal definitions for postgres logic
     19  * @author Christian Grothoff
     20  */
     21 #ifndef HELPER_H
     22 #define HELPER_H
     23 
     24 #include <gnunet/gnunet_util_lib.h>
     25 #include <gnunet/gnunet_db_lib.h>
     26 #include <gnunet/gnunet_time_lib.h>
     27 #include <taler/taler_util.h>
     28 
     29 /**
     30  * Type of the "cls" argument given to each of the functions in
     31  * our API.
     32  */
     33 struct TALER_MERCHANTDB_PostgresContext
     34 {
     35 
     36   /**
     37    * Postgres connection handle.
     38    */
     39   struct GNUNET_PQ_Context *conn;
     40 
     41   /**
     42    * Underlying configuration.
     43    */
     44   const struct GNUNET_CONFIGURATION_Handle *cfg;
     45 
     46   /**
     47    * Name of the currently active transaction, NULL if none is active.
     48    */
     49   const char *transaction_name;
     50 
     51   /**
     52    * Instance id ("merchant_id") that the search_path is currently
     53    * pointing at, or NULL if no per-instance schema is selected.
     54    * Owned by this struct; set by TALER_MERCHANTDB_set_instance().
     55    */
     56   char *current_merchant_id;
     57 
     58   /**
     59    * merchant_serial corresponding to @e current_merchant_id, or 0
     60    * if no per-instance schema is selected.  Used as the suffix in
     61    * per-instance prepared-statement names.
     62    */
     63   uint64_t current_merchant_serial;
     64 
     65   /**
     66    * Public key of the currently selected instance.  Populated by
     67    * TALER_MERCHANTDB_set_instance() together with @e current_merchant_id
     68    * and @e current_merchant_serial.  Used by call sites that emit
     69    * cross-process events (e.g. order-pay notifications) which carry the
     70    * instance public key in their payload.
     71    */
     72   struct TALER_MerchantPublicKeyP current_merchant_pub;
     73 
     74   /**
     75    * How many times have we connected to the DB.
     76    */
     77   uint64_t prep_gen;
     78 
     79 };
     80 
     81 
     82 /**
     83  * Prepares SQL statement @a sql under @a name for
     84  * connection @a pg once.
     85  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
     86  *
     87  * @param pg a `struct TALER_MERCHANTDB_PostgresContext`
     88  * @param name name to prepare the statement under
     89  * @param sql actual SQL text
     90  */
     91 #define PREPARE(pg,name,sql)                      \
     92         do {                                            \
     93           static unsigned long long gen;                \
     94                                                   \
     95           if (gen < pg->prep_gen)                       \
     96           {                                             \
     97             struct GNUNET_PQ_PreparedStatement ps[] = { \
     98               GNUNET_PQ_make_prepare (name, sql),       \
     99               GNUNET_PQ_PREPARED_STATEMENT_END          \
    100             };                                          \
    101                                                   \
    102             if (GNUNET_OK !=                            \
    103                 GNUNET_PQ_prepare_statements (pg->conn, \
    104                                               ps))      \
    105             {                                           \
    106               GNUNET_break (0);                         \
    107               return GNUNET_DB_STATUS_HARD_ERROR;       \
    108             }                                           \
    109             gen = pg->prep_gen;                         \
    110           }                                             \
    111         } while (0)
    112 
    113 
    114 /**
    115  * Prepares SQL statement @a sql under no name ("") for
    116  * connection @a pg.  Useful for prepared statements that
    117  * should not be cached.
    118  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
    119  *
    120  * @param pg a `struct TALER_MERCHANTDB_PostgresContext`
    121  * @param sql actual SQL text
    122  */
    123 #define TMH_PQ_prepare_anon(pg,sql)              \
    124         do {                                     \
    125           if (GNUNET_OK !=                       \
    126               GNUNET_PQ_prepare_anon (pg->conn,  \
    127                                       sql)) {    \
    128             GNUNET_break (0);                    \
    129             return GNUNET_DB_STATUS_HARD_ERROR;  \
    130           } } while (0)
    131 
    132 
    133 #endif