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/iterate_accounts_by_instance.c
18 : * @brief Implementation of the iterate_accounts_by_instance function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_accounts_by_instance.h"
24 : #include "helper.h"
25 :
26 :
27 : /**
28 : * Context for iterate_accounts().
29 : */
30 : struct SelectAccountsContext
31 : {
32 : /**
33 : * Function to call with the results.
34 : */
35 : TALER_MERCHANTDB_AccountCallback cb;
36 :
37 : /**
38 : * Closure for @e cb.
39 : */
40 : void *cb_cls;
41 :
42 : /**
43 : * Database context.
44 : */
45 : struct TALER_MERCHANTDB_PostgresContext *pg;
46 :
47 : /**
48 : * Merchant instance ID.
49 : */
50 : const char *id;
51 :
52 : /**
53 : * Set to the return value on errors.
54 : */
55 : enum GNUNET_DB_QueryStatus qs;
56 :
57 : };
58 :
59 :
60 : /**
61 : * Function to be called with the results of a SELECT statement
62 : * that has returned @a num_results results about accounts.
63 : *
64 : * @param cls of type `struct SelectAccountsContext *`
65 : * @param result the postgres result
66 : * @param num_results the number of results in @a result
67 : */
68 : static void
69 118 : select_account_cb (void *cls,
70 : PGresult *result,
71 : unsigned int num_results)
72 : {
73 118 : struct SelectAccountsContext *lic = cls;
74 :
75 174 : for (unsigned int i = 0; i < num_results; i++)
76 : {
77 : struct TALER_FullPayto payto;
78 56 : char *facade_url = NULL;
79 56 : char *extra_wire_subject_metadata = NULL;
80 56 : json_t *credential = NULL;
81 : struct TALER_MERCHANTDB_AccountDetails acc;
82 : struct TALER_MerchantPrivateKeyP merchant_priv;
83 : bool no_priv;
84 56 : struct GNUNET_PQ_ResultSpec rs[] = {
85 56 : GNUNET_PQ_result_spec_auto_from_type ("h_wire",
86 : &acc.h_wire),
87 56 : GNUNET_PQ_result_spec_auto_from_type ("salt",
88 : &acc.salt),
89 56 : GNUNET_PQ_result_spec_string ("payto_uri",
90 : &payto.full_payto),
91 56 : GNUNET_PQ_result_spec_allow_null (
92 : GNUNET_PQ_result_spec_string ("credit_facade_url",
93 : &facade_url),
94 : NULL),
95 56 : GNUNET_PQ_result_spec_allow_null (
96 : GNUNET_PQ_result_spec_string ("extra_wire_subject_metadata",
97 : &extra_wire_subject_metadata),
98 : NULL),
99 56 : GNUNET_PQ_result_spec_allow_null (
100 : TALER_PQ_result_spec_json ("credit_facade_credentials",
101 : &credential),
102 : NULL),
103 56 : GNUNET_PQ_result_spec_bool ("active",
104 : &acc.active),
105 56 : GNUNET_PQ_result_spec_allow_null (
106 : GNUNET_PQ_result_spec_auto_from_type ("merchant_priv",
107 : &merchant_priv),
108 : &no_priv),
109 : GNUNET_PQ_result_spec_end
110 : };
111 :
112 56 : if (GNUNET_OK !=
113 56 : GNUNET_PQ_extract_result (result,
114 : rs,
115 : i))
116 : {
117 0 : GNUNET_break (0);
118 0 : lic->qs = GNUNET_DB_STATUS_HARD_ERROR;
119 0 : return;
120 : }
121 56 : acc.instance_id = lic->id;
122 56 : acc.payto_uri = payto;
123 56 : acc.credit_facade_url = facade_url;
124 56 : acc.credit_facade_credentials = credential;
125 56 : acc.extra_wire_subject_metadata = extra_wire_subject_metadata;
126 56 : if (NULL != lic->cb)
127 56 : lic->cb (lic->cb_cls,
128 : no_priv ? NULL : &merchant_priv,
129 : &acc);
130 56 : GNUNET_PQ_cleanup_result (rs);
131 : }
132 : }
133 :
134 :
135 : enum GNUNET_DB_QueryStatus
136 118 : TALER_MERCHANTDB_iterate_accounts_by_instance (
137 : struct TALER_MERCHANTDB_PostgresContext *pg,
138 : const char *id,
139 : TALER_MERCHANTDB_AccountCallback cb,
140 : void *cb_cls)
141 : {
142 118 : struct SelectAccountsContext lic = {
143 : .cb = cb,
144 : .cb_cls = cb_cls,
145 : .pg = pg,
146 : .id = id
147 : };
148 118 : struct GNUNET_PQ_QueryParam params[] = {
149 118 : GNUNET_PQ_query_param_string (id),
150 : GNUNET_PQ_query_param_end
151 : };
152 : enum GNUNET_DB_QueryStatus qs;
153 :
154 118 : TMH_PQ_prepare_anon (pg,
155 : "SELECT"
156 : " ma.h_wire"
157 : ",ma.salt"
158 : ",ma.payto_uri"
159 : ",ma.credit_facade_url"
160 : ",ma.credit_facade_credentials::TEXT"
161 : ",ma.extra_wire_subject_metadata"
162 : ",ma.active"
163 : ",mi.merchant_priv"
164 : " FROM merchant_accounts ma"
165 : " JOIN merchant.merchant_instances mi"
166 : " ON (mi.merchant_id=$1)");
167 118 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
168 : "",
169 : params,
170 : &select_account_cb,
171 : &lic);
172 118 : if (0 > lic.qs)
173 0 : return lic.qs;
174 118 : return qs;
175 : }
|