sqltypes

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package sqltypes implements interfaces and types that represent SQL values.

Index

Constants

View Source
const (
	TimestampFormat           = "2006-01-02 15:04:05"
	TimestampFormatPrecision3 = "2006-01-02 15:04:05.000"
	TimestampFormatPrecision6 = "2006-01-02 15:04:05.000000"
)
View Source
const (
	Null       = Type_NULL_TYPE
	Int8       = Type_INT8
	Uint8      = Type_UINT8
	Int16      = Type_INT16
	Uint16     = Type_UINT16
	Int24      = Type_INT24
	Uint24     = Type_UINT24
	Int32      = Type_INT32
	Uint32     = Type_UINT32
	Int64      = Type_INT64
	Uint64     = Type_UINT64
	Float32    = Type_FLOAT32
	Float64    = Type_FLOAT64
	Timestamp  = Type_TIMESTAMP
	Date       = Type_DATE
	Time       = Type_TIME
	Datetime   = Type_DATETIME
	Year       = Type_YEAR
	Decimal    = Type_DECIMAL
	Text       = Type_TEXT
	Blob       = Type_BLOB
	VarChar    = Type_VARCHAR
	VarBinary  = Type_VARBINARY
	Char       = Type_CHAR
	Binary     = Type_BINARY
	Bit        = Type_BIT
	Enum       = Type_ENUM
	Set        = Type_SET
	Geometry   = Type_GEOMETRY
	TypeJSON   = Type_JSON
	Expression = Type_EXPRESSION
	HexNum     = Type_HEXNUM
	HexVal     = Type_HEXVAL
	Tuple      = Type_TUPLE
)

Vitess data types. These are idiomatically named synonyms for the Type values. Although these constants are interchangeable, they should be treated as different from Type. Use the synonyms only to refer to the type in Value. For proto variables, use the Type constants instead. The following is a complete listing of types that match each classification function in this API:

IsSigned(): INT8, INT16, INT24, INT32, INT64
IsFloat(): FLOAT32, FLOAT64
IsUnsigned(): UINT8, UINT16, UINT24, UINT32, UINT64, YEAR
IsIntegral(): INT8, UINT8, INT16, UINT16, INT24, UINT24, INT32, UINT32, INT64, UINT64, YEAR
IsText(): TEXT, VARCHAR, CHAR, HEXNUM, HEXVAL
IsNumber(): INT8, UINT8, INT16, UINT16, INT24, UINT24, INT32, UINT32, INT64, UINT64, FLOAT32, FLOAT64, YEAR, DECIMAL
IsQuoted(): TIMESTAMP, DATE, TIME, DATETIME, TEXT, BLOB, VARCHAR, VARBINARY, CHAR, BINARY, ENUM, SET, GEOMETRY, JSON
IsBinary(): BLOB, VARBINARY, BINARY
IsDate(): TIMESTAMP, DATE, TIME, DATETIME
IsNull(): NULL_TYPE

TODO(sougou): provide a categorization function that returns enums, which will allow for cleaner switch statements for those who want to cover types by their category.

Variables

