lookup_templates.c (3993B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2022 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_templates.c 18 * @brief Implementation of the lookup_templates 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_templates.h" 26 #include "helper.h" 27 28 29 /** 30 * Context used for TALER_MERCHANTDB_lookup_templates(). 31 */ 32 struct LookupTemplateContext 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_TemplatesCallback 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 template. 54 * 55 * @param[in,out] cls of type `struct LookupTemplateContext *` 56 * @param result the postgres result 57 * @param num_results the number of results in @a result 58 */ 59 static void 60 lookup_templates_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct LookupTemplateContext *tlc = cls; 65 66 for (unsigned int i = 0; i < num_results; i++) 67 { 68 char *template_id; 69 char *template_description; 70 struct GNUNET_PQ_ResultSpec rs[] = { 71 GNUNET_PQ_result_spec_string ("template_id", 72 &template_id), 73 GNUNET_PQ_result_spec_string ("template_description", 74 &template_description), 75 GNUNET_PQ_result_spec_end 76 }; 77 78 if (GNUNET_OK != 79 GNUNET_PQ_extract_result (result, 80 rs, 81 i)) 82 { 83 GNUNET_break (0); 84 tlc->extract_failed = true; 85 return; 86 } 87 tlc->cb (tlc->cb_cls, 88 template_id, 89 template_description); 90 GNUNET_PQ_cleanup_result (rs); 91 } 92 } 93 94 95 enum GNUNET_DB_QueryStatus 96 TALER_MERCHANTDB_lookup_templates (struct TALER_MERCHANTDB_PostgresContext *pg, 97 const char *instance_id, 98 TALER_MERCHANTDB_TemplatesCallback cb, 99 void *cb_cls) 100 { 101 struct LookupTemplateContext tlc = { 102 .cb = cb, 103 .cb_cls = cb_cls, 104 /* Can be overwritten by the lookup_template_cb */ 105 .extract_failed = false, 106 }; 107 struct GNUNET_PQ_QueryParam params[] = { 108 GNUNET_PQ_query_param_string (instance_id), 109 GNUNET_PQ_query_param_end 110 }; 111 enum GNUNET_DB_QueryStatus qs; 112 113 check_connection (pg); 114 PREPARE (pg, 115 "lookup_templates", 116 "SELECT" 117 " template_id" 118 ",template_description" 119 " FROM merchant_template" 120 " JOIN merchant_instances" 121 " USING (merchant_serial)" 122 " WHERE merchant_instances.merchant_id=$1"); 123 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 124 "lookup_templates", 125 params, 126 &lookup_templates_cb, 127 &tlc); 128 /* If there was an error inside lookup_templates_cb, return a hard error. */ 129 if (tlc.extract_failed) 130 return GNUNET_DB_STATUS_HARD_ERROR; 131 return qs; 132 }