-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_stringtype.go
executable file
·123 lines (104 loc) · 2.9 KB
/
system_stringtype.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
// 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 sql
import (
"github.com/dolthub/vitess/go/sqltypes"
"github.com/dolthub/vitess/go/vt/proto/query"
)
// systemStringType is an internal string type ONLY for system variables.
type systemStringType struct {
varName string
}
var _ SystemVariableType = systemStringType{}
// NewSystemStringType returns a new systemStringType.
func NewSystemStringType(varName string) SystemVariableType {
return systemStringType{varName}
}
// Compare implements Type interface.
func (t systemStringType) Compare(a interface{}, b interface{}) (int, error) {
as, err := t.Convert(a)
if err != nil {
return 0, err
}
bs, err := t.Convert(b)
if err != nil {
return 0, err
}
ai := as.(string)
bi := bs.(string)
if ai == bi {
return 0, nil
}
if ai < bi {
return -1, nil
}
return 1, nil
}
// Convert implements Type interface.
func (t systemStringType) Convert(v interface{}) (interface{}, error) {
if v == nil {
return "", nil
}
if value, ok := v.(string); ok {
return value, nil
}
return nil, ErrInvalidSystemVariableValue.New(t.varName, v)
}
// MustConvert implements the Type interface.
func (t systemStringType) MustConvert(v interface{}) interface{} {
value, err := t.Convert(v)
if err != nil {
panic(err)
}
return value
}
// Promote implements the Type interface.
func (t systemStringType) Promote() Type {
return t
}
// SQL implements Type interface.
func (t systemStringType) SQL(v interface{}) (sqltypes.Value, error) {
if v == nil {
return sqltypes.NULL, nil
}
v, err := t.Convert(v)
if err != nil {
return sqltypes.Value{}, err
}
return sqltypes.MakeTrusted(t.Type(), []byte(v.(string))), nil
}
// String implements Type interface.
func (t systemStringType) String() string {
return "SYSTEM_STRING"
}
// Type implements Type interface.
func (t systemStringType) Type() query.Type {
return sqltypes.VarChar
}
// Zero implements Type interface.
func (t systemStringType) Zero() interface{} {
return ""
}
// EncodeValue implements SystemVariableType interface.
func (t systemStringType) EncodeValue(val interface{}) (string, error) {
expectedVal, ok := val.(string)
if !ok {
return "", ErrSystemVariableCodeFail.New(val, t.String())
}
return expectedVal, nil
}
// DecodeValue implements SystemVariableType interface.
func (t systemStringType) DecodeValue(val string) (interface{}, error) {
return val, nil
}