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 backenddb/pg_lookup_token_families.c
18 : * @brief Implementation of the lookup_token_families function for Postgres
19 : * @author Christian Blättler
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_token_families.h"
26 : #include "pg_helper.h"
27 : #include "taler_merchantdb_plugin.h"
28 :
29 :
30 : /**
31 : * Context used for TMH_PG_lookup_token_families().
32 : */
33 : struct LookupTokenFamiliesContext
34 : {
35 : /**
36 : * Function to call with the results.
37 : */
38 : TALER_MERCHANTDB_TokenFamiliesCallback cb;
39 :
40 : /**
41 : * Closure for @a cb.
42 : */
43 : void *cb_cls;
44 :
45 : /**
46 : * Did database result extraction fail?
47 : */
48 : bool extract_failed;
49 : };
50 :
51 :
52 : /**
53 : * Function to be called with the results of a SELECT statement
54 : * that has returned @a num_results results about token families.
55 : *
56 : * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
57 : * @param result the postgres result
58 : * @param num_results the number of results in @a result
59 : */
60 : static void
61 0 : lookup_token_families_cb (void *cls,
62 : PGresult *result,
63 : unsigned int num_results)
64 : {
65 0 : struct LookupTokenFamiliesContext *tflc = cls;
66 :
67 0 : for (unsigned int i = 0; i < num_results; i++)
68 : {
69 : char *slug;
70 : char *name;
71 : char *kind;
72 : struct GNUNET_TIME_Timestamp valid_after;
73 : struct GNUNET_TIME_Timestamp valid_before;
74 0 : struct GNUNET_PQ_ResultSpec rs[] = {
75 0 : GNUNET_PQ_result_spec_string ("slug",
76 : &slug),
77 0 : GNUNET_PQ_result_spec_string ("name",
78 : &name),
79 0 : GNUNET_PQ_result_spec_timestamp ("valid_after",
80 : &valid_after),
81 0 : GNUNET_PQ_result_spec_timestamp ("valid_before",
82 : &valid_before),
83 0 : GNUNET_PQ_result_spec_string ("kind",
84 : &kind),
85 : GNUNET_PQ_result_spec_end
86 : };
87 :
88 0 : if (GNUNET_OK !=
89 0 : GNUNET_PQ_extract_result (result,
90 : rs,
91 : i))
92 : {
93 0 : GNUNET_break (0);
94 0 : tflc->extract_failed = true;
95 0 : return;
96 : }
97 :
98 0 : tflc->cb (tflc->cb_cls,
99 : slug,
100 : name,
101 : valid_after,
102 : valid_before,
103 : kind);
104 0 : GNUNET_PQ_cleanup_result (rs);
105 : }
106 : }
107 :
108 :
109 : enum GNUNET_DB_QueryStatus
110 0 : TMH_PG_lookup_token_families (void *cls,
111 : const char *instance_id,
112 : TALER_MERCHANTDB_TokenFamiliesCallback cb,
113 : void *cb_cls)
114 : {
115 0 : struct PostgresClosure *pg = cls;
116 0 : struct LookupTokenFamiliesContext context = {
117 : .cb = cb,
118 : .cb_cls = cb_cls,
119 : /* Can be overwritten by the lookup_token_families_cb */
120 : .extract_failed = false,
121 : };
122 0 : struct GNUNET_PQ_QueryParam params[] = {
123 0 : GNUNET_PQ_query_param_string (instance_id),
124 : GNUNET_PQ_query_param_end
125 : };
126 : enum GNUNET_DB_QueryStatus qs;
127 :
128 0 : check_connection (pg);
129 0 : PREPARE (pg,
130 : "lookup_token_families",
131 : "SELECT"
132 : " slug"
133 : ",name"
134 : ",valid_after"
135 : ",valid_before"
136 : ",kind"
137 : " FROM merchant_token_families"
138 : " JOIN merchant_instances"
139 : " USING (merchant_serial)"
140 : " WHERE merchant_instances.merchant_id=$1");
141 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
142 : "lookup_token_families",
143 : params,
144 : &lookup_token_families_cb,
145 : &context);
146 : /* If there was an error inside lookup_token_families_cb, return a hard error. */
147 0 : if (context.extract_failed)
148 0 : return GNUNET_DB_STATUS_HARD_ERROR;
149 0 : return qs;
150 : }
|