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