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