Documentation ¶
Overview ¶
Package sqltypes implements interfaces and types that represent SQL values.
Index ¶
- Constants
- Variables
- func IsFloat(t querypb.Type) bool
- func IsQuoted(t querypb.Type) bool
- func IsSigned(t querypb.Type) bool
- func IsTypeValid(typ querypb.Type) bool
- func IsUnsigned(t querypb.Type) bool
- type BinWriter
- type Value
- func (v Value) EncodeSQL(b BinWriter)
- func (v Value) IsQuoted() bool
- func (v Value) IsSigned() bool
- func (v Value) IsUnsigned() bool
- func (v Value) ParseFloat64() (val float64, err error)
- func (v Value) ParseInt64() (val int64, err error)
- func (v Value) ParseUint64() (val uint64, err error)
- func (v Value) String() string
- func (v Value) ToNative() interface{}
Constants ¶
const ( Null = querypb.Type_NULL_TYPE Int8 = querypb.Type_INT8 Uint8 = querypb.Type_UINT8 Int16 = querypb.Type_INT16 Uint16 = querypb.Type_UINT16 Int24 = querypb.Type_INT24 Uint24 = querypb.Type_UINT24 Int32 = querypb.Type_INT32 Uint32 = querypb.Type_UINT32 Int64 = querypb.Type_INT64 Uint64 = querypb.Type_UINT64 Float32 = querypb.Type_FLOAT32 Float64 = querypb.Type_FLOAT64 Timestamp = querypb.Type_TIMESTAMP Date = querypb.Type_DATE Time = querypb.Type_TIME Datetime = querypb.Type_DATETIME Year = querypb.Type_YEAR Decimal = querypb.Type_DECIMAL Text = querypb.Type_TEXT Blob = querypb.Type_BLOB VarChar = querypb.Type_VARCHAR VarBinary = querypb.Type_VARBINARY Char = querypb.Type_CHAR Binary = querypb.Type_BINARY Bit = querypb.Type_BIT Enum = querypb.Type_ENUM Set = querypb.Type_SET Tuple = querypb.Type_TUPLE Geometry = querypb.Type_GEOMETRY TypeJSON = querypb.Type_JSON Expression = querypb.Type_EXPRESSION )
// 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 ¶
var ( // NULL represents the NULL value. NULL = Value{} // DontEscape tells you if a character should not be escaped. DontEscape = byte(255) )
var SQLDecodeMap [256]byte
SQLDecodeMap is the reverse of SQLEncodeMap
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 IsSigned ¶
// 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 ¶
// 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 ¶
IsUnsigned returns true if querypb.Type is an unsigned integral. Caution: this is not the same as !IsSigned.
Types ¶
type BinWriter ¶
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 ¶
BuildIntegral builds an integral type from a string representaion. The type will be Int64 or Uint64. Int64 will be preferred where possible.
func BuildValue ¶
BuildValue builds a value from any go type. sqltype.Value is also allowed.
func MakeTrusted ¶
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 ¶
// 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) IsQuoted ¶
// 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 ¶
// 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 ¶
IsUnsigned returns true if Value is an unsigned integral.
func (Value) ParseFloat64 ¶
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 ¶
// 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 ¶
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 ¶
// 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.