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