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 src/backenddb/update_to_contract_terms_paid.c
18 : * @brief Implementation of the update_to_contract_terms_paid function for Postgres
19 : * @author Iván Ávalos
20 : */
21 : #include "platform.h"
22 : #include <taler/taler_pq_lib.h>
23 : #include "merchant-database/update_to_contract_terms_paid.h"
24 : #include "helper.h"
25 :
26 :
27 : enum GNUNET_DB_QueryStatus
28 70 : TALER_MERCHANTDB_update_to_contract_terms_paid (
29 : struct TALER_MERCHANTDB_PostgresContext *pg,
30 : const char *instance_id,
31 : const struct TALER_PrivateContractHashP *h_contract_terms,
32 : const char *session_id,
33 : int16_t choice_index)
34 : {
35 70 : struct GNUNET_PQ_QueryParam params[] = {
36 70 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
37 70 : GNUNET_PQ_query_param_string (session_id),
38 70 : (choice_index >= 0)
39 12 : ? GNUNET_PQ_query_param_int16 (&choice_index)
40 70 : : GNUNET_PQ_query_param_null (),
41 : GNUNET_PQ_query_param_end
42 : };
43 70 : struct GNUNET_PQ_QueryParam uparams[] = {
44 70 : GNUNET_PQ_query_param_auto_from_type (h_contract_terms),
45 : GNUNET_PQ_query_param_end
46 : };
47 : enum GNUNET_DB_QueryStatus qs;
48 :
49 : /* Session ID must always be given by the caller. */
50 70 : GNUNET_assert (NULL != session_id);
51 70 : GNUNET_assert (NULL != pg->current_merchant_id);
52 70 : GNUNET_assert (0 == strcmp (instance_id,
53 : pg->current_merchant_id));
54 :
55 : /* FIXME: combine the 3 individual statements into a stored procedure! */
56 : /* no preflight check here, run in transaction by caller! */
57 70 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
58 : "Marking h_contract_terms '%s' of %s as paid for session `%s'\n",
59 : GNUNET_h2s (&h_contract_terms->hash),
60 : instance_id,
61 : session_id);
62 70 : TMH_PQ_prepare_anon (pg,
63 : "UPDATE merchant_contract_terms SET"
64 : " paid=TRUE"
65 : ",session_id=$2"
66 : ",choice_index=$3"
67 : " WHERE h_contract_terms=$1");
68 70 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
69 : "",
70 : params);
71 70 : if (qs <= 0)
72 1 : return qs;
73 69 : TMH_PQ_prepare_anon (pg,
74 : "UPDATE merchant_inventory SET"
75 : " total_sold = total_sold"
76 : " + order_locks.total_locked"
77 : " + ((merchant_inventory.total_sold_frac::BIGINT"
78 : " + order_locks.total_locked_frac::BIGINT) / 1000000)"
79 : " ,total_sold_frac ="
80 : " ((merchant_inventory.total_sold_frac::BIGINT"
81 : " + order_locks.total_locked_frac::BIGINT) % 1000000)::INT4"
82 : " FROM (SELECT total_locked,total_locked_frac,product_serial"
83 : " FROM merchant_order_locks"
84 : " WHERE order_serial="
85 : " (SELECT order_serial"
86 : " FROM merchant_contract_terms"
87 : " WHERE h_contract_terms=$1)"
88 : " ) AS order_locks"
89 : " WHERE merchant_inventory.product_serial"
90 : " =order_locks.product_serial");
91 69 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
92 : "",
93 : uparams);
94 69 : if (qs < 0)
95 0 : return qs; /* 0: no inventory management, that's OK! */
96 : /* ON DELETE CASCADE deletes from merchant_order_locks */
97 69 : TMH_PQ_prepare_anon (pg,
98 : "DELETE"
99 : " FROM merchant_orders"
100 : " WHERE order_serial="
101 : " (SELECT order_serial"
102 : " FROM merchant_contract_terms"
103 : " WHERE h_contract_terms=$1)");
104 69 : return GNUNET_PQ_eval_prepared_non_select (pg->conn,
105 : "",
106 : uparams);
107 : }
|