pg_insert_purse_request.c (4781B)
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 exchangedb/pg_insert_purse_request.c 18 * @brief Implementation of the insert_purse_request function for Postgres 19 * @author Christian Grothoff 20 */ 21 #include "taler/taler_pq_lib.h" 22 #include "taler/exchange-database/insert_purse_request.h" 23 #include "taler/exchange-database/get_purse_request.h" 24 #include "helper.h" 25 26 27 enum GNUNET_DB_QueryStatus 28 TALER_EXCHANGEDB_insert_purse_request ( 29 struct TALER_EXCHANGEDB_PostgresContext *pg, 30 const struct TALER_PurseContractPublicKeyP *purse_pub, 31 const struct TALER_PurseMergePublicKeyP *merge_pub, 32 struct GNUNET_TIME_Timestamp purse_expiration, 33 const struct TALER_PrivateContractHashP *h_contract_terms, 34 uint32_t age_limit, 35 enum TALER_WalletAccountMergeFlags flags, 36 const struct TALER_Amount *purse_fee, 37 const struct TALER_Amount *amount, 38 const struct TALER_PurseContractSignatureP *purse_sig, 39 bool *in_conflict) 40 { 41 enum GNUNET_DB_QueryStatus qs; 42 struct GNUNET_TIME_Timestamp now = GNUNET_TIME_timestamp_get (); 43 uint32_t flags32 = (uint32_t) flags; 44 bool in_reserve_quota = (TALER_WAMF_MODE_CREATE_FROM_PURSE_QUOTA 45 == (flags & TALER_WAMF_MERGE_MODE_MASK)); 46 struct GNUNET_PQ_QueryParam params[] = { 47 GNUNET_PQ_query_param_auto_from_type (purse_pub), 48 GNUNET_PQ_query_param_auto_from_type (merge_pub), 49 GNUNET_PQ_query_param_timestamp (&now), 50 GNUNET_PQ_query_param_timestamp (&purse_expiration), 51 GNUNET_PQ_query_param_auto_from_type (h_contract_terms), 52 GNUNET_PQ_query_param_uint32 (&age_limit), 53 GNUNET_PQ_query_param_uint32 (&flags32), 54 GNUNET_PQ_query_param_bool (in_reserve_quota), 55 TALER_PQ_query_param_amount (pg->conn, 56 amount), 57 TALER_PQ_query_param_amount (pg->conn, 58 purse_fee), 59 GNUNET_PQ_query_param_auto_from_type (purse_sig), 60 GNUNET_PQ_query_param_end 61 }; 62 63 *in_conflict = false; 64 PREPARE (pg, 65 "insert_purse_request", 66 "INSERT INTO purse_requests" 67 " (purse_pub" 68 " ,merge_pub" 69 " ,purse_creation" 70 " ,purse_expiration" 71 " ,h_contract_terms" 72 " ,age_limit" 73 " ,flags" 74 " ,in_reserve_quota" 75 " ,amount_with_fee" 76 " ,purse_fee" 77 " ,purse_sig" 78 " ) VALUES " 79 " ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)" 80 " ON CONFLICT DO NOTHING;"); 81 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 82 "insert_purse_request", 83 params); 84 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs) 85 return qs; 86 { 87 struct TALER_PurseMergePublicKeyP merge_pub2; 88 struct GNUNET_TIME_Timestamp purse_expiration2; 89 struct TALER_PrivateContractHashP h_contract_terms2; 90 uint32_t age_limit2; 91 struct TALER_Amount amount2; 92 struct TALER_Amount balance; 93 struct TALER_PurseContractSignatureP purse_sig2; 94 95 qs = TALER_EXCHANGEDB_get_purse_request (pg, 96 purse_pub, 97 &merge_pub2, 98 &purse_expiration2, 99 &h_contract_terms2, 100 &age_limit2, 101 &amount2, 102 &balance, 103 &purse_sig2); 104 if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) 105 { 106 GNUNET_break (0); 107 return GNUNET_DB_STATUS_HARD_ERROR; 108 } 109 if ( (age_limit2 == age_limit) && 110 (0 == TALER_amount_cmp (amount, 111 &amount2)) && 112 (0 == GNUNET_memcmp (&h_contract_terms2, 113 h_contract_terms)) && 114 (0 == GNUNET_memcmp (&merge_pub2, 115 merge_pub)) ) 116 { 117 return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 118 } 119 *in_conflict = true; 120 return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 121 } 122 }