sqltypes

package
v0.0.0-...-47a6aab Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2017 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package sqltypes implements interfaces and types that represent SQL values.

Index

Constants

// IsText returns true if querypb.Type is a text.

func IsText(t querypb.Type) bool {
	return int(t)&flagIsText == flagIsText
}

// IsBinary returns true if querypb.Type is a binary.

func IsBinary(t querypb.Type) bool {
	return int(t)&flagIsBinary == flagIsBinary
}

// isNumber returns true if the type is any type of number.

func isNumber(t querypb.Type) bool {
	return IsIntegral(t) || IsFloat(t) || t == Decimal
}

Vitess data types. These are idiomatically named synonyms for the querypb.Type values.

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 SQLDecodeMap [256]byte

SQLDecodeMap is the reverse of SQLEncodeMap

View Source
var SQLEncodeMap [256]byte
func encodeBytesASCII(val []byte, b BinWriter) {
	buf := &bytes2.Buffer{}
	buf.WriteByte('\'')
	encoder := base64.NewEncoder(base64.StdEncoding, buf)
	encoder.Write(val)
	encoder.Close()
	buf.WriteByte('\'')
	b.Write(buf.Bytes())
}

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

Functions

func IsFloat

func IsFloat(t querypb.Type) bool

IsFloat returns true is querypb.Type is a floating point.

func IsQuoted

func IsQuoted(t querypb.Type) bool

IsQuoted returns true if querypb.Type is a quoted text or binary.

func IsSigned

func IsSigned(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.

func IsIntegral(t querypb.Type) bool {
	return int(t)&flagIsIntegral == flagIsIntegral
}

IsSigned returns true if querypb.Type is a signed integral.

func IsTypeValid

func IsTypeValid(typ querypb.Type) bool

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

func TypeToMySQL(typ querypb.Type) (mysqlType, flags int64) {
	val := typeToMySQL[typ]
	return val.typ, val.flags
}

IsTypeValid returns true if the type is valid.

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.

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 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 cannonical representation that matches how MySQL returns such values.

func BuildIntegral

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

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

func BuildValue

func BuildValue(goval interface{}) (v Value, err error)

BuildValue builds a value from any go type. sqltype.Value is also allowed.

func MakeString

func MakeString(val []byte) Value

MakeString makes a VarBinary Value.

func MakeTrusted

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

MakeTrusted makes a new Value based on the type. If the value is an integral, then val must be in its cannonical form. 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. Functions within this package are exempt.

func ValueFromBytes

func ValueFromBytes(typ querypb.Type, val []byte) (v Value, err error)

// BuildConverted is like BuildValue except that it tries to // convert a string or []byte to an integral if the target type // is an integral. We don't perform other implicit conversions // because they're unsafe.

func BuildConverted(typ querypb.Type, goval interface{}) (v Value, err error) {
	if IsIntegral(typ) {
		switch goval := goval.(type) {
		case []byte:
			return ValueFromBytes(typ, goval)
		case string:
			return ValueFromBytes(typ, []byte(goval))
		case Value:
			if goval.IsQuoted() {
				return ValueFromBytes(typ, goval.Raw())
			}
		case *querypb.BindVariable:
			if IsQuoted(goval.Type) {
				return ValueFromBytes(typ, goval.Value)
			}
		}
	}
	// TODO(sougou): We should not call BuildValue here, because it
	// may build a Value of a different type than requested. Things
	// work for now because the value is eventually just passed through.
	return BuildValue(goval)
}

ValueFromBytes builds a Value using typ and val. It ensures that val matches the requested type. If type is an integral it's converted to a cannonical form. Otherwise, the original representation is preserved.

func (Value) EncodeSQL

func (v Value) EncodeSQL(b BinWriter)

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

func (Value) IsQuoted

func (v Value) IsQuoted() bool

// IsFloat returns true if Value is a float.

func (v Value) IsFloat() bool {
	return IsFloat(v.typ)
}

IsQuoted returns true if Value must be SQL-quoted.

func (Value) IsSigned

func (v Value) IsSigned() bool

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

func (v Value) EncodeASCII(b BinWriter) {
	// ToNative panics if v is invalid.
	_ = v.ToNative()
	switch {
	case v.typ == Null:
		b.Write(nullstr)
	case IsQuoted(v.typ):
		encodeBytesASCII(v.val, b)
	default:
		b.Write(v.val)
	}
}

// IsNull returns true if Value is null.

func (v Value) IsNull() bool {
	return v.typ == Null
}

// IsIntegral returns true if Value is an integral.

func (v Value) IsIntegral() bool {
	return IsIntegral(v.typ)
}

IsSigned returns true if Value is a signed integral.

func (Value) IsUnsigned

func (v Value) IsUnsigned() bool

IsUnsigned returns true if Value is an unsigned integral.

func (Value) ParseFloat64

func (v Value) ParseFloat64() (val float64, err error)

ParseFloat64 will parse a Value into an float64. It does not check the type. TODO(sougou): deprecate this function in favor of a more type-aware implemention in arithmetic.

func (Value) ParseInt64

func (v Value) ParseInt64() (val int64, err error)

// ToProtoValue converts Value to a querypb.Value.

func (v Value) ToProtoValue() *querypb.Value {
	return &querypb.Value{
		Type:  v.typ,
		Value: v.val,
	}
}

ParseInt64 will parse a Value into an int64. It does not check the type. TODO(sougou): deprecate this function in favor of a more type-aware implemention in arithmetic.

func (Value) ParseUint64

func (v Value) ParseUint64() (val uint64, err error)

ParseUint64 will parse a Value into a uint64. It does not check the type. TODO(sougou): deprecate this function in favor of a more type-aware implemention in arithmetic.

func (Value) String

func (v Value) String() string

// Type returns the type of Value.

func (v Value) Type() querypb.Type {
	return v.typ
}

// Raw returns the raw bytes. All types are currently implemented as []byte. // You should avoid using this function. If you do, you should treat the // bytes as read-only.

func (v Value) Raw() []byte {
	return v.val
}

// Len returns the length.

func (v Value) Len() int {
	return len(v.val)
}

String returns the raw value as a string.

func (Value) ToNative

func (v Value) ToNative() interface{}

ToNative converts Value to a native go type. This does not work for sqltypes.Tuple. The function panics if there are inconsistencies.

Jump to

Keyboard shortcuts

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