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 "querylog.h"
28 : #include "mal_builder.h"
29 :
30 : #include "rel_select.h"
31 : #include "rel_prop.h"
32 : #include "rel_rel.h"
33 : #include "rel_exp.h"
34 : #include "rel_bin.h"
35 : #include "rel_dump.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 (strNil(name))\
44 : name = NULL;
45 :
46 : str
47 13 : SQLtransaction_release(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
48 : {
49 13 : mvc *sql = NULL;
50 13 : str msg;
51 13 : int chain = *getArgReference_int(stk, pci, 1);
52 13 : str name = *getArgReference_str(stk, pci, 2);
53 :
54 26 : initcontext();
55 :
56 13 : (void) chain;
57 13 : if (sql->session->auto_commit)
58 1 : throw(SQL, "sql.trans", SQLSTATE(3BM30) "RELEASE SAVEPOINT: not allowed in auto commit mode");
59 12 : return mvc_release(sql, name);
60 : }
61 :
62 : str
63 558 : SQLtransaction_commit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
64 : {
65 558 : mvc *sql = NULL;
66 558 : str msg;
67 558 : int chain = *getArgReference_int(stk, pci, 1);
68 558 : str name = *getArgReference_str(stk, pci, 2);
69 :
70 606 : initcontext();
71 :
72 558 : if (sql->session->auto_commit) {
73 9 : if (name)
74 2 : throw(SQL, "sql.trans", SQLSTATE(3BM30) "SAVEPOINT: not allowed in auto commit mode");
75 7 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "COMMIT: not allowed in auto commit mode");
76 : }
77 549 : return mvc_commit(sql, chain, name, false);
78 : }
79 :
80 : str
81 1084 : SQLtransaction_rollback(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
82 : {
83 1084 : mvc *sql = NULL;
84 1084 : str msg;
85 1084 : int chain = *getArgReference_int(stk, pci, 1);
86 1084 : str name = *getArgReference_str(stk, pci, 2);
87 :
88 1102 : initcontext();
89 :
90 1084 : if (sql->session->auto_commit)
91 3 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "ROLLBACK: not allowed in auto commit mode");
92 1081 : return mvc_rollback(sql, chain, name, false);
93 : }
94 :
95 : str
96 1507 : SQLtransaction_begin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
97 : {
98 1507 : mvc *sql = NULL;
99 1507 : str msg;
100 1507 : int chain = *getArgReference_int(stk, pci, 1);
101 1507 : str name = *getArgReference_str(stk, pci, 2);
102 :
103 1507 : initcontext();
104 :
105 1507 : (void) chain;
106 1507 : if (!sql->session->auto_commit)
107 5 : throw(SQL, "sql.trans", SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction");
108 1502 : if (sql->session->tr->active)
109 1502 : msg = mvc_rollback(sql, 0, NULL, false);
110 1502 : if (msg)
111 : return msg;
112 1502 : switch (mvc_trans(sql)) {
113 0 : case -1:
114 0 : throw(SQL, "sql.trans", SQLSTATE(HY013) MAL_MALLOC_FAIL);
115 0 : case -3:
116 0 : throw(SQL, "sql.trans", SQLSTATE(42000) "The session's schema was not found, this transaction won't start");
117 : default:
118 1502 : break;
119 : }
120 : /* set transaction properties after successfully starting */
121 1502 : sql->session->auto_commit = 0;
122 1502 : sql->session->ac_on_commit = 1;
123 1502 : return MAL_SUCCEED;
124 : }
|