forked from talent-plan/tinysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
114 lines (102 loc) · 3.53 KB
/
simple.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
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"context"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
// SimpleExec represents simple statement executor.
// For statements do simple execution.
// includes `UseStmt`,`BeginStmt`, `CommitStmt` and `RollbackStmt`.
type SimpleExec struct {
baseExecutor
Statement ast.StmtNode
done bool
is infoschema.InfoSchema
}
// Next implements the Executor Next interface.
func (e *SimpleExec) Next(ctx context.Context, req *chunk.Chunk) (err error) {
if e.done {
return nil
}
switch x := e.Statement.(type) {
case *ast.UseStmt:
err = e.executeUse(x)
case *ast.BeginStmt:
err = e.executeBegin(ctx, x)
case *ast.CommitStmt:
e.executeCommit(x)
case *ast.RollbackStmt:
err = e.executeRollback(x)
}
e.done = true
return err
}
func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
dbname := model.NewCIStr(s.DBName)
dbinfo, exists := e.is.SchemaByName(dbname)
if !exists {
return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbname)
}
e.ctx.GetSessionVars().CurrentDB = dbname.O
// character_set_database is the character set used by the default database.
// The server sets this variable whenever the default database changes.
// See http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_character_set_database
sessionVars := e.ctx.GetSessionVars()
terror.Log(sessionVars.SetSystemVar(variable.CharsetDatabase, dbinfo.Charset))
terror.Log(sessionVars.SetSystemVar(variable.CollationDatabase, dbinfo.Collate))
return nil
}
func (e *SimpleExec) executeBegin(ctx context.Context, s *ast.BeginStmt) error {
// If BEGIN is the first statement in TxnCtx, we can reuse the existing transaction, without the
// need to call NewTxn, which commits the existing transaction and begins a new one.
txnCtx := e.ctx.GetSessionVars().TxnCtx
if txnCtx.History != nil {
err := e.ctx.NewTxn(ctx)
if err != nil {
return err
}
}
// With START TRANSACTION, autocommit remains disabled until you end
// the transaction with COMMIT or ROLLBACK. The autocommit mode then
// reverts to its previous state.
e.ctx.GetSessionVars().SetStatusFlag(mysql.ServerStatusInTrans, true)
// Call ctx.Txn(true) to active pending txn.
_, err := e.ctx.Txn(true)
return err
}
func (e *SimpleExec) executeCommit(s *ast.CommitStmt) {
e.ctx.GetSessionVars().SetStatusFlag(mysql.ServerStatusInTrans, false)
}
func (e *SimpleExec) executeRollback(s *ast.RollbackStmt) error {
sessVars := e.ctx.GetSessionVars()
logutil.BgLogger().Debug("execute rollback statement", zap.Uint64("conn", sessVars.ConnectionID))
sessVars.SetStatusFlag(mysql.ServerStatusInTrans, false)
txn, err := e.ctx.Txn(false)
if err != nil {
return err
}
if txn.Valid() {
sessVars.TxnCtx.ClearDelta()
return txn.Rollback()
}
return nil
}