Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 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_donau_instances.c
18 : * @brief Implementation of the iterate_donau_instances function for Postgres
19 : * @author Bohdan Potuzhnyi
20 : * @author Vlada Svirsh
21 : */
22 : #include "platform.h"
23 : #include <taler/taler_pq_lib.h>
24 : #include <donau/donau_util.h>
25 : #include "merchant-database/iterate_donau_instances.h"
26 : #include "helper.h"
27 :
28 : /**
29 : * Context for iterate_donau_instances().
30 : */
31 : struct SelectDonauInstanceContext
32 : {
33 : /**
34 : * Database connection.
35 : */
36 : struct TALER_MERCHANTDB_PostgresContext *pg;
37 :
38 : /**
39 : * Function to call with the results.
40 : */
41 : TALER_MERCHANTDB_DonauInstanceCallback cb;
42 :
43 : /**
44 : * Closure for @e cb.
45 : */
46 : void *cb_cls;
47 :
48 : /**
49 : * Did database result extraction fail?
50 : */
51 : bool extract_failed;
52 : };
53 :
54 :
55 : /**
56 : * Function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results about donau instances.
58 : *
59 : * @param[in, out] cls of type `struct SelectDonauInstanceContext *`
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 6 : select_donau_instance_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 6 : struct SelectDonauInstanceContext *sdc = cls;
69 6 : struct TALER_MERCHANTDB_PostgresContext *pg = sdc->pg;
70 : struct DONAU_CharityPublicKeyP charity_pub_key;
71 :
72 : GNUNET_static_assert (sizeof (charity_pub_key) ==
73 : sizeof (pg->current_merchant_pub));
74 6 : memcpy (&charity_pub_key,
75 6 : &pg->current_merchant_pub,
76 : sizeof (charity_pub_key));
77 10 : for (unsigned int i = 0; i < num_results; i++)
78 : {
79 : uint64_t donau_instance_serial;
80 : char *donau_url;
81 : char *charity_name;
82 : uint64_t charity_id;
83 : struct TALER_Amount charity_max_per_year;
84 : struct TALER_Amount charity_receipts_to_date;
85 : int64_t current_year;
86 4 : json_t *donau_keys_json = NULL;
87 4 : struct GNUNET_PQ_ResultSpec rs[] = {
88 4 : GNUNET_PQ_result_spec_uint64 ("donau_instances_serial",
89 : &donau_instance_serial),
90 4 : GNUNET_PQ_result_spec_string ("donau_url",
91 : &donau_url),
92 4 : GNUNET_PQ_result_spec_string ("charity_name",
93 : &charity_name),
94 4 : GNUNET_PQ_result_spec_uint64 ("charity_id",
95 : &charity_id),
96 4 : TALER_PQ_result_spec_amount_with_currency ("charity_max_per_year",
97 : &charity_max_per_year),
98 4 : TALER_PQ_result_spec_amount_with_currency ("charity_receipts_to_date",
99 : &charity_receipts_to_date),
100 4 : GNUNET_PQ_result_spec_int64 ("current_year",
101 : ¤t_year),
102 4 : GNUNET_PQ_result_spec_allow_null (
103 : TALER_PQ_result_spec_json ("keys_json",
104 : &donau_keys_json),
105 : NULL),
106 : GNUNET_PQ_result_spec_end
107 : };
108 :
109 4 : if (GNUNET_OK !=
110 4 : GNUNET_PQ_extract_result (result,
111 : rs,
112 : i))
113 : {
114 0 : GNUNET_break (0);
115 0 : sdc->extract_failed = true;
116 0 : return;
117 : }
118 4 : sdc->cb (sdc->cb_cls,
119 : donau_instance_serial,
120 : donau_url,
121 : charity_name,
122 : &charity_pub_key,
123 : charity_id,
124 : &charity_max_per_year,
125 : &charity_receipts_to_date,
126 : current_year,
127 : donau_keys_json);
128 4 : GNUNET_PQ_cleanup_result (rs);
129 : }
130 : }
131 :
132 :
133 : enum GNUNET_DB_QueryStatus
134 6 : TALER_MERCHANTDB_iterate_donau_instances (
135 : struct TALER_MERCHANTDB_PostgresContext *pg,
136 : const char *id,
137 : TALER_MERCHANTDB_DonauInstanceCallback cb,
138 : void *cb_cls)
139 : {
140 6 : struct SelectDonauInstanceContext sdc = {
141 : .pg = pg,
142 : .cb = cb,
143 : .cb_cls = cb_cls,
144 : /* Can be overwritten by the select_donau_instance_cb */
145 : .extract_failed = false,
146 : };
147 6 : struct GNUNET_PQ_QueryParam params[] = {
148 : GNUNET_PQ_query_param_end
149 : };
150 : enum GNUNET_DB_QueryStatus qs;
151 :
152 6 : GNUNET_assert (NULL != pg->current_merchant_id);
153 6 : GNUNET_assert (0 == strcmp (id,
154 : pg->current_merchant_id));
155 6 : TMH_PQ_prepare_anon (pg,
156 : "SELECT"
157 : " di.donau_instances_serial"
158 : ",di.donau_url"
159 : ",di.charity_name"
160 : ",di.charity_id"
161 : ",di.charity_max_per_year"
162 : ",di.charity_receipts_to_date"
163 : ",di.current_year"
164 : ",dk.keys_json::TEXT"
165 : " FROM merchant_donau_instances di"
166 : " LEFT JOIN merchant.merchant_donau_keys dk"
167 : " ON di.donau_url = dk.donau_url");
168 6 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
169 : "",
170 : params,
171 : &select_donau_instance_cb,
172 : &sdc);
173 :
174 : /* If there was an error inside select_donau_instance_cb, return a hard error. */
175 6 : if (sdc.extract_failed)
176 0 : return GNUNET_DB_STATUS_HARD_ERROR;
177 :
178 6 : return qs;
179 : }
|