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 10 : 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 : struct GNUNET_TIME_Timestamp expiration_time) 35 : { 36 10 : struct PostgresClosure *pg = cls; 37 10 : struct GNUNET_PQ_QueryParam params[] = { 38 10 : GNUNET_PQ_query_param_string (instance_id), 39 10 : GNUNET_PQ_query_param_string (product_id), 40 10 : GNUNET_PQ_query_param_auto_from_type (uuid), 41 10 : GNUNET_PQ_query_param_uint64 (&quantity), 42 10 : GNUNET_PQ_query_param_timestamp (&expiration_time), 43 : GNUNET_PQ_query_param_end 44 : }; 45 : 46 10 : check_connection (pg); 47 10 : PREPARE (pg, 48 : "lock_product", 49 : "WITH ps AS" 50 : " (SELECT product_serial" 51 : " FROM merchant_inventory" 52 : " WHERE product_id=$2" 53 : " AND merchant_serial=" 54 : " (SELECT merchant_serial" 55 : " FROM merchant_instances" 56 : " WHERE merchant_id=$1))" 57 : "INSERT INTO merchant_inventory_locks" 58 : "(product_serial" 59 : ",lock_uuid" 60 : ",total_locked" 61 : ",expiration)" 62 : " SELECT product_serial, $3, $4, $5" 63 : " FROM merchant_inventory" 64 : " JOIN ps USING (product_serial)" 65 : " WHERE " 66 : " total_stock - total_sold - total_lost - $4 >= " 67 : " (SELECT COALESCE(SUM(total_locked), 0)" 68 : " FROM merchant_inventory_locks" 69 : " WHERE product_serial=ps.product_serial) + " 70 : " (SELECT COALESCE(SUM(total_locked), 0)" 71 : " FROM merchant_order_locks" 72 : " WHERE product_serial=ps.product_serial)"); 73 10 : return GNUNET_PQ_eval_prepared_non_select (pg->conn, 74 : "lock_product", 75 : params); 76 : }