Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022, 2024, 2025 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_update_product.c
18 : * @brief Implementation of the update_product function for Postgres
19 : * @author Christian Grothoff
20 : * @author Iván Ávalos
21 : */
22 : #include "platform.h"
23 : #include <taler/taler_error_codes.h>
24 : #include <taler/taler_dbevents.h>
25 : #include <taler/taler_pq_lib.h>
26 : #include "pg_update_product.h"
27 : #include "pg_helper.h"
28 :
29 :
30 : enum GNUNET_DB_QueryStatus
31 18 : TMH_PG_update_product (void *cls,
32 : const char *instance_id,
33 : const char *product_id,
34 : const struct TALER_MERCHANTDB_ProductDetails *pd,
35 : size_t num_cats,
36 : const uint64_t *cats,
37 : bool *no_instance,
38 : ssize_t *no_cat,
39 : bool *no_product,
40 : bool *lost_reduced,
41 : bool *sold_reduced,
42 : bool *stocked_reduced,
43 : bool *no_group,
44 : bool *no_pot)
45 : {
46 18 : struct PostgresClosure *pg = cls;
47 18 : struct GNUNET_PQ_QueryParam params[] = {
48 18 : GNUNET_PQ_query_param_string (instance_id), /* $1 */
49 18 : GNUNET_PQ_query_param_string (product_id),
50 18 : GNUNET_PQ_query_param_string (pd->description),
51 18 : TALER_PQ_query_param_json (pd->description_i18n), /* $4 */
52 18 : GNUNET_PQ_query_param_string (pd->unit),
53 18 : GNUNET_PQ_query_param_string (pd->image), /* $6 */
54 18 : TALER_PQ_query_param_json (pd->taxes), /* $7 */
55 18 : TALER_PQ_query_param_array_amount_with_currency (
56 18 : pd->price_array_length,
57 18 : pd->price_array,
58 : pg->conn), /* $8 */
59 18 : GNUNET_PQ_query_param_uint64 (&pd->total_stock), /* $9 */
60 18 : GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $10 */
61 18 : GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity), /* $11 */
62 18 : GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level),
63 18 : GNUNET_PQ_query_param_uint64 (&pd->total_lost),
64 18 : TALER_PQ_query_param_json (pd->address), /* $14 */
65 18 : GNUNET_PQ_query_param_timestamp (&pd->next_restock),
66 18 : GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
67 18 : GNUNET_PQ_query_param_array_uint64 (num_cats,
68 : cats,
69 : pg->conn), /* $17 */
70 18 : GNUNET_PQ_query_param_string (pd->product_name), /* $18 */
71 18 : (0 == pd->product_group_id)
72 18 : ? GNUNET_PQ_query_param_null ()
73 18 : : GNUNET_PQ_query_param_uint64 (&pd->product_group_id),
74 18 : (0 == pd->money_pot_id)
75 18 : ? GNUNET_PQ_query_param_null ()
76 18 : : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $20 */
77 18 : GNUNET_PQ_query_param_bool (pd->price_is_net),
78 : GNUNET_PQ_query_param_end
79 : };
80 : uint64_t ncat;
81 18 : bool cats_found = true;
82 18 : struct GNUNET_PQ_ResultSpec rs[] = {
83 18 : GNUNET_PQ_result_spec_bool ("no_instance",
84 : no_instance),
85 18 : GNUNET_PQ_result_spec_bool ("no_product",
86 : no_product),
87 18 : GNUNET_PQ_result_spec_bool ("lost_reduced",
88 : lost_reduced),
89 18 : GNUNET_PQ_result_spec_bool ("sold_reduced",
90 : sold_reduced),
91 18 : GNUNET_PQ_result_spec_bool ("stocked_reduced",
92 : stocked_reduced),
93 18 : GNUNET_PQ_result_spec_allow_null (
94 : GNUNET_PQ_result_spec_uint64 ("no_cat",
95 : &ncat),
96 : &cats_found),
97 18 : GNUNET_PQ_result_spec_bool ("no_group",
98 : no_group),
99 18 : GNUNET_PQ_result_spec_bool ("no_pot",
100 : no_pot),
101 : GNUNET_PQ_result_spec_end
102 : };
103 : enum GNUNET_DB_QueryStatus qs;
104 :
105 18 : if ( (pd->total_stock < pd->total_lost + pd->total_sold) ||
106 18 : (pd->total_lost < pd->total_lost
107 18 : + pd->total_sold) /* integer overflow */)
108 : {
109 0 : GNUNET_break (0);
110 0 : return GNUNET_DB_STATUS_HARD_ERROR;
111 : }
112 18 : check_connection (pg);
113 18 : PREPARE (pg,
114 : "update_product",
115 : "SELECT"
116 : " out_lost_reduced AS lost_reduced"
117 : ",out_sold_reduced AS sold_reduced"
118 : ",out_stocked_reduced AS stocked_reduced"
119 : ",out_no_product AS no_product"
120 : ",out_no_cat AS no_cat"
121 : ",out_no_instance AS no_instance"
122 : ",out_no_group AS no_group"
123 : ",out_no_pot AS no_pot"
124 : " FROM merchant_do_update_product"
125 : "($1,$2,$3,$4::TEXT::JSONB,$5,$6,$7::TEXT::JSONB,$8,$9"
126 : ",$10,$11,$12,$13,$14::TEXT::JSONB,$15,$16,$17,$18"
127 : ",$19,$20,$21);");
128 18 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
129 : "update_product",
130 : params,
131 : rs);
132 18 : GNUNET_PQ_cleanup_query_params_closures (params);
133 18 : *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
134 18 : return qs;
135 : }
|