當前位置: 首頁>>代碼示例>>Golang>>正文


Golang types.ToInt64函數代碼示例

本文整理匯總了Golang中github.com/pingcap/tidb/util/types.ToInt64函數的典型用法代碼示例。如果您正苦於以下問題:Golang ToInt64函數的具體用法?Golang ToInt64怎麽用?Golang ToInt64使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了ToInt64函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: buildTableRanges

func (r *rangeBuilder) buildTableRanges(rangePoints []rangePoint) []TableRange {
	tableRanges := make([]TableRange, 0, len(rangePoints)/2)
	for i := 0; i < len(rangePoints); i += 2 {
		startPoint := rangePoints[i]
		if startPoint.value.Kind() == types.KindNull || startPoint.value.Kind() == types.KindMinNotNull {
			startPoint.value.SetInt64(math.MinInt64)
		}
		startInt, err := types.ToInt64(startPoint.value.GetValue())
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		startDatum := types.NewDatum(startInt)
		cmp, err := startDatum.CompareDatum(startPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		if cmp < 0 || (cmp == 0 && startPoint.excl) {
			startInt++
		}
		endPoint := rangePoints[i+1]
		if endPoint.value.Kind() == types.KindNull {
			endPoint.value.SetInt64(math.MinInt64)
		} else if endPoint.value.Kind() == types.KindMaxValue {
			endPoint.value.SetInt64(math.MaxInt64)
		}
		endInt, err := types.ToInt64(endPoint.value.GetValue())
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		endDatum := types.NewDatum(endInt)
		cmp, err = endDatum.CompareDatum(endPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		if cmp > 0 || (cmp == 0 && endPoint.excl) {
			endInt--
		}
		if startInt > endInt {
			continue
		}
		tableRanges = append(tableRanges, TableRange{LowVal: startInt, HighVal: endInt})
	}
	return tableRanges
}
開發者ID:astaxie,項目名稱:tidb,代碼行數:48,代碼來源:range.go

示例2: builtinAbs

func builtinAbs(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
	switch x := args[0].(type) {
	case nil:
		return nil, nil
	case uint, uint8, uint16, uint32, uint64:
		return x, nil
	case int, int8, int16, int32, int64:
		// we don't need to handle error here, it must be success
		v, _ := types.ToInt64(args[0])
		if v >= 0 {
			return x, nil
		}

		// TODO: handle overflow if x is MinInt64
		return -v, nil
	case *types.DataItem:
		args[0] = x.Data
		return builtinAbs(args, ctx)
	default:
		// we will try to convert other types to float
		// TODO: if time has no precision, it will be a integer
		f, err := types.ToFloat64(args[0])
		return math.Abs(f), errors.Trace(err)
	}
}
開發者ID:js-for-kids,項目名稱:tidb,代碼行數:25,代碼來源:math.go

示例3: buildTableRanges

func (r *rangeBuilder) buildTableRanges(rangePoints []rangePoint) []TableRange {
	tableRanges := make([]TableRange, 0, len(rangePoints)/2)
	for i := 0; i < len(rangePoints); i += 2 {
		startPoint := rangePoints[i]
		if startPoint.value == nil || startPoint.value == MinNotNullVal {
			startPoint.value = math.MinInt64
		}
		startInt, err := types.ToInt64(startPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		cmp, err := types.Compare(startInt, startPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		if cmp < 0 || (cmp == 0 && startPoint.excl) {
			startInt++
		}
		endPoint := rangePoints[i+1]
		if endPoint.value == nil {
			endPoint.value = math.MinInt64
		} else if endPoint.value == MaxVal {
			endPoint.value = math.MaxInt64
		}
		endInt, err := types.ToInt64(endPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		cmp, err = types.Compare(endInt, endPoint.value)
		if err != nil {
			r.err = errors.Trace(err)
			return tableRanges
		}
		if cmp > 0 || (cmp == 0 && endPoint.excl) {
			endInt--
		}
		if startInt > endInt {
			continue
		}
		tableRanges = append(tableRanges, TableRange{LowVal: startInt, HighVal: endInt})
	}
	return tableRanges
}
開發者ID:52Jolynn,項目名稱:tidb,代碼行數:46,代碼來源:range.go

示例4: getFsp

func (v *typeInferrer) getFsp(x *ast.FuncCallExpr) int {
	if len(x.Args) == 1 {
		a := x.Args[0].GetValue()
		fsp, err := types.ToInt64(a)
		if err != nil {
			v.err = err
		}
		return int(fsp)
	}
	return 0
}
開發者ID:yuanfeng0905,項目名稱:tidb,代碼行數:11,代碼來源:typeinferer.go

示例5: builtinRand

func builtinRand(args []interface{}, ctx map[interface{}]interface{}) (v interface{}, err error) {
	if len(args) == 1 {
		seed, err := types.ToInt64(args[0])
		if err != nil {
			return nil, errors.Trace(err)
		}

		rand.Seed(seed)
	}

	return rand.Float64(), nil
}
開發者ID:js-for-kids,項目名稱:tidb,代碼行數:12,代碼來源:math.go

示例6: checkFsp

func checkFsp(arg interface{}) (int, error) {
	fsp, err := types.ToInt64(arg)
	if err != nil {
		return 0, errors.Trace(err)
	}
	if int(fsp) > mysql.MaxFsp {
		return 0, errors.Errorf("Too big precision %d specified. Maximum is 6.", fsp)
	} else if fsp < 0 {
		return 0, errors.Errorf("Invalid negative %d specified, must in [0, 6].", fsp)
	}
	return int(fsp), nil
}
開發者ID:lovedboy,項目名稱:tidb,代碼行數:12,代碼來源:time.go

示例7: toSeekKey

func (r *TableDefaultPlan) toSeekKey(seekVal interface{}) (kv.Key, error) {
	var handle int64
	var err error
	if seekVal == nil {
		handle = math.MinInt64
	} else {
		handle, err = types.ToInt64(seekVal)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}
	return tables.EncodeRecordKey(r.T.TableID(), handle, 0), nil
}
開發者ID:lovedboy,項目名稱:tidb,代碼行數:13,代碼來源:from.go

示例8: handleBitOp

func (e *Evaluator) handleBitOp(o *ast.BinaryOperationExpr) bool {
	a, b := types.Coerce(o.L.GetValue(), o.R.GetValue())

	if types.IsNil(a) || types.IsNil(b) {
		o.SetValue(nil)
		return true
	}

	x, err := types.ToInt64(a)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	y, err := types.ToInt64(b)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}

	// use a int64 for bit operator, return uint64
	switch o.Op {
	case opcode.And:
		o.SetValue(uint64(x & y))
	case opcode.Or:
		o.SetValue(uint64(x | y))
	case opcode.Xor:
		o.SetValue(uint64(x ^ y))
	case opcode.RightShift:
		o.SetValue(uint64(x) >> uint64(y))
	case opcode.LeftShift:
		o.SetValue(uint64(x) << uint64(y))
	default:
		e.err = ErrInvalidOperation.Gen("invalid op %v in bit operation", o.Op)
		return false
	}
	return true
}
開發者ID:lovedboy,項目名稱:tidb,代碼行數:38,代碼來源:evaluator_binop.go

示例9: evalDaysForm

func (da *DateArith) evalDaysForm(val interface{}) (interface{}, error) {
	switch val.(type) {
	case string:
		if strings.ToLower(val.(string)) == "false" {
			return 0, nil
		}
		if strings.ToLower(val.(string)) == "true" {
			return 1, nil
		}
		val = reg.FindString(val.(string))
	}

	return types.ToInt64(val)
}
開發者ID:yzl11,項目名稱:vessel,代碼行數:14,代碼來源:date_arith.go

示例10: parseDayInterval

func parseDayInterval(value interface{}) (int64, error) {
	switch v := value.(type) {
	case string:
		s := strings.ToLower(v)
		if s == "false" {
			return 0, nil
		} else if s == "true" {
			return 1, nil
		}
		value = reg.FindString(v)
	}

	return types.ToInt64(value)
}
開發者ID:mrtoms,項目名稱:tidb,代碼行數:14,代碼來源:evaluator.go

示例11: evalBitOp

// Operator: &, ~, |, ^, <<, >>
// See https://dev.mysql.com/doc/refman/5.7/en/bit-functions.html
func (o *BinaryOperation) evalBitOp(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	a, b, err := o.get2(ctx, args)
	if err != nil {
		return nil, err
	}

	if a == nil || b == nil {
		return nil, nil
	}

	x, err := types.ToInt64(a)
	if err != nil {
		return nil, o.traceErr(err)
	}

	y, err := types.ToInt64(b)
	if err != nil {
		return nil, o.traceErr(err)
	}

	// use a int64 for bit operator, return uint64
	switch o.Op {
	case opcode.And:
		return uint64(x & y), nil
	case opcode.Or:
		return uint64(x | y), nil
	case opcode.Xor:
		return uint64(x ^ y), nil
	case opcode.RightShift:
		return uint64(x) >> uint64(y), nil
	case opcode.LeftShift:
		return uint64(x) << uint64(y), nil
	default:
		return nil, o.errorf("invalid op %v in bit operation", o.Op)
	}
}
開發者ID:nengwang,項目名稱:tidb,代碼行數:38,代碼來源:binop.go

示例12: Eval

// Eval implements the Expression Eval interface.
func (f *FunctionSubstringIndex) Eval(ctx context.Context, args map[interface{}]interface{}) (interface{}, error) {
	fs, err := f.StrExpr.Eval(ctx, args)
	if err != nil {
		return nil, errors.Trace(err)
	}
	str, ok := fs.(string)
	if !ok {
		return nil, errors.Errorf("Substring_Index invalid args, need string but get %T", fs)
	}

	t, err := f.Delim.Eval(ctx, args)
	if err != nil {
		return nil, errors.Trace(err)
	}
	delim, ok := t.(string)
	if !ok {
		return nil, errors.Errorf("Substring_Index invalid delim, need string but get %T", t)
	}

	t, err = f.Count.Eval(ctx, args)
	if err != nil {
		return nil, errors.Trace(err)
	}
	c, err := types.ToInt64(t)
	if err != nil {
		return nil, errors.Trace(err)
	}
	count := int(c)
	strs := strings.Split(str, delim)
	var (
		start = 0
		end   = len(strs)
	)
	if count > 0 {
		// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
		if count < end {
			end = count
		}
	} else {
		// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
		count = -count
		if count < end {
			start = end - count
		}
	}
	substrs := strs[start:end]
	return strings.Join(substrs, delim), nil
}
開發者ID:kevinhuo88888,項目名稱:tidb,代碼行數:49,代碼來源:substring.go

示例13: funcLocate

func (e *Evaluator) funcLocate(v *ast.FuncLocateExpr) bool {
	// eval str
	fs := v.Str.GetValue()
	if types.IsNil(fs) {
		v.SetValue(nil)
		return true
	}
	str, err := types.ToString(fs)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	// eval substr
	fs = v.SubStr.GetValue()
	if types.IsNil(fs) {
		v.SetValue(nil)
		return true
	}
	substr, err := types.ToString(fs)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	// eval pos
	pos := 0
	if v.Pos != nil {
		t := v.Pos.GetValue()
		p, err := types.ToInt64(t)
		if err != nil {
			e.err = errors.Trace(err)
			return false
		}
		pos = int(p)
	}
	// eval locate
	if pos < 0 || pos > len(str) {
		e.err = ErrInvalidOperation.Gen("Locate invalid pos args: %d", pos)
		return false
	}
	str = str[pos:]
	i := strings.Index(str, substr)
	v.SetValue(i + 1 + pos)
	return true
}
開發者ID:mrtoms,項目名稱:tidb,代碼行數:44,代碼來源:evaluator.go

示例14: funcSubstringIndex

func (e *Evaluator) funcSubstringIndex(v *ast.FuncSubstringIndexExpr) bool {
	fs := v.StrExpr.GetValue()
	str, err := types.ToString(fs)
	if err != nil {
		e.err = ErrInvalidOperation.Gen("Substring_Index invalid args, need string but get %T", fs)
		return false
	}

	t := v.Delim.GetValue()
	delim, err := types.ToString(t)
	if err != nil {
		e.err = ErrInvalidOperation.Gen("Substring_Index invalid delim, need string but get %T", t)
		return false
	}

	t = v.Count.GetValue()
	c, err := types.ToInt64(t)
	if err != nil {
		e.err = errors.Trace(err)
		return false
	}
	count := int(c)
	strs := strings.Split(str, delim)
	var (
		start = 0
		end   = len(strs)
	)
	if count > 0 {
		// If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
		if count < end {
			end = count
		}
	} else {
		// If count is negative, everything to the right of the final delimiter (counting from the right) is returned.
		count = -count
		if count < end {
			start = end - count
		}
	}
	substrs := strs[start:end]
	v.SetValue(strings.Join(substrs, delim))
	return true
}
開發者ID:mrtoms,項目名稱:tidb,代碼行數:43,代碼來源:evaluator.go

示例15: AddRecord

// AddRecord implements table.Table AddRecord interface.
func (t *MemoryTable) AddRecord(ctx context.Context, r []types.Datum) (recordID int64, err error) {
	if t.pkHandleCol != nil {
		recordID, err = types.ToInt64(r[t.pkHandleCol.Offset].GetValue())
		if err != nil {
			return 0, errors.Trace(err)
		}
	} else {
		recordID, err = t.alloc.Alloc(t.ID)
		if err != nil {
			return 0, errors.Trace(err)
		}
	}
	item := &itemPair{
		handle: itemKey(recordID),
		data:   r,
	}
	t.mu.Lock()
	defer t.mu.Unlock()
	if t.tree.Get(itemKey(recordID)) != nil {
		return 0, kv.ErrKeyExists
	}
	t.tree.ReplaceOrInsert(item)
	return
}
開發者ID:astaxie,項目名稱:tidb,代碼行數:25,代碼來源:memory_tables.go


注:本文中的github.com/pingcap/tidb/util/types.ToInt64函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。