forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocedure.go
281 lines (249 loc) · 8.44 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// 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"
"strconv"
"strings"
"time"
"github.com/dolthub/go-mysql-server/sql/expression"
"github.com/dolthub/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.
Variadic bool // Variadic states whether the parameter is variadic.
}
// 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
ValidationError error
}
var _ sql.Node = (*Procedure)(nil)
var _ sql.DebugStringer = (*Procedure)(nil)
var _ sql.CollationCoercible = (*Procedure)(nil)
var _ RepresentsBlock = (*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,
Variadic: param.Variadic,
}
}
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()
}
func (p *Procedure) IsReadOnly() bool {
return p.Body.IsReadOnly()
}
// 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
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (p *Procedure) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.GetCoercibility(ctx, p.Body)
}
// implementsRepresentsBlock implements the RepresentsBlock interface.
func (p *Procedure) implementsRepresentsBlock() {}
// ExtendVariadic returns a new procedure that has the variadic parameter extended to match the CALL's parameter count.
func (p *Procedure) ExtendVariadic(ctx *sql.Context, length int) *Procedure {
if !p.HasVariadicParameter() {
return p
}
np := *p
body := p.Body.(*ExternalProcedure)
newBody := *body
np.Body = &newBody
newParamDefinitions := make([]ProcedureParam, length)
newParams := make([]*expression.ProcedureParam, length)
if length < len(p.Params) {
newParamDefinitions = p.Params[:len(p.Params)-1]
newParams = body.Params[:len(body.Params)-1]
} else {
for i := range p.Params {
newParamDefinitions[i] = p.Params[i]
newParams[i] = body.Params[i]
}
if length >= len(p.Params) {
variadicParam := p.Params[len(p.Params)-1]
for i := len(p.Params); i < length; i++ {
paramName := "A" + strconv.FormatInt(int64(i), 10)
newParamDefinitions[i] = ProcedureParam{
Direction: variadicParam.Direction,
Name: paramName,
Type: variadicParam.Type,
Variadic: variadicParam.Variadic,
}
newParams[i] = expression.NewProcedureParam(paramName, variadicParam.Type)
}
}
}
newBody.ParamDefinitions = newParamDefinitions
newBody.Params = newParams
np.Params = newParamDefinitions
return &np
}
// HasVariadicParameter returns if the last parameter is variadic.
func (p *Procedure) HasVariadicParameter() bool {
if len(p.Params) > 0 {
return p.Params[len(p.Params)-1].Variadic
}
return false
}
// IsExternal returns whether the stored procedure is external.
func (p *Procedure) IsExternal() bool {
if _, ok := p.Body.(*ExternalProcedure); ok {
return true
}
return false
}
// 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)))
}
}