-
-
Notifications
You must be signed in to change notification settings - Fork 195
/
signal.go
384 lines (336 loc) · 11 KB
/
signal.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// 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"
"sort"
"strings"
"github.com/dolthub/vitess/go/mysql"
"github.com/dolthub/go-mysql-server/sql"
)
// SignalConditionItemName represents the item name for the set conditions of a SIGNAL statement.
type SignalConditionItemName string
const (
SignalConditionItemName_Unknown SignalConditionItemName = ""
SignalConditionItemName_ClassOrigin SignalConditionItemName = "class_origin"
SignalConditionItemName_SubclassOrigin SignalConditionItemName = "subclass_origin"
SignalConditionItemName_MessageText SignalConditionItemName = "message_text"
SignalConditionItemName_MysqlErrno SignalConditionItemName = "mysql_errno"
SignalConditionItemName_ConstraintCatalog SignalConditionItemName = "constraint_catalog"
SignalConditionItemName_ConstraintSchema SignalConditionItemName = "constraint_schema"
SignalConditionItemName_ConstraintName SignalConditionItemName = "constraint_name"
SignalConditionItemName_CatalogName SignalConditionItemName = "catalog_name"
SignalConditionItemName_SchemaName SignalConditionItemName = "schema_name"
SignalConditionItemName_TableName SignalConditionItemName = "table_name"
SignalConditionItemName_ColumnName SignalConditionItemName = "column_name"
SignalConditionItemName_CursorName SignalConditionItemName = "cursor_name"
)
var SignalItems = []SignalConditionItemName{
SignalConditionItemName_ClassOrigin,
SignalConditionItemName_SubclassOrigin,
SignalConditionItemName_MessageText,
SignalConditionItemName_MysqlErrno,
SignalConditionItemName_ConstraintCatalog,
SignalConditionItemName_ConstraintSchema,
SignalConditionItemName_ConstraintName,
SignalConditionItemName_CatalogName,
SignalConditionItemName_SchemaName,
SignalConditionItemName_TableName,
SignalConditionItemName_ColumnName,
SignalConditionItemName_CursorName,
}
// SignalInfo represents a piece of information for a SIGNAL statement.
type SignalInfo struct {
ConditionItemName SignalConditionItemName
IntValue int64
StrValue string
ExprVal sql.Expression
}
// Signal represents the SIGNAL statement with a set SQLSTATE.
type Signal struct {
SqlStateValue string // Will always be a string with length 5
Info map[SignalConditionItemName]SignalInfo
}
// SignalName represents the SIGNAL statement with a condition name.
type SignalName struct {
Signal *Signal
Name string
}
var _ sql.Node = (*Signal)(nil)
var _ sql.Node = (*SignalName)(nil)
var _ sql.Expressioner = (*Signal)(nil)
var _ sql.CollationCoercible = (*Signal)(nil)
var _ sql.CollationCoercible = (*SignalName)(nil)
// NewSignal returns a *Signal node.
func NewSignal(sqlstate string, info map[SignalConditionItemName]SignalInfo) *Signal {
// https://dev.mysql.com/doc/refman/8.0/en/signal.html#signal-condition-information-items
// https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
firstTwo := sqlstate[0:2]
if _, ok := info[SignalConditionItemName_MessageText]; !ok {
si := SignalInfo{
ConditionItemName: SignalConditionItemName_MessageText,
}
switch firstTwo {
case "01":
si.StrValue = "Unhandled user-defined warning condition"
case "02":
si.StrValue = "Unhandled user-defined not found condition"
default:
si.StrValue = "Unhandled user-defined exception condition"
}
info[SignalConditionItemName_MessageText] = si
}
if _, ok := info[SignalConditionItemName_MysqlErrno]; !ok {
si := SignalInfo{
ConditionItemName: SignalConditionItemName_MysqlErrno,
}
switch firstTwo {
case "01":
si.IntValue = 1642
case "02":
si.IntValue = 1643
default:
si.IntValue = 1644
}
info[SignalConditionItemName_MysqlErrno] = si
}
return &Signal{
SqlStateValue: sqlstate,
Info: info,
}
}
// NewSignalName returns a *SignalName node.
func NewSignalName(name string, info map[SignalConditionItemName]SignalInfo) *SignalName {
return &SignalName{
Signal: &Signal{
Info: info,
},
Name: name,
}
}
// Resolved implements the sql.Node interface.
func (s *Signal) Resolved() bool {
for _, e := range s.Expressions() {
if !e.Resolved() {
return false
}
}
return true
}
// String implements the sql.Node interface.
func (s *Signal) String() string {
infoStr := ""
if len(s.Info) > 0 {
infoStr = " SET"
i := 0
for _, k := range SignalItems {
// enforce deterministic ordering
if info, ok := s.Info[k]; ok {
if i > 0 {
infoStr += ","
}
infoStr += " " + info.String()
i++
}
}
}
return fmt.Sprintf("SIGNAL SQLSTATE '%s'%s", s.SqlStateValue, infoStr)
}
func (s *Signal) IsReadOnly() bool {
return true
}
// DebugString implements the sql.DebugStringer interface.
func (s *Signal) DebugString() string {
infoStr := ""
if len(s.Info) > 0 {
infoStr = " SET"
i := 0
for _, k := range SignalItems {
// enforce deterministic ordering
if info, ok := s.Info[k]; ok {
if i > 0 {
infoStr += ","
}
infoStr += " " + info.DebugString()
i++
}
}
}
return fmt.Sprintf("SIGNAL SQLSTATE '%s'%s", s.SqlStateValue, infoStr)
}
// Schema implements the sql.Node interface.
func (s *Signal) Schema() sql.Schema {
return nil
}
// Children implements the sql.Node interface.
func (s *Signal) Children() []sql.Node {
return nil
}
// WithChildren implements the sql.Node interface.
func (s *Signal) WithChildren(children ...sql.Node) (sql.Node, error) {
return NillaryWithChildren(s, children...)
}
func (s *Signal) Expressions() []sql.Expression {
items := s.signalItemsWithExpressions()
var exprs []sql.Expression
for _, itemInfo := range items {
exprs = append(exprs, itemInfo.ExprVal)
}
return exprs
}
// signalItemsWithExpressions returns the subset of the Info map entries that have an expression value, sorted by
// item name
func (s *Signal) signalItemsWithExpressions() []SignalInfo {
var items []SignalInfo
for _, itemInfo := range s.Info {
if itemInfo.ExprVal != nil {
items = append(items, itemInfo)
}
}
// Very important to have a consistent sort order between here and the WithExpressions call
sort.Slice(items, func(i, j int) bool {
return items[i].ConditionItemName < items[j].ConditionItemName
})
return items
}
func (s Signal) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
itemsWithExprs := s.signalItemsWithExpressions()
if len(itemsWithExprs) != len(exprs) {
return nil, sql.ErrInvalidChildrenNumber.New(s, len(exprs), len(itemsWithExprs))
}
mapCopy := make(map[SignalConditionItemName]SignalInfo)
for k, v := range s.Info {
mapCopy[k] = v
}
for i := range exprs {
// transfer the expression to the new info map
newInfo := itemsWithExprs[i]
newInfo.ExprVal = exprs[i]
mapCopy[itemsWithExprs[i].ConditionItemName] = newInfo
}
s.Info = mapCopy
return &s, nil
}
// CheckPrivileges implements the interface sql.Node.
func (s *Signal) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return true
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*Signal) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
// RowIter implements the sql.Node interface.
func (s *Signal) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
//TODO: implement CLASS_ORIGIN
//TODO: implement SUBCLASS_ORIGIN
//TODO: implement CONSTRAINT_CATALOG
//TODO: implement CONSTRAINT_SCHEMA
//TODO: implement CONSTRAINT_NAME
//TODO: implement CATALOG_NAME
//TODO: implement SCHEMA_NAME
//TODO: implement TABLE_NAME
//TODO: implement COLUMN_NAME
//TODO: implement CURSOR_NAME
if s.SqlStateValue[0:2] == "01" {
//TODO: implement warnings
return nil, fmt.Errorf("warnings not yet implemented")
} else {
messageItem := s.Info[SignalConditionItemName_MessageText]
strValue := messageItem.StrValue
if messageItem.ExprVal != nil {
exprResult, err := messageItem.ExprVal.Eval(ctx, nil)
if err != nil {
return nil, err
}
s, ok := exprResult.(string)
if !ok {
return nil, fmt.Errorf("message text expression did not evaluate to a string")
}
strValue = s
}
return nil, mysql.NewSQLError(
int(s.Info[SignalConditionItemName_MysqlErrno].IntValue),
s.SqlStateValue,
strValue,
)
}
}
// Resolved implements the sql.Node interface.
func (s *SignalName) Resolved() bool {
return true
}
// String implements the sql.Node interface.
func (s *SignalName) String() string {
infoStr := ""
if len(s.Signal.Info) > 0 {
infoStr = " SET"
i := 0
for _, info := range s.Signal.Info {
if i > 0 {
infoStr += ","
}
infoStr += " " + info.String()
i++
}
}
return fmt.Sprintf("SIGNAL %s%s", s.Name, infoStr)
}
// Schema implements the sql.Node interface.
func (s *SignalName) Schema() sql.Schema {
return nil
}
func (s *SignalName) IsReadOnly() bool {
return true
}
// Children implements the sql.Node interface.
func (s *SignalName) Children() []sql.Node {
return nil // SignalName is an alternate form of Signal rather than an encapsulating node, thus no children
}
// WithChildren implements the sql.Node interface.
func (s *SignalName) WithChildren(children ...sql.Node) (sql.Node, error) {
return NillaryWithChildren(s, children...)
}
// CheckPrivileges implements the interface sql.Node.
func (s *SignalName) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
return true
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (*SignalName) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 7
}
// RowIter implements the sql.Node interface.
func (s *SignalName) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
return nil, fmt.Errorf("may not iterate over unresolved node *SignalName")
}
func (s SignalInfo) IsReadOnly() bool {
return true
}
func (s SignalInfo) String() string {
itemName := strings.ToUpper(string(s.ConditionItemName))
if s.ExprVal != nil {
return fmt.Sprintf("%s = %s", itemName, s.ExprVal.String())
} else if s.ConditionItemName == SignalConditionItemName_MysqlErrno {
return fmt.Sprintf("%s = %d", itemName, s.IntValue)
}
return fmt.Sprintf("%s = %s", itemName, s.StrValue)
}
func (s SignalInfo) DebugString() string {
itemName := strings.ToUpper(string(s.ConditionItemName))
if s.ExprVal != nil {
return fmt.Sprintf("%s = %s", itemName, sql.DebugString(s.ExprVal))
} else if s.ConditionItemName == SignalConditionItemName_MysqlErrno {
return fmt.Sprintf("%s = %d", itemName, s.IntValue)
}
return fmt.Sprintf("%s = %s", itemName, s.StrValue)
}