Line data Source code
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/iterate_webhooks.c
18 : * @brief Implementation of the iterate_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/iterate_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 6 : lookup_webhooks_cb (void *cls,
58 : PGresult *result,
59 : unsigned int num_results)
60 : {
61 6 : struct LookupWebhookContext *wlc = cls;
62 :
63 12 : for (unsigned int i = 0; i < num_results; i++)
64 : {
65 : char *webhook_id;
66 : char *event_type;
67 6 : struct GNUNET_PQ_ResultSpec rs[] = {
68 6 : GNUNET_PQ_result_spec_string ("webhook_id",
69 : &webhook_id),
70 6 : GNUNET_PQ_result_spec_string ("event_type",
71 : &event_type),
72 : GNUNET_PQ_result_spec_end
73 : };
74 :
75 6 : if (GNUNET_OK !=
76 6 : GNUNET_PQ_extract_result (result,
77 : rs,
78 : i))
79 : {
80 0 : GNUNET_break (0);
81 0 : wlc->extract_failed = true;
82 0 : return;
83 : }
84 6 : wlc->cb (wlc->cb_cls,
85 : webhook_id,
86 : event_type);
87 6 : GNUNET_PQ_cleanup_result (rs);
88 : }
89 : }
90 :
91 :
92 : enum GNUNET_DB_QueryStatus
93 6 : TALER_MERCHANTDB_iterate_webhooks (
94 : struct TALER_MERCHANTDB_PostgresContext *pg,
95 : const char *instance_id,
96 : TALER_MERCHANTDB_WebhooksCallback cb,
97 : void *cb_cls)
98 : {
99 6 : 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 6 : struct GNUNET_PQ_QueryParam params[] = {
106 : GNUNET_PQ_query_param_end
107 : };
108 : enum GNUNET_DB_QueryStatus qs;
109 :
110 6 : GNUNET_assert (NULL != pg->current_merchant_id);
111 6 : GNUNET_assert (0 == strcmp (instance_id,
112 : pg->current_merchant_id));
113 6 : TMH_PQ_prepare_anon (pg,
114 : "SELECT"
115 : " webhook_id"
116 : ",event_type"
117 : " FROM merchant_webhook");
118 :
119 6 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
120 : "",
121 : params,
122 : &lookup_webhooks_cb,
123 : &wlc);
124 : /* If there was an error inside lookup_webhook_cb, return a hard error. */
125 6 : if (wlc.extract_failed)
126 0 : return GNUNET_DB_STATUS_HARD_ERROR;
127 6 : return qs;
128 : }
|