merchant

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

pg.c (3761B)


      1 /*
      2   This file is part of TALER
      3   (C) 2014--2025 Taler Systems SA
      4 
      5   TALER 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   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/pg.c
     18  * @brief database helper functions for postgres used by the merchant
     19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
     20  * @author Christian Grothoff
     21  * @author Marcello Stanisci
     22  * @author Priscilla Huang
     23  * @author Iván Ávalos
     24  */
     25 struct TALER_MERCHANTDB_PostgresContext;
     26 #define GNUNET_PQ_RECONNECT_CALLBACK_CLOSURE \
     27         struct TALER_MERCHANTDB_PostgresContext
     28 #include <gnunet/gnunet_util_lib.h>
     29 #include <gnunet/gnunet_pq_lib.h>
     30 #include <taler/taler_util.h>
     31 #include <taler/taler_pq_lib.h>
     32 #include <taler/taler_json_lib.h>
     33 #include <taler/taler_mhd_lib.h>
     34 #include "merchantdb_lib.h"
     35 #include "helper.h"
     36 
     37 /**
     38  * How many times have we connected to the DB.
     39  */
     40 uint64_t TMH_PG_prep_gen_;
     41 
     42 /**
     43  * Function called each time we connect or reconnect to the
     44  * database. Gives the application a chance to run some
     45  * per-connection initialization logic.
     46  *
     47  * @param pg database context in the merchant backend
     48  * @param pq database connection handle
     49  */
     50 static void
     51 reconnect_cb (struct TALER_MERCHANTDB_PostgresContext *pg,
     52               struct GNUNET_PQ_Context *pq)
     53 {
     54   struct GNUNET_PQ_ExecuteStatement es[] = {
     55     GNUNET_PQ_make_try_execute ("SET search_path TO merchant;"),
     56     GNUNET_PQ_EXECUTE_STATEMENT_END
     57   };
     58 
     59   if (GNUNET_OK !=
     60       GNUNET_PQ_exec_statements (pq,
     61                                  es))
     62   {
     63     GNUNET_break (0);
     64     return;
     65   }
     66   TMH_PG_prep_gen_++;
     67 }
     68 
     69 
     70 /**
     71  * Initialize the database connection.
     72  *
     73  * @param cfg configuration to use
     74  * @param check_current true to check if the database schema is current
     75  * @return NULL on failure
     76  */
     77 static struct TALER_MERCHANTDB_PostgresContext *
     78 do_connect (
     79   const struct GNUNET_CONFIGURATION_Handle *cfg,
     80   bool check_current)
     81 {
     82   struct TALER_MERCHANTDB_PostgresContext *pg;
     83 
     84   pg = GNUNET_new (struct TALER_MERCHANTDB_PostgresContext);
     85   pg->cfg = cfg;
     86   pg->conn = GNUNET_PQ_init (pg->cfg,
     87                              "merchantdb-postgres",
     88                              &reconnect_cb,
     89                              pg);
     90   if (NULL == pg->conn)
     91   {
     92     TALER_MERCHANTDB_disconnect (pg);
     93     return NULL;
     94   }
     95   if (check_current &&
     96       (GNUNET_OK !=
     97        GNUNET_PQ_check_current (pg->conn,
     98                                 "merchant-")) )
     99   {
    100     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
    101                 "Database schema is not up-to-date. Try running taler-merchant-dbinit or taler-merchant-dbconfig!\n");
    102     TALER_MERCHANTDB_disconnect (pg);
    103     return NULL;
    104   }
    105   return pg;
    106 }
    107 
    108 
    109 struct TALER_MERCHANTDB_PostgresContext *
    110 TALER_MERCHANTDB_connect (
    111   const struct GNUNET_CONFIGURATION_Handle *cfg)
    112 {
    113   return do_connect (cfg,
    114                      true);
    115 }
    116 
    117 
    118 struct TALER_MERCHANTDB_PostgresContext *
    119 TALER_MERCHANTDB_connect_admin (
    120   const struct GNUNET_CONFIGURATION_Handle *cfg)
    121 {
    122   return do_connect (cfg,
    123                      false);
    124 }
    125 
    126 
    127 void
    128 TALER_MERCHANTDB_disconnect (
    129   struct TALER_MERCHANTDB_PostgresContext *pg)
    130 {
    131   GNUNET_PQ_disconnect (pg->conn);
    132   GNUNET_free (pg->current_merchant_id);
    133   GNUNET_free (pg);
    134 }
    135 
    136 
    137 /* end of pg.c */