Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2023 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 pg_helper.c 18 : * @brief shared internal definitions for postgres DB plugin 19 : * @author Christian Grothoff 20 : */ 21 : #include "platform.h" 22 : #include "pg_helper.h" 23 : #include <gnunet/gnunet_util_lib.h> 24 : #include <gnunet/gnunet_pq_lib.h> 25 : #include <taler/taler_util.h> 26 : #include <taler/taler_pq_lib.h> 27 : #include <taler/taler_json_lib.h> 28 : #include <taler/taler_mhd_lib.h> 29 : 30 : 31 : enum GNUNET_GenericReturnValue 32 380 : TMH_PG_start (void *cls, 33 : const char *name) 34 : { 35 380 : struct PostgresClosure *pg = cls; 36 380 : struct GNUNET_PQ_ExecuteStatement es[] = { 37 380 : GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"), 38 : GNUNET_PQ_EXECUTE_STATEMENT_END 39 : }; 40 : 41 380 : GNUNET_assert (NULL != name); 42 380 : check_connection (pg); 43 380 : postgres_preflight (pg); 44 380 : GNUNET_log (GNUNET_ERROR_TYPE_INFO, 45 : "Starting merchant DB transaction `%s'\n", 46 : name); 47 380 : if (GNUNET_OK != 48 380 : GNUNET_PQ_exec_statements (pg->conn, 49 : es)) 50 : { 51 0 : TALER_LOG_ERROR ("Failed to start transaction\n"); 52 0 : GNUNET_break (0); 53 0 : return GNUNET_SYSERR; 54 : } 55 380 : pg->transaction_name = name; 56 380 : return GNUNET_OK; 57 : } 58 : 59 : 60 : enum GNUNET_GenericReturnValue 61 0 : TMH_PG_start_read_committed (void *cls, 62 : const char *name) 63 : { 64 0 : struct PostgresClosure *pg = cls; 65 0 : struct GNUNET_PQ_ExecuteStatement es[] = { 66 0 : GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL READ COMMITTED"), 67 : GNUNET_PQ_EXECUTE_STATEMENT_END 68 : }; 69 : 70 0 : GNUNET_assert (NULL != name); 71 0 : check_connection (pg); 72 0 : postgres_preflight (pg); 73 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO, 74 : "Starting merchant DB transaction %s (READ COMMITTED)\n", 75 : name); 76 0 : if (GNUNET_OK != 77 0 : GNUNET_PQ_exec_statements (pg->conn, 78 : es)) 79 : { 80 0 : TALER_LOG_ERROR ("Failed to start transaction\n"); 81 0 : GNUNET_break (0); 82 0 : return GNUNET_SYSERR; 83 : } 84 0 : pg->transaction_name = name; 85 0 : return GNUNET_OK; 86 : } 87 : 88 : 89 : void 90 103 : TMH_PG_rollback (void *cls) 91 : { 92 103 : struct PostgresClosure *pg = cls; 93 103 : struct GNUNET_PQ_ExecuteStatement es[] = { 94 103 : GNUNET_PQ_make_execute ("ROLLBACK"), 95 : GNUNET_PQ_EXECUTE_STATEMENT_END 96 : }; 97 : 98 103 : if (NULL == pg->transaction_name) 99 27 : return; 100 76 : GNUNET_log (GNUNET_ERROR_TYPE_INFO, 101 : "Rolling back merchant DB transaction `%s'\n", 102 : pg->transaction_name); 103 76 : GNUNET_break (GNUNET_OK == 104 : GNUNET_PQ_exec_statements (pg->conn, 105 : es)); 106 76 : pg->transaction_name = NULL; 107 : } 108 : 109 : 110 : enum GNUNET_DB_QueryStatus 111 304 : TMH_PG_commit (void *cls) 112 : { 113 304 : struct PostgresClosure *pg = cls; 114 304 : struct GNUNET_PQ_QueryParam params[] = { 115 : GNUNET_PQ_query_param_end 116 : }; 117 : enum GNUNET_DB_QueryStatus qs; 118 : 119 304 : GNUNET_log (GNUNET_ERROR_TYPE_INFO, 120 : "Committing merchant DB transaction %s\n", 121 : pg->transaction_name); 122 304 : check_connection (pg); 123 304 : PREPARE (pg, 124 : "merchant_commit", 125 : "COMMIT"); 126 304 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 127 : "merchant_commit", 128 : params); 129 304 : if (qs < 0) 130 : { 131 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 132 : "Failed to commit transaction\n"); 133 0 : TMH_PG_rollback (pg); 134 0 : return qs; 135 : } 136 304 : pg->transaction_name = NULL; 137 304 : return qs; 138 : }