lookup_token_family.c (5170B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023, 2024, 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_token_family.c 18 * @brief Implementation of the lookup_token_family function for Postgres 19 * @author Christian Grothoff 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_token_family.h" 26 #include "helper.h" 27 28 29 enum GNUNET_DB_QueryStatus 30 TALER_MERCHANTDB_lookup_token_family (struct TALER_MERCHANTDB_PostgresContext *pg, 31 const char *instance_id, 32 const char *token_family_slug, 33 struct TALER_MERCHANTDB_TokenFamilyDetails *details) 34 { 35 struct GNUNET_PQ_QueryParam params[] = { 36 GNUNET_PQ_query_param_string (instance_id), 37 GNUNET_PQ_query_param_string (token_family_slug), 38 GNUNET_PQ_query_param_end 39 }; 40 41 if (NULL == details) 42 { 43 struct GNUNET_PQ_ResultSpec rs_null[] = { 44 GNUNET_PQ_result_spec_end 45 }; 46 47 check_connection (pg); 48 return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 49 "lookup_token_family", 50 params, 51 rs_null); 52 } 53 else 54 { 55 char *kind; 56 struct GNUNET_PQ_ResultSpec rs[] = { 57 GNUNET_PQ_result_spec_string ("slug", 58 &details->slug), 59 GNUNET_PQ_result_spec_string ("name", 60 &details->name), 61 GNUNET_PQ_result_spec_string ("cipher_choice", 62 &details->cipher_spec), 63 GNUNET_PQ_result_spec_string ("description", 64 &details->description), 65 TALER_PQ_result_spec_json ("description_i18n", 66 &details->description_i18n), 67 GNUNET_PQ_result_spec_allow_null ( 68 TALER_PQ_result_spec_json ("extra_data", 69 &details->extra_data), 70 NULL), 71 GNUNET_PQ_result_spec_timestamp ("valid_after", 72 &details->valid_after), 73 GNUNET_PQ_result_spec_timestamp ("valid_before", 74 &details->valid_before), 75 GNUNET_PQ_result_spec_relative_time ("duration", 76 &details->duration), 77 GNUNET_PQ_result_spec_relative_time ("validity_granularity", 78 &details->validity_granularity), 79 GNUNET_PQ_result_spec_relative_time ("start_offset", 80 &details->start_offset), 81 GNUNET_PQ_result_spec_string ("kind", 82 &kind), 83 GNUNET_PQ_result_spec_uint64 ("issued", 84 &details->issued), 85 GNUNET_PQ_result_spec_uint64 ("used", 86 &details->used), 87 GNUNET_PQ_result_spec_end 88 }; 89 enum GNUNET_DB_QueryStatus qs; 90 91 check_connection (pg); 92 PREPARE (pg, 93 "lookup_token_family", 94 "SELECT" 95 " slug" 96 ",name" 97 ",cipher_choice" 98 ",description" 99 ",description_i18n::TEXT" 100 ",extra_data::TEXT" 101 ",valid_after" 102 ",valid_before" 103 ",duration" 104 ",validity_granularity" 105 ",start_offset" 106 ",kind" 107 ",issued" 108 ",used" 109 " FROM merchant_token_families" 110 " JOIN merchant_instances" 111 " USING (merchant_serial)" 112 " WHERE merchant_instances.merchant_id=$1" 113 " AND merchant_token_families.slug=$2"); 114 memset (details, 115 0, 116 sizeof (*details)); 117 qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn, 118 "lookup_token_family", 119 params, 120 rs); 121 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs) 122 { 123 if (0 == strcmp (kind, "discount")) 124 details->kind = TALER_MERCHANTDB_TFK_Discount; 125 else if (0 == strcmp (kind, "subscription")) 126 details->kind = TALER_MERCHANTDB_TFK_Subscription; 127 else 128 { 129 GNUNET_break (0); 130 return GNUNET_DB_STATUS_HARD_ERROR; 131 } 132 } 133 return qs; 134 } 135 }