View Source
var (
	Type_name = map[int32]string{
		0:     "NULL_TYPE",
		257:   "INT8",
		770:   "UINT8",
		259:   "INT16",
		772:   "UINT16",
		261:   "INT24",
		774:   "UINT24",
		263:   "INT32",
		776:   "UINT32",
		265:   "INT64",
		778:   "UINT64",
		1035:  "FLOAT32",
		1036:  "FLOAT64",
		2061:  "TIMESTAMP",
		2062:  "DATE",
		2063:  "TIME",
		2064:  "DATETIME",
		785:   "YEAR",
		18:    "DECIMAL",
		6163:  "TEXT",
		10260: "BLOB",
		6165:  "VARCHAR",
		10262: "VARBINARY",
		6167:  "CHAR",
		10264: "BINARY",
		2073:  "BIT",
		2074:  "ENUM",
		2075:  "SET",
		28:    "TUPLE",
		2077:  "GEOMETRY",
		2078:  "JSON",
		31:    "EXPRESSION",
		4128:  "HEXNUM",
		4129:  "HEXVAL",
	}
	Type_value = map[string]int32{
		"NULL_TYPE":  0,
		"INT8":       257,
		"UINT8":      770,
		"INT16":      259,
		"UINT16":     772,
		"INT24":      261,
		"UINT24":     774,
		"INT32":      263,
		"UINT32":     776,
		"INT64":      265,
		"UINT64":     778,
		"FLOAT32":    1035,
		"FLOAT64":    1036,
		"TIMESTAMP":  2061,
		"DATE":       2062,
		"TIME":       2063,
		"DATETIME":   2064,
		"YEAR":       785,
		"DECIMAL":    18,
		"TEXT":       6163,
		"BLOB":       10260,
		"VARCHAR":    6165,
		"VARBINARY":  10262,
		"CHAR":       6167,
		"BINARY":     10264,
		"BIT":        2073,
		"ENUM":       2074,
		"SET":        2075,
		"TUPLE":      28,
		"GEOMETRY":   2077,
		"JSON":       2078,
		"EXPRESSION": 31,
		"HEXNUM":     4128,
		"HEXVAL":     4129,
	}
)

Enum value maps for Type.

View Source
var (
	// NULL represents the NULL value.
	NULL = Value{}

	// DontEscape tells you if a character should not be escaped.
	DontEscape = byte(255)

	// ErrIncompatibleTypeCast indicates a casting problem
	ErrIncompatibleTypeCast = errors.New("Cannot convert value to desired type")
)
View Source
var SQLDecodeMap [256]byte

SQLDecodeMap is the reverse of SQLEncodeMap

View Source
var SQLEncodeMap [256]byte

SQLEncodeMap specifies how to escape binary data with '\'. Complies to http://dev.mysql.com/doc/refman/5.1/en/string-syntax.html

Functions

func AreTypesEquivalent

func AreTypesEquivalent(mysqlTypeFromBinlog, mysqlTypeFromSchema Type) bool

AreTypesEquivalent returns whether two types are equivalent.

func BufEncodeStringSQL

func BufEncodeStringSQL(buf *strings.Builder, val string)

BufEncodeStringSQL encodes the string into a strings.Builder

func EncodeStringSQL

func EncodeStringSQL(val string) string

EncodeStringSQL encodes the string as a SQL string.

func IsBinary

func IsBinary(t Type) bool

IsBinary returns true if Type is a binary. If you have a Value object, use its member function.

func IsDate

func IsDate(t Type) bool

IsDate returns true if the type represents a date and/or time.

func IsFloat

func IsFloat(t Type) bool

IsFloat returns true is Type is a floating point. If you have a Value object, use its member function.

func IsIntegral

func IsIntegral(t Type) bool

IsIntegral returns true if Type is an integral (signed/unsigned) that can be represented using up to 64 binary bits. If you have a Value object, use its member function.

func IsNull

func IsNull(t Type) bool

IsNull returns true if the type is NULL type

func IsNumber

func IsNumber(t Type) bool

IsNumber returns true if the type is any type of number.

func IsQuoted

func IsQuoted(t Type) bool

IsQuoted returns true if Type is a quoted text or binary. If you have a Value object, use its member function.

func IsSigned

func IsSigned(t Type) bool

IsSigned returns true if Type is a signed integral. If you have a Value object, use its member function.

func IsText

func IsText(t Type) bool

IsText returns true if Type is a text. If you have a Value object, use its member function.

func IsUnsigned

func IsUnsigned(t Type) bool

IsUnsigned returns true if Type is an unsigned integral. Caution: this is not the same as !IsSigned. If you have a Value object, use its member function.

func TypeToMySQL

func TypeToMySQL(typ Type) (mysqlType, flags int64)

TypeToMySQL returns the equivalent mysql type and flag for a vitess type.

Types

type BinWriter

type BinWriter interface {
	Write([]byte) (int, error)
}

