forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
procedure.go
209 lines (185 loc) · 6.26 KB
/
procedure.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
// Copyright 2021 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 plan
import (
"fmt"
"strings"
"time"
"github.com/Sndav/go-mysql-server/sql"
)
// ProcedureSecurityContext determines whether the stored procedure is executed using the privileges of the definer or
// the invoker.
type ProcedureSecurityContext byte
const (
// ProcedureSecurityContext_Definer uses the definer's security context.
ProcedureSecurityContext_Definer ProcedureSecurityContext = iota
// ProcedureSecurityContext_Invoker uses the invoker's security context.
ProcedureSecurityContext_Invoker
)
// ProcedureParamDirection represents the use case of the stored procedure parameter.
type ProcedureParamDirection byte
const (
// ProcedureParamDirection_In means the parameter passes its contained value to the stored procedure.
ProcedureParamDirection_In ProcedureParamDirection = iota
// ProcedureParamDirection_Inout means the parameter passes its contained value to the stored procedure, while also
// modifying the given variable.
ProcedureParamDirection_Inout
// ProcedureParamDirection_Out means the parameter variable will be modified, but will not be read from within the
// stored procedure.
ProcedureParamDirection_Out
)
// ProcedureParam represents the parameter of a stored procedure.
type ProcedureParam struct {
Direction ProcedureParamDirection // Direction is the direction of the parameter.
Name string // Name is the name of the parameter.
Type sql.Type // Type is the SQL type of the parameter.
}
// Characteristic represents a characteristic that is defined on either a stored procedure or stored function.
type Characteristic byte
const (
Characteristic_LanguageSql Characteristic = iota
Characteristic_Deterministic
Characteristic_NotDeterministic
Characteristic_ContainsSql
Characteristic_NoSql
Characteristic_ReadsSqlData
Characteristic_ModifiesSqlData
)
// Procedure is a stored procedure that may be executed using the CALL statement.
type Procedure struct {
Name string
Definer string
Params []ProcedureParam
SecurityContext ProcedureSecurityContext
Comment string
Characteristics []Characteristic
CreateProcedureString string
Body sql.Node
CreatedAt time.Time
ModifiedAt time.Time
}
var _ sql.Node = (*Procedure)(nil)
var _ sql.DebugStringer = (*Procedure)(nil)
// NewProcedure returns a *Procedure. All names contained within are lowercase, and all methods are case-insensitive.
func NewProcedure(
name string,
definer string,
params []ProcedureParam,
securityContext ProcedureSecurityContext,
comment string,
characteristics []Characteristic,
createProcedureString string,
body sql.Node,
createdAt time.Time,
modifiedAt time.Time,
) *Procedure {
lowercasedParams := make([]ProcedureParam, len(params))
for i, param := range params {
lowercasedParams[i] = ProcedureParam{
Direction: param.Direction,
Name: strings.ToLower(param.Name),
Type: param.Type,
}
}
return &Procedure{
Name: strings.ToLower(name),
Definer: definer,
Params: lowercasedParams,
SecurityContext: securityContext,
Comment: comment,
Characteristics: characteristics,
CreateProcedureString: createProcedureString,
Body: body,
CreatedAt: createdAt,
ModifiedAt: modifiedAt,
}
}
// Resolved implements the sql.Node interface.
func (p *Procedure) Resolved() bool {
return p.Body.Resolved()
}
// String implements the sql.Node interface.
func (p *Procedure) String() string {
return p.Body.String()
}
// DebugString implements the sql.DebugStringer interface.
func (p *Procedure) DebugString() string {
return sql.DebugString(p.Body)
}
// Schema implements the sql.Node interface.
func (p *Procedure) Schema() sql.Schema {
return p.Body.Schema()
}
// Children implements the sql.Node interface.
func (p *Procedure) Children() []sql.Node {
return []sql.Node{p.Body}
}
// WithChildren implements the sql.Node interface.
func (p *Procedure) WithChildren(children ...sql.Node) (sql.Node, error) {
if len(children) != 1 {
return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
}
np := *p
np.Body = children[0]
return &np, nil
}
// RowIter implements the sql.Node interface.
func (p *Procedure) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return p.Body.RowIter(ctx, row)
}
// String returns the original SQL representation.
func (pst ProcedureSecurityContext) String() string {
switch pst {
case ProcedureSecurityContext_Definer:
return "SQL SECURITY DEFINER"
case ProcedureSecurityContext_Invoker:
return "SQL SECURITY INVOKER"
default:
panic(fmt.Errorf("invalid security context value `%d`", byte(pst)))
}
}
// String returns the original SQL representation.
func (pp ProcedureParam) String() string {
direction := ""
switch pp.Direction {
case ProcedureParamDirection_In:
direction = "IN"
case ProcedureParamDirection_Inout:
direction = "INOUT"
case ProcedureParamDirection_Out:
direction = "OUT"
}
return fmt.Sprintf("%s %s %s", direction, pp.Name, pp.Type.String())
}
// String returns the original SQL representation.
func (c Characteristic) String() string {
switch c {
case Characteristic_LanguageSql:
return "LANGUAGE SQL"
case Characteristic_Deterministic:
return "DETERMINISTIC"
case Characteristic_NotDeterministic:
return "NOT DETERMINISTIC"
case Characteristic_ContainsSql:
return "CONTAINS SQL"
case Characteristic_NoSql:
return "NO SQL"
case Characteristic_ReadsSqlData:
return "READS SQL DATA"
case Characteristic_ModifiesSqlData:
return "MODIFIES SQL DATA"
default:
panic(fmt.Errorf("invalid characteristic value `%d`", byte(c)))
}
}