Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2022-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 src/backenddb/insert_product.c
18 : * @brief Implementation of the insert_product function for Postgres
19 : * @author Christian Grothoff
20 : * @author Iván Ávalos
21 : */
22 : #include "platform.h"
23 : #include <taler/taler_pq_lib.h>
24 : #include "merchant-database/insert_product.h"
25 : #include "helper.h"
26 :
27 :
28 : enum GNUNET_DB_QueryStatus
29 49 : TALER_MERCHANTDB_insert_product (
30 : struct TALER_MERCHANTDB_PostgresContext *pg,
31 : const char *instance_id,
32 : const char *product_id,
33 : const struct TALER_MERCHANTDB_ProductDetails *pd,
34 : size_t num_cats,
35 : const uint64_t *cats,
36 : bool *conflict,
37 : ssize_t *no_cat,
38 : bool *no_group,
39 : bool *no_pot)
40 : {
41 49 : struct GNUNET_PQ_QueryParam params[] = {
42 49 : GNUNET_PQ_query_param_string (product_id),
43 49 : GNUNET_PQ_query_param_string (pd->description),
44 49 : TALER_PQ_query_param_json (pd->description_i18n), /* $3 */
45 49 : GNUNET_PQ_query_param_string (pd->unit),
46 49 : GNUNET_PQ_query_param_string (pd->image),
47 49 : TALER_PQ_query_param_json (pd->taxes), /* $6 */
48 49 : TALER_PQ_query_param_array_amount_with_currency (
49 49 : pd->price_array_length,
50 49 : pd->price_array,
51 : "merchant",
52 : pg->conn), /* $7 */
53 49 : GNUNET_PQ_query_param_uint64 (&pd->total_stock), /* $8 */
54 49 : GNUNET_PQ_query_param_uint32 (&pd->total_stock_frac), /* $9 */
55 49 : GNUNET_PQ_query_param_bool (pd->allow_fractional_quantity),
56 49 : GNUNET_PQ_query_param_uint32 (&pd->fractional_precision_level),
57 49 : TALER_PQ_query_param_json (pd->address), /* $12 */
58 49 : GNUNET_PQ_query_param_timestamp (&pd->next_restock),
59 49 : GNUNET_PQ_query_param_uint32 (&pd->minimum_age),
60 49 : GNUNET_PQ_query_param_array_uint64 (num_cats,
61 : cats,
62 : pg->conn), /* $15 */
63 49 : GNUNET_PQ_query_param_string (pd->product_name),
64 49 : (0 == pd->product_group_id)
65 49 : ? GNUNET_PQ_query_param_null ()
66 49 : : GNUNET_PQ_query_param_uint64 (&pd->product_group_id), /* $17 */
67 49 : (0 == pd->money_pot_id)
68 49 : ? GNUNET_PQ_query_param_null ()
69 49 : : GNUNET_PQ_query_param_uint64 (&pd->money_pot_id), /* $18 */
70 49 : GNUNET_PQ_query_param_bool (pd->price_is_net), /* $19 */
71 : GNUNET_PQ_query_param_end
72 : };
73 : uint64_t ncat;
74 49 : bool cats_found = true;
75 49 : struct GNUNET_PQ_ResultSpec rs[] = {
76 49 : GNUNET_PQ_result_spec_bool ("conflict",
77 : conflict),
78 49 : GNUNET_PQ_result_spec_allow_null (
79 : GNUNET_PQ_result_spec_uint64 ("no_cat",
80 : &ncat),
81 : &cats_found),
82 49 : GNUNET_PQ_result_spec_bool ("no_group",
83 : no_group),
84 49 : GNUNET_PQ_result_spec_bool ("no_pot",
85 : no_pot),
86 : GNUNET_PQ_result_spec_end
87 : };
88 : enum GNUNET_DB_QueryStatus qs;
89 :
90 49 : GNUNET_assert (NULL != pg->current_merchant_id);
91 49 : GNUNET_assert (0 == strcmp (instance_id,
92 : pg->current_merchant_id));
93 49 : TMH_PQ_prepare_anon (pg,
94 : "SELECT"
95 : " out_conflict AS conflict"
96 : ",out_no_cat AS no_cat"
97 : ",out_no_group AS no_group"
98 : ",out_no_pot AS no_pot"
99 : " FROM merchant_do_insert_product"
100 : "($1, $2, $3::TEXT::JSONB, $4, $5, $6::TEXT::JSONB, $7"
101 : ",$8, $9, $10, $11, $12::TEXT::JSONB, $13, $14, $15"
102 : ",$16, $17, $18, $19);");
103 49 : qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
104 : "",
105 : params,
106 : rs);
107 49 : GNUNET_PQ_cleanup_query_params_closures (params);
108 49 : *no_cat = (cats_found) ? -1 : (ssize_t) ncat;
109 49 : return qs;
110 : }
|