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 (2972B)


      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 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   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 /**
     63  * Prepares SQL statement @a sql under @a name for
     64  * connection @a pg once.
     65  * Returns with #GNUNET_DB_STATUS_HARD_ERROR on failure.
     66  *
     67  * @param pg a `struct CHALLENGERDB_PostgresContext`
     68  * @param name name to prepare the statement under
     69  * @param sql actual SQL text
     70  */
     71 #define PREPARE(pg,name,sql)                      \
     72         do {                                            \
     73           static unsigned long long gen;                \
     74                                                   \
     75           if (gen < CH_PG_prep_gen_)                    \
     76           {                                             \
     77             struct GNUNET_PQ_PreparedStatement ps[] = { \
     78               GNUNET_PQ_make_prepare (name, sql),       \
     79               GNUNET_PQ_PREPARED_STATEMENT_END          \
     80             };                                          \
     81                                                   \
     82             if (GNUNET_OK !=                            \
     83                 GNUNET_PQ_prepare_statements (pg->conn, \
     84                                               ps))      \
     85             {                                           \
     86               GNUNET_break (0);                         \
     87               return GNUNET_DB_STATUS_HARD_ERROR;       \
     88             }                                           \
     89             gen = CH_PG_prep_gen_;                      \
     90           }                                             \
     91         } while (0)
     92 
     93 #endif