Exemplo n.º 1
0
Arquivo: util.go Projeto: pingcap/tidb
func dumpTextValue(mysqlType uint8, value types.Datum) ([]byte, error) {
	switch value.Kind() {
	case types.KindInt64:
		return strconv.AppendInt(nil, value.GetInt64(), 10), nil
	case types.KindUint64:
		return strconv.AppendUint(nil, value.GetUint64(), 10), nil
	case types.KindFloat32:
		return strconv.AppendFloat(nil, value.GetFloat64(), 'f', -1, 32), nil
	case types.KindFloat64:
		return strconv.AppendFloat(nil, value.GetFloat64(), 'f', -1, 64), nil
	case types.KindString, types.KindBytes:
		return value.GetBytes(), nil
	case types.KindMysqlTime:
		return hack.Slice(value.GetMysqlTime().String()), nil
	case types.KindMysqlDuration:
		return hack.Slice(value.GetMysqlDuration().String()), nil
	case types.KindMysqlDecimal:
		return hack.Slice(value.GetMysqlDecimal().String()), nil
	case types.KindMysqlEnum:
		return hack.Slice(value.GetMysqlEnum().String()), nil
	case types.KindMysqlSet:
		return hack.Slice(value.GetMysqlSet().String()), nil
	case types.KindMysqlBit:
		return hack.Slice(value.GetMysqlBit().ToString()), nil
	case types.KindMysqlHex:
		return hack.Slice(value.GetMysqlHex().ToString()), nil
	default:
		return nil, errInvalidType.Gen("invalid type %T", value)
	}
}
Exemplo n.º 2
0
func toDecimal(l yyLexer, lval *yySymType, str string) int {
	dec := new(types.MyDecimal)
	err := dec.FromString(hack.Slice(str))
	if err != nil {
		l.Errorf("decimal literal: %v", err)
	}
	lval.item = dec
	return decLit
}
Exemplo n.º 3
0
Arquivo: util.go Projeto: sumpan/tidb
func dumpTextValue(mysqlType uint8, value interface{}) ([]byte, error) {
	switch v := value.(type) {
	case int8:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int16:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int32:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int64:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case int:
		return strconv.AppendInt(nil, int64(v), 10), nil
	case uint8:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint16:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint32:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint64:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case uint:
		return strconv.AppendUint(nil, uint64(v), 10), nil
	case float32:
		return strconv.AppendFloat(nil, float64(v), 'f', -1, 64), nil
	case float64:
		return strconv.AppendFloat(nil, float64(v), 'f', -1, 64), nil
	case []byte:
		return v, nil
	case string:
		return hack.Slice(v), nil
	case mysql.Time:
		return hack.Slice(v.String()), nil
	case mysql.Duration:
		return hack.Slice(v.String()), nil
	case mysql.Decimal:
		return hack.Slice(v.String()), nil
	case mysql.Enum:
		return hack.Slice(v.String()), nil
	default:
		return nil, errors.Errorf("invalid type %T", value)
	}
}
Exemplo n.º 4
0
// See http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_hex
func builtinHex(args []types.Datum, ctx context.Context) (d types.Datum, err error) {
	switch args[0].Kind() {
	case types.KindNull:
		return d, nil
	case types.KindString:
		x, err := args[0].ToString()
		if err != nil {
			return d, errors.Trace(err)
		}
		d.SetString(strings.ToUpper(hex.EncodeToString(hack.Slice(x))))
		return d, nil
	case types.KindInt64, types.KindUint64, types.KindMysqlHex, types.KindFloat32, types.KindFloat64, types.KindMysqlDecimal:
		x, _ := args[0].Cast(ctx.GetSessionVars().StmtCtx, types.NewFieldType(mysql.TypeLonglong))
		h := fmt.Sprintf("%x", uint64(x.GetInt64()))
		d.SetString(strings.ToUpper(h))
		return d, nil
	default:
		return d, errors.Errorf("Hex invalid args, need int or string but get %T", args[0].GetValue())
	}
}
Exemplo n.º 5
0
// SetMysqlSet sets mysql.Set value
func (d *Datum) SetMysqlSet(b mysql.Set) {
	d.k = KindMysqlSet
	d.i = int64(b.Value)
	sink(b.Name)
	d.b = hack.Slice(b.Name)
}
Exemplo n.º 6
0
// SetMysqlEnum sets mysql.Enum value
func (d *Datum) SetMysqlEnum(b mysql.Enum) {
	d.k = KindMysqlEnum
	d.i = int64(b.Value)
	sink(b.Name)
	d.b = hack.Slice(b.Name)
}
Exemplo n.º 7
0
// SetString sets string value.
func (d *Datum) SetString(s string) {
	d.k = KindString
	sink(s)
	d.b = hack.Slice(s)
}
Exemplo n.º 8
0
func dumpRowValuesBinary(alloc arena.Allocator, columns []*ColumnInfo, row []interface{}) (data []byte, err error) {
	if len(columns) != len(row) {
		err = mysql.ErrMalformPacket
		return
	}
	data = append(data, mysql.OKHeader)
	nullsLen := ((len(columns) + 7 + 2) / 8)
	nulls := make([]byte, nullsLen)
	for i, val := range row {
		if val == nil {
			bytePos := (i + 2) / 8
			bitPos := byte((i + 2) % 8)
			nulls[bytePos] |= 1 << bitPos
		}
	}
	data = append(data, nulls...)
	for i, val := range row {
		val = uniformValue(val)
		switch v := val.(type) {
		case int64:
			switch columns[i].Type {
			case mysql.TypeTiny:
				data = append(data, byte(v))
			case mysql.TypeShort, mysql.TypeYear:
				data = append(data, dumpUint16(uint16(v))...)
			case mysql.TypeInt24, mysql.TypeLong:
				data = append(data, dumpUint32(uint32(v))...)
			case mysql.TypeLonglong:
				data = append(data, dumpUint64(uint64(v))...)
			}
		case uint64:
			switch columns[i].Type {
			case mysql.TypeTiny:
				data = append(data, byte(v))
			case mysql.TypeShort, mysql.TypeYear:
				data = append(data, dumpUint16(uint16(v))...)
			case mysql.TypeInt24, mysql.TypeLong:
				data = append(data, dumpUint32(uint32(v))...)
			case mysql.TypeLonglong:
				data = append(data, dumpUint64(uint64(v))...)
			}
		case float32:
			floatBits := math.Float32bits(float32(val.(float64)))
			data = append(data, dumpUint32(floatBits)...)
		case float64:
			floatBits := math.Float64bits(val.(float64))
			data = append(data, dumpUint64(floatBits)...)
		case string:
			data = append(data, dumpLengthEncodedString(hack.Slice(v), alloc)...)
		case []byte:
			data = append(data, dumpLengthEncodedString(v, alloc)...)
		case mysql.Time:
			data = append(data, dumpBinaryDateTime(v, nil)...)
		case time.Time:
			myTime := mysql.Time{Time: v, Type: columns[i].Type, Fsp: mysql.DefaultFsp}
			data = append(data, dumpBinaryDateTime(myTime, nil)...)
		case mysql.Duration:
			data = append(data, dumpBinaryTime(v.Duration)...)
		case time.Duration:
			data = append(data, dumpBinaryTime(v)...)
		case mysql.Decimal:
			data = append(data, dumpLengthEncodedString(hack.Slice(v.String()), alloc)...)
		case mysql.Enum:
			data = append(data, dumpLengthEncodedString(hack.Slice(v.String()), alloc)...)
		case mysql.Set:
			data = append(data, dumpLengthEncodedString(hack.Slice(v.String()), alloc)...)
		case mysql.Bit:
			data = append(data, dumpLengthEncodedString(hack.Slice(v.ToString()), alloc)...)
		}
	}
	return
}
Exemplo n.º 9
0
Arquivo: util.go Projeto: pingcap/tidb
func dumpRowValuesBinary(alloc arena.Allocator, columns []*ColumnInfo, row []types.Datum) (data []byte, err error) {
	if len(columns) != len(row) {
		err = mysql.ErrMalformPacket
		return
	}
	data = append(data, mysql.OKHeader)
	nullsLen := ((len(columns) + 7 + 2) / 8)
	nulls := make([]byte, nullsLen)
	for i, val := range row {
		if val.IsNull() {
			bytePos := (i + 2) / 8
			bitPos := byte((i + 2) % 8)
			nulls[bytePos] |= 1 << bitPos
		}
	}
	data = append(data, nulls...)
	for i, val := range row {
		switch val.Kind() {
		case types.KindInt64:
			v := val.GetInt64()
			switch columns[i].Type {
			case mysql.TypeTiny:
				data = append(data, byte(v))
			case mysql.TypeShort, mysql.TypeYear:
				data = append(data, dumpUint16(uint16(v))...)
			case mysql.TypeInt24, mysql.TypeLong:
				data = append(data, dumpUint32(uint32(v))...)
			case mysql.TypeLonglong:
				data = append(data, dumpUint64(uint64(v))...)
			}
		case types.KindUint64:
			v := val.GetUint64()
			switch columns[i].Type {
			case mysql.TypeTiny:
				data = append(data, byte(v))
			case mysql.TypeShort, mysql.TypeYear:
				data = append(data, dumpUint16(uint16(v))...)
			case mysql.TypeInt24, mysql.TypeLong:
				data = append(data, dumpUint32(uint32(v))...)
			case mysql.TypeLonglong:
				data = append(data, dumpUint64(uint64(v))...)
			}
		case types.KindFloat32:
			floatBits := math.Float32bits(val.GetFloat32())
			data = append(data, dumpUint32(floatBits)...)
		case types.KindFloat64:
			floatBits := math.Float64bits(val.GetFloat64())
			data = append(data, dumpUint64(floatBits)...)
		case types.KindString, types.KindBytes:
			data = append(data, dumpLengthEncodedString(val.GetBytes(), alloc)...)
		case types.KindMysqlDecimal:
			data = append(data, dumpLengthEncodedString(hack.Slice(val.GetMysqlDecimal().String()), alloc)...)
		case types.KindMysqlTime:
			data = append(data, dumpBinaryDateTime(val.GetMysqlTime(), nil)...)
		case types.KindMysqlDuration:
			data = append(data, dumpBinaryTime(val.GetMysqlDuration().Duration)...)
		case types.KindMysqlSet:
			data = append(data, dumpLengthEncodedString(hack.Slice(val.GetMysqlSet().String()), alloc)...)
		case types.KindMysqlHex:
			data = append(data, dumpLengthEncodedString(hack.Slice(val.GetMysqlHex().ToString()), alloc)...)
		case types.KindMysqlEnum:
			data = append(data, dumpLengthEncodedString(hack.Slice(val.GetMysqlEnum().String()), alloc)...)
		case types.KindMysqlBit:
			data = append(data, dumpLengthEncodedString(hack.Slice(val.GetMysqlBit().ToString()), alloc)...)
		}
	}
	return
}