lookup_webhooks.c (3722B)
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_webhooks.c 18 * @brief Implementation of the lookup_webhooks function for Postgres 19 * @author Iván Ávalos 20 */ 21 #include "platform.h" 22 #include <taler/taler_pq_lib.h> 23 #include "merchant-database/lookup_webhooks.h" 24 #include "helper.h" 25 26 /** 27 * Context used for postgres_lookup_webhooks(). 28 */ 29 struct LookupWebhookContext 30 { 31 /** 32 * Function to call with the results. 33 */ 34 TALER_MERCHANTDB_WebhooksCallback cb; 35 36 /** 37 * Closure for @a cb. 38 */ 39 void *cb_cls; 40 41 /** 42 * Did database result extraction fail? 43 */ 44 bool extract_failed; 45 }; 46 47 48 /** 49 * Function to be called with the results of a SELECT statement 50 * that has returned @a num_results results about webhook. 51 * 52 * @param[in,out] cls of type `struct LookupWebhookContext *` 53 * @param result the postgres result 54 * @param num_results the number of results in @a result 55 */ 56 static void 57 lookup_webhooks_cb (void *cls, 58 PGresult *result, 59 unsigned int num_results) 60 { 61 struct LookupWebhookContext *wlc = cls; 62 63 for (unsigned int i = 0; i < num_results; i++) 64 { 65 char *webhook_id; 66 char *event_type; 67 struct GNUNET_PQ_ResultSpec rs[] = { 68 GNUNET_PQ_result_spec_string ("webhook_id", 69 &webhook_id), 70 GNUNET_PQ_result_spec_string ("event_type", 71 &event_type), 72 GNUNET_PQ_result_spec_end 73 }; 74 75 if (GNUNET_OK != 76 GNUNET_PQ_extract_result (result, 77 rs, 78 i)) 79 { 80 GNUNET_break (0); 81 wlc->extract_failed = true; 82 return; 83 } 84 wlc->cb (wlc->cb_cls, 85 webhook_id, 86 event_type); 87 GNUNET_PQ_cleanup_result (rs); 88 } 89 } 90 91 92 enum GNUNET_DB_QueryStatus 93 TALER_MERCHANTDB_lookup_webhooks ( 94 struct TALER_MERCHANTDB_PostgresContext *pg, 95 const char *instance_id, 96 TALER_MERCHANTDB_WebhooksCallback cb, 97 void *cb_cls) 98 { 99 struct LookupWebhookContext wlc = { 100 .cb = cb, 101 .cb_cls = cb_cls, 102 /* Can be overwritten by the lookup_webhook_cb */ 103 .extract_failed = false, 104 }; 105 struct GNUNET_PQ_QueryParam params[] = { 106 GNUNET_PQ_query_param_end 107 }; 108 enum GNUNET_DB_QueryStatus qs; 109 110 GNUNET_assert (NULL != pg->current_merchant_id); 111 GNUNET_assert (0 == strcmp (instance_id, 112 pg->current_merchant_id)); 113 check_connection (pg); 114 TMH_PQ_prepare_anon (pg, 115 "SELECT" 116 " webhook_id" 117 ",event_type" 118 " FROM merchant_webhook"); 119 120 qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn, 121 "", 122 params, 123 &lookup_webhooks_cb, 124 &wlc); 125 /* If there was an error inside lookup_webhook_cb, return a hard error. */ 126 if (wlc.extract_failed) 127 return GNUNET_DB_STATUS_HARD_ERROR; 128 return qs; 129 }