lookup_categories_by_ids.c (4695B)
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_categories_by_ids.c 18 * @brief Lookup product categories by ID 19 * @author Bohdan Potuzhnyi 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 "merchant-database/lookup_categories_by_ids.h" 26 #include "helper.h" 27 28 29 /** 30 * Context used for TALER_MERCHANTDB_lookup_categories_by_ids(). 31 */ 32 struct LookupCategoryContext 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_CategoriesCallback cb; 38 39 /** 40 * Closure for @a cb. 41 */ 42 void *cb_cls; 43 44 /** 45 * Did database result extraction fail? 46 */ 47 bool extract_failed; 48 }; 49 50 51 static void 52 lookup_categories_cb (void *cls, 53 PGresult *result, 54 unsigned int num_results) 55 { 56 struct LookupCategoryContext *tlc = cls; 57 58 for (unsigned int i = 0; i < num_results; i++) 59 { 60 uint64_t category_id; 61 char *category_name; 62 json_t *category_name_i18n; 63 uint64_t product_count; 64 struct GNUNET_PQ_ResultSpec rs[] = { 65 GNUNET_PQ_result_spec_uint64 ("category_serial", 66 &category_id), 67 GNUNET_PQ_result_spec_string ("category_name", 68 &category_name), 69 TALER_PQ_result_spec_json ("category_name_i18n", 70 &category_name_i18n), 71 GNUNET_PQ_result_spec_uint64 ("product_count", 72 &product_count), 73 GNUNET_PQ_result_spec_end 74 }; 75 76 if (GNUNET_OK != 77 GNUNET_PQ_extract_result (result, 78 rs, 79 i)) 80 { 81 GNUNET_break (0); 82 tlc->extract_failed = true; 83 return; 84 } 85 tlc->cb (tlc->cb_cls, 86 category_id, 87 category_name, 88 category_name_i18n, 89 product_count); 90 GNUNET_PQ_cleanup_result (rs); 91 } 92 } 93 94 95 enum GNUNET_DB_QueryStatus 96 TALER_MERCHANTDB_lookup_categories_by_ids (struct TALER_MERCHANTDB_PostgresContext *pg, 97 const char *instance_id, 98 const uint64_t *category_ids, 99 size_t num_category_ids, 100 TALER_MERCHANTDB_CategoriesCallback cb, 101 void *cb_cls) 102 { 103 struct LookupCategoryContext tlc = { 104 .cb = cb, 105 .cb_cls = cb_cls, 106 /* Can be overwritten by the lookup_categories_cb */ 107 .extract_failed = false, 108 }; 109 struct GNUNET_PQ_QueryParam params[] = { 110 GNUNET_PQ_query_param_string (instance_id), 111 GNUNET_PQ_query_param_array_uint64 (num_category_ids, 112 category_ids, 113 pg->conn), 114 GNUNET_PQ_query_param_end 115 }; 116 enum GNUNET_DB_QueryStatus qs; 117 118 check_connection (pg); 119 PREPARE (pg, 120 "lookup_categories_by_ids", 121 "SELECT" 122 " mc.category_serial" 123 ",mc.category_name" 124 ",mc.category_name_i18n::TEXT" 125 ",COALESCE(COUNT(mpc.product_serial),0)" 126 " AS product_count" 127 " FROM merchant_categories mc" 128 " LEFT JOIN merchant_product_categories mpc" 129 " USING (category_serial)" 130 " JOIN merchant_instances mi" 131 " USING (merchant_serial)" 132 " WHERE mi.merchant_id=$1" 133 " AND mc.category_serial = ANY ($2)" 134 " GROUP BY mc.category_serial" 135 " ORDER BY mc.category_serial;"); 136 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 137 "lookup_categories_by_ids", 138 params, 139 &lookup_categories_cb, 140 &tlc); 141 GNUNET_PQ_cleanup_query_params_closures (params); 142 if (tlc.extract_failed) 143 return GNUNET_DB_STATUS_HARD_ERROR; 144 return qs; 145 }