lookup_token_families.c (4911B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023 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_token_families.c 18 * @brief Implementation of the lookup_token_families function for Postgres 19 * @author Christian Blättler 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_token_families.h" 24 #include "helper.h" 25 #include "merchantdb_lib.h" 26 27 28 /** 29 * Context used for TALER_MERCHANTDB_lookup_token_families(). 30 */ 31 struct LookupTokenFamiliesContext 32 { 33 /** 34 * Function to call with the results. 35 */ 36 TALER_MERCHANTDB_TokenFamiliesCallback 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 token families. 53 * 54 * @param[in,out] cls of type `struct LookupTokenFamiliesContext *` 55 * @param result the postgres result 56 * @param num_results the number of results in @a result 57 */ 58 static void 59 lookup_token_families_cb (void *cls, 60 PGresult *result, 61 unsigned int num_results) 62 { 63 struct LookupTokenFamiliesContext *tflc = cls; 64 65 for (unsigned int i = 0; i < num_results; i++) 66 { 67 char *slug; 68 char *name; 69 char *description; 70 json_t *description_i18n = NULL; 71 char *kind; 72 struct GNUNET_TIME_Timestamp valid_after; 73 struct GNUNET_TIME_Timestamp valid_before; 74 struct GNUNET_PQ_ResultSpec rs[] = { 75 GNUNET_PQ_result_spec_string ("slug", 76 &slug), 77 GNUNET_PQ_result_spec_string ("name", 78 &name), 79 GNUNET_PQ_result_spec_string ("description", 80 &description), 81 GNUNET_PQ_result_spec_allow_null ( 82 TALER_PQ_result_spec_json ("description_i18n", 83 &description_i18n), 84 NULL), 85 GNUNET_PQ_result_spec_timestamp ("valid_after", 86 &valid_after), 87 GNUNET_PQ_result_spec_timestamp ("valid_before", 88 &valid_before), 89 GNUNET_PQ_result_spec_string ("kind", 90 &kind), 91 GNUNET_PQ_result_spec_end 92 }; 93 94 if (GNUNET_OK != 95 GNUNET_PQ_extract_result (result, 96 rs, 97 i)) 98 { 99 GNUNET_break (0); 100 tflc->extract_failed = true; 101 return; 102 } 103 104 tflc->cb (tflc->cb_cls, 105 slug, 106 name, 107 description, 108 description_i18n, 109 valid_after, 110 valid_before, 111 kind); 112 GNUNET_PQ_cleanup_result (rs); 113 } 114 } 115 116 117 enum GNUNET_DB_QueryStatus 118 TALER_MERCHANTDB_lookup_token_families ( 119 struct TALER_MERCHANTDB_PostgresContext *pg, 120 const char *instance_id, 121 TALER_MERCHANTDB_TokenFamiliesCallback cb, 122 void *cb_cls) 123 { 124 struct LookupTokenFamiliesContext context = { 125 .cb = cb, 126 .cb_cls = cb_cls, 127 /* Can be overwritten by the lookup_token_families_cb */ 128 .extract_failed = false, 129 }; 130 struct GNUNET_PQ_QueryParam params[] = { 131 GNUNET_PQ_query_param_end 132 }; 133 enum GNUNET_DB_QueryStatus qs; 134 135 GNUNET_assert (NULL != pg->current_merchant_id); 136 GNUNET_assert (0 == strcmp (instance_id, 137 pg->current_merchant_id)); 138 check_connection (pg); 139 TMH_PQ_prepare_anon (pg, 140 "SELECT" 141 " slug" 142 ",name" 143 ",description" 144 ",description_i18n::TEXT" 145 ",valid_after" 146 ",valid_before" 147 ",kind" 148 " FROM merchant_token_families"); 149 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 150 "", 151 params, 152 &lookup_token_families_cb, 153 &context); 154 /* If there was an error inside lookup_token_families_cb, return a hard error. */ 155 if (context.extract_failed) 156 return GNUNET_DB_STATUS_HARD_ERROR; 157 return qs; 158 }