exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

pg_helper.h (4276B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022 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 
     25 #include "taler/taler_auditordb_plugin.h"
     26 
     27 
     28 // FIXME: comment
     29 const char *
     30 TAH_PG_get_deletable_suppressable_table_name (enum
     31                                               TALER_AUDITORDB_DeletableSuppressableTables
     32                                               table);
     33 
     34 
     35 /**
     36  * Type of the "cls" argument given to each of the functions in
     37  * our API.
     38  */
     39 struct PostgresClosure
     40 {
     41 
     42   /**
     43    * Postgres connection handle.
     44    */
     45   struct GNUNET_PQ_Context *conn;
     46 
     47   /**
     48    * Name of the ongoing transaction, used to debug cases where
     49    * a transaction is not properly terminated via COMMIT or
     50    * ROLLBACK.
     51    */
     52   const char *transaction_name;
     53 
     54   /**
     55    * Our configuration.
     56    */
     57   const struct GNUNET_CONFIGURATION_Handle *cfg;
     58 
     59   /**
     60    * How often have we connected to the DB so far?
     61    */
     62   unsigned long long prep_gen;
     63 
     64   /**
     65    * Which currency should we assume all amounts to be in?
     66    */
     67   char *currency;
     68 };
     69 
     70 
     71 /**
     72  * Prepares SQL statement @a sql under @a name for
     73  * connection @a pg once.
     74  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
     75  *
     76  * @param pg a `struct PostgresClosure`
     77  * @param name name to prepare the statement under
     78  * @param sql actual SQL text
     79  */
     80 #define PREPARE(pg,name,sql)                            \
     81         do {                                            \
     82           static struct {                               \
     83             unsigned long long cnt;                     \
     84             struct PostgresClosure *pg;                 \
     85           } preps_[2]; /* 2 ctrs for taler-auditor-sync*/ \
     86           unsigned int off_ = 0;                        \
     87                                                         \
     88           while ( (NULL != preps_[off_].pg) &&          \
     89                   (pg != preps_[off_].pg) &&            \
     90                   (off_ < sizeof(preps_) / sizeof(*preps_)) ) \
     91           off_++;                                       \
     92           GNUNET_assert (off_ <                         \
     93                          sizeof(preps_) / sizeof(*preps_)); \
     94           if (preps_[off_].cnt < pg->prep_gen)          \
     95           {                                             \
     96             struct GNUNET_PQ_PreparedStatement ps[] = { \
     97               GNUNET_PQ_make_prepare (name, sql),       \
     98               GNUNET_PQ_PREPARED_STATEMENT_END          \
     99             };                                          \
    100                                                         \
    101             if (GNUNET_OK !=                            \
    102                 GNUNET_PQ_prepare_statements (pg->conn, \
    103                                               ps))      \
    104             {                                           \
    105               GNUNET_break (0);                         \
    106               return GNUNET_DB_STATUS_HARD_ERROR;       \
    107             }                                           \
    108             preps_[off_].pg = pg;                       \
    109             preps_[off_].cnt = pg->prep_gen;            \
    110           }                                             \
    111         } while (0)
    112 
    113 
    114 /**
    115  * Wrapper macro to add the currency from the plugin's state
    116  * when fetching amounts from the database.
    117  *
    118  * @param field name of the database field to fetch amount from
    119  * @param[out] amountp pointer to amount to set
    120  */
    121 #define TALER_PQ_RESULT_SPEC_AMOUNT(field, \
    122                                     amountp) TALER_PQ_result_spec_amount ( \
    123           field,pg->currency,amountp)
    124 
    125 
    126 #endif