Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2023 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 auditordb/pg_select_pending_deposits.c
18 : * @brief Implementation of the select_pending_deposits 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_pending_deposits.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Closure for #wire_missing_cb().
31 : */
32 : struct WireMissingContext
33 : {
34 :
35 : /**
36 : * Function to call for each pending deposit.
37 : */
38 : TALER_AUDITORDB_WireMissingCallback cb;
39 :
40 : /**
41 : * Closure for @e cb
42 : */
43 : void *cb_cls;
44 :
45 : /**
46 : * Plugin context.
47 : */
48 : struct PostgresClosure *pg;
49 :
50 : /**
51 : * Query status to return.
52 : */
53 : enum GNUNET_DB_QueryStatus qs;
54 : };
55 :
56 :
57 : /**
58 : * Helper function for #TAH_PG_select_purse_expired().
59 : * To be called with the results of a SELECT statement
60 : * that has returned @a num_results results.
61 : *
62 : * @param cls closure of type `struct WireMissingContext *`
63 : * @param result the postgres result
64 : * @param num_results the number of results in @a result
65 : */
66 : static void
67 0 : wire_missing_cb (void *cls,
68 : PGresult *result,
69 : unsigned int num_results)
70 : {
71 0 : struct WireMissingContext *eic = cls;
72 0 : struct PostgresClosure *pg = eic->pg;
73 :
74 0 : for (unsigned int i = 0; i < num_results; i++)
75 : {
76 : uint64_t batch_deposit_serial_id;
77 : struct TALER_Amount total_amount;
78 : struct TALER_FullPaytoHashP wire_target_h_payto;
79 : struct GNUNET_TIME_Timestamp deadline;
80 0 : struct GNUNET_PQ_ResultSpec rs[] = {
81 0 : GNUNET_PQ_result_spec_uint64 ("batch_deposit_serial_id",
82 : &batch_deposit_serial_id),
83 0 : TALER_PQ_RESULT_SPEC_AMOUNT ("total_amount",
84 : &total_amount),
85 0 : GNUNET_PQ_result_spec_auto_from_type ("wire_target_h_payto",
86 : &wire_target_h_payto),
87 0 : GNUNET_PQ_result_spec_timestamp ("deadline",
88 : &deadline),
89 : GNUNET_PQ_result_spec_end
90 : };
91 :
92 0 : if (GNUNET_OK !=
93 0 : GNUNET_PQ_extract_result (result,
94 : rs,
95 : i))
96 : {
97 0 : GNUNET_break (0);
98 0 : eic->qs = GNUNET_DB_STATUS_HARD_ERROR;
99 0 : return;
100 : }
101 0 : eic->cb (eic->cb_cls,
102 : batch_deposit_serial_id,
103 : &total_amount,
104 : &wire_target_h_payto,
105 : deadline);
106 : }
107 0 : eic->qs = num_results;
108 : }
109 :
110 :
111 : enum GNUNET_DB_QueryStatus
112 0 : TAH_PG_select_pending_deposits (
113 : void *cls,
114 : struct GNUNET_TIME_Absolute deadline,
115 : TALER_AUDITORDB_WireMissingCallback cb,
116 : void *cb_cls)
117 : {
118 0 : struct PostgresClosure *pg = cls;
119 0 : struct GNUNET_PQ_QueryParam params[] = {
120 0 : GNUNET_PQ_query_param_absolute_time (&deadline),
121 : GNUNET_PQ_query_param_end
122 : };
123 0 : struct WireMissingContext eic = {
124 : .cb = cb,
125 : .cb_cls = cb_cls,
126 : .pg = pg
127 : };
128 : enum GNUNET_DB_QueryStatus qs;
129 :
130 0 : PREPARE (pg,
131 : "auditor_select_pending_deposits",
132 : "SELECT"
133 : " batch_deposit_serial_id"
134 : ",total_amount"
135 : ",wire_target_h_payto"
136 : ",deadline"
137 : " FROM auditor_pending_deposits"
138 : " WHERE deadline<$1;");
139 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
140 : pg->conn,
141 : "auditor_select_pending_deposits",
142 : params,
143 : &wire_missing_cb,
144 : &eic);
145 0 : if (0 > qs)
146 0 : return qs;
147 0 : GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != eic.qs);
148 0 : return eic.qs;
149 : }
|