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_lock_product.c
18 : * @brief Implementation of the lock_product 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_lock_product.h"
26 : #include "pg_helper.h"
27 :
28 : enum GNUNET_DB_QueryStatus
29 12 : TMH_PG_lock_product (void *cls,
30 : const char *instance_id,
31 : const char *product_id,
32 : const struct GNUNET_Uuid *uuid,
33 : uint64_t quantity,
34 : uint32_t quantity_frac,
35 : struct GNUNET_TIME_Timestamp expiration_time)
36 : {
37 12 : struct PostgresClosure *pg = cls;
38 12 : struct GNUNET_PQ_QueryParam params[] = {
39 12 : GNUNET_PQ_query_param_string (instance_id),
40 12 : GNUNET_PQ_query_param_string (product_id),
41 12 : GNUNET_PQ_query_param_auto_from_type (uuid),
42 12 : GNUNET_PQ_query_param_uint64 (&quantity),
43 12 : GNUNET_PQ_query_param_uint32 (&quantity_frac),
44 12 : GNUNET_PQ_query_param_timestamp (&expiration_time),
45 : GNUNET_PQ_query_param_end
46 : };
47 :
48 12 : check_connection (pg);
49 12 : PREPARE (pg,
50 : "lock_product",
51 : "WITH ps AS"
52 : " (SELECT product_serial"
53 : " FROM merchant_inventory"
54 : " WHERE product_id=$2"
55 : " AND merchant_serial="
56 : " (SELECT merchant_serial"
57 : " FROM merchant_instances"
58 : " WHERE merchant_id=$1))"
59 : ",tmp AS"
60 : " (SELECT"
61 : " mi.product_serial"
62 : " ,mi.total_stock"
63 : " ,mi.total_stock_frac"
64 : " ,mi.total_sold"
65 : " ,mi.total_sold_frac"
66 : " ,mi.total_lost"
67 : " ,mi.total_lost_frac"
68 : " ,mi.allow_fractional_quantity"
69 : " FROM merchant_inventory mi"
70 : " JOIN ps USING (product_serial))"
71 : "INSERT INTO merchant_inventory_locks"
72 : "(product_serial"
73 : ",lock_uuid"
74 : ",total_locked"
75 : ",total_locked_frac"
76 : ",expiration)"
77 : " SELECT tmp.product_serial, $3, $4::INT8, $5::INT4, $6"
78 : " FROM tmp"
79 : " WHERE (tmp.allow_fractional_quantity OR $5 = 0)"
80 : " AND (tmp.total_stock = 9223372036854775807"
81 : " OR ("
82 : " (tmp.total_stock::NUMERIC * 1000000"
83 : " + tmp.total_stock_frac::NUMERIC)"
84 : " - (tmp.total_sold::NUMERIC * 1000000"
85 : " + tmp.total_sold_frac::NUMERIC)"
86 : " - (tmp.total_lost::NUMERIC * 1000000"
87 : " + tmp.total_lost_frac::NUMERIC)"
88 : " >= "
89 : " (($4::NUMERIC * 1000000) + $5::NUMERIC)"
90 : " + (SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
91 : " + total_locked_frac::NUMERIC), 0)"
92 : " FROM merchant_inventory_locks mil"
93 : " WHERE mil.product_serial = tmp.product_serial)"
94 : " + (SELECT COALESCE(SUM(total_locked::NUMERIC * 1000000"
95 : " + total_locked_frac::NUMERIC), 0)"
96 : " FROM merchant_order_locks mol"
97 : " WHERE mol.product_serial = tmp.product_serial)"
98 : " ))");
99 12 : return GNUNET_PQ_eval_prepared_non_select (pg->conn,
100 : "lock_product",
101 : params);
102 : }
|