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