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