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