-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
transactions.go
120 lines (104 loc) · 3.45 KB
/
transactions.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
// Copyright 2023 Dolthub, 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,
// 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 planbuilder
import (
"strings"
ast "github.com/dolthub/vitess/go/vt/sqlparser"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/types"
)
func (b *Builder) buildUse(inScope *scope, n *ast.Use) (outScope *scope) {
name := n.DBName.String()
ret := plan.NewUse(b.resolveDb(name))
ret.Catalog = b.cat
outScope = inScope.push()
outScope.node = ret
return
}
func (b *Builder) buildPrepare(inScope *scope, n *ast.Prepare) (outScope *scope) {
outScope = inScope.push()
expr := n.Expr
if strings.HasPrefix(n.Expr, "@") {
// TODO resolve user variable
varName := strings.ToLower(strings.Trim(n.Expr, "@"))
_, val, err := b.ctx.GetUserVariable(b.ctx, varName)
if err != nil {
b.handleErr(err)
}
strVal, _, err := types.LongText.Convert(val)
if err != nil {
b.handleErr(err)
}
if strVal == nil {
expr = "NULL"
} else {
expr = strVal.(string)
}
}
sqlMode := sql.LoadSqlMode(b.ctx)
childStmt, err := ast.ParseWithOptions(expr, sqlMode.ParserOptions())
if err != nil {
b.handleErr(err)
}
oldCtx := b.BindCtx()
defer func() {
b.bindCtx = oldCtx
}()
// test for query structure; bind variables will be discarded
b.bindCtx = &BindvarContext{resolveOnly: true}
childScope := b.build(inScope, childStmt, expr)
outScope.node = plan.NewPrepareQuery(n.Name, childScope.node)
return outScope
}
func (b *Builder) buildExecute(inScope *scope, n *ast.Execute) (outScope *scope) {
outScope = inScope.push()
exprs := make([]sql.Expression, len(n.VarList))
for i, e := range n.VarList {
if strings.HasPrefix(e, "@") {
exprs[i] = expression.NewUserVar(strings.TrimPrefix(e, "@"))
} else {
exprs[i] = expression.NewUnresolvedProcedureParam(e)
}
}
outScope.node = plan.NewExecuteQuery(n.Name, exprs...)
return outScope
}
func (b *Builder) buildDeallocate(inScope *scope, n *ast.Deallocate) (outScope *scope) {
outScope = inScope.push()
outScope.node = plan.NewDeallocateQuery(n.Name)
return outScope
}
func (b *Builder) buildLockTables(inScope *scope, s *ast.LockTables) (outScope *scope) {
outScope = inScope.push()
tables := make([]*plan.TableLock, len(s.Tables))
for i, tbl := range s.Tables {
tableScope := b.buildDataSource(inScope, tbl.Table)
write := tbl.Lock == ast.LockWrite || tbl.Lock == ast.LockLowPriorityWrite
// TODO: Allow for other types of locks (LOW PRIORITY WRITE & LOCAL READ)
tables[i] = &plan.TableLock{Table: tableScope.node, Write: write}
}
lockTables := plan.NewLockTables(tables)
lockTables.Catalog = b.cat
outScope.node = lockTables
return outScope
}
func (b *Builder) buildUnlockTables(inScope *scope, s *ast.UnlockTables) (outScope *scope) {
outScope = inScope.push()
unlockTables := plan.NewUnlockTables()
unlockTables.Catalog = b.cat
outScope.node = unlockTables
return outScope
}