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 exchangedb/pg_insert_contract.c 18 : * @brief Implementation of the insert_contract function for Postgres 19 : * @author Christian Grothoff 20 : */ 21 : #include "platform.h" 22 : #include "taler_error_codes.h" 23 : #include "taler_dbevents.h" 24 : #include "taler_pq_lib.h" 25 : #include "pg_insert_contract.h" 26 : #include "pg_select_contract_by_purse.h" 27 : #include "pg_helper.h" 28 : 29 : enum GNUNET_DB_QueryStatus 30 20 : TEH_PG_insert_contract ( 31 : void *cls, 32 : const struct TALER_PurseContractPublicKeyP *purse_pub, 33 : const struct TALER_EncryptedContract *econtract, 34 : bool *in_conflict) 35 : { 36 20 : struct PostgresClosure *pg = cls; 37 : enum GNUNET_DB_QueryStatus qs; 38 20 : struct GNUNET_PQ_QueryParam params[] = { 39 20 : GNUNET_PQ_query_param_auto_from_type (purse_pub), 40 20 : GNUNET_PQ_query_param_auto_from_type (&econtract->contract_pub), 41 20 : GNUNET_PQ_query_param_fixed_size (econtract->econtract, 42 20 : econtract->econtract_size), 43 20 : GNUNET_PQ_query_param_auto_from_type (&econtract->econtract_sig), 44 : GNUNET_PQ_query_param_end 45 : }; 46 : 47 20 : *in_conflict = false; 48 : /* Used in #postgres_insert_contract() */ 49 20 : PREPARE (pg, 50 : "insert_contract", 51 : "INSERT INTO contracts" 52 : " (purse_pub" 53 : " ,pub_ckey" 54 : " ,e_contract" 55 : " ,contract_sig" 56 : " ,purse_expiration" 57 : " ) SELECT " 58 : " $1, $2, $3, $4, purse_expiration" 59 : " FROM purse_requests" 60 : " WHERE purse_pub=$1" 61 : " ON CONFLICT DO NOTHING;"); 62 20 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 63 : "insert_contract", 64 : params); 65 20 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS != qs) 66 20 : return qs; 67 : { 68 : struct TALER_EncryptedContract econtract2; 69 : 70 0 : qs = TEH_PG_select_contract_by_purse (pg, 71 : purse_pub, 72 : &econtract2); 73 0 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT != qs) 74 : { 75 0 : GNUNET_break (0); 76 0 : return GNUNET_DB_STATUS_HARD_ERROR; 77 : } 78 0 : if ( (0 == GNUNET_memcmp (&econtract->contract_pub, 79 0 : &econtract2.contract_pub)) && 80 0 : (econtract2.econtract_size == 81 0 : econtract->econtract_size) && 82 0 : (0 == memcmp (econtract2.econtract, 83 0 : econtract->econtract, 84 0 : econtract->econtract_size)) ) 85 : { 86 0 : GNUNET_free (econtract2.econtract); 87 0 : return GNUNET_DB_STATUS_SUCCESS_NO_RESULTS; 88 : } 89 0 : GNUNET_free (econtract2.econtract); 90 0 : *in_conflict = true; 91 0 : return GNUNET_DB_STATUS_SUCCESS_ONE_RESULT; 92 : } 93 : }