challenger

OAuth 2.0-based authentication service that validates user can receive messages at a certain address
Log | Files | Refs | Submodules | README | LICENSE

pg_helper.h (3155B)


      1 /*
      2   This file is part of Challenger
      3   (C) 2023 Taler Systems SA
      4 
      5   Challenger is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Lesser General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   Challenger is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of ANASTASISABILITY 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   Challenger; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file challengerdb/pg_helper.h
     18  * @brief database helper definitions
     19  * @author Christian Grothoff
     20  */
     21 #ifndef PG_HELPER_H
     22 #define PG_HELPER_H
     23 
     24 
     25 /**
     26  * Type of the "cls" argument given to each of the functions in
     27  * our API.
     28  */
     29 struct CHALLENGERDB_PostgresContext
     30 {
     31 
     32   /**
     33    * Postgres connection handle.
     34    */
     35   struct GNUNET_PQ_Context *conn;
     36 
     37   /**
     38    * Directory with SQL statements to run to create tables.
     39    */
     40   char *sql_dir;
     41 
     42   /**
     43    * Underlying configuration.
     44    */
     45   const struct GNUNET_CONFIGURATION_Handle *cfg;
     46 
     47   /**
     48    * Name of the currently active transaction, NULL if none is active.
     49    */
     50   const char *transaction_name;
     51 
     52 };
     53 
     54 
     55 /**
     56  * Counts how often we have established a fresh @e conn
     57  * to the database. Used to re-prepare statements.
     58  */
     59 extern unsigned long long CH_PG_prep_gen_;
     60 
     61 /**
     62  * Check that the database connection is still up.
     63  *
     64  * @param cls a `struct CHALLENGERDB_PostgresContext` with connection to check
     65  */
     66 void
     67 CH_PG_check_connection (void *cls);
     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 CHALLENGERDB_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 unsigned long long gen;                \
     81                                                   \
     82           if (gen < CH_PG_prep_gen_)                    \
     83           {                                             \
     84             struct GNUNET_PQ_PreparedStatement ps[] = { \
     85               GNUNET_PQ_make_prepare (name, sql),       \
     86               GNUNET_PQ_PREPARED_STATEMENT_END          \
     87             };                                          \
     88                                                   \
     89             if (GNUNET_OK !=                            \
     90                 GNUNET_PQ_prepare_statements (pg->conn, \
     91                                               ps))      \
     92             {                                           \
     93               GNUNET_break (0);                         \
     94               return GNUNET_DB_STATUS_HARD_ERROR;       \
     95             }                                           \
     96             gen = CH_PG_prep_gen_;                      \
     97           }                                             \
     98         } while (0)
     99 
    100 #endif