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