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 *description;
72 0 : json_t *description_i18n = NULL;
73 : char *kind;
74 : struct GNUNET_TIME_Timestamp valid_after;
75 : struct GNUNET_TIME_Timestamp valid_before;
76 0 : struct GNUNET_PQ_ResultSpec rs[] = {
77 0 : GNUNET_PQ_result_spec_string ("slug",
78 : &slug),
79 0 : GNUNET_PQ_result_spec_string ("name",
80 : &name),
81 0 : GNUNET_PQ_result_spec_string ("description",
82 : &description),
83 0 : GNUNET_PQ_result_spec_allow_null (
84 : TALER_PQ_result_spec_json ("description_i18n",
85 : &description_i18n),
86 : NULL),
87 0 : GNUNET_PQ_result_spec_timestamp ("valid_after",
88 : &valid_after),
89 0 : GNUNET_PQ_result_spec_timestamp ("valid_before",
90 : &valid_before),
91 0 : GNUNET_PQ_result_spec_string ("kind",
92 : &kind),
93 : GNUNET_PQ_result_spec_end
94 : };
95 :
96 0 : if (GNUNET_OK !=
97 0 : GNUNET_PQ_extract_result (result,
98 : rs,
99 : i))
100 : {
101 0 : GNUNET_break (0);
102 0 : tflc->extract_failed = true;
103 0 : return;
104 : }
105 :
106 0 : tflc->cb (tflc->cb_cls,
107 : slug,
108 : name,
109 : description,
110 : description_i18n,
111 : valid_after,
112 : valid_before,
113 : kind);
114 0 : GNUNET_PQ_cleanup_result (rs);
115 : }
116 : }
117 :
118 :
119 : enum GNUNET_DB_QueryStatus
120 0 : TMH_PG_lookup_token_families (void *cls,
121 : const char *instance_id,
122 : TALER_MERCHANTDB_TokenFamiliesCallback cb,
123 : void *cb_cls)
124 : {
125 0 : struct PostgresClosure *pg = cls;
126 0 : struct LookupTokenFamiliesContext context = {
127 : .cb = cb,
128 : .cb_cls = cb_cls,
129 : /* Can be overwritten by the lookup_token_families_cb */
130 : .extract_failed = false,
131 : };
132 0 : struct GNUNET_PQ_QueryParam params[] = {
133 0 : GNUNET_PQ_query_param_string (instance_id),
134 : GNUNET_PQ_query_param_end
135 : };
136 : enum GNUNET_DB_QueryStatus qs;
137 :
138 0 : check_connection (pg);
139 0 : PREPARE (pg,
140 : "lookup_token_families",
141 : "SELECT"
142 : " slug"
143 : ",name"
144 : ",description"
145 : ",description_i18n::TEXT"
146 : ",valid_after"
147 : ",valid_before"
148 : ",kind"
149 : " FROM merchant_token_families"
150 : " JOIN merchant_instances"
151 : " USING (merchant_serial)"
152 : " WHERE merchant_instances.merchant_id=$1");
153 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
154 : "lookup_token_families",
155 : params,
156 : &lookup_token_families_cb,
157 : &context);
158 : /* If there was an error inside lookup_token_families_cb, return a hard error. */
159 0 : if (context.extract_failed)
160 0 : return GNUNET_DB_STATUS_HARD_ERROR;
161 0 : return qs;
162 : }
|