iterate_product_groups.c (5112B)
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_product_groups.c 18 * @brief Implementation of the iterate_product_groups function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/iterate_product_groups.h" 24 #include "helper.h" 25 26 27 /** 28 * Hard upper bound on the number of records returned by a single 29 * call, regardless of the limit requested by the client. 30 */ 31 #define MAX_RECORDS 50000 32 33 34 /** 35 * Context used for TALER_MERCHANTDB_iterate_product_groups(). 36 */ 37 struct LookupProductGroupsContext 38 { 39 /** 40 * Function to call with the results. 41 */ 42 TALER_MERCHANTDB_ProductGroupsCallback cb; 43 44 /** 45 * Closure for @a cb. 46 */ 47 void *cb_cls; 48 49 /** 50 * Did database result extraction fail? 51 */ 52 bool extract_failed; 53 }; 54 55 56 /** 57 * Function to be called with the results of a SELECT statement 58 * that has returned @a num_results results about product groups. 59 * 60 * @param[in,out] cls of type `struct LookupProductGroupsContext *` 61 * @param result the postgres result 62 * @param num_results the number of results in @a result 63 */ 64 static void 65 lookup_product_groups_cb (void *cls, 66 PGresult *result, 67 unsigned int num_results) 68 { 69 struct LookupProductGroupsContext *plc = cls; 70 71 for (unsigned int i = 0; i < num_results; i++) 72 { 73 uint64_t product_group_serial; 74 char *product_group_name; 75 char *product_group_description; 76 struct GNUNET_PQ_ResultSpec rs[] = { 77 GNUNET_PQ_result_spec_uint64 ("product_group_serial", 78 &product_group_serial), 79 GNUNET_PQ_result_spec_string ("product_group_name", 80 &product_group_name), 81 GNUNET_PQ_result_spec_string ("product_group_description", 82 &product_group_description), 83 GNUNET_PQ_result_spec_end 84 }; 85 86 if (GNUNET_OK != 87 GNUNET_PQ_extract_result (result, 88 rs, 89 i)) 90 { 91 GNUNET_break (0); 92 plc->extract_failed = true; 93 return; 94 } 95 plc->cb (plc->cb_cls, 96 product_group_serial, 97 product_group_name, 98 product_group_description); 99 GNUNET_PQ_cleanup_result (rs); 100 } 101 } 102 103 104 enum GNUNET_DB_QueryStatus 105 TALER_MERCHANTDB_iterate_product_groups ( 106 struct TALER_MERCHANTDB_PostgresContext *pg, 107 const char *instance_id, 108 int64_t limit, 109 uint64_t offset, 110 TALER_MERCHANTDB_ProductGroupsCallback cb, 111 void *cb_cls) 112 { 113 uint64_t plimit = GNUNET_MIN ((uint64_t) MAX_RECORDS, 114 TALER_MERCHANTDB_abs_limit (limit)); 115 struct LookupProductGroupsContext plc = { 116 .cb = cb, 117 .cb_cls = cb_cls, 118 /* Can be overwritten by the lookup_product_groups_cb */ 119 .extract_failed = false, 120 }; 121 struct GNUNET_PQ_QueryParam params[] = { 122 GNUNET_PQ_query_param_uint64 (&offset), 123 GNUNET_PQ_query_param_uint64 (&plimit), 124 GNUNET_PQ_query_param_end 125 }; 126 enum GNUNET_DB_QueryStatus qs; 127 128 GNUNET_assert (NULL != pg->current_merchant_id); 129 GNUNET_assert (0 == strcmp (instance_id, 130 pg->current_merchant_id)); 131 if (limit > 0) 132 { 133 TMH_PQ_prepare_anon (pg, 134 "SELECT" 135 " product_group_serial" 136 " ,product_group_name" 137 " ,product_group_description" 138 " FROM merchant_product_groups" 139 " WHERE product_group_serial > $1" 140 " ORDER BY product_group_serial ASC" 141 " LIMIT $2"); 142 } 143 else 144 { 145 TMH_PQ_prepare_anon (pg, 146 "SELECT" 147 " product_group_serial" 148 " ,product_group_name" 149 " ,product_group_description" 150 " FROM merchant_product_groups" 151 " WHERE product_group_serial < $1" 152 " ORDER BY product_group_serial DESC" 153 " LIMIT $2"); 154 } 155 qs = GNUNET_PQ_eval_prepared_multi_select ( 156 pg->conn, 157 "", 158 params, 159 &lookup_product_groups_cb, 160 &plc); 161 /* If there was an error inside lookup_product_groups_cb, return a hard error. */ 162 if (plc.extract_failed) 163 { 164 GNUNET_break (0); 165 return GNUNET_DB_STATUS_HARD_ERROR; 166 } 167 return qs; 168 }