github.com/dolthub/go-mysql-server@v0.18.0/sql/types/null.go (about) 1 // Copyright 2022 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package types 16 17 import ( 18 "reflect" 19 20 "github.com/dolthub/vitess/go/sqltypes" 21 "github.com/dolthub/vitess/go/vt/proto/query" 22 "gopkg.in/src-d/go-errors.v1" 23 24 "github.com/dolthub/go-mysql-server/sql" 25 ) 26 27 var ( 28 Null sql.NullType = nullType{} 29 30 // ErrValueNotNil is thrown when a value that was expected to be nil, is not 31 ErrValueNotNil = errors.NewKind("value not nil: %#v") 32 ) 33 34 type nullType struct{} 35 36 // Compare implements Type interface. Note that while this returns 0 (equals) 37 // for ordering purposes, in SQL NULL != NULL. 38 func (t nullType) Compare(a interface{}, b interface{}) (int, error) { 39 return 0, nil 40 } 41 42 // Convert implements Type interface. 43 func (t nullType) Convert(v interface{}) (interface{}, sql.ConvertInRange, error) { 44 if v != nil { 45 return nil, sql.InRange, ErrValueNotNil.New(v) 46 } 47 48 return nil, sql.InRange, nil 49 } 50 51 // MaxTextResponseByteLength implements the Type interface 52 func (t nullType) MaxTextResponseByteLength(_ *sql.Context) uint32 { 53 return 0 54 } 55 56 // MustConvert implements the Type interface. 57 func (t nullType) MustConvert(v interface{}) interface{} { 58 value, _, err := t.Convert(v) 59 if err != nil { 60 panic(err) 61 } 62 return value 63 } 64 65 // Equals implements the Type interface. 66 func (t nullType) Equals(otherType sql.Type) bool { 67 _, ok := otherType.(nullType) 68 return ok 69 } 70 71 // Promote implements the Type interface. 72 func (t nullType) Promote() sql.Type { 73 return t 74 } 75 76 // SQL implements Type interface. 77 func (t nullType) SQL(*sql.Context, []byte, interface{}) (sqltypes.Value, error) { 78 return sqltypes.NULL, nil 79 } 80 81 // String implements Type interface. 82 func (t nullType) String() string { 83 return "null" 84 } 85 86 // Type implements Type interface. 87 func (t nullType) Type() query.Type { 88 return sqltypes.Null 89 } 90 91 // ValueType implements Type interface. 92 func (t nullType) ValueType() reflect.Type { 93 return nil 94 } 95 96 // Zero implements Type interface. 97 func (t nullType) Zero() interface{} { 98 return nil 99 } 100 101 // CollationCoercibility implements sql.CollationCoercible interface. 102 func (nullType) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 103 return sql.Collation_binary, 6 104 }