当前位置: 首页>>代码示例>>Golang>>正文


Golang hack.Slice函数代码示例

本文整理汇总了Golang中github.com/pingcap/tidb/util/hack.Slice函数的典型用法代码示例。如果您正苦于以下问题:Golang Slice函数的具体用法?Golang Slice怎么用?Golang Slice使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Slice函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: dumpTextValue

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)
	}
}
开发者ID:pingcap,项目名称:tidb,代码行数:30,代码来源:util.go

示例2: toDecimal

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
}
开发者ID:pingcap,项目名称:tidb,代码行数:9,代码来源:yy_parser.go

示例3: dumpTextValue

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)
	}
}
开发者ID:sumpan,项目名称:tidb,代码行数:42,代码来源:util.go

示例4: builtinHex

// 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())
	}
}
开发者ID:pingcap,项目名称:tidb,代码行数:21,代码来源:builtin_string.go

示例5: SetMysqlSet

// 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)
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:7,代码来源:datum.go

示例6: SetMysqlEnum

// 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)
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:7,代码来源:datum.go

示例7: SetString

// SetString sets string value.
func (d *Datum) SetString(s string) {
	d.k = KindString
	sink(s)
	d.b = hack.Slice(s)
}
开发者ID:yangxuanjia,项目名称:tidb,代码行数:6,代码来源:datum.go

示例8: dumpRowValuesBinary

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
}
开发者ID:lovedboy,项目名称:tidb,代码行数:72,代码来源:util.go

示例9: dumpRowValuesBinary

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
}
开发者ID:pingcap,项目名称:tidb,代码行数:68,代码来源:util.go


注:本文中的github.com/pingcap/tidb/util/hack.Slice函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。