BinWriter interface is used for encoding values. Types like bytes.Buffer conform to this interface. We expect the writer objects to be in-memory buffers. So, we don't expect the write operations to fail.

type Flag

type Flag int32
const (
	Flag_NONE       Flag = 0
	Flag_ISINTEGRAL Flag = 256
	Flag_ISUNSIGNED Flag = 512
	Flag_ISFLOAT    Flag = 1024
	Flag_ISQUOTED   Flag = 2048
	Flag_ISTEXT     Flag = 4096
	Flag_ISBINARY   Flag = 8192
)

type Row

type Row = []Value

type Type

type Type int32
const (
	// NULL_TYPE specifies a NULL type.
	Type_NULL_TYPE Type = 0
	// INT8 specifies a TINYINT type.
	// Properties: 1, IsNumber.
	Type_INT8 Type = 257
	// UINT8 specifies a TINYINT UNSIGNED type.
	// Properties: 2, IsNumber, IsUnsigned.
	Type_UINT8 Type = 770
	// INT16 specifies a SMALLINT type.
	// Properties: 3, IsNumber.
	Type_INT16 Type = 259
	// UINT16 specifies a SMALLINT UNSIGNED type.
	// Properties: 4, IsNumber, IsUnsigned.
	Type_UINT16 Type = 772
	// INT24 specifies a MEDIUMINT type.
	// Properties: 5, IsNumber.
	Type_INT24 Type = 261
	// UINT24 specifies a MEDIUMINT UNSIGNED type.
	// Properties: 6, IsNumber, IsUnsigned.
	Type_UINT24 Type = 774
	// INT32 specifies a INTEGER type.
	// Properties: 7, IsNumber.
	Type_INT32 Type = 263
	// UINT32 specifies a INTEGER UNSIGNED type.
	// Properties: 8, IsNumber, IsUnsigned.
	Type_UINT32 Type = 776
	// INT64 specifies a BIGINT type.
	// Properties: 9, IsNumber.
	Type_INT64 Type = 265
	// UINT64 specifies a BIGINT UNSIGNED type.
	// Properties: 10, IsNumber, IsUnsigned.
	Type_UINT64 Type = 778
	// FLOAT32 specifies a FLOAT type.
	// Properties: 11, IsFloat.
	Type_FLOAT32 Type = 1035
	// FLOAT64 specifies a DOUBLE or REAL type.
	// Properties: 12, IsFloat.
	Type_FLOAT64 Type = 1036
	// TIMESTAMP specifies a TIMESTAMP type.
	// Properties: 13, IsQuoted.
	Type_TIMESTAMP Type = 2061
	// DATE specifies a DATE type.
	// Properties: 14, IsQuoted.
	Type_DATE Type = 2062
	// TIME specifies a TIME type.
	// Properties: 15, IsQuoted.
	Type_TIME Type = 2063
	// DATETIME specifies a DATETIME type.
	// Properties: 16, IsQuoted.
	Type_DATETIME Type = 2064
	// YEAR specifies a YEAR type.
	// Properties: 17, IsNumber, IsUnsigned.
	Type_YEAR Type = 785
	// DECIMAL specifies a DECIMAL or NUMERIC type.
	// Properties: 18, None.
	Type_DECIMAL Type = 18
	// TEXT specifies a TEXT type.
	// Properties: 19, IsQuoted, IsText.
	Type_TEXT Type = 6163
	// BLOB specifies a BLOB type.
	// Properties: 20, IsQuoted, IsBinary.
	Type_BLOB Type = 10260
	// VARCHAR specifies a VARCHAR type.
	// Properties: 21, IsQuoted, IsText.
	Type_VARCHAR Type = 6165
	// VARBINARY specifies a VARBINARY type.
	// Properties: 22, IsQuoted, IsBinary.
	Type_VARBINARY Type = 10262
	// CHAR specifies a CHAR type.
	// Properties: 23, IsQuoted, IsText.
	Type_CHAR Type = 6167
	// BINARY specifies a BINARY type.
	// Properties: 24, IsQuoted, IsBinary.
	Type_BINARY Type = 10264
	// BIT specifies a BIT type.
	// Properties: 25, IsQuoted.
	Type_BIT Type = 2073
	// ENUM specifies an ENUM type.
	// Properties: 26, IsQuoted.
	Type_ENUM Type = 2074
	// SET specifies a SET type.
	// Properties: 27, IsQuoted.
	Type_SET Type = 2075
	// TUPLE specifies a tuple. This cannot
	// be returned in a QueryResult, but it can
	// be sent as a bind var.
	// Properties: 28, None.
	Type_TUPLE Type = 28
	// GEOMETRY specifies a GEOMETRY type.
	// Properties: 29, IsQuoted.
	Type_GEOMETRY Type = 2077
	// JSON specifies a JSON type.
	// Properties: 30, IsQuoted.
	Type_JSON Type = 2078
	// EXPRESSION specifies a SQL expression.
	// This type is for internal use only.
	// Properties: 31, None.
	Type_EXPRESSION Type = 31
	// HEXNUM specifies a HEXNUM type (unquoted varbinary).
	// Properties: 32, IsText.
	Type_HEXNUM Type = 4128
	// HEXVAL specifies a HEXVAL type (unquoted varbinary).
	// Properties: 33, IsText.
	Type_HEXVAL Type = 4129
)

