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 backenddb/pg_lookup_reports_pending.c
18 : * @brief Implementation of the lookup_reports_pending function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_error_codes.h>
23 : #include <taler/taler_dbevents.h>
24 : #include <taler/taler_pq_lib.h>
25 : #include "pg_lookup_reports_pending.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Context used for TMH_PG_lookup_reports_pending().
31 : */
32 : struct SelectReportsContext
33 : {
34 : /**
35 : * Function to call with the results.
36 : */
37 : TALER_MERCHANTDB_ReportsPendingCallback cb;
38 :
39 : /**
40 : * Closure for @a cb.
41 : */
42 : void *cb_cls;
43 :
44 : /**
45 : * Did database result extraction fail?
46 : */
47 : bool extract_failed;
48 : };
49 :
50 :
51 : /**
52 : * Function to be called with the results of a SELECT statement
53 : * that has returned @a num_results results about reports.
54 : *
55 : * @param[in,out] cls of type `struct SelectReportsContext *`
56 : * @param result the postgres result
57 : * @param num_results the number of results in @a result
58 : */
59 : static void
60 0 : select_pending_reports_cb (void *cls,
61 : PGresult *result,
62 : unsigned int num_results)
63 : {
64 0 : struct SelectReportsContext *plc = cls;
65 :
66 0 : for (unsigned int i = 0; i < num_results; i++)
67 : {
68 : char *instance_id;
69 : uint64_t report_serial;
70 : char *report_program_section;
71 : char *report_description;
72 : char *mime_type;
73 : struct TALER_MERCHANT_ReportToken report_token;
74 : char *target_address;
75 : struct GNUNET_TIME_Relative frequency;
76 : struct GNUNET_TIME_Relative frequency_shift;
77 : struct GNUNET_TIME_Absolute next_transmission;
78 0 : struct GNUNET_PQ_ResultSpec rs[] = {
79 0 : GNUNET_PQ_result_spec_string ("merchant_id",
80 : &instance_id),
81 0 : GNUNET_PQ_result_spec_uint64 ("report_serial",
82 : &report_serial),
83 0 : GNUNET_PQ_result_spec_string ("report_program_section",
84 : &report_program_section),
85 0 : GNUNET_PQ_result_spec_string ("report_description",
86 : &report_description),
87 0 : GNUNET_PQ_result_spec_string ("mime_type",
88 : &mime_type),
89 0 : GNUNET_PQ_result_spec_auto_from_type ("report_token",
90 : &report_token),
91 0 : GNUNET_PQ_result_spec_string ("target_address",
92 : &target_address),
93 0 : GNUNET_PQ_result_spec_relative_time ("frequency",
94 : &frequency),
95 0 : GNUNET_PQ_result_spec_relative_time ("frequency_shift",
96 : &frequency_shift),
97 0 : GNUNET_PQ_result_spec_absolute_time ("next_transmission",
98 : &next_transmission),
99 : GNUNET_PQ_result_spec_end
100 : };
101 :
102 0 : if (GNUNET_OK !=
103 0 : GNUNET_PQ_extract_result (result,
104 : rs,
105 : i))
106 : {
107 0 : GNUNET_break (0);
108 0 : plc->extract_failed = true;
109 0 : return;
110 : }
111 0 : plc->cb (plc->cb_cls,
112 : instance_id,
113 : report_serial,
114 : report_program_section,
115 : report_description,
116 : mime_type,
117 : &report_token,
118 : target_address,
119 : frequency,
120 : frequency_shift,
121 : next_transmission);
122 0 : GNUNET_PQ_cleanup_result (rs);
123 : }
124 : }
125 :
126 :
127 : enum GNUNET_DB_QueryStatus
128 0 : TMH_PG_lookup_reports_pending (void *cls,
129 : TALER_MERCHANTDB_ReportsPendingCallback cb,
130 : void *cb_cls)
131 : {
132 0 : struct PostgresClosure *pg = cls;
133 0 : struct SelectReportsContext plc = {
134 : .cb = cb,
135 : .cb_cls = cb_cls,
136 : /* Can be overwritten by the lookup_reports_cb */
137 : .extract_failed = false,
138 : };
139 0 : struct GNUNET_PQ_QueryParam params[] = {
140 : GNUNET_PQ_query_param_end
141 : };
142 : enum GNUNET_DB_QueryStatus qs;
143 :
144 0 : check_connection (pg);
145 0 : PREPARE (pg,
146 : "lookup_reports_pending",
147 : "SELECT"
148 : " mi.merchant_id"
149 : " ,mr.report_serial"
150 : " ,mr.report_program_section"
151 : " ,mr.report_description"
152 : " ,mr.mime_type"
153 : " ,mr.report_token"
154 : " ,mr.target_address"
155 : " ,mr.frequency"
156 : " ,mr.frequency_shift"
157 : " ,mr.next_transmission"
158 : " FROM merchant_reports mr"
159 : " JOIN merchant_instances mi"
160 : " USING (merchant_serial)"
161 : " ORDER BY next_transmission ASC"
162 : " LIMIT 1;");
163 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
164 : pg->conn,
165 : "lookup_reports_pending",
166 : params,
167 : &select_pending_reports_cb,
168 : &plc);
169 : /* If there was an error inside select_pending_reports_cb, return a hard error. */
170 0 : if (plc.extract_failed)
171 : {
172 0 : GNUNET_break (0);
173 0 : return GNUNET_DB_STATUS_HARD_ERROR;
174 : }
175 0 : return qs;
176 : }
|