33
33
// ErrValueNotNil is thrown when a value that was expected to be nil, is not
34
34
ErrValueNotNil = errors .NewKind ("value not nil: %#v" )
35
35
36
- // ErrNotTuple is retuned when the value is not a tuple.
36
+ // ErrNotTuple is returned when the value is not a tuple.
37
37
ErrNotTuple = errors .NewKind ("value of type %T is not a tuple" )
38
38
39
39
// ErrInvalidColumnNumber is returned when a tuple has an invalid number of
@@ -200,8 +200,6 @@ var (
200
200
Date dateT
201
201
// Text is a string type.
202
202
Text textT
203
- // VarChar is a string type with a length.
204
- VarChar varcharT
205
203
// Boolean is a boolean type.
206
204
Boolean booleanT
207
205
// JSON is a type that holds any valid JSON object.
@@ -220,6 +218,11 @@ func Array(underlying Type) Type {
220
218
return arrayT {underlying }
221
219
}
222
220
221
+ // VarChar returns a new VarChar type of the given length.
222
+ func VarChar (length int ) Type {
223
+ return varCharT {length : length }
224
+ }
225
+
223
226
// MysqlTypeToType gets the column type using the mysql type
224
227
func MysqlTypeToType (sql query.Type ) (Type , error ) {
225
228
switch sql {
@@ -249,10 +252,12 @@ func MysqlTypeToType(sql query.Type) (Type, error) {
249
252
return Timestamp , nil
250
253
case sqltypes .Date :
251
254
return Date , nil
252
- case sqltypes .VarChar :
253
- return VarChar , nil
254
255
case sqltypes .Text :
255
256
return Text , nil
257
+ case sqltypes .VarChar :
258
+ // Since we can't get the size of the sqltypes.VarChar to instantiate a
259
+ // specific VarChar(length) type we return a Text here
260
+ return Text , nil
256
261
case sqltypes .Bit :
257
262
return Boolean , nil
258
263
case sqltypes .TypeJSON :
@@ -557,28 +562,35 @@ func (t dateT) Compare(a, b interface{}) (int, error) {
557
562
return 0 , nil
558
563
}
559
564
560
- type varcharT struct {
565
+ type varCharT struct {
561
566
length int
562
567
}
563
568
564
- func (t varcharT ) String () string { return fmt .Sprintf ("VARCHAR(%d)" , t .length ) }
569
+ func (t varCharT ) Capacity () int { return t .length }
570
+
571
+ func (t varCharT ) String () string { return fmt .Sprintf ("VARCHAR(%d)" , t .length ) }
565
572
566
573
// Type implements Type interface
567
- func (t varcharT ) Type () query.Type {
574
+ func (t varCharT ) Type () query.Type {
568
575
return sqltypes .VarChar
569
576
}
570
577
571
578
// SQL implements Type interface
572
- func (t varcharT ) SQL (v interface {}) sqltypes.Value {
573
- if _ , ok := v .( nullT ); ok {
574
- return sqltypes .NULL
579
+ func (t varCharT ) SQL (v interface {}) ( sqltypes.Value , error ) {
580
+ if v == nil {
581
+ return sqltypes .MakeTrusted ( sqltypes . VarChar , nil ), nil
575
582
}
576
583
577
- return sqltypes .MakeTrusted (sqltypes .VarChar , []byte (MustConvert (t , v ).(string )))
584
+ v , err := t .Convert (v )
585
+ if err != nil {
586
+ return sqltypes.Value {}, err
587
+ }
588
+
589
+ return sqltypes .MakeTrusted (sqltypes .VarChar , []byte (v .(string ))), nil
578
590
}
579
591
580
592
// Convert implements Type interface
581
- func (t varcharT ) Convert (v interface {}) (interface {}, error ) {
593
+ func (t varCharT ) Convert (v interface {}) (interface {}, error ) {
582
594
val , err := cast .ToStringE (v )
583
595
if err != nil {
584
596
return nil , ErrConvertToSQL .New (t )
@@ -591,9 +603,10 @@ func (t varcharT) Convert(v interface{}) (interface{}, error) {
591
603
}
592
604
593
605
// Compare implements Type interface.
594
- func (t varcharT ) Compare (a interface {}, b interface {}) (int , error ) {
606
+ func (t varCharT ) Compare (a interface {}, b interface {}) (int , error ) {
595
607
return strings .Compare (a .(string ), b .(string )), nil
596
608
}
609
+
597
610
type textT struct {}
598
611
599
612
func (t textT ) String () string { return "TEXT" }
@@ -972,11 +985,12 @@ func IsDecimal(t Type) bool {
972
985
973
986
// IsText checks if t is a text type.
974
987
func IsText (t Type ) bool {
975
- return t == Text || t == Blob || t == JSON || t == VarChar
988
+ return t == Text || t == Blob || t == JSON || IsVarChar ( t )
976
989
}
977
990
978
991
func IsVarChar (t Type ) bool {
979
- return t == VarChar
992
+ _ , ok := t .(varCharT )
993
+ return ok
980
994
}
981
995
982
996
// IsTuple checks if t is a tuple type.
0 commit comments