func MySQLToType

func MySQLToType(mysqlType, flags int64) (typ Type, err error)

MySQLToType computes the vitess type from mysql type and flags.

func (Type) Enum

func (x Type) Enum() *Type

func (Type) String

func (x Type) String() string

type Value

type Value struct {
	// contains filtered or unexported fields
}

Value can store any SQL value. If the value represents an integral type, the bytes are always stored as a canonical representation that matches how MySQL returns such values.

func InterfaceToValue

func InterfaceToValue(goval any) (Value, error)

InterfaceToValue builds a value from a go type. Supported types are nil, int64, uint64, float64, string and []byte. This function is deprecated. Use the type-specific functions instead.

func MakeTrusted

func MakeTrusted(typ Type, val []byte) Value

MakeTrusted makes a new Value based on the type. This function should only be used if you know the value and type conform to the rules. Every place this function is called, a comment is needed that explains why it's justified. Exceptions: The current package and mysql package do not need comments. Other packages can also use the function to create VarBinary or VarChar values.

func NewDate

func NewDate(v string) Value

NewDate builds a Date value.

func NewDatetime

func NewDatetime(v string) Value

NewDatetime builds a Datetime value.

func NewDecimal

func NewDecimal(v string) Value

NewDecimal builds a Decimal value.

func NewFloat64

func NewFloat64(v float64) Value

NewFloat64 builds an Float64 Value.

func NewHexNum

func NewHexNum(v []byte) Value

NewHexNum builds an Hex Value.

func NewHexVal

func NewHexVal(v []byte) Value

NewHexVal builds a HexVal Value.

func NewInt32

func NewInt32(v int32) Value

NewInt32 builds an Int64 Value.

func NewInt64

func NewInt64(v int64) Value

NewInt64 builds an Int64 Value.

func NewInt8

func NewInt8(v int8) Value

NewInt8 builds an Int8 Value.

func NewIntegral

func NewIntegral(val string) (n Value, err error)

NewIntegral builds an integral type from a string representation. The type will be Int64 or Uint64. Int64 will be preferred where possible.

func NewTime

func NewTime(v string) Value

NewTime builds a Time value.

func NewTimestamp

func NewTimestamp(v string) Value

NewTimestamp builds a Timestamp value.

func NewUint32

func NewUint32(v uint32) Value

NewUint32 builds an Uint32 Value.

func NewUint64

func NewUint64(v uint64) Value

NewUint64 builds an Uint64 Value.

func NewValue

func NewValue(typ Type, val []byte) (v Value, err error)

NewValue builds a Value using typ and val. If the value and typ don't match, it returns an error.

