Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Varchar type #775

Merged
merged 2 commits into from
Jul 3, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added varchar definition to sql/type.go
Signed-off-by: Juanjo Alvarez <juanjo@sourced.tech>
  • Loading branch information
Juanjo Alvarez committed Jun 27, 2019
commit 25d7888de37a623ce81ba8171ef3d0d76d3f8024
56 changes: 53 additions & 3 deletions sql/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var (
// ErrConvertingToTime is thrown when a value cannot be converted to a Time
ErrConvertingToTime = errors.NewKind("value %q can't be converted to time.Time")

// ErrVarCharTruncation is thrown when a value is textually longer than the destination capacity
ErrVarCharTruncation = errors.NewKind("string value of %q is longer than destination capacity %d")

// ErrValueNotNil is thrown when a value that was expected to be nil, is not
ErrValueNotNil = errors.NewKind("value not nil: %#v")

Expand Down Expand Up @@ -197,6 +200,8 @@ var (
Date dateT
// Text is a string type.
Text textT
// VarChar is a string type with a length.
VarChar varcharT
// Boolean is a boolean type.
Boolean booleanT
// JSON is a type that holds any valid JSON object.
Expand Down Expand Up @@ -244,7 +249,9 @@ func MysqlTypeToType(sql query.Type) (Type, error) {
return Timestamp, nil
case sqltypes.Date:
return Date, nil
case sqltypes.Text, sqltypes.VarChar:
case sqltypes.VarChar:
return VarChar, nil
case sqltypes.Text:
return Text, nil
case sqltypes.Bit:
return Boolean, nil
Expand Down Expand Up @@ -550,6 +557,43 @@ func (t dateT) Compare(a, b interface{}) (int, error) {
return 0, nil
}

type varcharT struct{
length int
}

func (t varcharT) String() string { return fmt.Sprintf("VARCHAR(%d)", t.length) }

// Type implements Type interface
func (t varcharT) Type() query.Type {
return sqltypes.VarChar
}

// SQL implements Type interface
func (t varcharT) SQL(v interface{}) sqltypes.Value {
if _, ok := v.(nullT); ok {
return sqltypes.NULL
}

return sqltypes.MakeTrusted(sqltypes.VarChar, []byte(MustConvert(t, v).(string)))
}

// Convert implements Type interface
func (t varcharT) Convert(v interface{}) (interface{}, error) {
val, err := cast.ToStringE(v)
if err != nil {
return nil, ErrConvertToSQL.New(t)
}

if len(val) > t.length {
return nil, ErrVarCharTruncation.New(val, t.length)
}
return val, nil
}

// Compare implements Type interface.
func (t varcharT) Compare(a interface{}, b interface{}) (int, error) {
return strings.Compare(a.(string), b.(string)), nil
}
type textT struct{}

func (t textT) String() string { return "TEXT" }
Expand Down Expand Up @@ -928,7 +972,11 @@ func IsDecimal(t Type) bool {

// IsText checks if t is a text type.
func IsText(t Type) bool {
return t == Text || t == Blob || t == JSON
return t == Text || t == Blob || t == JSON || t == VarChar
}

func IsVarChar(t Type) bool {
return t == VarChar
}

// IsTuple checks if t is a tuple type.
Expand Down Expand Up @@ -982,7 +1030,9 @@ func MySQLTypeName(t Type) string {
return "DATETIME"
case sqltypes.Date:
return "DATE"
case sqltypes.Text, sqltypes.VarChar:
case sqltypes.VarChar:
return "VARCHAR"
case sqltypes.Text:
return "TEXT"
case sqltypes.Bit:
return "BIT"
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy