/
stmt_kind.go
215 lines (194 loc) · 6.44 KB
/
stmt_kind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2021 Matrix Origin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package frontend
import (
"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect/mysql"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
)
// IsAdministrativeStatement checks the statement is the administrative statement.
func IsAdministrativeStatement(stmt tree.Statement) bool {
switch st := stmt.(type) {
case *tree.CreateAccount, *tree.DropAccount, *tree.AlterAccount,
*tree.CreateUser, *tree.DropUser, *tree.AlterUser,
*tree.CreateRole, *tree.DropRole,
*tree.Revoke, *tree.Grant,
*tree.SetDefaultRole, *tree.SetRole, *tree.SetPassword:
return true
case *tree.Use:
return st.IsUseRole()
}
return false
}
// IsParameterModificationStatement checks the statement is the statement of parameter modification statement.
func IsParameterModificationStatement(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.SetVar:
return true
}
return false
}
// IsPrepareStatement checks the statement is the Prepare statement.
func IsPrepareStatement(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.PrepareStmt, *tree.PrepareString:
return true
}
return false
}
// IsDDL checks the statement is the DDL statement.
func IsDDL(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.CreateTable, *tree.DropTable,
*tree.CreateView, *tree.DropView, *tree.AlterView, *tree.AlterTable,
*tree.CreateDatabase, *tree.DropDatabase, *tree.CreateSequence, *tree.DropSequence,
*tree.CreateIndex, *tree.DropIndex, *tree.TruncateTable:
return true
}
return false
}
// IsDropStatement checks the statement is the drop statement.
func IsDropStatement(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.DropDatabase, *tree.DropTable, *tree.DropView, *tree.DropIndex, *tree.DropSequence:
return true
}
return false
}
func IsCreateDropDatabase(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.CreateDatabase, *tree.DropDatabase:
return true
}
return false
}
func IsCreateDropSequence(stmt tree.Statement) bool {
switch stmt.(type) {
case *tree.CreateSequence, *tree.DropSequence:
return true
}
return false
}
/*
NeedToBeCommittedInActiveTransaction checks the statement that need to be committed
in an active transaction.
Currently, it includes the drop statement, the administration statement ,
the parameter modification statement.
*/
func NeedToBeCommittedInActiveTransaction(stmt tree.Statement) bool {
if stmt == nil {
return false
}
return IsCreateDropSequence(stmt) || IsAdministrativeStatement(stmt) || IsParameterModificationStatement(stmt)
}
/*
StatementCanBeExecutedInUncommittedTransaction checks the statement can be executed in an active transaction.
Cases | set Autocommit = 1/0 | BEGIN statement |
---------------------------------------------------
Case1 1 Yes
Case2 1 No
Case3 0 Yes
Case4 0 No
---------------------------------------------------
If it is Case1,Case3, Then
Create/Drop database reports error
If it is Case2, Then
Create/Drop database as other statement.
If it is Case4, Then
Create/Drop database commits current txn. a new txn for the next statement if needed.
*/
func statementCanBeExecutedInUncommittedTransaction(ses *Session, stmt tree.Statement) (bool, error) {
switch st := stmt.(type) {
//ddl statement
case *tree.CreateTable, *tree.CreateIndex, *tree.CreateView, *tree.AlterView, *tree.AlterTable:
return true, nil
case *tree.CreateDatabase, *tree.CreateSequence: //Case1, Case3 above
return ses.IsBackgroundSession() || !ses.OptionBitsIsSet(OPTION_BEGIN), nil
//dml statement
case *tree.Insert, *tree.Update, *tree.Delete, *tree.Select, *tree.Load, *tree.MoDump, *tree.ValuesStatement, *tree.Replace:
return true, nil
//transaction
case *tree.BeginTransaction, *tree.CommitTransaction, *tree.RollbackTransaction:
return true, nil
//show
case *tree.ShowCreateTable,
*tree.ShowCreateView,
*tree.ShowCreateDatabase,
*tree.ShowColumns,
*tree.ShowDatabases,
*tree.ShowTarget,
*tree.ShowTableStatus,
*tree.ShowGrants,
*tree.ShowTables,
*tree.ShowSequences,
*tree.ShowProcessList,
*tree.ShowErrors,
*tree.ShowWarnings,
*tree.ShowCollation,
*tree.ShowVariables,
*tree.ShowStatus,
*tree.ShowIndex,
*tree.ShowFunctionOrProcedureStatus,
*tree.ShowNodeList,
*tree.ShowLocks,
*tree.ShowTableNumber,
*tree.ShowColumnNumber,
*tree.ShowTableValues,
*tree.ShowAccounts,
*tree.ShowPublications,
*tree.ShowSubscriptions,
*tree.ShowCreatePublications,
*tree.ShowBackendServers,
*tree.ShowConnectors:
return true, nil
//others
case *tree.ExplainStmt, *tree.ExplainAnalyze, *tree.ExplainFor, *InternalCmdFieldList:
return true, nil
case *tree.PrepareStmt:
return statementCanBeExecutedInUncommittedTransaction(ses, st.Stmt)
case *tree.PrepareString:
v, err := ses.GetGlobalVar("lower_case_table_names")
if err != nil {
return false, err
}
preStmt, err := mysql.ParseOne(ses.requestCtx, st.Sql, v.(int64))
if err != nil {
return false, err
}
return statementCanBeExecutedInUncommittedTransaction(ses, preStmt)
case *tree.Execute:
preName := string(st.Name)
preStmt, err := ses.GetPrepareStmt(preName)
if err != nil {
return false, err
}
return statementCanBeExecutedInUncommittedTransaction(ses, preStmt.PrepareStmt)
case *tree.Deallocate, *tree.Reset:
return true, nil
case *tree.Use:
/*
These statements can not be executed in an uncommitted transaction:
USE SECONDARY ROLE { ALL | NONE }
USE ROLE role;
*/
return !st.IsUseRole(), nil
case *tree.DropTable, *tree.DropIndex, *tree.DropView, *tree.TruncateTable:
return true, nil
case *tree.DropDatabase, *tree.DropSequence: //Case1, Case3 above
//background transaction can execute the DROPxxx in one transaction
return ses.IsBackgroundSession() || !ses.OptionBitsIsSet(OPTION_BEGIN), nil
case *tree.SetVar:
return true, nil
}
return false, nil
}