func NewVarBinary

func NewVarBinary(v string) Value

NewVarBinary builds a VarBinary Value. The input is a string because it's the most common use case.

func NewVarChar

func NewVarChar(v string) Value

NewVarChar builds a VarChar Value.

func TestValue

func TestValue(typ Type, val string) Value

TestValue builds a Value from typ and val. This function should only be used for testing.

func (Value) EncodeASCII

func (v Value) EncodeASCII(b BinWriter)

EncodeASCII encodes the value using 7-bit clean ascii bytes.

func (Value) EncodeSQL

func (v Value) EncodeSQL(b BinWriter)

EncodeSQL encodes the value into an SQL statement. Can be binary.

func (Value) EncodeSQLBytes2

func (v Value) EncodeSQLBytes2(b *bytes2.Buffer)

EncodeSQLBytes2 is identical to EncodeSQL but it takes a bytes2.Buffer as its writer, so it can be inlined for performance.

func (Value) EncodeSQLStringBuilder

func (v Value) EncodeSQLStringBuilder(b *strings.Builder)

EncodeSQLStringBuilder is identical to EncodeSQL but it takes a strings.Builder as its writer, so it can be inlined for performance.

func (Value) IsBinary

func (v Value) IsBinary() bool

IsBinary returns true if Value is binary.

func (*Value) IsComparable

func (v *Value) IsComparable() bool

IsComparable returns true if the Value is null safe comparable without collation information.

func (Value) IsDateTime

func (v Value) IsDateTime() bool

IsDateTime returns true if Value is datetime.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat returns true if Value is a float.

func (Value) IsIntegral

func (v Value) IsIntegral() bool

IsIntegral returns true if Value is an integral.

func (Value) IsNull

func (v Value) IsNull() bool

IsNull returns true if Value is null.

func (Value) IsQuoted

func (v Value) IsQuoted() bool

IsQuoted returns true if Value must be SQL-quoted.

func (Value) IsSigned

func (v Value) IsSigned() bool

IsSigned returns true if Value is a signed integral.

func (Value) IsText

func (v Value) IsText() bool

IsText returns true if Value is a collatable text.

func (Value) IsUnsigned

func (v Value) IsUnsigned() bool

IsUnsigned returns true if Value is an unsigned integral.

func (Value) Len

func (v Value) Len() int

Len returns the length.

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

MarshalJSON should only be used for testing. It's not a complete implementation.

func (Value) Raw

func (v Value) Raw() []byte

Raw returns the internal representation of the value. For newer types, this may not match MySQL's representation.

func (Value) RawStr

func (v Value) RawStr() string

RawStr returns the internal representation of the value as a string instead of a byte slice. This is equivalent to calling `string(v.Raw())` but does not allocate.

func (Value) String

func (v Value) String() string

String returns a printable version of the value.

func (Value) ToBool

func (v Value) ToBool() (bool, error)

ToBool returns the value as a bool value

func (Value) ToBytes

func (v Value) ToBytes() ([]byte, error)

ToBytes returns the value as MySQL would return it as []byte. In contrast, Raw returns the internal representation of the Value, which may not match MySQL's representation for hex encoded binary data or newer types. If the value is not convertible like in the case of Expression, it returns an error.

func (Value) ToFloat64

func (v Value) ToFloat64() (float64, error)

ToFloat64 returns the value as MySQL would return it as a float64.

func (Value) ToInt64

func (v Value) ToInt64() (int64, error)

ToInt64 returns the value as MySQL would return it as a int64.

func (Value) ToString

func (v Value) ToString() string

ToString returns the value as MySQL would return it as string. If the value is not convertible like in the case of Expression, it returns nil.

func (Value) ToUint64

func (v Value) ToUint64() (uint64, error)

ToUint64 returns the value as MySQL would return it as a uint64.

func (Value) Type

func (v Value) Type() Type

Type returns the type of Value.

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(b []byte) error

UnmarshalJSON should only be used for testing. It's not a complete implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL