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