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_products.c
18 : * @brief Implementation of the lookup_products function for Postgres
19 : * @author Iván Ávalos
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_products.h"
26 : #include "pg_helper.h"
27 :
28 : /**
29 : * Context used for TMH_PG_lookup_products().
30 : */
31 : struct LookupProductsContext
32 : {
33 : /**
34 : * Function to call with the results.
35 : */
36 : TALER_MERCHANTDB_ProductsCallback cb;
37 :
38 : /**
39 : * Closure for @a cb.
40 : */
41 : void *cb_cls;
42 :
43 : /**
44 : * Did database result extraction fail?
45 : */
46 : bool extract_failed;
47 : };
48 :
49 :
50 : /**
51 : * Function to be called with the results of a SELECT statement
52 : * that has returned @a num_results results about products.
53 : *
54 : * @param[in,out] cls of type `struct LookupProductsContext *`
55 : * @param result the postgres result
56 : * @param num_results the number of results in @a result
57 : */
58 : static void
59 12 : lookup_products_cb (void *cls,
60 : PGresult *result,
61 : unsigned int num_results)
62 : {
63 12 : struct LookupProductsContext *plc = cls;
64 :
65 15 : for (unsigned int i = 0; i < num_results; i++)
66 : {
67 : char *product_id;
68 : uint64_t product_serial;
69 3 : struct GNUNET_PQ_ResultSpec rs[] = {
70 3 : GNUNET_PQ_result_spec_string ("product_id",
71 : &product_id),
72 3 : GNUNET_PQ_result_spec_uint64 ("product_serial",
73 : &product_serial),
74 : GNUNET_PQ_result_spec_end
75 : };
76 :
77 3 : if (GNUNET_OK !=
78 3 : GNUNET_PQ_extract_result (result,
79 : rs,
80 : i))
81 : {
82 0 : GNUNET_break (0);
83 0 : plc->extract_failed = true;
84 0 : return;
85 : }
86 3 : plc->cb (plc->cb_cls,
87 : product_serial,
88 : product_id);
89 3 : GNUNET_PQ_cleanup_result (rs);
90 : }
91 : }
92 :
93 :
94 : enum GNUNET_DB_QueryStatus
95 12 : TMH_PG_lookup_products (void *cls,
96 : const char *instance_id,
97 : uint64_t offset,
98 : int64_t limit,
99 : const char *category_filter,
100 : const char *name_filter,
101 : const char *description_filter,
102 : TALER_MERCHANTDB_ProductsCallback cb,
103 : void *cb_cls)
104 : {
105 12 : struct PostgresClosure *pg = cls;
106 12 : uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
107 12 : struct LookupProductsContext plc = {
108 : .cb = cb,
109 : .cb_cls = cb_cls,
110 : /* Can be overwritten by the lookup_products_cb */
111 : .extract_failed = false,
112 : };
113 12 : struct GNUNET_PQ_QueryParam params[] = {
114 12 : GNUNET_PQ_query_param_string (instance_id),
115 12 : GNUNET_PQ_query_param_uint64 (&offset),
116 : NULL == category_filter
117 12 : ? GNUNET_PQ_query_param_null ()
118 12 : : GNUNET_PQ_query_param_string (category_filter),
119 : NULL == name_filter
120 11 : ? GNUNET_PQ_query_param_null ()
121 12 : : GNUNET_PQ_query_param_string (name_filter),
122 : NULL == description_filter
123 12 : ? GNUNET_PQ_query_param_null ()
124 12 : : GNUNET_PQ_query_param_string (description_filter),
125 12 : GNUNET_PQ_query_param_uint64 (&plimit),
126 : GNUNET_PQ_query_param_end
127 : };
128 : enum GNUNET_DB_QueryStatus qs;
129 :
130 12 : check_connection (pg);
131 12 : PREPARE (pg,
132 : "lookup_products_asc",
133 : "SELECT"
134 : " product_id"
135 : " ,product_serial"
136 : " FROM merchant_inventory"
137 : " JOIN merchant_instances"
138 : " USING (merchant_serial)"
139 : " WHERE merchant_instances.merchant_id=$1"
140 : " AND product_serial > $2"
141 : " AND ( ($3::TEXT IS NULL) OR"
142 : " (product_serial IN"
143 : " (SELECT product_serial"
144 : " FROM merchant_product_categories"
145 : " WHERE category_serial IN"
146 : " (SELECT category_serial"
147 : " FROM merchant_categories"
148 : " WHERE category_name LIKE LOWER($3)))) )"
149 : " AND ( ($4::TEXT IS NULL) OR"
150 : " (product_name LIKE LOWER($4)) )"
151 : " AND ( ($5::TEXT IS NULL) OR"
152 : " (description LIKE LOWER($5)) )"
153 : " ORDER BY product_serial ASC"
154 : " LIMIT $6");
155 12 : PREPARE (pg,
156 : "lookup_products_desc",
157 : "SELECT"
158 : " product_id"
159 : " ,product_serial"
160 : " FROM merchant_inventory"
161 : " JOIN merchant_instances"
162 : " USING (merchant_serial)"
163 : " WHERE merchant_instances.merchant_id=$1"
164 : " AND product_serial < $2"
165 : " AND ( ($3::TEXT IS NULL) OR"
166 : " (product_serial IN"
167 : " (SELECT product_serial"
168 : " FROM merchant_product_categories"
169 : " WHERE category_serial IN"
170 : " (SELECT category_serial"
171 : " FROM merchant_categories"
172 : " WHERE category_name LIKE LOWER($3)))) )"
173 : " AND ( ($4::TEXT IS NULL) OR"
174 : " (product_name LIKE LOWER($4)) )"
175 : " AND ( ($5::TEXT IS NULL) OR"
176 : " (description LIKE LOWER($5)) )"
177 : " ORDER BY product_serial DESC"
178 : " LIMIT $6");
179 12 : qs = GNUNET_PQ_eval_prepared_multi_select (
180 : pg->conn,
181 : (limit > 0)
182 : ? "lookup_products_asc"
183 : : "lookup_products_desc",
184 : params,
185 : &lookup_products_cb,
186 : &plc);
187 : /* If there was an error inside lookup_products_cb, return a hard error. */
188 12 : if (plc.extract_failed)
189 : {
190 0 : GNUNET_break (0);
191 0 : return GNUNET_DB_STATUS_HARD_ERROR;
192 : }
193 12 : return qs;
194 : }
|