vitess: github.com/youtube/vitess/go/sqltypes Index | Files

package sqltypes

import "github.com/youtube/vitess/go/sqltypes"

Package sqltypes implements interfaces and types that represent SQL values.

Index

Package Files

arithmetic.go bind_variables.go event_token.go plan_value.go proto3.go query_response.go result.go testing.go type.go value.go

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
    Geometry   = querypb.Type_GEOMETRY
    TypeJSON   = querypb.Type_JSON
    Expression = querypb.Type_EXPRESSION
)

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

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

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

NullBindVariable is a bindvar with NULL value.

var SQLDecodeMap [256]byte

SQLDecodeMap is the reverse of SQLEncodeMap

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

func BindVariablesEqual Uses

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

BindVariablesEqual compares two maps of bind variables. For protobuf messages we have to use "proto.Equal".

func BuildBindVariable Uses

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

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

func BuildBindVariables Uses

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 Uses

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

BytesBindVariable converts a []byte to a bind var.

func CopyBindVariables Uses

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

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

func CopyRow Uses

func CopyRow(r []Value) []Value

CopyRow makes a copy of the row.

func EventTokenMinimum Uses

func EventTokenMinimum(ev1, ev2 *querypb.EventToken) *querypb.EventToken

EventTokenMinimum returns an event token that is guaranteed to happen before both provided EventToken objects. Note it doesn't parse the position, but rather only uses the timestamp. This is meant to be used for EventToken objects coming from different source shard.

func FieldsEqual Uses

func FieldsEqual(f1, f2 []*querypb.Field) bool

FieldsEqual compares two arrays of fields. reflect.DeepEqual shouldn't be used because of the protos.

func Float64BindVariable Uses

func Float64BindVariable(v float64) *querypb.BindVariable

Float64BindVariable converts a float64 to a bind var.

func IncludeFieldsOrDefault Uses

func IncludeFieldsOrDefault(options *querypb.ExecuteOptions) querypb.ExecuteOptions_IncludedFields

IncludeFieldsOrDefault normalizes the passed Execution Options. It returns the default value if options is nil.

func Int64BindVariable Uses

func Int64BindVariable(v int64) *querypb.BindVariable

Int64BindVariable converts an int64 to a bind var.

func IsBinary Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 MakeRowTrusted Uses

func MakeRowTrusted(fields []*querypb.Field, row *querypb.Row) []Value

MakeRowTrusted converts a *querypb.Row to []Value based on the types in fields. It does not sanity check the values against the type. Every place this function is called, a comment is needed that explains why it's justified.

func MakeTestFields Uses

func MakeTestFields(names, types string) []*querypb.Field

MakeTestFields builds a []*querypb.Field for testing.

fields := sqltypes.MakeTestFields(
  "a|b",
  "int64|varchar",
)

The field types are as defined in querypb and are case insensitive. Column delimiters must be used only to sepearate strings and not at the beginning or the end.

func MakeTestStreamingResults Uses

func MakeTestStreamingResults(fields []*querypb.Field, rows ...string) []*Result

MakeTestStreamingResults builds a list of results for streaming.

  results := sqltypes.MakeStreamingResults(
    fields,
		 "1|a",
    "2|b",
    "---",
    "c|c",
  )

The first result contains only the fields. Subsequent results are built using the field types. Every input that starts with a "-" is treated as streaming delimiter for one result. A final delimiter must not be supplied.

func MySQLToType Uses

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

MySQLToType computes the vitess type from mysql type and flags.

func NullsafeCompare Uses

func NullsafeCompare(v1, v2 Value) (int, error)

NullsafeCompare returns 0 if v1==v2, -1 if v1<v2, and 1 if v1>v2. NULL is the lowest value. If any value is numeric, then a numeric comparison is performed after necessary conversions. If none are numeric, then it's a simple binary comparison. Uncomparable values return an error.

func PrintResults Uses

func PrintResults(results []*Result) string

PrintResults prints []*Results into a string. This function should only be used for testing.

func Proto3QueryResponsesEqual Uses

func Proto3QueryResponsesEqual(r1, r2 []*querypb.ResultWithError) bool

Proto3QueryResponsesEqual compares two arrays of proto3 QueryResponse. reflect.DeepEqual shouldn't be used because of the protos.

func Proto3ResultsEqual Uses

func Proto3ResultsEqual(r1, r2 []*querypb.QueryResult) bool

Proto3ResultsEqual compares two arrays of proto3 Result. reflect.DeepEqual shouldn't be used because of the protos.

func Proto3ToQueryReponses Uses

func Proto3ToQueryReponses(qr []*querypb.ResultWithError) []QueryResponse

Proto3ToQueryReponses converts proto3 queryResponse to []QueryResponse.

func Proto3ToResults Uses

func Proto3ToResults(qr []*querypb.QueryResult) []Result

Proto3ToResults converts proto3 results to []Result.

func Proto3ValuesEqual Uses

func Proto3ValuesEqual(v1, v2 []*querypb.Value) bool

Proto3ValuesEqual compares two arrays of proto3 Value.

func QueryResponsesEqual Uses

func QueryResponsesEqual(r1, r2 []QueryResponse) bool

QueryResponsesEqual compares two arrays of QueryResponse. They contain protos, so we cannot use reflect.DeepEqual.

func QueryResponsesToProto3 Uses

func QueryResponsesToProto3(qr []QueryResponse) []*querypb.ResultWithError

QueryResponsesToProto3 converts []QueryResponse to proto3.

func ResolveRows Uses

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

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

func ResultToProto3 Uses

func ResultToProto3(qr *Result) *querypb.QueryResult

ResultToProto3 converts Result to proto3.

func ResultsEqual Uses

func ResultsEqual(r1, r2 []Result) bool

ResultsEqual compares two arrays of Result. reflect.DeepEqual shouldn't be used because of the protos.

func ResultsToProto3 Uses

func ResultsToProto3(qr []Result) []*querypb.QueryResult

ResultsToProto3 converts []Result to proto3.

func RowToProto3 Uses

func RowToProto3(row []Value) *querypb.Row

RowToProto3 converts []Value to proto3.

func RowsToProto3 Uses

func RowsToProto3(rows [][]Value) []*querypb.Row

RowsToProto3 converts [][]Value to proto3.

func SplitQueryResponsePartsEqual Uses

func SplitQueryResponsePartsEqual(s1, s2 []*vtgatepb.SplitQueryResponse_Part) bool

SplitQueryResponsePartsEqual compares two arrays of SplitQueryResponse_Part.

func StringBindVariable Uses

func StringBindVariable(v string) *querypb.BindVariable

StringBindVariable converts a string to a bind var.

func TestBindVariable Uses

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 ToFloat64 Uses

func ToFloat64(v Value) (float64, error)

ToFloat64 converts Value to float64.

func ToInt64 Uses

func ToInt64(v Value) (int64, error)

ToInt64 converts Value to uint64.

func ToNative Uses

func ToNative(v Value) (interface{}, error)

ToNative converts Value to a native go type. Decimal is returned as []byte.

func ToUint64 Uses

func ToUint64(v Value) (uint64, error)

ToUint64 converts Value to uint64.

func TypeToMySQL Uses

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

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

func Uint64BindVariable Uses

func Uint64BindVariable(v uint64) *querypb.BindVariable

Uint64BindVariable converts a uint64 to a bind var.

func ValidateBindVariable Uses

func ValidateBindVariable(bv *querypb.BindVariable) error

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

func ValidateBindVariables Uses

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

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

func ValueBindVariable Uses

func ValueBindVariable(v Value) *querypb.BindVariable

ValueBindVariable converts a Value to a bind var.

func ValueToProto Uses

func ValueToProto(v Value) *querypb.Value

ValueToProto converts Value to a *querypb.Value.

type BinWriter Uses

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 Uses

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 Uses

func (pv PlanValue) IsList() bool

IsList returns true if the PlanValue is a list.

func (PlanValue) IsNull Uses

func (pv PlanValue) IsNull() bool

IsNull returns true if the PlanValue is NULL.

func (PlanValue) MarshalJSON Uses

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

MarshalJSON should be used only for testing.

func (PlanValue) ResolveList Uses

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 Uses

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 QueryResponse Uses

type QueryResponse struct {
    QueryResult *Result
    QueryError  error
}

QueryResponse represents a query response for ExecuteBatch.

type Result Uses

type Result struct {
    Fields       []*querypb.Field      `json:"fields"`
    RowsAffected uint64                `json:"rows_affected"`
    InsertID     uint64                `json:"insert_id"`
    Rows         [][]Value             `json:"rows"`
    Extras       *querypb.ResultExtras `json:"extras"`
}

Result represents a query result.

func CustomProto3ToResult Uses

func CustomProto3ToResult(fields []*querypb.Field, qr *querypb.QueryResult) *Result

CustomProto3ToResult converts a proto3 Result to an internal data structure. This function takes a separate fields input because not all QueryResults contain the field info. In particular, only the first packet of streaming queries contain the field info.

func MakeTestResult Uses

func MakeTestResult(fields []*querypb.Field, rows ...string) *Result

MakeTestResult builds a *sqltypes.Result object for testing.

result := sqltypes.MakeTestResult(
  fields,
  " 1|a",
  "10|abcd",
)

The field type values are set as the types for the rows built. Spaces are trimmed from row values.

func Proto3ToResult Uses

func Proto3ToResult(qr *querypb.QueryResult) *Result

Proto3ToResult converts a proto3 Result to an internal data structure. This function should be used only if the field info is populated in qr.

func (*Result) AppendResult Uses

func (result *Result) AppendResult(src *Result)

