set_instance.c (3353B)
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 PREPARE (pg, 64 "set_instance_lookup_serial", 65 "SELECT merchant_serial" 66 " ,merchant_pub" 67 " FROM merchant.merchant_instances" 68 " WHERE merchant_id=$1"); 69 { 70 struct GNUNET_PQ_QueryParam params[] = { 71 GNUNET_PQ_query_param_string (instance_id), 72 GNUNET_PQ_query_param_end 73 }; 74 75 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 76 "set_instance_lookup_serial", 77 params, 78 rs); 79 } 80 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) 81 return qs; 82 GNUNET_snprintf (sp_sql, 83 sizeof (sp_sql), 84 "SET search_path TO merchant_instance_%llu", 85 (unsigned long long) serial); 86 if (GNUNET_OK != 87 GNUNET_PQ_exec_statements (pg->conn, 88 es)) 89 { 90 GNUNET_break (0); 91 return GNUNET_DB_STATUS_HARD_ERROR; 92 } 93 GNUNET_free (pg->current_merchant_id); 94 pg->current_merchant_id = GNUNET_strdup (instance_id); 95 pg->current_merchant_serial = serial; 96 pg->current_merchant_pub = merchant_pub; 97 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 98 }