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 src/backenddb/iterate_token_families.c
18 : * @brief Implementation of the iterate_token_families function for Postgres
19 : * @author Christian Blättler
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_token_families.h"
24 : #include "helper.h"
25 : #include "merchantdb_lib.h"
26 :
27 :
28 : /**
29 : * Context used for TALER_MERCHANTDB_iterate_token_families().
30 : */
31 : struct LookupTokenFamiliesContext
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_TokenFamiliesCallback cb;
37 :
38 : /**
39 : * Closure for @a cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Did database result extraction fail?
45 : */
46 : bool extract_failed;
47 : };
48 :
49 :
50 : /**
51 : * Function to be called with the results of a SELECT statement
52 : * that has returned @a num_results results about token families.
53 : *
54 : * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
55 : * @param result the postgres result
56 : * @param num_results the number of results in @a result
57 : */
58 : static void
59 0 : lookup_token_families_cb (void *cls,
60 : PGresult *result,
61 : unsigned int num_results)
62 : {
63 0 : struct LookupTokenFamiliesContext *tflc = cls;
64 :
65 0 : for (unsigned int i = 0; i < num_results; i++)
66 : {
67 : char *slug;
68 : char *name;
69 : char *description;
70 0 : json_t *description_i18n = NULL;
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_string ("description",
80 : &description),
81 0 : GNUNET_PQ_result_spec_allow_null (
82 : TALER_PQ_result_spec_json ("description_i18n",
83 : &description_i18n),
84 : NULL),
85 0 : GNUNET_PQ_result_spec_timestamp ("valid_after",
86 : &valid_after),
87 0 : GNUNET_PQ_result_spec_timestamp ("valid_before",
88 : &valid_before),
89 0 : GNUNET_PQ_result_spec_string ("kind",
90 : &kind),
91 : GNUNET_PQ_result_spec_end
92 : };
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 : tflc->extract_failed = true;
101 0 : return;
102 : }
103 :
104 0 : tflc->cb (tflc->cb_cls,
105 : slug,
106 : name,
107 : description,
108 : description_i18n,
109 : valid_after,
110 : valid_before,
111 : kind);
112 0 : GNUNET_PQ_cleanup_result (rs);
113 : }
114 : }
115 :
116 :
117 : enum GNUNET_DB_QueryStatus
118 0 : TALER_MERCHANTDB_iterate_token_families (
119 : struct TALER_MERCHANTDB_PostgresContext *pg,
120 : const char *instance_id,
121 : TALER_MERCHANTDB_TokenFamiliesCallback cb,
122 : void *cb_cls)
123 : {
124 0 : struct LookupTokenFamiliesContext context = {
125 : .cb = cb,
126 : .cb_cls = cb_cls,
127 : /* Can be overwritten by the lookup_token_families_cb */
128 : .extract_failed = false,
129 : };
130 0 : struct GNUNET_PQ_QueryParam params[] = {
131 : GNUNET_PQ_query_param_end
132 : };
133 : enum GNUNET_DB_QueryStatus qs;
134 :
135 0 : GNUNET_assert (NULL != pg->current_merchant_id);
136 0 : GNUNET_assert (0 == strcmp (instance_id,
137 : pg->current_merchant_id));
138 0 : TMH_PQ_prepare_anon (pg,
139 : "SELECT"
140 : " slug"
141 : ",name"
142 : ",description"
143 : ",description_i18n::TEXT"
144 : ",valid_after"
145 : ",valid_before"
146 : ",kind"
147 : " FROM merchant_token_families");
148 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
149 : "",
150 : params,
151 : &lookup_token_families_cb,
152 : &context);
153 : /* If there was an error inside lookup_token_families_cb, return a hard error. */
154 0 : if (context.extract_failed)
155 0 : return GNUNET_DB_STATUS_HARD_ERROR;
156 0 : return qs;
157 : }
|