types

package
v0.0.0-...-3056823 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2016 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KindNull  byte = 0
	KindInt64 byte = iota + 1
	KindUint64
	KindFloat32
	KindFloat64
	KindString
	KindBytes
	KindMysqlBit
	KindMysqlDecimal
	KindMysqlDuration
	KindMysqlEnum
	KindMysqlHex
	KindMysqlSet
	KindMysqlTime
	KindRow
	KindInterface
	KindMinNotNull
	KindMaxValue
)

Kind constants.

View Source
const (
	UnspecifiedLength int = -1
)

UnspecifiedLength is unspecified length.

Variables

View Source
var ErrArithOverflow = errors.New("operation overflow")

ErrArithOverflow is the error for arthimetic operation overflow.

View Source
var (
	// ErrDataTooLong is returned when converts a string value that is longer than field type length.
	ErrDataTooLong = terror.ClassTypes.New(codeDataTooLong, "Data Too Long")
)
View Source
var (
	// ErrValueTruncated is used when a value has been truncated during conversion.
	ErrValueTruncated = errors.New("value has been truncated")
)

Functions

func AddInt64

func AddInt64(a int64, b int64) (int64, error)

AddInt64 adds int64 a and b if no overflow, otherwise returns error.

func AddInteger

func AddInteger(a uint64, b int64) (uint64, error)

AddInteger adds uint64 a and int64 b and returns uint64 if no overflow error.

func AddUint64

func AddUint64(a uint64, b uint64) (uint64, error)

AddUint64 adds uint64 a and b if no overflow, else returns error.

func Compare

func Compare(a, b interface{}) (int, error)

Compare returns an integer comparing the interface a with b. a > b -> 1 a = b -> 0 a < b -> -1

func CompareFloat64

func CompareFloat64(x, y float64) int

CompareFloat64 returns an integer comparing the float64 x to y.

func CompareInt64

func CompareInt64(x, y int64) int

CompareInt64 returns an integer comparing the int64 x to y.

func CompareString

func CompareString(x, y string) int

CompareString returns an integer comparing the string x to y.

func CompareUint64

func CompareUint64(x, y uint64) int

CompareUint64 returns an integer comparing the uint64 x to y.

func Convert

func Convert(val interface{}, target *FieldType) (v interface{}, err error)

Convert converts the val with type tp.

func ConvertDatumToDecimal

func ConvertDatumToDecimal(d Datum) (*mysql.MyDecimal, error)

ConvertDatumToDecimal converts datum to decimal.

func DatumsToInterfaces

func DatumsToInterfaces(datums []Datum) []interface{}

DatumsToInterfaces converts a datum slice to interface slice.

func DefaultCharsetForType

func DefaultCharsetForType(tp byte) (string, string)

DefaultCharsetForType returns the default charset/collation for mysql type.

func DefaultTypeForValue

func DefaultTypeForValue(value interface{}, tp *FieldType)

DefaultTypeForValue returns the default FieldType for the value.

func DivInt64

func DivInt64(a int64, b int64) (int64, error)

DivInt64 divides int64 a with b, returns int64 if no overflow error. It just checks overflow, if b is zero, a "divide by zero" panic throws.

func DivIntWithUint

func DivIntWithUint(a int64, b uint64) (uint64, error)

DivIntWithUint divides int64 a with uint64 b, returns uint64 if no overflow error. It just checks overflow, if b is zero, a "divide by zero" panic throws.

func DivUintWithInt

func DivUintWithInt(a uint64, b int64) (uint64, error)

DivUintWithInt divides uint64 a with int64 b, returns uint64 if no overflow error. It just checks overflow, if b is zero, a "divide by zero" panic throws.

func EOFAsNil

func EOFAsNil(err error) error

EOFAsNil filtrates errors, If err is equal to io.EOF returns nil.

func EqualDatums

func EqualDatums(a []Datum, b []Datum) (bool, error)

EqualDatums compare if a and b contains the same datum values.

func InvOp2

func InvOp2(x, y interface{}, o opcode.Op) (interface{}, error)

InvOp2 returns an invalid operation error.

func IsTypeBlob

func IsTypeBlob(tp byte) bool

IsTypeBlob returns a boolean indicating whether the tp is a blob type.

func IsTypeChar

func IsTypeChar(tp byte) bool

IsTypeChar returns a boolean indicating whether the tp is the char type like a string type or a varchar type.

func MergeFieldType

func MergeFieldType(a byte, b byte) byte

