lookup_products.c (6706B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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_products.c 18 * @brief Implementation of the lookup_products function for Postgres 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_products.h" 24 #include "helper.h" 25 26 /** 27 * Context used for TALER_MERCHANTDB_lookup_products(). 28 */ 29 struct LookupProductsContext 30 { 31 /** 32 * Function to call with the results. 33 */ 34 TALER_MERCHANTDB_ProductsCallback cb; 35 36 /** 37 * Closure for @a cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Did database result extraction fail? 43 */ 44 bool extract_failed; 45 }; 46 47 48 /** 49 * Function to be called with the results of a SELECT statement 50 * that has returned @a num_results results about products. 51 * 52 * @param[in,out] cls of type `struct LookupProductsContext *` 53 * @param result the postgres result 54 * @param num_results the number of results in @a result 55 */ 56 static void 57 lookup_products_cb (void *cls, 58 PGresult *result, 59 unsigned int num_results) 60 { 61 struct LookupProductsContext *plc = cls; 62 63 for (unsigned int i = 0; i < num_results; i++) 64 { 65 char *product_id; 66 uint64_t product_serial; 67 struct GNUNET_PQ_ResultSpec rs[] = { 68 GNUNET_PQ_result_spec_string ("product_id", 69 &product_id), 70 GNUNET_PQ_result_spec_uint64 ("product_serial", 71 &product_serial), 72 GNUNET_PQ_result_spec_end 73 }; 74 75 if (GNUNET_OK != 76 GNUNET_PQ_extract_result (result, 77 rs, 78 i)) 79 { 80 GNUNET_break (0); 81 plc->extract_failed = true; 82 return; 83 } 84 plc->cb (plc->cb_cls, 85 product_serial, 86 product_id); 87 GNUNET_PQ_cleanup_result (rs); 88 } 89 } 90 91 92 enum GNUNET_DB_QueryStatus 93 TALER_MERCHANTDB_lookup_products ( 94 struct TALER_MERCHANTDB_PostgresContext *pg, 95 const char *instance_id, 96 uint64_t offset, 97 int64_t limit, 98 const char *category_filter, 99 const char *name_filter, 100 const char *description_filter, 101 uint64_t product_group_id_filter, 102 TALER_MERCHANTDB_ProductsCallback cb, 103 void *cb_cls) 104 { 105 uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit); 106 struct LookupProductsContext plc = { 107 .cb = cb, 108 .cb_cls = cb_cls, 109 /* Can be overwritten by the lookup_products_cb */ 110 .extract_failed = false, 111 }; 112 struct GNUNET_PQ_QueryParam params[] = { 113 GNUNET_PQ_query_param_uint64 (&offset), 114 NULL == category_filter 115 ? GNUNET_PQ_query_param_null () 116 : GNUNET_PQ_query_param_string (category_filter), 117 NULL == name_filter 118 ? GNUNET_PQ_query_param_null () 119 : GNUNET_PQ_query_param_string (name_filter), 120 NULL == description_filter 121 ? GNUNET_PQ_query_param_null () 122 : GNUNET_PQ_query_param_string (description_filter), 123 GNUNET_PQ_query_param_uint64 (&plimit), 124 GNUNET_PQ_query_param_uint64 (&product_group_id_filter), 125 GNUNET_PQ_query_param_end 126 }; 127 enum GNUNET_DB_QueryStatus qs; 128 129 GNUNET_assert (NULL != pg->current_merchant_id); 130 GNUNET_assert (0 == strcmp (instance_id, 131 pg->current_merchant_id)); 132 if (limit > 0) 133 { 134 TMH_PQ_prepare_anon (pg, 135 "SELECT" 136 " product_id" 137 " ,product_serial" 138 " FROM merchant_inventory" 139 " WHERE product_serial > $1" 140 " AND ( ($2::TEXT IS NULL) OR" 141 " (product_serial IN" 142 " (SELECT product_serial" 143 " FROM merchant_product_categories" 144 " WHERE category_serial IN" 145 " (SELECT category_serial" 146 " FROM merchant_categories" 147 " WHERE LOWER(category_name) LIKE LOWER($2)))) )" 148 " AND ( (0 = $6::INT8) OR" 149 " (product_group_serial = $6) )" 150 " AND ( ($3::TEXT IS NULL) OR" 151 " (product_name LIKE LOWER($3)) )" 152 " AND ( ($4::TEXT IS NULL) OR" 153 " (description LIKE LOWER($4)) )" 154 " ORDER BY product_serial ASC" 155 " LIMIT $5"); 156 } 157 else 158 { 159 TMH_PQ_prepare_anon (pg, 160 "SELECT" 161 " product_id" 162 " ,product_serial" 163 " FROM merchant_inventory" 164 " WHERE product_serial < $1" 165 " AND ( ($2::TEXT IS NULL) OR" 166 " (product_serial IN" 167 " (SELECT product_serial" 168 " FROM merchant_product_categories" 169 " WHERE category_serial IN" 170 " (SELECT category_serial" 171 " FROM merchant_categories" 172 " WHERE LOWER(category_name) LIKE LOWER($2)))) )" 173 " AND ( (0 = $6::INT8) OR" 174 " (product_group_serial = $6) )" 175 " AND ( ($3::TEXT IS NULL) OR" 176 " (LOWER(product_name) LIKE LOWER($3)) )" 177 " AND ( ($4::TEXT IS NULL) OR" 178 " (description LIKE LOWER($4)) )" 179 " ORDER BY product_serial DESC" 180 " LIMIT $5"); 181 } 182 qs = GNUNET_PQ_eval_prepared_multi_select ( 183 pg->conn, 184 "", 185 params, 186 &lookup_products_cb, 187 &plc); 188 /* If there was an error inside lookup_products_cb, return a hard error. */ 189 if (plc.extract_failed) 190 { 191 GNUNET_break (0); 192 return GNUNET_DB_STATUS_HARD_ERROR; 193 } 194 return qs; 195 }