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 12 : SQLtransaction_release(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
49 : {
50 12 : mvc *sql = NULL;
51 12 : str msg;
52 12 : int chain = *getArgReference_int(stk, pci, 1);
53 12 : str name = *getArgReference_str(stk, pci, 2);
54 :
55 24 : initcontext();
56 :
57 12 : (void) chain;
58 12 : if (sql->session->auto_commit)
59 1 : throw(SQL, "sql.trans", SQLSTATE(3BM30) "RELEASE SAVEPOINT: not allowed in auto commit mode");
60 11 : return mvc_release(sql, name);
61 : }
62 :
63 : str
64 558 : SQLtransaction_commit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
65 : {
66 558 : mvc *sql = NULL;
67 558 : str msg;
68 558 : int chain = *getArgReference_int(stk, pci, 1);
69 558 : str name = *getArgReference_str(stk, pci, 2);
70 :
71 605 : initcontext();
72 :
73 558 : 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 549 : return mvc_commit(sql, chain, name, false);
79 : }
80 :
81 : str
82 1026 : SQLtransaction_rollback(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
83 : {
84 1026 : mvc *sql = NULL;
85 1026 : str msg;
86 1026 : int chain = *getArgReference_int(stk, pci, 1);
87 1026 : str name = *getArgReference_str(stk, pci, 2);
88 :
89 1044 : initcontext();
90 :
91 1026 : if (sql->session->auto_commit)
92 1 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "ROLLBACK: not allowed in auto commit mode");
93 1025 : return mvc_rollback(sql, chain, name, false);
94 : }
95 :
96 : str
97 1444 : SQLtransaction_begin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
98 : {
99 1444 : mvc *sql = NULL;
100 1444 : str msg;
101 1444 : int chain = *getArgReference_int(stk, pci, 1);
102 1444 : str name = *getArgReference_str(stk, pci, 2);
103 :
104 1444 : initcontext();
105 :
106 1444 : (void) chain;
107 1444 : if (!sql->session->auto_commit)
108 5 : throw(SQL, "sql.trans", SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction");
109 1439 : if (sql->session->tr->active)
110 1439 : msg = mvc_rollback(sql, 0, NULL, false);
111 1439 : if (msg)
112 : return msg;
113 1439 : 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 1439 : break;
120 : }
121 : /* set transaction properties after successfuly starting */
122 1439 : sql->session->auto_commit = 0;
123 1439 : sql->session->ac_on_commit = 1;
124 1439 : return MAL_SUCCEED;
125 : }
|