Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 2024 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/select_reserves_in_above_serial_id_by_account.c
18 : * @brief Implementation of the select_reserves_in_above_serial_id_by_account function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include \
23 : "exchange-database/select_reserves_in_above_serial_id_by_account.h"
24 : #include "helper.h"
25 :
26 :
27 : /**
28 : * Closure for #reserves_in_serial_helper_cb().
29 : */
30 : struct ReservesInSerialContext
31 : {
32 :
33 : /**
34 : * Callback to call.
35 : */
36 : TALER_EXCHANGEDB_ReserveInCallback cb;
37 :
38 : /**
39 : * Closure for @e cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Plugin context.
45 : */
46 : struct TALER_EXCHANGEDB_PostgresContext *pg;
47 :
48 : /**
49 : * Status code, set to #GNUNET_SYSERR on hard errors.
50 : */
51 : enum GNUNET_GenericReturnValue status;
52 : };
53 :
54 :
55 : /**
56 : * Helper function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results.
58 : *
59 : * @param cls closure of type `struct ReservesInSerialContext`
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 0 : reserves_in_serial_helper_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 0 : struct ReservesInSerialContext *risc = cls;
69 0 : struct TALER_EXCHANGEDB_PostgresContext *pg = risc->pg;
70 :
71 0 : for (unsigned int i = 0; i<num_results; i++)
72 : {
73 : struct TALER_ReservePublicKeyP reserve_pub;
74 : struct TALER_Amount credit;
75 : struct TALER_FullPayto sender_account_details;
76 : struct GNUNET_TIME_Timestamp execution_date;
77 : uint64_t rowid;
78 : uint64_t wire_reference;
79 0 : struct GNUNET_PQ_ResultSpec rs[] = {
80 0 : GNUNET_PQ_result_spec_auto_from_type ("reserve_pub",
81 : &reserve_pub),
82 0 : GNUNET_PQ_result_spec_uint64 ("wire_reference",
83 : &wire_reference),
84 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("credit",
85 : &credit),
86 0 : GNUNET_PQ_result_spec_timestamp ("execution_date",
87 : &execution_date),
88 0 : GNUNET_PQ_result_spec_string ("sender_account_details",
89 : &sender_account_details.full_payto),
90 0 : GNUNET_PQ_result_spec_uint64 ("reserve_in_serial_id",
91 : &rowid),
92 : GNUNET_PQ_result_spec_end
93 : };
94 : enum GNUNET_GenericReturnValue ret;
95 :
96 0 : if (GNUNET_OK !=
97 0 : GNUNET_PQ_extract_result (result,
98 : rs,
99 : i))
100 : {
101 0 : GNUNET_break (0);
102 0 : risc->status = GNUNET_SYSERR;
103 0 : return;
104 : }
105 0 : ret = risc->cb (risc->cb_cls,
106 : rowid,
107 : &reserve_pub,
108 : &credit,
109 : sender_account_details,
110 : wire_reference,
111 : execution_date);
112 0 : GNUNET_PQ_cleanup_result (rs);
113 0 : if (GNUNET_OK != ret)
114 0 : break;
115 : }
116 : }
117 :
118 :
119 : enum GNUNET_DB_QueryStatus
120 0 : TALER_TALER_EXCHANGEDB_select_reserves_in_above_serial_id_by_account (
121 : struct TALER_EXCHANGEDB_PostgresContext *pg,
122 : const char *account_name,
123 : uint64_t serial_id,
124 : TALER_EXCHANGEDB_ReserveInCallback cb,
125 : void *cb_cls)
126 : {
127 0 : struct GNUNET_PQ_QueryParam params[] = {
128 0 : GNUNET_PQ_query_param_uint64 (&serial_id),
129 0 : GNUNET_PQ_query_param_string (account_name),
130 : GNUNET_PQ_query_param_end
131 : };
132 0 : struct ReservesInSerialContext risc = {
133 : .cb = cb,
134 : .cb_cls = cb_cls,
135 : .pg = pg,
136 : .status = GNUNET_OK
137 : };
138 : enum GNUNET_DB_QueryStatus qs;
139 :
140 0 : PREPARE (pg,
141 : "select_reserves_in_above_serial_id_by_account",
142 : "SELECT"
143 : " reserves.reserve_pub"
144 : ",wire_reference"
145 : ",credit"
146 : ",execution_date"
147 : ",wt.payto_uri AS sender_account_details"
148 : ",reserve_in_serial_id"
149 : " FROM reserves_in"
150 : " JOIN reserves "
151 : " USING (reserve_pub)"
152 : " JOIN wire_targets wt"
153 : " ON (wire_source_h_payto = wire_target_h_payto)"
154 : " WHERE reserve_in_serial_id>=$1"
155 : " AND exchange_account_section=$2"
156 : " ORDER BY reserve_in_serial_id ASC;");
157 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
158 : pg->conn,
159 : "select_reserves_in_above_serial_id_by_account",
160 : params,
161 : &reserves_in_serial_helper_cb,
162 : &risc);
163 0 : if (GNUNET_OK != risc.status)
164 0 : return GNUNET_DB_STATUS_HARD_ERROR;
165 0 : return qs;
166 : }
|