merchant

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

set_instance.c (3378B)


      1 /*
      2   This file is part of TALER
      3   (C) 2026 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/set_instance.c
     18  * @brief Implementation of TALER_MERCHANTDB_set_instance
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <gnunet/gnunet_util_lib.h>
     23 #include <gnunet/gnunet_pq_lib.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/set_instance.h"
     26 #include "helper.h"
     27 
     28 
     29 enum GNUNET_DB_QueryStatus
     30 TALER_MERCHANTDB_set_instance (
     31   struct TALER_MERCHANTDB_PostgresContext *pg,
     32   const char *instance_id)
     33 {
     34   uint64_t serial;
     35   struct TALER_MerchantPublicKeyP merchant_pub;
     36   struct GNUNET_PQ_ResultSpec rs[] = {
     37     GNUNET_PQ_result_spec_uint64 ("merchant_serial",
     38                                   &serial),
     39     GNUNET_PQ_result_spec_auto_from_type ("merchant_pub",
     40                                           &merchant_pub),
     41     GNUNET_PQ_result_spec_end
     42   };
     43   enum GNUNET_DB_QueryStatus qs;
     44   char sp_sql[128];
     45   struct GNUNET_PQ_ExecuteStatement es[] = {
     46     GNUNET_PQ_make_execute (sp_sql),
     47     GNUNET_PQ_EXECUTE_STATEMENT_END
     48   };
     49 
     50   if (NULL == instance_id)
     51   {
     52     pg->current_merchant_id = 0;
     53     pg->current_merchant_serial = 0;
     54     memset (&pg->current_merchant_pub,
     55             0,
     56             sizeof (pg->current_merchant_pub));
     57     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     58   }
     59   if ( (NULL != pg->current_merchant_id) &&
     60        (0 == strcmp (pg->current_merchant_id,
     61                      instance_id)) )
     62     return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     63   check_connection (pg);
     64   PREPARE (pg,
     65            "set_instance_lookup_serial",
     66            "SELECT merchant_serial"
     67            "      ,merchant_pub"
     68            "  FROM merchant.merchant_instances"
     69            " WHERE merchant_id=$1");
     70   {
     71     struct GNUNET_PQ_QueryParam params[] = {
     72       GNUNET_PQ_query_param_string (instance_id),
     73       GNUNET_PQ_query_param_end
     74     };
     75 
     76     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     77                                                    "set_instance_lookup_serial",
     78                                                    params,
     79                                                    rs);
     80   }
     81   if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs)
     82     return qs;
     83   GNUNET_snprintf (sp_sql,
     84                    sizeof (sp_sql),
     85                    "SET search_path TO merchant_instance_%llu",
     86                    (unsigned long long) serial);
     87   if (GNUNET_OK !=
     88       GNUNET_PQ_exec_statements (pg->conn,
     89                                  es))
     90   {
     91     GNUNET_break (0);
     92     return GNUNET_DB_STATUS_HARD_ERROR;
     93   }
     94   GNUNET_free (pg->current_merchant_id);
     95   pg->current_merchant_id = GNUNET_strdup (instance_id);
     96   pg->current_merchant_serial = serial;
     97   pg->current_merchant_pub = merchant_pub;
     98   return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT;
     99 }