insert_exchange_keys.c (3253B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022, 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 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/insert_exchange_keys.c 18 * @brief Implementation of the insert_exchange_keys function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/insert_exchange_keys.h" 24 #include "helper.h" 25 26 27 enum GNUNET_DB_QueryStatus 28 TALER_MERCHANTDB_insert_exchange_keys ( 29 struct TALER_MERCHANTDB_PostgresContext *pg, 30 const char *exchange_url, 31 const struct TALER_EXCHANGE_Keys *keys, 32 struct GNUNET_TIME_Absolute first_retry, 33 uint32_t http_status, 34 enum TALER_ErrorCode ec) 35 { 36 uint32_t ec32 = (uint32_t) ec; 37 json_t *jkeys = (NULL == keys) 38 ? NULL 39 : TALER_EXCHANGE_keys_to_json (keys); 40 struct GNUNET_TIME_Timestamp ldid = (NULL == keys) 41 ? GNUNET_TIME_UNIT_ZERO_TS 42 : keys->last_denom_issue_date; 43 struct GNUNET_PQ_QueryParam params[] = { 44 keys == NULL 45 ? GNUNET_PQ_query_param_null () 46 : TALER_PQ_query_param_json (jkeys), 47 GNUNET_PQ_query_param_absolute_time (&first_retry), 48 GNUNET_PQ_query_param_timestamp (&ldid), 49 GNUNET_PQ_query_param_string (exchange_url), 50 GNUNET_PQ_query_param_uint32 (&http_status), 51 GNUNET_PQ_query_param_uint32 (&ec32), 52 GNUNET_PQ_query_param_end 53 }; 54 enum GNUNET_DB_QueryStatus qs; 55 56 check_connection (pg); 57 // FIXME: use a stored procedure instead! 58 PREPARE (pg, 59 "insert_exchange_keys", 60 "INSERT INTO merchant.merchant_exchange_keys" 61 "(keys_json" 62 ",first_retry" 63 ",expiration_time" 64 ",exchange_url" 65 ",exchange_http_status" 66 ",exchange_ec_code" 67 ") VALUES (" 68 " $1::TEXT::JSONB, $2, $3, $4, $5, $6" 69 ");"); 70 PREPARE (pg, 71 "update_exchange_keys", 72 "UPDATE merchant.merchant_exchange_keys SET" 73 /* preserve old keys if new ones failed to download */ 74 " keys_json=COALESCE($1::TEXT::JSONB,keys_json)" 75 ",first_retry=$2" 76 ",expiration_time=$3" 77 ",exchange_http_status=$5" 78 ",exchange_ec_code=$6" 79 " WHERE" 80 " exchange_url=$4;"); 81 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 82 "update_exchange_keys", 83 params); 84 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 85 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 86 "insert_exchange_keys", 87 params); 88 json_decref (jkeys); 89 return qs; 90 }