MergeFieldType merges two MySQL type to a new type. This is used in hybrid field type expression. For example "select case c when 1 then 2 when 2 then 'tidb' from t;" The result field type of the case expression is the merged type of the two when clause. See https://github.com/mysql/mysql-server/blob/5.7/sql/field.cc#L1042

func MulInt64

func MulInt64(a int64, b int64) (int64, error)

MulInt64 multiplies int64 a and b and returns int64 if no overflow error.

func MulInteger

func MulInteger(a uint64, b int64) (uint64, error)

MulInteger multiplies uint64 a and int64 b, and returns uint64 if no overflow error.

func MulUint64

func MulUint64(a uint64, b uint64) (uint64, error)

MulUint64 multiplies uint64 a and b and returns uint64 if no overflow error.

func Round

func Round(f float64, dec int) float64

Round rounds the argument f to dec decimal places. dec defaults to 0 if not specified. dec can be negative to cause dec digits left of the decimal point of the value f to become zero.

func RoundFloat

func RoundFloat(f float64) float64

RoundFloat rounds float val to the nearest integer value with float64 format, like MySQL Round function. RoundFloat uses default rounding mode, see https://dev.mysql.com/doc/refman/5.7/en/precision-math-rounding.html so rounding use "round half away from zero". e.g, 1.5 -> 2, -1.5 -> -2.

func SortDatums

func SortDatums(datums []Datum) error

SortDatums sorts a slice of datum.

func StrToFloat

func StrToFloat(str string) (float64, error)

StrToFloat converts a string to a float64 in best effort.

func StrToInt

func StrToInt(str string) (int64, error)

StrToInt converts a string to an integer in best effort. TODO: handle overflow and add unittest.

func SubInt64

func SubInt64(a int64, b int64) (int64, error)

SubInt64 subtracts int64 a with b and returns int64 if no overflow error.

func SubIntWithUint

func SubIntWithUint(a int64, b uint64) (uint64, error)

SubIntWithUint subtracts int64 a with uint64 b and returns uint64 if no overflow error.

func SubUint64

func SubUint64(a uint64, b uint64) (uint64, error)

SubUint64 subtracts uint64 a with b and returns uint64 if no overflow error.

func SubUintWithInt

func SubUintWithInt(a uint64, b int64) (uint64, error)

SubUintWithInt subtracts uint64 a with int64 b and returns uint64 if no overflow error.

func ToString

func ToString(value interface{}) (string, error)

ToString converts an interface to a string.

func TruncateFloat

func TruncateFloat(f float64, flen int, decimal int) (float64, error)

TruncateFloat tries to truncate f. If the result exceeds the max/min float that flen/decimal allowed, returns the max/min float allowed.

func TypeStr

func TypeStr(tp byte) (r string)

TypeStr converts tp to a string.

func TypeToStr

func TypeToStr(tp byte, cs string) (r string)

TypeToStr converts a field to a string. It is used for converting Text to Blob, or converting Char to Binary. Args:

tp: type enum
cs: charset

Types

type Datum

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

Datum is a data box holds different kind of data. It has better performance and is easier to use than `interface{}`.

func CalculateSum

func CalculateSum(sum Datum, v Datum) (Datum, error)

CalculateSum adds v to sum.

func CoerceArithmetic

func CoerceArithmetic(a Datum) (d Datum, err error)

CoerceArithmetic converts datum to appropriate datum for arithmetic computing.

func CoerceDatum

func CoerceDatum(a, b Datum) (x, y Datum, err error)

CoerceDatum changes type. If a or b is Float, changes the both to Float. Else if a or b is Decimal, changes the both to Decimal. Else if a or b is Uint and op is not div, mod, or intDiv changes the both to Uint.

func ComputeDiv

func ComputeDiv(a, b Datum) (d Datum, err error)

ComputeDiv computes the result of a/b.

func ComputeIntDiv

func ComputeIntDiv(a, b Datum) (d Datum, err error)

ComputeIntDiv computes the result of a / b, both a and b are integer.

func ComputeMinus

func ComputeMinus(a, b Datum) (d Datum, err error)

ComputeMinus computes the result of a-b.

func ComputeMod

func ComputeMod(a, b Datum) (d Datum, err error)

ComputeMod computes the result of a mod b.

func ComputeMul

func ComputeMul(a, b Datum) (d Datum, err error)

ComputeMul computes the result of a*b.

func ComputePlus

func ComputePlus(a, b Datum) (d Datum, err error)

