Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * authors M Kersten, N Nes
15 : * SQL catalog support implementation
16 : * This module contains the wrappers around the SQL catalog operations
17 : */
18 : #include "monetdb_config.h"
19 : #include "sql_transaction.h"
20 : #include "sql_gencode.h"
21 : #include "sql_optimizer.h"
22 : #include "sql_scenario.h"
23 : #include "sql_mvc.h"
24 : #include "sql_qc.h"
25 : #include "sql_optimizer.h"
26 : #include "mal_namespace.h"
27 : #include "opt_prelude.h"
28 : #include "querylog.h"
29 : #include "mal_builder.h"
30 :
31 : #include "rel_select.h"
32 : #include "rel_prop.h"
33 : #include "rel_rel.h"
34 : #include "rel_exp.h"
35 : #include "rel_bin.h"
36 : #include "rel_dump.h"
37 : #include "orderidx.h"
38 :
39 : #define initcontext() \
40 : if ((msg = getSQLContext(cntxt, mb, &sql, NULL)) != NULL)\
41 : return msg;\
42 : if ((msg = checkSQLContext(cntxt)) != NULL)\
43 : return msg; \
44 : if (strNil(name))\
45 : name = NULL;
46 :
47 : str
48 13 : SQLtransaction_release(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
49 : {
50 13 : mvc *sql = NULL;
51 13 : str msg;
52 13 : int chain = *getArgReference_int(stk, pci, 1);
53 13 : str name = *getArgReference_str(stk, pci, 2);
54 :
55 26 : initcontext();
56 :
57 13 : (void) chain;
58 13 : if (sql->session->auto_commit)
59 1 : throw(SQL, "sql.trans", SQLSTATE(3BM30) "RELEASE SAVEPOINT: not allowed in auto commit mode");
60 12 : return mvc_release(sql, name);
61 : }
62 :
63 : str
64 556 : SQLtransaction_commit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
65 : {
66 556 : mvc *sql = NULL;
67 556 : str msg;
68 556 : int chain = *getArgReference_int(stk, pci, 1);
69 556 : str name = *getArgReference_str(stk, pci, 2);
70 :
71 604 : initcontext();
72 :
73 556 : if (sql->session->auto_commit) {
74 9 : if (name)
75 2 : throw(SQL, "sql.trans", SQLSTATE(3BM30) "SAVEPOINT: not allowed in auto commit mode");
76 7 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "COMMIT: not allowed in auto commit mode");
77 : }
78 547 : return mvc_commit(sql, chain, name, false);
79 : }
80 :
81 : str
82 1082 : SQLtransaction_rollback(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
83 : {
84 1082 : mvc *sql = NULL;
85 1082 : str msg;
86 1082 : int chain = *getArgReference_int(stk, pci, 1);
87 1082 : str name = *getArgReference_str(stk, pci, 2);
88 :
89 1100 : initcontext();
90 :
91 1082 : if (sql->session->auto_commit)
92 3 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "ROLLBACK: not allowed in auto commit mode");
93 1079 : return mvc_rollback(sql, chain, name, false);
94 : }
95 :
96 : str
97 1506 : SQLtransaction_begin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
98 : {
99 1506 : mvc *sql = NULL;
100 1506 : str msg;
101 1506 : int chain = *getArgReference_int(stk, pci, 1);
102 1506 : str name = *getArgReference_str(stk, pci, 2);
103 :
104 1506 : initcontext();
105 :
106 1506 : (void) chain;
107 1506 : if (!sql->session->auto_commit)
108 5 : throw(SQL, "sql.trans", SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction");
109 1501 : if (sql->session->tr->active)
110 1501 : msg = mvc_rollback(sql, 0, NULL, false);
111 1501 : if (msg)
112 : return msg;
113 1501 : switch (mvc_trans(sql)) {
114 0 : case -1:
115 0 : throw(SQL, "sql.trans", SQLSTATE(HY013) MAL_MALLOC_FAIL);
116 0 : case -3:
117 0 : throw(SQL, "sql.trans", SQLSTATE(42000) "The session's schema was not found, this transaction won't start");
118 : default:
119 1501 : break;
120 : }
121 : /* set transaction properties after successfully starting */
122 1501 : sql->session->auto_commit = 0;
123 1501 : sql->session->ac_on_commit = 1;
124 1501 : return MAL_SUCCEED;
125 : }
|