donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

helper.h (4079B)


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