| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * authors M Kersten, N Nes |
| 11 | * SQL catalog support implementation |
| 12 | * This module contains the wrappers around the SQL catalog operations |
| 13 | */ |
| 14 | #include "monetdb_config.h" |
| 15 | #include "sql_transaction.h" |
| 16 | #include "sql_gencode.h" |
| 17 | #include "sql_optimizer.h" |
| 18 | #include "sql_scenario.h" |
| 19 | #include "sql_mvc.h" |
| 20 | #include "sql_qc.h" |
| 21 | #include "sql_optimizer.h" |
| 22 | #include "mal_namespace.h" |
| 23 | #include "opt_prelude.h" |
| 24 | #include "querylog.h" |
| 25 | #include "mal_builder.h" |
| 26 | #include "mal_debugger.h" |
| 27 | |
| 28 | #include "rel_select.h" |
| 29 | #include "rel_optimizer.h" |
| 30 | #include "rel_prop.h" |
| 31 | #include "rel_rel.h" |
| 32 | #include "rel_exp.h" |
| 33 | #include "rel_bin.h" |
| 34 | #include "rel_dump.h" |
| 35 | #include "rel_remote.h" |
| 36 | #include "orderidx.h" |
| 37 | |
| 38 | #define initcontext() \ |
| 39 | if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)\ |
| 40 | return msg;\ |
| 41 | if ((msg = checkSQLContext(cntxt)) != NULL)\ |
| 42 | return msg; \ |
| 43 | if (name && strcmp(name, str_nil) == 0)\ |
| 44 | name = NULL; |
| 45 | |
| 46 | str |
| 47 | SQLtransaction_release(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 48 | { |
| 49 | mvc *sql = NULL; |
| 50 | str msg; |
| 51 | int chain = *getArgReference_int(stk, pci, 1); |
| 52 | str name = *getArgReference_str(stk, pci, 2); |
| 53 | |
| 54 | initcontext(); |
| 55 | |
| 56 | (void) chain; |
| 57 | if (sql->session->auto_commit == 1) |
| 58 | throw(SQL, "sql.trans" , SQLSTATE(3BM30) "RELEASE SAVEPOINT: not allowed in auto commit mode" ); |
| 59 | return mvc_release(sql, name); |
| 60 | } |
| 61 | |
| 62 | str |
| 63 | SQLtransaction_commit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 64 | { |
| 65 | mvc *sql = NULL; |
| 66 | str msg; |
| 67 | int chain = *getArgReference_int(stk, pci, 1); |
| 68 | str name = *getArgReference_str(stk, pci, 2); |
| 69 | |
| 70 | initcontext(); |
| 71 | |
| 72 | if (sql->session->auto_commit == 1) { |
| 73 | if (name) |
| 74 | throw(SQL, "sql.trans" , SQLSTATE(3BM30) "SAVEPOINT: not allowed in auto commit mode" ); |
| 75 | else |
| 76 | throw(SQL, "sql.trans" , SQLSTATE(2DM30) "COMMIT: not allowed in auto commit mode" ); |
| 77 | } |
| 78 | return mvc_commit(sql, chain, name, false); |
| 79 | } |
| 80 | |
| 81 | str |
| 82 | SQLtransaction_rollback(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 83 | { |
| 84 | mvc *sql = NULL; |
| 85 | str msg; |
| 86 | int chain = *getArgReference_int(stk, pci, 1); |
| 87 | str name = *getArgReference_str(stk, pci, 2); |
| 88 | |
| 89 | initcontext(); |
| 90 | |
| 91 | if (sql->session->auto_commit == 1) |
| 92 | throw(SQL, "sql.trans" , SQLSTATE(2DM30) "ROLLBACK: not allowed in auto commit mode" ); |
| 93 | return mvc_rollback(sql, chain, name, false); |
| 94 | } |
| 95 | |
| 96 | str |
| 97 | SQLtransaction_begin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 98 | { |
| 99 | mvc *sql = NULL; |
| 100 | str msg; |
| 101 | int chain = *getArgReference_int(stk, pci, 1); |
| 102 | str name = *getArgReference_str(stk, pci, 2); |
| 103 | int ret = 0; |
| 104 | |
| 105 | initcontext(); |
| 106 | |
| 107 | if (sql->session->auto_commit == 0) |
| 108 | throw(SQL, "sql.trans" , SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction" ); |
| 109 | if (sql->session->tr->active) |
| 110 | msg = mvc_rollback(sql, 0, NULL, false); |
| 111 | sql->session->auto_commit = 0; |
| 112 | sql->session->ac_on_commit = 1; |
| 113 | sql->session->level = chain; |
| 114 | ret = mvc_trans(sql); |
| 115 | if(msg) |
| 116 | return msg; |
| 117 | else if(ret < 0) |
| 118 | throw(SQL, "sql.trans" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 119 | return MAL_SUCCEED; |
| 120 | } |
| 121 | |
| 122 | str |
| 123 | SQLtransaction2(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) |
| 124 | { |
| 125 | mvc *sql = NULL; |
| 126 | str msg; |
| 127 | int ret = 0; |
| 128 | |
| 129 | (void) stk; |
| 130 | (void) pci; |
| 131 | |
| 132 | if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL) |
| 133 | return msg; |
| 134 | if ((msg = checkSQLContext(cntxt)) != NULL) |
| 135 | return msg; |
| 136 | if (sql->session->auto_commit == 0) |
| 137 | throw(SQL, "sql.trans" , SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction" ); |
| 138 | if (sql->session->tr->active) |
| 139 | msg = mvc_rollback(sql, 0, NULL, false); |
| 140 | sql->session->auto_commit = 0; |
| 141 | sql->session->ac_on_commit = 1; |
| 142 | sql->session->level = 0; |
| 143 | ret = mvc_trans(sql); |
| 144 | if(msg) |
| 145 | return msg; |
| 146 | else if(ret < 0) |
| 147 | throw(SQL, "sql.trans" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 148 | return MAL_SUCCEED; |
| 149 | } |
| 150 | |