Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 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 src/backenddb/iterate_inventory_products.c
18 : * @brief Implementation of the iterate_inventory_products function for Postgres
19 : * @author Bohdan Potuzhnyi
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/iterate_inventory_products.h"
24 : #include "helper.h"
25 :
26 : /**
27 : * Context used for TALER_MERCHANTDB_iterate_inventory_products().
28 : */
29 : struct LookupInventoryProductsContext
30 : {
31 : /**
32 : * Function to call with the results.
33 : */
34 : TALER_MERCHANTDB_InventoryProductCallback cb;
35 :
36 : /**
37 : * Closure for @a cb.
38 : */
39 : void *cb_cls;
40 :
41 : /**
42 : * Postgres context.
43 : */
44 : struct TALER_MERCHANTDB_PostgresContext *pg;
45 :
46 : /**
47 : * Did database result extraction fail?
48 : */
49 : bool extract_failed;
50 : };
51 :
52 :
53 : /**
54 : * Function to be called with the results of a SELECT statement
55 : * that has returned @a num_results results about products.
56 : *
57 : * @param[in,out] cls of type `struct LookupInventoryProductsContext *`
58 : * @param result the postgres result
59 : * @param num_results the number of results in @a result
60 : */
61 : static void
62 0 : lookup_inventory_products_cb (void *cls,
63 : PGresult *result,
64 : unsigned int num_results)
65 : {
66 0 : struct LookupInventoryProductsContext *plc = cls;
67 0 : struct TALER_MERCHANTDB_PostgresContext *pg = plc->pg;
68 :
69 0 : for (unsigned int i = 0; i < num_results; i++)
70 : {
71 : char *product_id;
72 : struct TALER_MERCHANTDB_InventoryProductDetails pd;
73 : size_t num_categories;
74 : uint64_t *categories;
75 : bool no_image_hash;
76 0 : struct GNUNET_PQ_ResultSpec rs[] = {
77 0 : GNUNET_PQ_result_spec_string ("product_id",
78 : &product_id),
79 0 : GNUNET_PQ_result_spec_string ("product_name",
80 : &pd.product_name),
81 0 : GNUNET_PQ_result_spec_string ("description",
82 : &pd.description),
83 0 : TALER_PQ_result_spec_json ("description_i18n",
84 : &pd.description_i18n),
85 0 : GNUNET_PQ_result_spec_string ("unit",
86 : &pd.unit),
87 0 : TALER_PQ_result_spec_array_amount_with_currency (pg->conn,
88 : "merchant",
89 : "price_array",
90 : &pd.price_array_length,
91 : &pd.price_array),
92 0 : GNUNET_PQ_result_spec_uint64 ("remaining_stock",
93 : &pd.remaining_stock),
94 0 : GNUNET_PQ_result_spec_uint32 ("remaining_stock_frac",
95 : &pd.remaining_stock_frac),
96 0 : TALER_PQ_result_spec_json ("taxes",
97 : &pd.taxes),
98 0 : GNUNET_PQ_result_spec_allow_null (
99 : GNUNET_PQ_result_spec_string ("image_hash",
100 : &pd.image_hash),
101 : &no_image_hash),
102 0 : GNUNET_PQ_result_spec_bool ("allow_fractional_quantity",
103 : &pd.allow_fractional_quantity),
104 0 : GNUNET_PQ_result_spec_uint32 ("fractional_precision_level",
105 : &pd.fractional_precision_level),
106 0 : GNUNET_PQ_result_spec_array_uint64 (pg->conn,
107 : "categories",
108 : &num_categories,
109 : &categories),
110 : GNUNET_PQ_result_spec_end
111 : };
112 :
113 0 : if (GNUNET_OK !=
114 0 : GNUNET_PQ_extract_result (result,
115 : rs,
116 : i))
117 : {
118 0 : GNUNET_break (0);
119 0 : plc->extract_failed = true;
120 0 : return;
121 : }
122 0 : plc->cb (plc->cb_cls,
123 : product_id,
124 : &pd,
125 : num_categories,
126 : categories);
127 0 : GNUNET_PQ_cleanup_result (rs);
128 : }
129 : }
130 :
131 :
132 : enum GNUNET_DB_QueryStatus
133 0 : TALER_MERCHANTDB_iterate_inventory_products (
134 : struct TALER_MERCHANTDB_PostgresContext *pg,
135 : const char *instance_id,
136 : TALER_MERCHANTDB_InventoryProductCallback cb,
137 : void *cb_cls)
138 : {
139 0 : struct LookupInventoryProductsContext plc = {
140 : .cb = cb,
141 : .cb_cls = cb_cls,
142 : .pg = pg,
143 : /* Can be overwritten by the lookup_inventory_products_cb */
144 : .extract_failed = false,
145 : };
146 0 : struct GNUNET_PQ_QueryParam params[] = {
147 : GNUNET_PQ_query_param_end
148 : };
149 : enum GNUNET_DB_QueryStatus qs;
150 :
151 0 : GNUNET_assert (NULL != pg->current_merchant_id);
152 0 : GNUNET_assert (0 == strcmp (instance_id,
153 : pg->current_merchant_id));
154 0 : TMH_PQ_prepare_anon (pg,
155 : "SELECT"
156 : " description"
157 : ",description_i18n::TEXT"
158 : ",product_name"
159 : ",unit"
160 : ",price_array"
161 : ",CASE WHEN minv.total_stock = 9223372036854775807"
162 : " THEN minv.total_stock"
163 : " ELSE (GREATEST(0, rt.remaining_total) / 1000000)::INT8"
164 : " END AS remaining_stock"
165 : ",CASE WHEN minv.total_stock = 9223372036854775807"
166 : " THEN 2147483647"
167 : " ELSE mod(GREATEST(0, rt.remaining_total), 1000000)::INT4"
168 : " END AS remaining_stock_frac"
169 : ",taxes::TEXT"
170 : ",image_hash"
171 : ",allow_fractional_quantity"
172 : ",fractional_precision_level"
173 : ",product_id"
174 : ",t.category_array AS categories"
175 : " FROM merchant_inventory minv"
176 : ",LATERAL ("
177 : " SELECT ARRAY ("
178 : " SELECT mpc.category_serial"
179 : " FROM merchant_product_categories mpc"
180 : " WHERE mpc.product_serial = minv.product_serial"
181 : " ) AS category_array"
182 : " ) t"
183 : ",LATERAL ("
184 : " SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
185 : " + total_locked_frac::NUMERIC), 0)"
186 : " AS total_locked"
187 : " FROM merchant_inventory_locks mil"
188 : " WHERE mil.product_serial = minv.product_serial"
189 : " ) il"
190 : ",LATERAL ("
191 : " SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
192 : " + total_locked_frac::NUMERIC), 0)"
193 : " AS total_locked"
194 : " FROM merchant_order_locks mol"
195 : " WHERE mol.product_serial = minv.product_serial"
196 : " ) ol"
197 : ",LATERAL ("
198 : " SELECT"
199 : " (minv.total_stock::NUMERIC * 1000000"
200 : " + minv.total_stock_frac::NUMERIC)"
201 : " - (minv.total_sold::NUMERIC * 1000000"
202 : " + minv.total_sold_frac::NUMERIC)"
203 : " - (minv.total_lost::NUMERIC * 1000000"
204 : " + minv.total_lost_frac::NUMERIC)"
205 : " - il.total_locked"
206 : " - ol.total_locked"
207 : " AS remaining_total"
208 : " ) rt");
209 0 : qs = GNUNET_PQ_eval_prepared_multi_select (
210 : pg->conn,
211 : "",
212 : params,
213 : &lookup_inventory_products_cb,
214 : &plc);
215 : /* If there was an error inside lookup_inventory_products_cb, return a hard error. */
216 0 : if (plc.extract_failed)
217 0 : return GNUNET_DB_STATUS_HARD_ERROR;
218 0 : return qs;
219 : }
|