Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 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 exchangedb/pg_get_wire_accounts.c
18 : * @brief Implementation of the get_wire_accounts function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/platform.h"
22 : #include "taler/taler_error_codes.h"
23 : #include "taler/taler_dbevents.h"
24 : #include "taler/taler_pq_lib.h"
25 : #include "pg_get_wire_accounts.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #get_wire_accounts_cb().
31 : */
32 : struct GetWireAccountsContext
33 : {
34 : /**
35 : * Function to call per result.
36 : */
37 : TALER_EXCHANGEDB_WireAccountCallback cb;
38 :
39 : /**
40 : * Closure for @e cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Flag set to #GNUNET_OK as long as everything is fine.
46 : */
47 : enum GNUNET_GenericReturnValue status;
48 :
49 : };
50 :
51 :
52 : /**
53 : * Invoke the callback for each result.
54 : *
55 : * @param cls a `struct MissingWireContext *`
56 : * @param result SQL result
57 : * @param num_results number of rows in @a result
58 : */
59 : static void
60 19 : get_wire_accounts_cb (void *cls,
61 : PGresult *result,
62 : unsigned int num_results)
63 : {
64 19 : struct GetWireAccountsContext *ctx = cls;
65 :
66 40 : for (unsigned int i = 0; i < num_results; i++)
67 : {
68 : struct TALER_FullPayto payto_uri;
69 21 : char *conversion_url = NULL;
70 21 : char *open_banking_gateway = NULL;
71 21 : char *wire_transfer_gateway = NULL;
72 21 : json_t *debit_restrictions = NULL;
73 21 : json_t *credit_restrictions = NULL;
74 : struct TALER_MasterSignatureP master_sig;
75 21 : char *bank_label = NULL;
76 : int64_t priority;
77 21 : struct GNUNET_PQ_ResultSpec rs[] = {
78 21 : GNUNET_PQ_result_spec_string ("payto_uri",
79 : &payto_uri.full_payto),
80 21 : GNUNET_PQ_result_spec_allow_null (
81 : GNUNET_PQ_result_spec_string ("conversion_url",
82 : &conversion_url),
83 : NULL),
84 21 : GNUNET_PQ_result_spec_allow_null (
85 : GNUNET_PQ_result_spec_string ("open_banking_gateway",
86 : &open_banking_gateway),
87 : NULL),
88 21 : GNUNET_PQ_result_spec_allow_null (
89 : GNUNET_PQ_result_spec_string ("wire_transfer_gateway",
90 : &wire_transfer_gateway),
91 : NULL),
92 21 : GNUNET_PQ_result_spec_allow_null (
93 : GNUNET_PQ_result_spec_string ("bank_label",
94 : &bank_label),
95 : NULL),
96 21 : GNUNET_PQ_result_spec_int64 ("priority",
97 : &priority),
98 21 : GNUNET_PQ_result_spec_allow_null (
99 : TALER_PQ_result_spec_json ("debit_restrictions",
100 : &debit_restrictions),
101 : NULL),
102 21 : GNUNET_PQ_result_spec_allow_null (
103 : TALER_PQ_result_spec_json ("credit_restrictions",
104 : &credit_restrictions),
105 : NULL),
106 21 : GNUNET_PQ_result_spec_auto_from_type ("master_sig",
107 : &master_sig),
108 : GNUNET_PQ_result_spec_end
109 : };
110 :
111 21 : if (GNUNET_OK !=
112 21 : GNUNET_PQ_extract_result (result,
113 : rs,
114 : i))
115 : {
116 0 : GNUNET_break (0);
117 0 : ctx->status = GNUNET_SYSERR;
118 0 : return;
119 : }
120 21 : if (NULL == debit_restrictions)
121 : {
122 0 : debit_restrictions = json_array ();
123 0 : GNUNET_assert (NULL != debit_restrictions);
124 : }
125 21 : if (NULL == credit_restrictions)
126 : {
127 0 : credit_restrictions = json_array ();
128 0 : GNUNET_assert (NULL != credit_restrictions);
129 : }
130 21 : ctx->cb (ctx->cb_cls,
131 : payto_uri,
132 : conversion_url,
133 : open_banking_gateway,
134 : wire_transfer_gateway,
135 : debit_restrictions,
136 : credit_restrictions,
137 : &master_sig,
138 : bank_label,
139 : priority);
140 21 : GNUNET_PQ_cleanup_result (rs);
141 : }
142 : }
143 :
144 :
145 : enum GNUNET_DB_QueryStatus
146 19 : TEH_PG_get_wire_accounts (void *cls,
147 : TALER_EXCHANGEDB_WireAccountCallback cb,
148 : void *cb_cls)
149 : {
150 19 : struct PostgresClosure *pg = cls;
151 19 : struct GetWireAccountsContext ctx = {
152 : .cb = cb,
153 : .cb_cls = cb_cls,
154 : .status = GNUNET_OK
155 : };
156 19 : struct GNUNET_PQ_QueryParam params[] = {
157 : GNUNET_PQ_query_param_end
158 : };
159 : enum GNUNET_DB_QueryStatus qs;
160 :
161 19 : PREPARE (pg,
162 : "get_wire_accounts",
163 : "SELECT"
164 : " payto_uri"
165 : ",conversion_url"
166 : ",open_banking_gateway"
167 : ",wire_transfer_gateway"
168 : ",debit_restrictions::TEXT"
169 : ",credit_restrictions::TEXT"
170 : ",master_sig"
171 : ",bank_label"
172 : ",priority"
173 : " FROM wire_accounts"
174 : " WHERE is_active");
175 19 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
176 : "get_wire_accounts",
177 : params,
178 : &get_wire_accounts_cb,
179 : &ctx);
180 19 : if (GNUNET_OK != ctx.status)
181 0 : return GNUNET_DB_STATUS_HARD_ERROR;
182 19 : return qs;
183 : }
|