lookup_inventory_products.c (8488B)
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/lookup_inventory_products.c 18 * @brief Implementation of the lookup_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/lookup_inventory_products.h" 24 #include "helper.h" 25 26 /** 27 * Context used for TALER_MERCHANTDB_lookup_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 lookup_inventory_products_cb (void *cls, 63 PGresult *result, 64 unsigned int num_results) 65 { 66 struct LookupInventoryProductsContext *plc = cls; 67 struct TALER_MERCHANTDB_PostgresContext *pg = plc->pg; 68 69 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 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_string ("product_id", 78 &product_id), 79 GNUNET_PQ_result_spec_string ("product_name", 80 &pd.product_name), 81 GNUNET_PQ_result_spec_string ("description", 82 &pd.description), 83 TALER_PQ_result_spec_json ("description_i18n", 84 &pd.description_i18n), 85 GNUNET_PQ_result_spec_string ("unit", 86 &pd.unit), 87 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 GNUNET_PQ_result_spec_uint64 ("remaining_stock", 93 &pd.remaining_stock), 94 GNUNET_PQ_result_spec_uint32 ("remaining_stock_frac", 95 &pd.remaining_stock_frac), 96 TALER_PQ_result_spec_json ("taxes", 97 &pd.taxes), 98 GNUNET_PQ_result_spec_allow_null ( 99 GNUNET_PQ_result_spec_string ("image_hash", 100 &pd.image_hash), 101 &no_image_hash), 102 GNUNET_PQ_result_spec_bool ("allow_fractional_quantity", 103 &pd.allow_fractional_quantity), 104 GNUNET_PQ_result_spec_uint32 ("fractional_precision_level", 105 &pd.fractional_precision_level), 106 GNUNET_PQ_result_spec_array_uint64 (pg->conn, 107 "categories", 108 &num_categories, 109 &categories), 110 GNUNET_PQ_result_spec_end 111 }; 112 113 if (GNUNET_OK != 114 GNUNET_PQ_extract_result (result, 115 rs, 116 i)) 117 { 118 GNUNET_break (0); 119 plc->extract_failed = true; 120 return; 121 } 122 plc->cb (plc->cb_cls, 123 product_id, 124 &pd, 125 num_categories, 126 categories); 127 GNUNET_PQ_cleanup_result (rs); 128 } 129 } 130 131 132 enum GNUNET_DB_QueryStatus 133 TALER_MERCHANTDB_lookup_inventory_products ( 134 struct TALER_MERCHANTDB_PostgresContext *pg, 135 const char *instance_id, 136 TALER_MERCHANTDB_InventoryProductCallback cb, 137 void *cb_cls) 138 { 139 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 struct GNUNET_PQ_QueryParam params[] = { 147 GNUNET_PQ_query_param_end 148 }; 149 enum GNUNET_DB_QueryStatus qs; 150 151 GNUNET_assert (NULL != pg->current_merchant_id); 152 GNUNET_assert (0 == strcmp (instance_id, 153 pg->current_merchant_id)); 154 check_connection (pg); 155 TMH_PQ_prepare_anon (pg, 156 "SELECT" 157 " description" 158 ",description_i18n::TEXT" 159 ",product_name" 160 ",unit" 161 ",price_array" 162 ",CASE WHEN minv.total_stock = 9223372036854775807" 163 " THEN minv.total_stock" 164 " ELSE (GREATEST(0, rt.remaining_total) / 1000000)::INT8" 165 " END AS remaining_stock" 166 ",CASE WHEN minv.total_stock = 9223372036854775807" 167 " THEN 2147483647" 168 " ELSE mod(GREATEST(0, rt.remaining_total), 1000000)::INT4" 169 " END AS remaining_stock_frac" 170 ",taxes::TEXT" 171 ",image_hash" 172 ",allow_fractional_quantity" 173 ",fractional_precision_level" 174 ",product_id" 175 ",t.category_array AS categories" 176 " FROM merchant_inventory minv" 177 ",LATERAL (" 178 " SELECT ARRAY (" 179 " SELECT mpc.category_serial" 180 " FROM merchant_product_categories mpc" 181 " WHERE mpc.product_serial = minv.product_serial" 182 " ) AS category_array" 183 " ) t" 184 ",LATERAL (" 185 " SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000" 186 " + total_locked_frac::NUMERIC), 0)" 187 " AS total_locked" 188 " FROM merchant_inventory_locks mil" 189 " WHERE mil.product_serial = minv.product_serial" 190 " ) il" 191 ",LATERAL (" 192 " SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000" 193 " + total_locked_frac::NUMERIC), 0)" 194 " AS total_locked" 195 " FROM merchant_order_locks mol" 196 " WHERE mol.product_serial = minv.product_serial" 197 " ) ol" 198 ",LATERAL (" 199 " SELECT" 200 " (minv.total_stock::NUMERIC * 1000000" 201 " + minv.total_stock_frac::NUMERIC)" 202 " - (minv.total_sold::NUMERIC * 1000000" 203 " + minv.total_sold_frac::NUMERIC)" 204 " - (minv.total_lost::NUMERIC * 1000000" 205 " + minv.total_lost_frac::NUMERIC)" 206 " - il.total_locked" 207 " - ol.total_locked" 208 " AS remaining_total" 209 " ) rt"); 210 qs = GNUNET_PQ_eval_prepared_multi_select ( 211 pg->conn, 212 "", 213 params, 214 &lookup_inventory_products_cb, 215 &plc); 216 /* If there was an error inside lookup_inventory_products_cb, return a hard error. */ 217 if (plc.extract_failed) 218 return GNUNET_DB_STATUS_HARD_ERROR; 219 return qs; 220 }