Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (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 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/iterate_exchanges.c
18 : * @brief Implementation of the iterate_exchanges function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_exchanges.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Context used for TALER_MERCHANTDB_iterate_exchanges().
28 : */
29 : struct LookupExchangesContext
30 : {
31 : /**
32 : * Function to call with the results.
33 : */
34 : TALER_MERCHANTDB_ExchangesCallback cb;
35 :
36 : /**
37 : * Closure for @a cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Did database result extraction fail?
43 : */
44 : bool extract_failed;
45 : };
46 :
47 :
48 : /**
49 : * Function to be called with the results of a SELECT statement
50 : * that has returned @a num_results results about product exchanges.
51 : *
52 : * @param[in,out] cls of type `struct LookupExchangesContext *`
53 : * @param result the postgres result
54 : * @param num_results the number of results in @a result
55 : */
56 : static void
57 1 : lookup_exchanges_cb (void *cls,
58 : PGresult *result,
59 : unsigned int num_results)
60 : {
61 1 : struct LookupExchangesContext *plc = cls;
62 :
63 2 : for (unsigned int i = 0; i < num_results; i++)
64 : {
65 : char *exchange_url;
66 1 : json_t *keys = NULL;
67 : struct GNUNET_TIME_Absolute next_download;
68 : struct GNUNET_TIME_Absolute keys_expiration;
69 : uint32_t hc;
70 : uint32_t ec;
71 1 : struct GNUNET_PQ_ResultSpec rs[] = {
72 1 : GNUNET_PQ_result_spec_absolute_time ("first_retry",
73 : &next_download),
74 1 : GNUNET_PQ_result_spec_absolute_time ("expiration_time",
75 : &keys_expiration),
76 1 : GNUNET_PQ_result_spec_string ("exchange_url",
77 : &exchange_url),
78 1 : GNUNET_PQ_result_spec_allow_null (
79 : TALER_PQ_result_spec_json ("keys_json",
80 : &keys),
81 : NULL),
82 1 : GNUNET_PQ_result_spec_uint32 ("exchange_http_status",
83 : &hc),
84 1 : GNUNET_PQ_result_spec_uint32 ("exchange_ec_code",
85 : &ec),
86 : GNUNET_PQ_result_spec_end
87 : };
88 :
89 1 : if (GNUNET_OK !=
90 1 : GNUNET_PQ_extract_result (result,
91 : rs,
92 : i))
93 : {
94 0 : GNUNET_break (0);
95 0 : plc->extract_failed = true;
96 0 : return;
97 : }
98 1 : plc->cb (plc->cb_cls,
99 : exchange_url,
100 : keys,
101 : next_download,
102 : keys_expiration,
103 : (unsigned int) hc,
104 : (enum TALER_ErrorCode) ec);
105 1 : GNUNET_PQ_cleanup_result (rs);
106 : }
107 : }
108 :
109 :
110 : enum GNUNET_DB_QueryStatus
111 1 : TALER_MERCHANTDB_iterate_exchanges (
112 : struct TALER_MERCHANTDB_PostgresContext *pg,
113 : TALER_MERCHANTDB_ExchangesCallback cb,
114 : void *cb_cls)
115 : {
116 1 : struct LookupExchangesContext plc = {
117 : .cb = cb,
118 : .cb_cls = cb_cls,
119 : /* Can be overwritten by the lookup_exchanges_cb */
120 : .extract_failed = false,
121 : };
122 1 : struct GNUNET_PQ_QueryParam params[] = {
123 : GNUNET_PQ_query_param_end
124 : };
125 : enum GNUNET_DB_QueryStatus qs;
126 :
127 1 : PREPARE (pg,
128 : "iterate_exchanges",
129 : "SELECT"
130 : " exchange_url"
131 : " ,first_retry"
132 : " ,expiration_time"
133 : " ,keys_json::TEXT"
134 : " ,exchange_http_status"
135 : " ,exchange_ec_code"
136 : " FROM merchant.merchant_exchange_keys");
137 1 : qs = GNUNET_PQ_eval_prepared_multi_select (
138 : pg->conn,
139 : "iterate_exchanges",
140 : params,
141 : &lookup_exchanges_cb,
142 : &plc);
143 : /* If there was an error inside lookup_exchanges_cb, return a hard error. */
144 1 : if (plc.extract_failed)
145 : {
146 0 : GNUNET_break (0);
147 0 : return GNUNET_DB_STATUS_HARD_ERROR;
148 : }
149 1 : return qs;
150 : }
|