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_open_transfers.c
18 : * @brief Implementation of the select_open_transfers 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_open_transfers.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Context used for open_transfers_cb().
31 : */
32 : struct SelectOpenTransfersContext
33 : {
34 : /**
35 : * Postgres context.
36 : */
37 : struct PostgresClosure *pg;
38 :
39 : /**
40 : * Function to call with the results.
41 : */
42 : TALER_MERCHANTDB_OpenTransferCallback cb;
43 :
44 : /**
45 : * Closure for @a cb.
46 : */
47 : void *cb_cls;
48 :
49 : /**
50 : * Internal result.
51 : */
52 : enum GNUNET_DB_QueryStatus qs;
53 : };
54 :
55 :
56 : /**
57 : * Function to be called with the results of a SELECT statement
58 : * that has returned @a num_results results about rewards.
59 : *
60 : * @param[in,out] cls of type `struct SelectOpenTransfersContext *`
61 : * @param result the postgres result
62 : * @param num_results the number of results in @a result
63 : */
64 : static void
65 10 : open_transfers_cb (void *cls,
66 : PGresult *result,
67 : unsigned int num_results)
68 : {
69 10 : struct SelectOpenTransfersContext *plc = cls;
70 :
71 20 : for (unsigned int i = 0; i < num_results; i++)
72 : {
73 : uint64_t rowid;
74 : char *instance_id;
75 : char *exchange_url;
76 : struct TALER_FullPayto payto_uri;
77 : struct TALER_WireTransferIdentifierRawP wtid;
78 : struct TALER_Amount total;
79 : struct GNUNET_TIME_Absolute next_attempt;
80 10 : struct GNUNET_PQ_ResultSpec rs[] = {
81 10 : GNUNET_PQ_result_spec_uint64 ("credit_serial",
82 : &rowid),
83 10 : GNUNET_PQ_result_spec_string ("instance_id",
84 : &instance_id),
85 10 : GNUNET_PQ_result_spec_string ("exchange_url",
86 : &exchange_url),
87 10 : GNUNET_PQ_result_spec_string ("payto_uri",
88 : &payto_uri.full_payto),
89 10 : GNUNET_PQ_result_spec_auto_from_type ("wtid",
90 : &wtid),
91 10 : TALER_PQ_result_spec_amount_with_currency ("credit_amount",
92 : &total),
93 10 : GNUNET_PQ_result_spec_absolute_time ("next_attempt",
94 : &next_attempt),
95 : GNUNET_PQ_result_spec_end
96 : };
97 :
98 10 : if (GNUNET_OK !=
99 10 : GNUNET_PQ_extract_result (result,
100 : rs,
101 : i))
102 : {
103 0 : GNUNET_break (0);
104 0 : plc->qs = GNUNET_DB_STATUS_HARD_ERROR;
105 0 : return;
106 : }
107 10 : plc->cb (plc->cb_cls,
108 : rowid,
109 : instance_id,
110 : exchange_url,
111 : payto_uri,
112 : &wtid,
113 : &total,
114 : next_attempt);
115 10 : GNUNET_PQ_cleanup_result (rs);
116 : }
117 : }
118 :
119 :
120 : enum GNUNET_DB_QueryStatus
121 10 : TMH_PG_select_open_transfers (void *cls,
122 : uint64_t limit,
123 : TALER_MERCHANTDB_OpenTransferCallback cb,
124 : void *cb_cls)
125 : {
126 10 : struct PostgresClosure *pg = cls;
127 10 : struct SelectOpenTransfersContext plc = {
128 : .pg = pg,
129 : .cb = cb,
130 : .cb_cls = cb_cls
131 : };
132 10 : struct GNUNET_PQ_QueryParam params[] = {
133 10 : GNUNET_PQ_query_param_uint64 (&limit),
134 : GNUNET_PQ_query_param_end
135 : };
136 : enum GNUNET_DB_QueryStatus qs;
137 :
138 10 : PREPARE (pg,
139 : "select_open_transfers",
140 : "SELECT"
141 : " credit_serial"
142 : ",merchant_id AS instance_id"
143 : ",exchange_url"
144 : ",payto_uri"
145 : ",wtid"
146 : ",credit_amount"
147 : ",ready_time AS next_attempt"
148 : " FROM merchant_transfers"
149 : " JOIN merchant_accounts"
150 : " USING (account_serial)"
151 : " JOIN merchant_instances"
152 : " USING (merchant_serial)"
153 : " WHERE confirmed AND"
154 : " NOT (failed OR verified)"
155 : " ORDER BY ready_time ASC"
156 : " LIMIT $1;");
157 :
158 10 : qs = GNUNET_PQ_eval_prepared_multi_select (
159 : pg->conn,
160 : "select_open_transfers",
161 : params,
162 : &open_transfers_cb,
163 : &plc);
164 10 : if (0 != plc.qs)
165 0 : return plc.qs;
166 10 : return qs;
167 : }
|