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 11 : lookup_products_cb (void *cls,
60 : PGresult *result,
61 : unsigned int num_results)
62 : {
63 11 : struct LookupProductsContext *plc = cls;
64 :
65 13 : for (unsigned int i = 0; i < num_results; i++)
66 : {
67 : char *product_id;
68 : uint64_t product_serial;
69 2 : struct GNUNET_PQ_ResultSpec rs[] = {
70 2 : GNUNET_PQ_result_spec_string ("product_id",
71 : &product_id),
72 2 : GNUNET_PQ_result_spec_uint64 ("product_serial",
73 : &product_serial),
74 : GNUNET_PQ_result_spec_end
75 : };
76 :
77 2 : if (GNUNET_OK !=
78 2 : 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 2 : plc->cb (plc->cb_cls,
87 : product_serial,
88 : product_id);
89 2 : GNUNET_PQ_cleanup_result (rs);
90 : }
91 : }
92 :
93 :
94 : enum GNUNET_DB_QueryStatus
95 11 : TMH_PG_lookup_products (void *cls,
96 : const char *instance_id,
97 : uint64_t offset,
98 : int64_t limit,
99 : TALER_MERCHANTDB_ProductsCallback cb,
100 : void *cb_cls)
101 : {
102 11 : struct PostgresClosure *pg = cls;
103 11 : uint64_t plimit = (uint64_t) ((limit < 0) ? -limit : limit);
104 11 : struct LookupProductsContext plc = {
105 : .cb = cb,
106 : .cb_cls = cb_cls,
107 : /* Can be overwritten by the lookup_products_cb */
108 : .extract_failed = false,
109 : };
110 11 : struct GNUNET_PQ_QueryParam params[] = {
111 11 : GNUNET_PQ_query_param_string (instance_id),
112 11 : GNUNET_PQ_query_param_uint64 (&offset),
113 11 : GNUNET_PQ_query_param_uint64 (&plimit),
114 : GNUNET_PQ_query_param_end
115 : };
116 : enum GNUNET_DB_QueryStatus qs;
117 :
118 11 : check_connection (pg);
119 11 : PREPARE (pg,
120 : "lookup_products_asc",
121 : "SELECT"
122 : " product_id"
123 : " ,product_serial"
124 : " FROM merchant_inventory"
125 : " JOIN merchant_instances"
126 : " USING (merchant_serial)"
127 : " WHERE merchant_instances.merchant_id=$1"
128 : " AND product_serial > $2"
129 : " ORDER BY product_serial ASC"
130 : " LIMIT $3");
131 11 : PREPARE (pg,
132 : "lookup_products_desc",
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 : " ORDER BY product_serial DESC"
142 : " LIMIT $3");
143 11 : qs = GNUNET_PQ_eval_prepared_multi_select (
144 : pg->conn,
145 : (limit > 0)
146 : ? "lookup_products_asc"
147 : : "lookup_products_desc",
148 : params,
149 : &lookup_products_cb,
150 : &plc);
151 : /* If there was an error inside lookup_products_cb, return a hard error. */
152 11 : if (plc.extract_failed)
153 0 : return GNUNET_DB_STATUS_HARD_ERROR;
154 11 : return qs;
155 : }
|