AppendResult will combine the Results Objects of one result to another result.Note currently it doesn't handle cases like if two results have different fields.We will enhance this function.

func (*Result) Copy Uses

func (result *Result) Copy() *Result

Copy creates a deep copy of Result.

func (*Result) Equal Uses

func (result *Result) Equal(other *Result) bool

Equal compares the Result with another one. reflect.DeepEqual shouldn't be used because of the protos.

func (*Result) Repair Uses

func (result *Result) Repair(fields []*querypb.Field)

Repair fixes the type info in the rows to conform to the supplied field types.

func (*Result) StripMetadata Uses

func (result *Result) StripMetadata(incl querypb.ExecuteOptions_IncludedFields) *Result

StripMetadata will return a new Result that has the same Rows, but the Field objects will have their non-critical metadata emptied. Note we don't proto.Copy each Field for performance reasons, but we only copy the individual fields.

type ResultStream Uses

type ResultStream interface {
    // Recv returns the next result on the stream.
    // It will return io.EOF if the stream ended.
    Recv() (*Result, error)
}

ResultStream is an interface for receiving Result. It is used for RPC interfaces.

type Value Uses

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 Uses

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

BindVariableToValue converts a bind var into a Value.

func Cast Uses

func Cast(v Value, typ querypb.Type) (Value, error)

Cast converts a Value to the target type.

func InterfaceToValue Uses

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 Uses

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 Max Uses

func Max(v1, v2 Value) (Value, error)

Max returns the maximum of v1 and v2. If one of the values is NULL, it returns the other value. If both are NULL, it returns NULL.

func Min Uses

func Min(v1, v2 Value) (Value, error)

Min returns the minimum of v1 and v2. If one of the values is NULL, it returns the other value. If both are NULL, it returns NULL.

func NewFloat64 Uses

func NewFloat64(v float64) Value

NewFloat64 builds an Float64 Value.

func NewInt32 Uses

func NewInt32(v int32) Value

NewInt32 builds an Int64 Value.

func NewInt64 Uses

func NewInt64(v int64) Value

NewInt64 builds an Int64 Value.

func NewIntegral Uses

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 Uses

func NewUint64(v uint64) Value

NewUint64 builds an Uint64 Value.

func NewValue Uses

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 Uses

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 Uses

func NewVarChar(v string) Value

NewVarChar builds a VarChar Value.

func NullsafeAdd Uses

func NullsafeAdd(v1, v2 Value, resultType querypb.Type) (Value, error)

NullsafeAdd adds two Values in a null-safe manner. A null value is treated as 0. If both values are null, then a null is returned. If both values are not null, a numeric value is built from each input: Signed->int64, Unsigned->uint64, Float->float64. Otherwise the 'best type fit' is chosen for the number: int64 or float64. Addition is performed by upgrading types as needed, or in case of overflow: int64->uint64, int64->float64, uint64->float64. Unsigned ints can only be added to positive ints. After the addition, if one of the input types was Decimal, then a Decimal is built. Otherwise, the final type of the result is preserved.

func ProtoToValue Uses

func ProtoToValue(v *querypb.Value) Value

ProtoToValue converts a *querypb.Value to a Value.

func TestValue Uses

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 Uses

func (v Value) EncodeASCII(b BinWriter)

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

func (Value) EncodeSQL Uses

func (v Value) EncodeSQL(b BinWriter)

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

func (Value) IsBinary Uses

func (v Value) IsBinary() bool

IsBinary returns true if Value is binary.

func (Value) IsFloat Uses

func (v Value) IsFloat() bool

IsFloat returns true if Value is a float.

func (Value) IsIntegral Uses

func (v Value) IsIntegral() bool

IsIntegral returns true if Value is an integral.

func (Value) IsNull Uses

func (v Value) IsNull() bool

IsNull returns true if Value is null.

func (Value) IsQuoted Uses

func (v Value) IsQuoted() bool

IsQuoted returns true if Value must be SQL-quoted.

func (Value) IsSigned Uses

func (v Value) IsSigned() bool

IsSigned returns true if Value is a signed integral.

func (Value) IsText Uses

func (v Value) IsText() bool

IsText returns true if Value is a collatable text.

func (Value) IsUnsigned Uses

func (v Value) IsUnsigned() bool

IsUnsigned returns true if Value is an unsigned integral.

func (Value) Len Uses

func (v Value) Len() int

Len returns the length.

func (Value) MarshalJSON Uses

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

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

func (Value) Raw Uses

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 Uses

func (v Value) String() string

String returns a printable version of the value.

func (Value) ToBytes Uses

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 Uses

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 Uses

func (v Value) Type() querypb.Type

Type returns the type of Value.

func (*Value) UnmarshalJSON Uses

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

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

Package sqltypes imports 15 packages (graph) and is imported by 87 packages. Updated 2017-10-26. Refresh now. Tools for package owners.