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 564 : SQLtransaction_commit(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
65 : {
66 564 : mvc *sql = NULL;
67 564 : str msg;
68 564 : int chain = *getArgReference_int(stk, pci, 1);
69 564 : str name = *getArgReference_str(stk, pci, 2);
70 :
71 611 : initcontext();
72 :
73 564 : 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 555 : return mvc_commit(sql, chain, name, false);
79 : }
80 :
81 : str
82 1077 : SQLtransaction_rollback(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
83 : {
84 1077 : mvc *sql = NULL;
85 1077 : str msg;
86 1077 : int chain = *getArgReference_int(stk, pci, 1);
87 1077 : str name = *getArgReference_str(stk, pci, 2);
88 :
89 1095 : initcontext();
90 :
91 1077 : if (sql->session->auto_commit)
92 1 : throw(SQL, "sql.trans", SQLSTATE(2DM30) "ROLLBACK: not allowed in auto commit mode");
93 1076 : return mvc_rollback(sql, chain, name, false);
94 : }
95 :
96 : str
97 1500 : SQLtransaction_begin(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
98 : {
99 1500 : mvc *sql = NULL;
100 1500 : str msg;
101 1500 : int chain = *getArgReference_int(stk, pci, 1);
102 1500 : str name = *getArgReference_str(stk, pci, 2);
103 :
104 1500 : initcontext();
105 :
106 1500 : (void) chain;
107 1500 : if (!sql->session->auto_commit)
108 5 : throw(SQL, "sql.trans", SQLSTATE(25001) "START TRANSACTION: cannot start a transaction within a transaction");
109 1495 : if (sql->session->tr->active)
110 1495 : msg = mvc_rollback(sql, 0, NULL, false);
111 1495 : if (msg)
112 : return msg;
113 1495 : 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 1495 : break;
120 : }
121 : /* set transaction properties after successfuly starting */
122 1495 : sql->session->auto_commit = 0;
123 1495 : sql->session->ac_on_commit = 1;
124 1495 : return MAL_SUCCEED;
125 : }
|