sqltypes

package
v0.0.0-...-982e07a Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0, Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package sqltypes implements interfaces and types that represent SQL values.

Index

Constants

Vitess data types. These are idiomatically named synonyms for the querypb.Type values. Although these constants are interchangeable, they should be treated as different from querypb.Type. Use the synonyms only to refer to the type in Value. For proto variables, use the querypb.Type constants instead. The following conditions are non-overlapping and cover all types: IsSigned(), IsUnsigned(), IsFloat(), IsQuoted(), Null, Decimal, Expression. Also, IsIntegral() == (IsSigned()||IsUnsigned()). TestCategory needs to be updated accordingly if you add a new type. If IsBinary or IsText is true, then IsQuoted is also true. But there are IsQuoted types that are neither binary or text. querypb.Type_TUPLE is not included in this list because it's not a valid Value 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 (
	// NULL represents the NULL value.
	NULL = Value{}

	// DontEscape tells you if a character should not be escaped.
	DontEscape = byte(255)
)
View Source
var NullBindVariable = &querypb.BindVariable{Type: querypb.Type_NULL_TYPE}

NullBindVariable is a bindvar with NULL value.

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 BindVariablesEqual

func BindVariablesEqual(x, y map[string]*querypb.BindVariable) bool

BindVariablesEqual compares two maps of bind variables.

func BuildBindVariable

func BuildBindVariable(v interface{}) (*querypb.BindVariable, error)

BuildBindVariable builds a *querypb.BindVariable from a valid input type.

func BuildBindVariables

func BuildBindVariables(in map[string]interface{}) (map[string]*querypb.BindVariable, error)

BuildBindVariables builds a map[string]*querypb.BindVariable from a map[string]interface{}.

func BytesBindVariable

func BytesBindVariable(v []byte) *querypb.BindVariable

BytesBindVariable converts a []byte to a bind var.

func CopyBindVariables

func CopyBindVariables(bindVariables map[string]*querypb.BindVariable) map[string]*querypb.BindVariable

CopyBindVariables returns a shallow-copy of the given bindVariables map.

func EncodeBytesSQLWithoutQuotes

func EncodeBytesSQLWithoutQuotes(val []byte) []byte

EncodeBytesSQLWithoutQuotes escape special symbols in byte value

func Float64BindVariable

func Float64BindVariable(v float64) *querypb.BindVariable

Float64BindVariable converts a float64 to a bind var.

func Int32BindVariable

func Int32BindVariable(v int32) *querypb.BindVariable

Int32BindVariable converts an int32 to a bind var.

func Int64BindVariable

func Int64BindVariable(v int64) *querypb.BindVariable

Int64BindVariable converts an int64 to a bind var.

func IsBinary

func IsBinary(t querypb.Type) bool

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

func IsFloat

func IsFloat(t querypb.Type) bool

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

func IsIntegral

func IsIntegral(t querypb.Type) bool

IsIntegral returns true if querypb.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 IsQuoted

func IsQuoted(t querypb.Type) bool

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

func IsSigned

func IsSigned(t querypb.Type) bool

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

func IsText

func IsText(t querypb.Type) bool

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

func IsUnsigned

func IsUnsigned(t querypb.Type) bool

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

func MySQLToType

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

MySQLToType computes the vitess type from mysql type and flags.

func ResolveRows

func ResolveRows(pvs []PlanValue, bindVars map[string]*querypb.BindVariable) ([][]Value, error)

ResolveRows resolves a []PlanValue as rows based on the supplied bindvars.

func StringBindVariable

func StringBindVariable(v string) *querypb.BindVariable

StringBindVariable converts a string to a bind var.

func TestBindVariable

func TestBindVariable(v interface{}) *querypb.BindVariable

TestBindVariable makes a *querypb.BindVariable from an interface{}.It panics on invalid input. This function should only be used for testing.

func TypeToMySQL

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

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

func Uint64BindVariable

func Uint64BindVariable(v uint64) *querypb.BindVariable

Uint64BindVariable converts a uint64 to a bind var.

func ValidateBindVariable

func ValidateBindVariable(bv *querypb.BindVariable) error

ValidateBindVariable returns an error if the bind variable has inconsistent fields.

func ValidateBindVariables

func ValidateBindVariables(bv map[string]*querypb.BindVariable) error

ValidateBindVariables validates a map[string]*querypb.BindVariable.

func ValueBindVariable

func ValueBindVariable(v Value) *querypb.BindVariable

ValueBindVariable converts a Value to a bind var.

func ValueToProto

func ValueToProto(v Value) *querypb.Value

ValueToProto converts Value to a *querypb.Value.

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 PlanValue

type PlanValue struct {
	Key     string
	Value   Value
	ListKey string
	Values  []PlanValue
}

PlanValue represents a value or a list of values for a column that will later be resolved using bind vars and used to perform plan actions like generating the final query or deciding on a route.

Plan values are typically used as a slice ([]planValue) where each entry is for one column. For situations where the required output is a list of rows (like in the case of multi-value inserts), the representation is pivoted. For example, a statement like this:

INSERT INTO t VALUES (1, 2), (3, 4)

will be represented as follows:

[]PlanValue{
	Values: {1, 3},
	Values: {2, 4},
}

For WHERE clause items that contain a combination of equality expressions and IN clauses like this:

WHERE pk1 = 1 AND pk2 IN (2, 3, 4)

The plan values will be represented as follows:

[]PlanValue{
	Value: 1,
	Values: {2, 3, 4},
}

When converted into rows, columns with single values are replicated as the same for all rows:

[][]Value{
	{1, 2},
	{1, 3},
	{1, 4},
}

func (PlanValue) IsList

func (pv PlanValue) IsList() bool

IsList returns true if the PlanValue is a list.

func (PlanValue) IsNull

func (pv PlanValue) IsNull() bool

IsNull returns true if the PlanValue is NULL.

func (PlanValue) MarshalJSON

func (pv PlanValue) MarshalJSON() ([]byte, error)

MarshalJSON should be used only for testing.

func (PlanValue) ResolveList

func (pv PlanValue) ResolveList(bindVars map[string]*querypb.BindVariable) ([]Value, error)

ResolveList resolves a PlanValue as a list of values based on the supplied bindvars.

func (PlanValue) ResolveValue

func (pv PlanValue) ResolveValue(bindVars map[string]*querypb.BindVariable) (Value, error)

ResolveValue resolves a PlanValue as a single value based on the supplied bindvars.

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 BindVariableToValue

func BindVariableToValue(bv *querypb.BindVariable) (Value, error)

BindVariableToValue converts a bind var into a Value.

func InterfaceToValue

func InterfaceToValue(goval interface{}) (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 querypb.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 NewFloat64

func NewFloat64(v float64) Value

NewFloat64 builds an Float64 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 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 NewUint64

func NewUint64(v uint64) Value

NewUint64 builds an Uint64 Value.

func NewValue

func NewValue(typ querypb.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 ProtoToValue

func ProtoToValue(v *querypb.Value) Value

ProtoToValue converts a *querypb.Value to a Value.

func TestValue

func TestValue(typ querypb.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) IsBinary

func (v Value) IsBinary() bool

IsBinary returns true if Value is binary.

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) String

func (v Value) String() string

String returns a printable version of the value.

func (Value) ToBytes

func (v Value) ToBytes() []byte

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 newer types. If the value is not convertible like in the case of Expression, it returns nil.

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) Type

func (v Value) Type() querypb.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