ComputePlus computes the result of a+b.

func MakeDatums

func MakeDatums(args ...interface{}) []Datum

MakeDatums creates datum slice from interfaces.

func MaxValueDatum

func MaxValueDatum() Datum

MaxValueDatum returns a datum represents max value.

func MinNotNullDatum

func MinNotNullDatum() Datum

MinNotNullDatum returns a datum represents minimum not null value.

func NewBytesDatum

func NewBytesDatum(b []byte) (d Datum)

NewBytesDatum creates a new Datum from a byte slice.

func NewDatum

func NewDatum(in interface{}) (d Datum)

NewDatum creates a new Datum from an interface{}.

func NewDecimalDatum

func NewDecimalDatum(dec *mysql.MyDecimal) (d Datum)

NewDecimalDatum creates a new Datum form a mysql.Decimal value.

func NewDurationDatum

func NewDurationDatum(dur mysql.Duration) (d Datum)

NewDurationDatum creates a new Datum from a mysql.Duration value.

func NewFloat32Datum

func NewFloat32Datum(f float32) (d Datum)

NewFloat32Datum creates a new Datum from a float32 value.

func NewFloat64Datum

func NewFloat64Datum(f float64) (d Datum)

NewFloat64Datum creates a new Datum from a float64 value.

func NewIntDatum

func NewIntDatum(i int64) (d Datum)

NewIntDatum creates a new Datum from an int64 value.

func NewStringDatum

func NewStringDatum(s string) (d Datum)

NewStringDatum creates a new Datum from a string.

func NewUintDatum

func NewUintDatum(i uint64) (d Datum)

NewUintDatum creates a new Datum from an uint64 value.

func (*Datum) Cast

func (d *Datum) Cast(target *FieldType) (ad Datum, err error)

Cast casts datum to certain types.

func (*Datum) Collation

func (d *Datum) Collation() byte

Collation gets the collation of the datum.

func (*Datum) CompareDatum

func (d *Datum) CompareDatum(ad Datum) (int, error)

CompareDatum compares datum to another datum. TODO: return error properly.

func (*Datum) ConvertTo

func (d *Datum) ConvertTo(target *FieldType) (Datum, error)

ConvertTo converts a datum to the target field type.

func (*Datum) Frac

func (d *Datum) Frac() int

Frac gets the frac of the datum.

func (*Datum) GetBytes

func (d *Datum) GetBytes() []byte

GetBytes gets bytes value.

func (*Datum) GetFloat32

func (d *Datum) GetFloat32() float32

GetFloat32 gets float32 value.

func (*Datum) GetFloat64

func (d *Datum) GetFloat64() float64

GetFloat64 gets float64 value.

func (*Datum) GetInt64

func (d *Datum) GetInt64() int64

GetInt64 gets int64 value.

func (*Datum) GetInterface

func (d *Datum) GetInterface() interface{}

GetInterface gets interface value.

func (*Datum) GetMysqlBit

func (d *Datum) GetMysqlBit() mysql.Bit

GetMysqlBit gets mysql.Bit value

func (*Datum) GetMysqlDecimal

func (d *Datum) GetMysqlDecimal() *mysql.MyDecimal

GetMysqlDecimal gets mysql.Decimal value

func (*Datum) GetMysqlDuration

func (d *Datum) GetMysqlDuration() mysql.Duration

GetMysqlDuration gets mysql.Duration value

func (*Datum) GetMysqlEnum

func (d *Datum) GetMysqlEnum() mysql.Enum

GetMysqlEnum gets mysql.Enum value

func (*Datum) GetMysqlHex

func (d *Datum) GetMysqlHex() mysql.Hex

GetMysqlHex gets mysql.Hex value

func (*Datum) GetMysqlSet

func (d *Datum) GetMysqlSet() mysql.Set

GetMysqlSet gets mysql.Set value

func (*Datum) GetMysqlTime

func (d *Datum) GetMysqlTime() mysql.Time

GetMysqlTime gets mysql.Time value

func (*Datum) GetRow

func (d *Datum) GetRow() []Datum

GetRow gets row value.

func (*Datum) GetString

func (d *Datum) GetString() string

GetString gets string value.

func (*Datum) GetUint64

func (d *Datum) GetUint64() uint64

GetUint64 gets uint64 value.

func (*Datum) GetValue

func (d *Datum) GetValue() interface{}

GetValue gets the value of the datum of any kind.

func (*Datum) IsNull

func (d *Datum) IsNull() bool

