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