pg_lookup_otp_devices.c (3964B)
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 backenddb/pg_lookup_otp_devices.c 18 * @brief Implementation of the lookup_otp_devices 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_otp_devices.h" 26 #include "pg_helper.h" 27 28 29 /** 30 * Context used for TMH_PG_lookup_otp_devices(). 31 */ 32 struct LookupOtpDeviceContext 33 { 34 /** 35 * Function to call with the results. 36 */ 37 TALER_MERCHANTDB_OtpDeviceCallback 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 otp_device. 54 * 55 * @param[in,out] cls of type `struct LookupOtpDeviceContext *` 56 * @param result the postgres result 57 * @param num_results the number of results in @a result 58 */ 59 static void 60 lookup_otp_devices_cb (void *cls, 61 PGresult *result, 62 unsigned int num_results) 63 { 64 struct LookupOtpDeviceContext *tlc = cls; 65 66 for (unsigned int i = 0; i < num_results; i++) 67 { 68 char *otp_device_id; 69 char *otp_device_description; 70 struct GNUNET_PQ_ResultSpec rs[] = { 71 GNUNET_PQ_result_spec_string ("otp_id", 72 &otp_device_id), 73 GNUNET_PQ_result_spec_string ("otp_description", 74 &otp_device_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 otp_device_id, 89 otp_device_description); 90 GNUNET_PQ_cleanup_result (rs); 91 } 92 } 93 94 95 enum GNUNET_DB_QueryStatus 96 TMH_PG_lookup_otp_devices (void *cls, 97 const char *instance_id, 98 TALER_MERCHANTDB_OtpDeviceCallback cb, 99 void *cb_cls) 100 { 101 struct PostgresClosure *pg = cls; 102 struct LookupOtpDeviceContext tlc = { 103 .cb = cb, 104 .cb_cls = cb_cls, 105 /* Can be overwritten by the lookup_otp_device_cb */ 106 .extract_failed = false, 107 }; 108 struct GNUNET_PQ_QueryParam params[] = { 109 GNUNET_PQ_query_param_string (instance_id), 110 GNUNET_PQ_query_param_end 111 }; 112 enum GNUNET_DB_QueryStatus qs; 113 114 check_connection (pg); 115 PREPARE (pg, 116 "lookup_otp_devices", 117 "SELECT" 118 " otp_id" 119 ",otp_description" 120 " FROM merchant_otp_devices" 121 " JOIN merchant_instances" 122 " USING (merchant_serial)" 123 " WHERE merchant_instances.merchant_id=$1"); 124 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 125 "lookup_otp_devices", 126 params, 127 &lookup_otp_devices_cb, 128 &tlc); 129 /* If there was an error inside lookup_otp_device_cb, return a hard error. */ 130 if (tlc.extract_failed) 131 return GNUNET_DB_STATUS_HARD_ERROR; 132 return qs; 133 }