Line data Source code
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_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 "pg_lookup_templates.h"
26 : #include "pg_helper.h"
27 :
28 :
29 : /**
30 : * Context used for TMH_PG_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 4 : lookup_templates_cb (void *cls,
61 : PGresult *result,
62 : unsigned int num_results)
63 : {
64 4 : struct LookupTemplateContext *tlc = cls;
65 :
66 6 : for (unsigned int i = 0; i < num_results; i++)
67 : {
68 : char *template_id;
69 : char *template_description;
70 2 : struct GNUNET_PQ_ResultSpec rs[] = {
71 2 : GNUNET_PQ_result_spec_string ("template_id",
72 : &template_id),
73 2 : GNUNET_PQ_result_spec_string ("template_description",
74 : &template_description),
75 : GNUNET_PQ_result_spec_end
76 : };
77 :
78 2 : if (GNUNET_OK !=
79 2 : GNUNET_PQ_extract_result (result,
80 : rs,
81 : i))
82 : {
83 0 : GNUNET_break (0);
84 0 : tlc->extract_failed = true;
85 0 : return;
86 : }
87 2 : tlc->cb (tlc->cb_cls,
88 : template_id,
89 : template_description);
90 2 : GNUNET_PQ_cleanup_result (rs);
91 : }
92 : }
93 :
94 :
95 : enum GNUNET_DB_QueryStatus
96 4 : TMH_PG_lookup_templates (void *cls,
97 : const char *instance_id,
98 : TALER_MERCHANTDB_TemplatesCallback cb,
99 : void *cb_cls)
100 : {
101 4 : struct PostgresClosure *pg = cls;
102 4 : struct LookupTemplateContext tlc = {
103 : .cb = cb,
104 : .cb_cls = cb_cls,
105 : /* Can be overwritten by the lookup_template_cb */
106 : .extract_failed = false,
107 : };
108 4 : struct GNUNET_PQ_QueryParam params[] = {
109 4 : GNUNET_PQ_query_param_string (instance_id),
110 : GNUNET_PQ_query_param_end
111 : };
112 : enum GNUNET_DB_QueryStatus qs;
113 :
114 4 : check_connection (pg);
115 4 : PREPARE (pg,
116 : "lookup_templates",
117 : "SELECT"
118 : " template_id"
119 : ",template_description"
120 : " FROM merchant_template"
121 : " JOIN merchant_instances"
122 : " USING (merchant_serial)"
123 : " WHERE merchant_instances.merchant_id=$1");
124 4 : qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
125 : "lookup_templates",
126 : params,
127 : &lookup_templates_cb,
128 : &tlc);
129 : /* If there was an error inside lookup_templates_cb, return a hard error. */
130 4 : if (tlc.extract_failed)
131 0 : return GNUNET_DB_STATUS_HARD_ERROR;
132 4 : return qs;
133 : }
|