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_reserve_open_above_serial_id.c
18 : * @brief Low-level (statement-level) Postgres database access for the exchange
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/select_reserve_open_above_serial_id.h"
23 : #include "helper.h"
24 :
25 :
26 : /**
27 : * Closure for #reserve_open_serial_helper_cb().
28 : */
29 : struct ReserveOpenSerialContext
30 : {
31 :
32 : /**
33 : * Callback to call.
34 : */
35 : TALER_EXCHANGEDB_ReserveOpenCallback cb;
36 :
37 : /**
38 : * Closure for @e cb.
39 : */
40 : void *cb_cls;
41 :
42 : /**
43 : * Plugin's 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 ReserveOpenSerialContext`
59 : * @param result the postgres result
60 : * @param num_results the number of results in @a result
61 : */
62 : static void
63 4 : reserve_open_serial_helper_cb (void *cls,
64 : PGresult *result,
65 : unsigned int num_results)
66 : {
67 4 : struct ReserveOpenSerialContext *rcsc = cls;
68 4 : struct TALER_EXCHANGEDB_PostgresContext *pg = rcsc->pg;
69 :
70 4 : for (unsigned int i = 0; i<num_results; i++)
71 : {
72 : uint64_t rowid;
73 : struct TALER_ReservePublicKeyP reserve_pub;
74 : struct TALER_ReserveSignatureP reserve_sig;
75 : uint32_t requested_purse_limit;
76 : struct GNUNET_TIME_Timestamp request_timestamp;
77 : struct GNUNET_TIME_Timestamp reserve_expiration;
78 : struct TALER_Amount reserve_payment;
79 0 : struct GNUNET_PQ_ResultSpec rs[] = {
80 0 : GNUNET_PQ_result_spec_uint64 ("open_request_uuid",
81 : &rowid),
82 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
83 : &reserve_pub),
84 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_sig",
85 : &reserve_sig),
86 0 : GNUNET_PQ_result_spec_timestamp ("request_timestamp",
87 : &request_timestamp),
88 0 : GNUNET_PQ_result_spec_timestamp ("expiration_date",
89 : &reserve_expiration),
90 0 : GNUNET_PQ_result_spec_uint32 ("requested_purse_limit",
91 : &requested_purse_limit),
92 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("reserve_payment",
93 : &reserve_payment),
94 : GNUNET_PQ_result_spec_end
95 : };
96 : enum GNUNET_GenericReturnValue ret;
97 :
98 0 : if (GNUNET_OK !=
99 0 : GNUNET_PQ_extract_result (result,
100 : rs,
101 : i))
102 : {
103 0 : GNUNET_break (0);
104 0 : rcsc->status = GNUNET_SYSERR;
105 0 : return;
106 : }
107 0 : ret = rcsc->cb (rcsc->cb_cls,
108 : rowid,
109 : &reserve_payment,
110 : request_timestamp,
111 : reserve_expiration,
112 : requested_purse_limit,
113 : &reserve_pub,
114 : &reserve_sig);
115 0 : GNUNET_PQ_cleanup_result (rs);
116 0 : if (GNUNET_OK != ret)
117 0 : break;
118 : }
119 : }
120 :
121 :
122 : enum GNUNET_DB_QueryStatus
123 4 : TALER_EXCHANGEDB_select_reserve_open_above_serial_id (
124 : struct TALER_EXCHANGEDB_PostgresContext *pg,
125 : uint64_t serial_id,
126 : TALER_EXCHANGEDB_ReserveOpenCallback cb,
127 : void *cb_cls)
128 : {
129 4 : struct GNUNET_PQ_QueryParam params[] = {
130 4 : GNUNET_PQ_query_param_uint64 (&serial_id),
131 : GNUNET_PQ_query_param_end
132 : };
133 4 : struct ReserveOpenSerialContext rcsc = {
134 : .cb = cb,
135 : .cb_cls = cb_cls,
136 : .pg = pg,
137 : .status = GNUNET_OK
138 : };
139 : enum GNUNET_DB_QueryStatus qs;
140 :
141 4 : PREPARE (
142 : pg,
143 : "reserves_open_get_incr",
144 : "SELECT"
145 : " open_request_uuid"
146 : ",reserve_pub"
147 : ",request_timestamp"
148 : ",expiration_date"
149 : ",reserve_sig"
150 : ",reserve_payment"
151 : ",requested_purse_limit"
152 : " FROM reserves_open_requests"
153 : " WHERE open_request_uuid>=$1"
154 : " ORDER BY open_request_uuid ASC;");
155 4 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
156 : "reserves_open_get_incr",
157 : params,
158 : &reserve_open_serial_helper_cb,
159 : &rcsc);
160 4 : if (GNUNET_OK != rcsc.status)
161 0 : return GNUNET_DB_STATUS_HARD_ERROR;
162 4 : return qs;
163 : }
|