Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024, 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_all_products.c
18 : * @brief Implementation of the lookup_all_products 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_all_products.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Context used for TMH_PG_lookup_all_products().
30 : */
31 : struct LookupProductsContext
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_ProductCallback cb;
37 :
38 : /**
39 : * Closure for @a cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Postgres context.
45 : */
46 : struct PostgresClosure *pg;
47 :
48 : /**
49 : * Did database result extraction fail?
50 : */
51 : bool extract_failed;
52 : };
53 :
54 :
55 : /**
56 : * Function to be called with the results of a SELECT statement
57 : * that has returned @a num_results results about products.
58 : *
59 : * @param[in,out] cls of type `struct LookupProductsContext *`
60 : * @param result the postgres result
61 : * @param num_results the number of results in @a result
62 : */
63 : static void
64 0 : lookup_products_cb (void *cls,
65 : PGresult *result,
66 : unsigned int num_results)
67 : {
68 0 : struct LookupProductsContext *plc = cls;
69 0 : struct PostgresClosure *pg = plc->pg;
70 :
71 0 : for (unsigned int i = 0; i < num_results; i++)
72 : {
73 : char *product_id;
74 : uint64_t product_serial;
75 : struct TALER_MERCHANTDB_ProductDetails pd;
76 : size_t num_categories;
77 : uint64_t *categories;
78 0 : struct GNUNET_PQ_ResultSpec rs[] = {
79 0 : GNUNET_PQ_result_spec_string ("product_id",
80 : &product_id),
81 0 : GNUNET_PQ_result_spec_uint64 ("product_serial",
82 : &product_serial),
83 0 : GNUNET_PQ_result_spec_string ("product_name",
84 : &pd.product_name),
85 0 : GNUNET_PQ_result_spec_string ("description",
86 : &pd.description),
87 0 : TALER_PQ_result_spec_json ("description_i18n",
88 : &pd.description_i18n),
89 0 : GNUNET_PQ_result_spec_string ("unit",
90 : &pd.unit),
91 0 : TALER_PQ_result_spec_amount_with_currency ("price",
92 : &pd.price),
93 0 : TALER_PQ_result_spec_json ("taxes",
94 : &pd.taxes),
95 0 : GNUNET_PQ_result_spec_uint64 ("total_stock",
96 : &pd.total_stock),
97 0 : GNUNET_PQ_result_spec_uint64 ("total_sold",
98 : &pd.total_sold),
99 0 : GNUNET_PQ_result_spec_uint64 ("total_lost",
100 : &pd.total_lost),
101 0 : GNUNET_PQ_result_spec_string ("image",
102 : &pd.image),
103 0 : TALER_PQ_result_spec_json ("address",
104 : &pd.address),
105 0 : GNUNET_PQ_result_spec_timestamp ("next_restock",
106 : &pd.next_restock),
107 0 : GNUNET_PQ_result_spec_uint32 ("minimum_age",
108 : &pd.minimum_age),
109 0 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
110 : "categories",
111 : &num_categories,
112 : &categories),
113 : GNUNET_PQ_result_spec_end
114 : };
115 :
116 0 : if (GNUNET_OK !=
117 0 : GNUNET_PQ_extract_result (result,
118 : rs,
119 : i))
120 : {
121 0 : GNUNET_break (0);
122 0 : plc->extract_failed = true;
123 0 : return;
124 : }
125 0 : plc->cb (plc->cb_cls,
126 : product_serial,
127 : product_id,
128 : &pd,
129 : num_categories,
130 : categories);
131 0 : GNUNET_PQ_cleanup_result (rs);
132 : }
133 : }
134 :
135 :
136 : enum GNUNET_DB_QueryStatus
137 0 : TMH_PG_lookup_all_products (void *cls,
138 : const char *instance_id,
139 : TALER_MERCHANTDB_ProductCallback cb,
140 : void *cb_cls)
141 : {
142 0 : struct PostgresClosure *pg = cls;
143 0 : struct LookupProductsContext plc = {
144 : .cb = cb,
145 : .cb_cls = cb_cls,
146 : .pg = pg,
147 : /* Can be overwritten by the lookup_products_cb */
148 : .extract_failed = false,
149 : };
150 0 : struct GNUNET_PQ_QueryParam params[] = {
151 0 : GNUNET_PQ_query_param_string (instance_id),
152 : GNUNET_PQ_query_param_end
153 : };
154 : enum GNUNET_DB_QueryStatus qs;
155 :
156 0 : check_connection (pg);
157 0 : PREPARE (pg,
158 : "lookup_all_products",
159 : "SELECT"
160 : " description"
161 : ",description_i18n"
162 : ",product_name"
163 : ",unit"
164 : ",price"
165 : ",taxes"
166 : ",total_stock"
167 : ",total_sold"
168 : ",total_lost"
169 : ",image"
170 : ",minv.address"
171 : ",next_restock"
172 : ",minimum_age"
173 : ",product_id"
174 : ",product_serial"
175 : ",t.category_array AS categories"
176 : " FROM merchant_inventory minv"
177 : " JOIN merchant_instances inst"
178 : " USING (merchant_serial)"
179 : ",LATERAL ("
180 : " SELECT ARRAY ("
181 : " SELECT mpc.category_serial"
182 : " FROM merchant_product_categories mpc"
183 : " WHERE mpc.product_serial = minv.product_serial"
184 : " ) AS category_array"
185 : " ) t"
186 : " WHERE inst.merchant_id=$1");
187 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
188 : pg->conn,
189 : "lookup_all_products",
190 : params,
191 : &lookup_products_cb,
192 : &plc);
193 : /* If there was an error inside lookup_products_cb, return a hard error. */
194 0 : if (plc.extract_failed)
195 0 : return GNUNET_DB_STATUS_HARD_ERROR;
196 0 : return qs;
197 : }
|