1
- // Copyright 2020-2021 Dolthub, Inc.
1
+ // Copyright 2022 Dolthub, Inc.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package sql
15
+ package types
16
16
17
17
import (
18
18
"fmt"
@@ -21,62 +21,40 @@ import (
21
21
"strings"
22
22
"unicode/utf8"
23
23
24
- "github.com/dolthub/go-mysql-server/sql/types"
24
+ "github.com/dolthub/go-mysql-server/sql"
25
+ "github.com/dolthub/go-mysql-server/sql/encodings"
25
26
"github.com/dolthub/vitess/go/sqltypes"
26
27
"github.com/dolthub/vitess/go/vt/proto/query"
27
28
"github.com/shopspring/decimal"
28
29
"gopkg.in/src-d/go-errors.v1"
29
-
30
- "github.com/dolthub/go-mysql-server/sql/encodings"
31
30
)
32
31
33
32
const (
34
33
// EnumTypeMinElements returns the minimum number of enumerations for the Enum type.
35
34
EnumTypeMinElements = 1
36
35
// EnumTypeMaxElements returns the maximum number of enumerations for the Enum type.
37
36
EnumTypeMaxElements = 65535
38
- /// An ENUM column can have a maximum of 65,535 distinct elements.
37
+ // / An ENUM column can have a maximum of 65,535 distinct elements.
39
38
)
40
39
41
40
var (
42
- ErrConvertingToEnum = errors .NewKind ("value %v is not valid for this Enum" )
43
- ErrUnmarshallingEnum = errors .NewKind ("value %v is not a marshalled value for this Enum" )
44
- ErrTemporaryEnum = errors .NewKind ("attempted to use temporary enum" )
41
+ ErrConvertingToEnum = errors .NewKind ("value %v is not valid for this Enum" )
45
42
46
43
enumValueType = reflect .TypeOf (uint16 (0 ))
47
44
)
48
45
49
- // Comments with three slashes were taken directly from the linked documentation.
50
-
51
- // EnumType represents the ENUM type.
52
- // https://dev.mysql.com/doc/refman/8.0/en/enum.html
53
- // The type of the returned value is uint16.
54
- type EnumType interface {
55
- Type
56
- // At returns the string at the given index, as well if the string was found.
57
- At (index int ) (string , bool )
58
- CharacterSet () CharacterSetID
59
- Collation () CollationID
60
- // IndexOf returns the index of the given string. If the string was not found, then this returns -1.
61
- IndexOf (v string ) int
62
- // NumberOfElements returns the number of enumerations.
63
- NumberOfElements () uint16
64
- // Values returns the elements, in order, of every enumeration.
65
- Values () []string
66
- }
67
-
68
46
type EnumType_ struct {
69
- collation CollationID
47
+ collation sql. CollationID
70
48
hashedValToIndex map [uint64 ]int
71
49
indexToVal []string
72
50
maxResponseByteLength uint32
73
51
}
74
52
75
- var _ EnumType = EnumType_ {}
76
- var _ TypeWithCollation = EnumType_ {}
53
+ var _ sql. EnumType = EnumType_ {}
54
+ var _ sql. TypeWithCollation = EnumType_ {}
77
55
78
56
// CreateEnumType creates a EnumType.
79
- func CreateEnumType (values []string , collation CollationID ) (EnumType , error ) {
57
+ func CreateEnumType (values []string , collation sql. CollationID ) (sql. EnumType , error ) {
80
58
if len (values ) < EnumTypeMinElements {
81
59
return nil , fmt .Errorf ("number of values may not be zero" )
82
60
}
@@ -90,7 +68,7 @@ func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
90
68
maxCharLength := collation .Collation ().CharacterSet .MaxLength ()
91
69
valToIndex := make (map [uint64 ]int )
92
70
for i , value := range values {
93
- if ! collation .Equals (Collation_binary ) {
71
+ if ! collation .Equals (sql . Collation_binary ) {
94
72
// Trailing spaces are automatically deleted from ENUM member values in the table definition when a table
95
73
// is created, unless the binary charset and collation is in use
96
74
value = strings .TrimRight (value , " " )
@@ -120,7 +98,7 @@ func CreateEnumType(values []string, collation CollationID) (EnumType, error) {
120
98
}
121
99
122
100
// MustCreateEnumType is the same as CreateEnumType except it panics on errors.
123
- func MustCreateEnumType (values []string , collation CollationID ) EnumType {
101
+ func MustCreateEnumType (values []string , collation sql. CollationID ) sql. EnumType {
124
102
et , err := CreateEnumType (values , collation )
125
103
if err != nil {
126
104
panic (err )
@@ -135,7 +113,7 @@ func (t EnumType_) MaxTextResponseByteLength() uint32 {
135
113
136
114
// Compare implements Type interface.
137
115
func (t EnumType_ ) Compare (a interface {}, b interface {}) (int , error ) {
138
- if hasNulls , res := CompareNulls (a , b ); hasNulls {
116
+ if hasNulls , res := sql . CompareNulls (a , b ); hasNulls {
139
117
return res , nil
140
118
}
141
119
@@ -230,7 +208,7 @@ func (t EnumType_) MustConvert(v interface{}) interface{} {
230
208
}
231
209
232
210
// Equals implements the Type interface.
233
- func (t EnumType_ ) Equals (otherType Type ) bool {
211
+ func (t EnumType_ ) Equals (otherType sql. Type ) bool {
234
212
if ot , ok := otherType .(EnumType_ ); ok && t .collation .Equals (ot .collation ) && len (t .indexToVal ) == len (ot .indexToVal ) {
235
213
for i , val := range t .indexToVal {
236
214
if ot .indexToVal [i ] != val {
@@ -243,12 +221,12 @@ func (t EnumType_) Equals(otherType Type) bool {
243
221
}
244
222
245
223
// Promote implements the Type interface.
246
- func (t EnumType_ ) Promote () Type {
224
+ func (t EnumType_ ) Promote () sql. Type {
247
225
return t
248
226
}
249
227
250
228
// SQL implements Type interface.
251
- func (t EnumType_ ) SQL (ctx * Context , dest []byte , v interface {}) (sqltypes.Value , error ) {
229
+ func (t EnumType_ ) SQL (ctx * sql. Context , dest []byte , v interface {}) (sqltypes.Value , error ) {
252
230
if v == nil {
253
231
return sqltypes .NULL , nil
254
232
}
@@ -259,25 +237,25 @@ func (t EnumType_) SQL(ctx *Context, dest []byte, v interface{}) (sqltypes.Value
259
237
value , _ := t .At (int (convertedValue .(uint16 )))
260
238
261
239
resultCharset := ctx .GetCharacterSetResults ()
262
- if resultCharset == CharacterSet_Unspecified || resultCharset == CharacterSet_binary {
240
+ if resultCharset == sql . CharacterSet_Unspecified || resultCharset == sql . CharacterSet_binary {
263
241
resultCharset = t .collation .CharacterSet ()
264
242
}
265
243
encodedBytes , ok := resultCharset .Encoder ().Encode (encodings .StringToBytes (value ))
266
244
if ! ok {
267
- return sqltypes.Value {}, ErrCharSetFailedToEncode .New (t .collation .CharacterSet ().Name ())
245
+ return sqltypes.Value {}, sql . ErrCharSetFailedToEncode .New (t .collation .CharacterSet ().Name ())
268
246
}
269
- val := types . AppendAndSliceBytes (dest , encodedBytes )
247
+ val := AppendAndSliceBytes (dest , encodedBytes )
270
248
271
249
return sqltypes .MakeTrusted (sqltypes .Enum , val ), nil
272
250
}
273
251
274
252
// String implements Type interface.
275
253
func (t EnumType_ ) String () string {
276
254
s := fmt .Sprintf ("enum('%v')" , strings .Join (t .indexToVal , `','` ))
277
- if t .CharacterSet () != Collation_Default .CharacterSet () {
255
+ if t .CharacterSet () != sql . Collation_Default .CharacterSet () {
278
256
s += " CHARACTER SET " + t .CharacterSet ().String ()
279
257
}
280
- if ! t .collation .Equals (Collation_Default ) {
258
+ if ! t .collation .Equals (sql . Collation_Default ) {
281
259
s += " COLLATE " + t .collation .String ()
282
260
}
283
261
return s
@@ -295,13 +273,13 @@ func (t EnumType_) ValueType() reflect.Type {
295
273
296
274
// Zero implements Type interface.
297
275
func (t EnumType_ ) Zero () interface {} {
298
- /// If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
276
+ // / If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
299
277
return uint16 (1 )
300
278
}
301
279
302
280
// At implements EnumType interface.
303
281
func (t EnumType_ ) At (index int ) (string , bool ) {
304
- /// The elements listed in the column specification are assigned index numbers, beginning with 1.
282
+ // / The elements listed in the column specification are assigned index numbers, beginning with 1.
305
283
index -= 1
306
284
if index < 0 || index >= len (t .indexToVal ) {
307
285
return "" , false
@@ -310,12 +288,12 @@ func (t EnumType_) At(index int) (string, bool) {
310
288
}
311
289
312
290
// CharacterSet implements EnumType interface.
313
- func (t EnumType_ ) CharacterSet () CharacterSetID {
291
+ func (t EnumType_ ) CharacterSet () sql. CharacterSetID {
314
292
return t .collation .CharacterSet ()
315
293
}
316
294
317
295
// Collation implements EnumType interface.
318
- func (t EnumType_ ) Collation () CollationID {
296
+ func (t EnumType_ ) Collation () sql. CollationID {
319
297
return t .collation
320
298
}
321
299
@@ -327,8 +305,8 @@ func (t EnumType_) IndexOf(v string) int {
327
305
return index
328
306
}
329
307
}
330
- /// ENUM('0','1','2')
331
- /// If you store '3', it does not match any enumeration value, so it is treated as an index and becomes '2' (the value with index 3).
308
+ // / ENUM('0','1','2')
309
+ // / If you store '3', it does not match any enumeration value, so it is treated as an index and becomes '2' (the value with index 3).
332
310
if parsedIndex , err := strconv .ParseInt (v , 10 , 32 ); err == nil {
333
311
if _ , ok := t .At (int (parsedIndex )); ok {
334
312
return int (parsedIndex )
@@ -350,6 +328,6 @@ func (t EnumType_) Values() []string {
350
328
}
351
329
352
330
// WithNewCollation implements TypeWithCollation interface.
353
- func (t EnumType_ ) WithNewCollation (collation CollationID ) (Type , error ) {
331
+ func (t EnumType_ ) WithNewCollation (collation sql. CollationID ) (sql. Type , error ) {
354
332
return CreateEnumType (t .indexToVal , collation )
355
333
}
0 commit comments