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 select_account_merges_above_serial_id.c
18 : * @brief Implementation of the select_account_merges_above_serial_id function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_account_merges_above_serial_id.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #account_merge_serial_helper_cb().
28 : */
29 : struct AccountMergeSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_AccountMergeCallback cb;
36 :
37 : /**
38 : * Closure for @e cb.
39 : */
40 : void *cb_cls;
41 :
42 : /**
43 : * Plugin context.
44 : */
45 : struct TALER_EXCHANGEDB_PostgresContext *pg;
46 :
47 : /**
48 : * Status code, set to #GNUNET_SYSERR on hard errors.
49 : */
50 : enum GNUNET_GenericReturnValue status;
51 : };
52 :
53 :
54 : /**
55 : * Helper function to be called with the results of a SELECT statement
56 : * that has returned @a num_results results.
57 : *
58 : * @param cls closure of type `struct AccountMergeSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 8 : account_merge_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 8 : struct AccountMergeSerialContext *dsc = cls;
68 8 : struct TALER_EXCHANGEDB_PostgresContext *pg = dsc->pg;
69 :
70 8 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : struct TALER_ReservePublicKeyP reserve_pub;
73 : struct TALER_PurseContractPublicKeyP purse_pub;
74 : struct TALER_PrivateContractHashP h_contract_terms;
75 : struct GNUNET_TIME_Timestamp purse_expiration;
76 : struct TALER_Amount amount;
77 : uint32_t min_age;
78 : uint32_t flags32;
79 : enum TALER_WalletAccountMergeFlags flags;
80 : struct TALER_Amount purse_fee;
81 : struct GNUNET_TIME_Timestamp merge_timestamp;
82 : struct TALER_ReserveSignatureP reserve_sig;
83 : uint64_t rowid;
84 0 : struct GNUNET_PQ_ResultSpec rs[] = {
85 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount_with_fee",
86 : &amount),
87 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("purse_fee",
88 : &purse_fee),
89 0 : GNUNET_PQ_result_spec_uint32 ("flags",
90 : &flags32),
91 0 : GNUNET_PQ_result_spec_uint32 ("age_limit",
92 : &min_age),
93 0 : GNUNET_PQ_result_spec_timestamp ("purse_expiration",
94 : &purse_expiration),
95 0 : GNUNET_PQ_result_spec_timestamp ("merge_timestamp",
96 : &merge_timestamp),
97 0 : GNUNET_PQ_result_spec_auto_from_type ("h_contract_terms",
98 : &h_contract_terms),
99 0 : GNUNET_PQ_result_spec_auto_from_type ("purse_pub",
100 : &purse_pub),
101 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_sig",
102 : &reserve_sig),
103 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
104 : &reserve_pub),
105 0 : GNUNET_PQ_result_spec_uint64 ("account_merge_request_serial_id",
106 : &rowid),
107 : GNUNET_PQ_result_spec_end
108 : };
109 : enum GNUNET_GenericReturnValue ret;
110 :
111 0 : if (GNUNET_OK !=
112 0 : GNUNET_PQ_extract_result (result,
113 : rs,
114 : i))
115 : {
116 0 : GNUNET_break (0);
117 0 : dsc->status = GNUNET_SYSERR;
118 0 : return;
119 : }
120 0 : flags = (enum TALER_WalletAccountMergeFlags) flags32;
121 0 : ret = dsc->cb (dsc->cb_cls,
122 : rowid,
123 : &reserve_pub,
124 : &purse_pub,
125 : &h_contract_terms,
126 : purse_expiration,
127 : &amount,
128 : min_age,
129 : flags,
130 : &purse_fee,
131 : merge_timestamp,
132 : &reserve_sig);
133 0 : GNUNET_PQ_cleanup_result (rs);
134 0 : if (GNUNET_OK != ret)
135 0 : break;
136 : }
137 : }
138 :
139 :
140 : enum GNUNET_DB_QueryStatus
141 8 : TALER_EXCHANGEDB_select_account_merges_above_serial_id (
142 : struct TALER_EXCHANGEDB_PostgresContext *pg,
143 : uint64_t serial_id,
144 : TALER_EXCHANGEDB_AccountMergeCallback cb,
145 : void *cb_cls)
146 : {
147 8 : struct GNUNET_PQ_QueryParam params[] = {
148 8 : GNUNET_PQ_query_param_uint64 (&serial_id),
149 : GNUNET_PQ_query_param_end
150 : };
151 8 : struct AccountMergeSerialContext dsc = {
152 : .cb = cb,
153 : .cb_cls = cb_cls,
154 : .pg = pg,
155 : .status = GNUNET_OK
156 : };
157 : enum GNUNET_DB_QueryStatus qs;
158 :
159 8 : PREPARE (pg,
160 : "audit_get_account_merge_incr",
161 : "SELECT"
162 : " am.account_merge_request_serial_id"
163 : ",am.reserve_pub"
164 : ",am.purse_pub"
165 : ",pr.h_contract_terms"
166 : ",pr.purse_expiration"
167 : ",pr.amount_with_fee"
168 : ",pr.age_limit"
169 : ",pr.flags"
170 : ",pr.purse_fee"
171 : ",pm.merge_timestamp"
172 : ",am.reserve_sig"
173 : " FROM account_merges am"
174 : " JOIN purse_requests pr USING (purse_pub)"
175 : " JOIN purse_merges pm USING (purse_pub)"
176 : " WHERE ("
177 : " (account_merge_request_serial_id>=$1)"
178 : " )"
179 : " ORDER BY account_merge_request_serial_id ASC;");
180 8 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
181 : "audit_get_account_merge_incr",
182 : params,
183 : &account_merge_serial_helper_cb,
184 : &dsc);
185 8 : if (GNUNET_OK != dsc.status)
186 0 : return GNUNET_DB_STATUS_HARD_ERROR;
187 8 : return qs;
188 : }
|