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