IsNull checks if datum is null.

func (*Datum) Kind

func (d *Datum) Kind() byte

Kind gets the kind of the datum.

func (*Datum) Length

func (d *Datum) Length() int

Length gets the length of the datum.

func (*Datum) SetBytes

func (d *Datum) SetBytes(b []byte)

SetBytes sets bytes value to datum.

func (*Datum) SetBytesAsString

func (d *Datum) SetBytesAsString(b []byte)

SetBytesAsString sets bytes value to datum as string type.

func (*Datum) SetCollation

func (d *Datum) SetCollation(collation byte)

SetCollation sets the collation of the datum.

func (*Datum) SetFloat32

func (d *Datum) SetFloat32(f float32)

SetFloat32 sets float32 value.

func (*Datum) SetFloat64

func (d *Datum) SetFloat64(f float64)

SetFloat64 sets float64 value.

func (*Datum) SetFrac

func (d *Datum) SetFrac(frac int)

SetFrac sets the frac of the datum.

func (*Datum) SetInt64

func (d *Datum) SetInt64(i int64)

SetInt64 sets int64 value.

func (*Datum) SetInterface

func (d *Datum) SetInterface(x interface{})

SetInterface sets interface to datum.

func (*Datum) SetLength

func (d *Datum) SetLength(l int)

SetLength sets the length of the datum

func (*Datum) SetMysqlBit

func (d *Datum) SetMysqlBit(b mysql.Bit)

SetMysqlBit sets mysql.Bit value

func (*Datum) SetMysqlDecimal

func (d *Datum) SetMysqlDecimal(b *mysql.MyDecimal)

SetMysqlDecimal sets mysql.Decimal value

func (*Datum) SetMysqlDuration

func (d *Datum) SetMysqlDuration(b mysql.Duration)

SetMysqlDuration sets mysql.Duration value

func (*Datum) SetMysqlEnum

func (d *Datum) SetMysqlEnum(b mysql.Enum)

SetMysqlEnum sets mysql.Enum value

func (*Datum) SetMysqlHex

func (d *Datum) SetMysqlHex(b mysql.Hex)

SetMysqlHex sets mysql.Hex value

func (*Datum) SetMysqlSet

func (d *Datum) SetMysqlSet(b mysql.Set)

SetMysqlSet sets mysql.Set value

func (*Datum) SetMysqlTime

func (d *Datum) SetMysqlTime(b mysql.Time)

SetMysqlTime sets mysql.Time value

func (*Datum) SetNull

func (d *Datum) SetNull()

SetNull sets datum to nil.

func (*Datum) SetRow

func (d *Datum) SetRow(ds []Datum)

SetRow sets row value.

func (*Datum) SetString

func (d *Datum) SetString(s string)

SetString sets string value.

func (*Datum) SetUint64

func (d *Datum) SetUint64(i uint64)

SetUint64 sets uint64 value.

func (*Datum) SetValue

func (d *Datum) SetValue(val interface{})

SetValue sets any kind of value.

func (*Datum) ToBool

func (d *Datum) ToBool() (int64, error)

ToBool converts to a bool. We will use 1 for true, and 0 for false.

func (*Datum) ToDecimal

func (d *Datum) ToDecimal() (*mysql.MyDecimal, error)

ToDecimal converts to a decimal.

func (*Datum) ToFloat64

func (d *Datum) ToFloat64() (float64, error)

ToFloat64 converts to a float64

func (*Datum) ToInt64

func (d *Datum) ToInt64() (int64, error)

ToInt64 converts to a int64.

func (*Datum) ToString

func (d *Datum) ToString() (string, error)

ToString gets the string representation of the datum.

type FieldType

type FieldType struct {
	Tp      byte
	Flag    uint
	Flen    int
	Decimal int
	Charset string
	Collate string
	// Elems is the element list for enum and set type.
	Elems []string
}

FieldType records field type information.

func NewFieldType

func NewFieldType(tp byte) *FieldType

NewFieldType returns a FieldType, with a type and other information about field type.

func (*FieldType) CompactStr

func (ft *FieldType) CompactStr() string

CompactStr only considers Tp/CharsetBin/Flen/Deimal. This is used for showing column type in infoschema.

func (*FieldType) Init

func (ft *FieldType) Init(tp byte)

Init initializes the FieldType data.

func (*FieldType) String

func (ft *FieldType) String() string

String joins the information of FieldType and returns a string.

Jump to

Keyboard shortcuts

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