merchant

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

pg.c (3675B)


      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 /**
     39  * Function called each time we connect or reconnect to the
     40  * database. Gives the application a chance to run some
     41  * per-connection initialization logic.
     42  *
     43  * @param pg database context in the merchant backend
     44  * @param pq database connection handle
     45  */
     46 static void
     47 reconnect_cb (struct TALER_MERCHANTDB_PostgresContext *pg,
     48               struct GNUNET_PQ_Context *pq)
     49 {
     50   struct GNUNET_PQ_ExecuteStatement es[] = {
     51     GNUNET_PQ_make_try_execute ("SET search_path TO merchant;"),
     52     GNUNET_PQ_EXECUTE_STATEMENT_END
     53   };
     54 
     55   if (GNUNET_OK !=
     56       GNUNET_PQ_exec_statements (pq,
     57                                  es))
     58   {
     59     GNUNET_break (0);
     60     return;
     61   }
     62   pg->prep_gen++;
     63 }
     64 
     65 
     66 /**
     67  * Initialize the database connection.
     68  *
     69  * @param cfg configuration to use
     70  * @param check_current true to check if the database schema is current
     71  * @return NULL on failure
     72  */
     73 static struct TALER_MERCHANTDB_PostgresContext *
     74 do_connect (
     75   const struct GNUNET_CONFIGURATION_Handle *cfg,
     76   bool check_current)
     77 {
     78   struct TALER_MERCHANTDB_PostgresContext *pg;
     79 
     80   pg = GNUNET_new (struct TALER_MERCHANTDB_PostgresContext);
     81   pg->cfg = cfg;
     82   pg->conn = GNUNET_PQ_init (pg->cfg,
     83                              "merchantdb-postgres",
     84                              &reconnect_cb,
     85                              pg);
     86   if (NULL == pg->conn)
     87   {
     88     TALER_MERCHANTDB_disconnect (pg);
     89     return NULL;
     90   }
     91   if (check_current &&
     92       (GNUNET_OK !=
     93        GNUNET_PQ_check_current (pg->conn,
     94                                 "merchant-")) )
     95   {
     96     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     97                 "Database schema is not up-to-date. Try running taler-merchant-dbinit or taler-merchant-dbconfig!\n");
     98     TALER_MERCHANTDB_disconnect (pg);
     99     return NULL;
    100   }
    101   return pg;
    102 }
    103 
    104 
    105 struct TALER_MERCHANTDB_PostgresContext *
    106 TALER_MERCHANTDB_connect (
    107   const struct GNUNET_CONFIGURATION_Handle *cfg)
    108 {
    109   return do_connect (cfg,
    110                      true);
    111 }
    112 
    113 
    114 struct TALER_MERCHANTDB_PostgresContext *
    115 TALER_MERCHANTDB_connect_admin (
    116   const struct GNUNET_CONFIGURATION_Handle *cfg)
    117 {
    118   return do_connect (cfg,
    119                      false);
    120 }
    121 
    122 
    123 void
    124 TALER_MERCHANTDB_disconnect (
    125   struct TALER_MERCHANTDB_PostgresContext *pg)
    126 {
    127   GNUNET_PQ_disconnect (pg->conn);
    128   GNUNET_free (pg->current_merchant_id);
    129   GNUNET_free (pg);
    130 }
    131 
    132 
    133 /* end of pg.c */