Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 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_categories.c
18 : * @brief Implementation of the iterate_categories function for Postgres
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_categories.h"
24 : #include "helper.h"
25 :
26 :
27 : /**
28 : * Context used for TALER_MERCHANTDB_iterate_categories().
29 : */
30 : struct LookupCategoryContext
31 : {
32 : /**
33 : * Function to call with the results.
34 : */
35 : TALER_MERCHANTDB_CategoriesCallback cb;
36 :
37 : /**
38 : * Closure for @a cb.
39 : */
40 : void *cb_cls;
41 :
42 : /**
43 : * Did database result extraction fail?
44 : */
45 : bool extract_failed;
46 : };
47 :
48 :
49 : /**
50 : * Function to be called with the results of a SELECT statement
51 : * that has returned @a num_results results about otp_device.
52 : *
53 : * @param[in,out] cls of type `struct LookupCategoryContext *`
54 : * @param result the postgres result
55 : * @param num_results the number of results in @a result
56 : */
57 : static void
58 0 : lookup_categories_cb (void *cls,
59 : PGresult *result,
60 : unsigned int num_results)
61 : {
62 0 : struct LookupCategoryContext *tlc = cls;
63 :
64 0 : for (unsigned int i = 0; i < num_results; i++)
65 : {
66 : uint64_t category_id;
67 : char *category_name;
68 : json_t *category_name_i18n;
69 : uint64_t product_count;
70 0 : struct GNUNET_PQ_ResultSpec rs[] = {
71 0 : GNUNET_PQ_result_spec_uint64 ("category_serial",
72 : &category_id),
73 0 : GNUNET_PQ_result_spec_string ("category_name",
74 : &category_name),
75 0 : TALER_PQ_result_spec_json ("category_name_i18n",
76 : &category_name_i18n),
77 0 : GNUNET_PQ_result_spec_uint64 ("product_count",
78 : &product_count),
79 : GNUNET_PQ_result_spec_end
80 : };
81 :
82 0 : if (GNUNET_OK !=
83 0 : GNUNET_PQ_extract_result (result,
84 : rs,
85 : i))
86 : {
87 0 : GNUNET_break (0);
88 0 : tlc->extract_failed = true;
89 0 : return;
90 : }
91 0 : tlc->cb (tlc->cb_cls,
92 : category_id,
93 : category_name,
94 : category_name_i18n,
95 : product_count);
96 0 : GNUNET_PQ_cleanup_result (rs);
97 : }
98 : }
99 :
100 :
101 : enum GNUNET_DB_QueryStatus
102 0 : TALER_MERCHANTDB_iterate_categories (
103 : struct TALER_MERCHANTDB_PostgresContext *pg,
104 : const char *instance_id,
105 : TALER_MERCHANTDB_CategoriesCallback cb,
106 : void *cb_cls)
107 : {
108 0 : struct LookupCategoryContext tlc = {
109 : .cb = cb,
110 : .cb_cls = cb_cls,
111 : /* Can be overwritten by the lookup_categories_cb */
112 : .extract_failed = false,
113 : };
114 0 : struct GNUNET_PQ_QueryParam params[] = {
115 : GNUNET_PQ_query_param_end
116 : };
117 : enum GNUNET_DB_QueryStatus qs;
118 :
119 0 : GNUNET_assert (NULL != pg->current_merchant_id);
120 0 : GNUNET_assert (0 == strcmp (instance_id,
121 : pg->current_merchant_id));
122 0 : TMH_PQ_prepare_anon (pg,
123 : "SELECT"
124 : " mc.category_serial"
125 : ",mc.category_name"
126 : ",mc.category_name_i18n::TEXT"
127 : ",COALESCE(COUNT(mpc.product_serial),0)"
128 : " AS product_count"
129 : " FROM merchant_categories mc"
130 : " LEFT JOIN merchant_product_categories mpc"
131 : " USING (category_serial)"
132 : " GROUP BY mc.category_serial"
133 : " ORDER BY mc.category_serial;");
134 0 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
135 : "",
136 : params,
137 : &lookup_categories_cb,
138 : &tlc);
139 : /* If there was an error inside lookup_categories_cb, return a hard error. */
140 0 : if (tlc.extract_failed)
141 0 : return GNUNET_DB_STATUS_HARD_ERROR;
142 0 : return qs;
143 : }
|