pg_delete_order.c (3302B)
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_delete_order.c 18 * @brief Implementation of the delete_order 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_delete_order.h" 26 #include "pg_helper.h" 27 28 enum GNUNET_DB_QueryStatus 29 TMH_PG_delete_order (void *cls, 30 const char *instance_id, 31 const char *order_id, 32 bool force) 33 { 34 struct PostgresClosure *pg = cls; 35 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 36 struct GNUNET_PQ_QueryParam params[] = { 37 GNUNET_PQ_query_param_string (instance_id), 38 GNUNET_PQ_query_param_string (order_id), 39 GNUNET_PQ_query_param_absolute_time (&now), 40 GNUNET_PQ_query_param_bool (force), 41 GNUNET_PQ_query_param_end 42 }; 43 struct GNUNET_PQ_QueryParam params2[] = { 44 GNUNET_PQ_query_param_string (instance_id), 45 GNUNET_PQ_query_param_string (order_id), 46 GNUNET_PQ_query_param_end 47 }; 48 enum GNUNET_DB_QueryStatus qs; 49 enum GNUNET_DB_QueryStatus qs2; 50 51 check_connection (pg); 52 PREPARE (pg, 53 "delete_order", 54 "WITH ms AS" 55 "(SELECT merchant_serial " 56 " FROM merchant_instances" 57 " WHERE merchant_id=$1)" 58 ", mc AS" 59 "(SELECT paid" 60 " FROM merchant_contract_terms" 61 " JOIN ms USING (merchant_serial)" 62 " WHERE order_id=$2) " 63 "DELETE" 64 " FROM merchant_orders mo" 65 " WHERE order_id=$2" 66 " AND merchant_serial=(SELECT merchant_serial FROM ms)" 67 " AND ( (pay_deadline < $3)" 68 " OR (NOT EXISTS (SELECT paid FROM mc))" 69 " OR ($4 AND (FALSE=(SELECT paid FROM mc))) );"); 70 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 71 "delete_order", 72 params); 73 if ( (qs < 0) || (! force) ) 74 return qs; 75 PREPARE (pg, 76 "delete_contract", 77 "DELETE" 78 " FROM merchant_contract_terms" 79 " WHERE order_id=$2 AND" 80 " merchant_serial=" 81 " (SELECT merchant_serial " 82 " FROM merchant_instances" 83 " WHERE merchant_id=$1)" 84 " AND NOT paid;"); 85 qs2 = GNUNET_PQ_eval_prepared_non_select (pg->conn, 86 "delete_contract", 87 params2); 88 if (qs2 < 0) 89 return qs2; 90 if (qs2 > 0) 91 return qs2; 92 return qs; 93 }