Line data Source code
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 40 : 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 40 : uint32_t ec32 = (uint32_t) ec;
37 40 : json_t *jkeys = (NULL == keys)
38 : ? NULL
39 40 : : TALER_EXCHANGE_keys_to_json (keys);
40 40 : struct GNUNET_TIME_Timestamp ldid = (NULL == keys)
41 : ? GNUNET_TIME_UNIT_ZERO_TS
42 40 : : keys->last_denom_issue_date;
43 40 : struct GNUNET_PQ_QueryParam params[] = {
44 : keys == NULL
45 26 : ? GNUNET_PQ_query_param_null ()
46 40 : : TALER_PQ_query_param_json (jkeys),
47 40 : GNUNET_PQ_query_param_absolute_time (&first_retry),
48 40 : GNUNET_PQ_query_param_timestamp (&ldid),
49 40 : GNUNET_PQ_query_param_string (exchange_url),
50 40 : GNUNET_PQ_query_param_uint32 (&http_status),
51 40 : GNUNET_PQ_query_param_uint32 (&ec32),
52 : GNUNET_PQ_query_param_end
53 : };
54 : enum GNUNET_DB_QueryStatus qs;
55 :
56 : // FIXME: use a stored procedure instead!
57 40 : PREPARE (pg,
58 : "insert_exchange_keys",
59 : "INSERT INTO merchant.merchant_exchange_keys"
60 : "(keys_json"
61 : ",first_retry"
62 : ",expiration_time"
63 : ",exchange_url"
64 : ",exchange_http_status"
65 : ",exchange_ec_code"
66 : ") VALUES ("
67 : " $1::TEXT::JSONB, $2, $3, $4, $5, $6"
68 : ");");
69 40 : PREPARE (pg,
70 : "insert_exchange_keys_update",
71 : "UPDATE merchant.merchant_exchange_keys SET"
72 : /* preserve old keys if new ones failed to download */
73 : " keys_json=COALESCE($1::TEXT::JSONB,keys_json)"
74 : ",first_retry=$2"
75 : ",expiration_time=$3"
76 : ",exchange_http_status=$5"
77 : ",exchange_ec_code=$6"
78 : " WHERE"
79 : " exchange_url=$4;");
80 40 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
81 : "insert_exchange_keys_update",
82 : params);
83 40 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
84 18 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
85 : "insert_exchange_keys",
86 : params);
87 40 : json_decref (jkeys);
88 40 : return qs;
89 : }
|