Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2025 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 exchangedb/pg_select_exchange_debit_transfers.c
18 : * @brief Implementation of the select_exchange_debit_transfers function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler_error_codes.h"
23 : #include "taler_dbevents.h"
24 : #include "taler_pq_lib.h"
25 : #include "pg_select_exchange_debit_transfers.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #handle_aml_result.
31 : */
32 : struct SelectTransferContext
33 : {
34 : /**
35 : * Function to call on each result.
36 : */
37 : TALER_EXCHANGEDB_AmlTransferCallback cb;
38 :
39 : /**
40 : * Closure for @e cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Plugin context.
46 : */
47 : struct PostgresClosure *pg;
48 :
49 : /**
50 : * Set to #GNUNET_SYSERR on serious errors.
51 : */
52 : enum GNUNET_GenericReturnValue status;
53 : };
54 :
55 :
56 : /**
57 : * Function to be called with the results of a SELECT statement
58 : * that has returned @a num_results results. Helper function
59 : * for #TEH_PG_select_exchange_debit_transfers().
60 : *
61 : * @param cls closure of type `struct SelectTransferContext *`
62 : * @param result the postgres result
63 : * @param num_results the number of results in @a result
64 : */
65 : static void
66 0 : handle_transfer_result (void *cls,
67 : PGresult *result,
68 : unsigned int num_results)
69 : {
70 0 : struct SelectTransferContext *stc = cls;
71 0 : struct PostgresClosure *pg = stc->pg;
72 :
73 0 : for (unsigned int i = 0; i<num_results; i++)
74 : {
75 : char *payto_uri;
76 : uint64_t rowid;
77 : struct GNUNET_TIME_Absolute execution_time;
78 : struct TALER_Amount amount;
79 0 : struct GNUNET_PQ_ResultSpec rs[] = {
80 0 : GNUNET_PQ_result_spec_uint64 ("serial_id",
81 : &rowid),
82 0 : GNUNET_PQ_result_spec_string ("payto_uri",
83 : &payto_uri),
84 0 : GNUNET_PQ_result_spec_absolute_time ("execution_time",
85 : &execution_time),
86 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("amount",
87 : &amount),
88 : GNUNET_PQ_result_spec_end
89 : };
90 :
91 0 : if (GNUNET_OK !=
92 0 : GNUNET_PQ_extract_result (result,
93 : rs,
94 : i))
95 : {
96 0 : GNUNET_break (0);
97 0 : stc->status = GNUNET_SYSERR;
98 0 : return;
99 : }
100 0 : stc->cb (stc->cb_cls,
101 : rowid,
102 : payto_uri,
103 : execution_time,
104 : &amount);
105 0 : GNUNET_PQ_cleanup_result (rs);
106 : }
107 : }
108 :
109 :
110 : enum GNUNET_DB_QueryStatus
111 0 : TEH_PG_select_exchange_debit_transfers (
112 : void *cls,
113 : const struct TALER_Amount *threshold,
114 : uint64_t offset,
115 : int64_t limit,
116 : TALER_EXCHANGEDB_AmlTransferCallback cb,
117 : void *cb_cls)
118 : {
119 0 : struct PostgresClosure *pg = cls;
120 0 : struct SelectTransferContext stc = {
121 : .pg = pg,
122 : .cb = cb,
123 : .cb_cls = cb_cls,
124 : .status = GNUNET_OK
125 : };
126 0 : uint64_t ulimit = (limit > 0) ? limit : -limit;
127 0 : struct GNUNET_PQ_QueryParam params[] = {
128 0 : GNUNET_PQ_query_param_uint64 (&offset),
129 0 : GNUNET_PQ_query_param_uint64 (&ulimit),
130 0 : TALER_PQ_query_param_amount (pg->conn,
131 : threshold),
132 : GNUNET_PQ_query_param_end
133 : };
134 : enum GNUNET_DB_QueryStatus qs;
135 :
136 0 : PREPARE (pg,
137 : "select_exchange_debit_transfers_inc",
138 : "SELECT"
139 : " wo.wireout_uuid AS serial_id"
140 : ",wt.payto_uri"
141 : ",wo.execution_date AS execution_time"
142 : ",wo.amount"
143 : " FROM wire_out wo"
144 : " LEFT JOIN wire_targets wt"
145 : " USING (wire_target_h_payto)"
146 : " WHERE (wo.wireout_uuid > $1)"
147 : " AND ( ( (wo.amount).val > ($3::taler_amount).val)"
148 : " OR ( ( (wo.amount).val >= ($3::taler_amount).val)"
149 : " AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )"
150 : " ORDER BY wo.wireout_uuid ASC"
151 : " LIMIT $2");
152 0 : PREPARE (pg,
153 : "select_exchange_debit_transfers_dec",
154 : "SELECT"
155 : " wo.wireout_uuid AS serial_id"
156 : ",wt.payto_uri"
157 : ",wo.execution_date AS execution_time"
158 : ",wo.amount"
159 : " FROM wire_out wo"
160 : " LEFT JOIN wire_targets wt"
161 : " USING (wire_target_h_payto)"
162 : " WHERE (wo.wireout_uuid < $1)"
163 : " AND ( ( (wo.amount).val > ($3::taler_amount).val)"
164 : " OR ( ( (wo.amount).val >= ($3::taler_amount).val)"
165 : " AND ( (wo.amount).frac >= ($3::taler_amount).frac) ) )"
166 : " ORDER BY wo.wireout_uuid DESC"
167 : " LIMIT $2");
168 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
169 : pg->conn,
170 : (limit > 0)
171 : ? "select_exchange_debit_transfers_inc"
172 : : "select_exchange_debit_transfers_dec",
173 : params,
174 : &handle_transfer_result,
175 : &stc);
176 0 : if (GNUNET_OK != stc.status)
177 0 : return GNUNET_DB_STATUS_HARD_ERROR;
178 0 : return qs;
179 : }
|