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/wire_prepare_data_get.c
18 : * @brief Implementation of the wire_prepare_data_get function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "taler/taler_pq_lib.h"
22 : #include "exchange-database/wire_prepare_data_get.h"
23 : #include "helper.h"
24 :
25 : /**
26 : * Closure for #prewire_cb().
27 : */
28 : struct PrewireContext
29 : {
30 : /**
31 : * Function to call on each result.
32 : */
33 : TALER_EXCHANGEDB_WirePreparationIterator cb;
34 :
35 : /**
36 : * Closure for @a cb.
37 : */
38 : void *cb_cls;
39 :
40 : /**
41 : * #GNUNET_OK if everything went fine.
42 : */
43 : enum GNUNET_GenericReturnValue status;
44 : };
45 :
46 :
47 : /**
48 : * Invoke the callback for each result.
49 : *
50 : * @param cls a `struct MissingWireContext *`
51 : * @param result SQL result
52 : * @param num_results number of rows in @a result
53 : */
54 : static void
55 108 : prewire_cb (void *cls,
56 : PGresult *result,
57 : unsigned int num_results)
58 : {
59 108 : struct PrewireContext *pc = cls;
60 :
61 169 : for (unsigned int i = 0; i < num_results; i++)
62 : {
63 : uint64_t prewire_uuid;
64 : char *wire_method;
65 61 : void *buf = NULL;
66 : size_t buf_size;
67 61 : struct GNUNET_PQ_ResultSpec rs[] = {
68 61 : GNUNET_PQ_result_spec_uint64 ("prewire_uuid",
69 : &prewire_uuid),
70 61 : GNUNET_PQ_result_spec_string ("wire_method",
71 : &wire_method),
72 61 : GNUNET_PQ_result_spec_variable_size ("buf",
73 : &buf,
74 : &buf_size),
75 : GNUNET_PQ_result_spec_end
76 : };
77 :
78 61 : if (GNUNET_OK !=
79 61 : GNUNET_PQ_extract_result (result,
80 : rs,
81 : i))
82 : {
83 0 : GNUNET_break (0);
84 0 : pc->status = GNUNET_SYSERR;
85 0 : return;
86 : }
87 61 : pc->cb (pc->cb_cls,
88 : prewire_uuid,
89 : wire_method,
90 : buf,
91 : buf_size);
92 61 : GNUNET_PQ_cleanup_result (rs);
93 : }
94 : }
95 :
96 :
97 : enum GNUNET_DB_QueryStatus
98 108 : TALER_EXCHANGEDB_wire_prepare_data_get (struct
99 : TALER_EXCHANGEDB_PostgresContext *pg,
100 : uint64_t start_row,
101 : uint64_t limit,
102 : TALER_EXCHANGEDB_WirePreparationIterator
103 : cb,
104 : void *cb_cls)
105 : {
106 108 : struct GNUNET_PQ_QueryParam params[] = {
107 108 : GNUNET_PQ_query_param_uint64 (&start_row),
108 108 : GNUNET_PQ_query_param_uint64 (&limit),
109 : GNUNET_PQ_query_param_end
110 : };
111 108 : struct PrewireContext pc = {
112 : .cb = cb,
113 : .cb_cls = cb_cls,
114 : .status = GNUNET_OK
115 : };
116 : enum GNUNET_DB_QueryStatus qs;
117 :
118 108 : PREPARE (pg,
119 : "wire_prepare_data_get",
120 : "SELECT"
121 : " prewire_uuid"
122 : ",wire_method"
123 : ",buf"
124 : " FROM prewire"
125 : " WHERE prewire_uuid >= $1"
126 : " AND finished=FALSE"
127 : " AND failed=FALSE"
128 : " ORDER BY prewire_uuid ASC"
129 : " LIMIT $2;");
130 108 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
131 : "wire_prepare_data_get",
132 : params,
133 : &prewire_cb,
134 : &pc);
135 108 : if (GNUNET_OK != pc.status)
136 0 : return GNUNET_DB_STATUS_HARD_ERROR;
137 108 : return qs;
138 : }
|