pg_helper.c (3973B)
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 TMH_PG_start (void *cls, 33 const char *name) 34 { 35 struct PostgresClosure *pg = cls; 36 struct GNUNET_PQ_ExecuteStatement es[] = { 37 GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL SERIALIZABLE"), 38 GNUNET_PQ_EXECUTE_STATEMENT_END 39 }; 40 41 GNUNET_assert (NULL != name); 42 check_connection (pg); 43 postgres_preflight (pg); 44 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 45 "Starting merchant DB transaction `%s'\n", 46 name); 47 if (GNUNET_OK != 48 GNUNET_PQ_exec_statements (pg->conn, 49 es)) 50 { 51 TALER_LOG_ERROR ("Failed to start transaction\n"); 52 GNUNET_break (0); 53 return GNUNET_SYSERR; 54 } 55 pg->transaction_name = name; 56 return GNUNET_OK; 57 } 58 59 60 enum GNUNET_GenericReturnValue 61 TMH_PG_start_read_committed (void *cls, 62 const char *name) 63 { 64 struct PostgresClosure *pg = cls; 65 struct GNUNET_PQ_ExecuteStatement es[] = { 66 GNUNET_PQ_make_execute ("START TRANSACTION ISOLATION LEVEL READ COMMITTED"), 67 GNUNET_PQ_EXECUTE_STATEMENT_END 68 }; 69 70 GNUNET_assert (NULL != name); 71 check_connection (pg); 72 postgres_preflight (pg); 73 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 74 "Starting merchant DB transaction %s (READ COMMITTED)\n", 75 name); 76 if (GNUNET_OK != 77 GNUNET_PQ_exec_statements (pg->conn, 78 es)) 79 { 80 TALER_LOG_ERROR ("Failed to start transaction\n"); 81 GNUNET_break (0); 82 return GNUNET_SYSERR; 83 } 84 pg->transaction_name = name; 85 return GNUNET_OK; 86 } 87 88 89 void 90 TMH_PG_rollback (void *cls) 91 { 92 struct PostgresClosure *pg = cls; 93 struct GNUNET_PQ_ExecuteStatement es[] = { 94 GNUNET_PQ_make_execute ("ROLLBACK"), 95 GNUNET_PQ_EXECUTE_STATEMENT_END 96 }; 97 98 if (NULL == pg->transaction_name) 99 return; 100 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 101 "Rolling back merchant DB transaction `%s'\n", 102 pg->transaction_name); 103 GNUNET_break (GNUNET_OK == 104 GNUNET_PQ_exec_statements (pg->conn, 105 es)); 106 pg->transaction_name = NULL; 107 } 108 109 110 enum GNUNET_DB_QueryStatus 111 TMH_PG_commit (void *cls) 112 { 113 struct PostgresClosure *pg = cls; 114 struct GNUNET_PQ_QueryParam params[] = { 115 GNUNET_PQ_query_param_end 116 }; 117 enum GNUNET_DB_QueryStatus qs; 118 119 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 120 "Committing merchant DB transaction %s\n", 121 pg->transaction_name); 122 check_connection (pg); 123 PREPARE (pg, 124 "merchant_commit", 125 "COMMIT"); 126 qs = GNUNET_PQ_eval_prepared_non_select (pg->conn, 127 "merchant_commit", 128 params); 129 if (qs < 0) 130 { 131 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 132 "Failed to commit transaction\n"); 133 TMH_PG_rollback (pg); 134 return qs; 135 } 136 pg->transaction_name = NULL; 137 return qs; 138 }