// ValueToInt64 Convert a value type to a int64 if possible
773046
func ValueToInt64(val Value) (int64, bool) {
	if val == nil || val.Nil() || val.Err() {
		return 0, false
	}
	switch v := val.(type) {
	case NumericValue:
		return v.Int(), true
	case StringValue:
		return convertStringToInt64(0, v.Val())
	case Slice:
		if v.Len() > 0 {
			return ValueToInt64(v.SliceValue()[0])
		}
	}
	return 0, false
}
659573
func (nn NullNumber) Int64() (int64, error) {
	if nn.IsNil() {
		return 0, errors.New("NullNumber is nil")
	}
	return strconv.ParseInt(nn.value.(string), 10, 64)
}
677782
func (v Value) Uint64() (uint64, error) {
	i, err := strconv.ParseUint(v.String(), 10, 64)
	if err != nil {
		return 0, v.invalidTypeError(err, "uint64")
	}
	return i, nil
}
1177593
func (c *JSONConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
	if v, err := c.Int64(key); err == nil {
		return v
	}
	return defaultval
}
16982
func ToInt(str string) (int64, error) {
	res, err := strconv.ParseInt(str, 0, 64)
	if err != nil {
		res = 0
	}
	return res, err
}
932572
func (l *Literal) Int64() (int64, error) {
	if l.t != Int64 {
		return 0, fmt.Errorf("literal.Int64: literal is of type %v; cannot be converted to a int64", l.t)
	}
	return l.v.(int64), nil
}
141282
func (i Int) Int64() (_ int64, ok bool) {
	x, acc := bigintToInt64(i.bigint)
	if acc != big.Exact {
		return // inexact
	}
	return x, true
}
187796
func toInt64(v interface{}) int64 {
	if str, ok := v.(string); ok {
		iv, err := strconv.ParseInt(str, 10, 64)
		if err != nil {
			return 0
		}
		return iv
	}

	val := reflect.Indirect(reflect.ValueOf(v))
	switch val.Kind() {
	case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
		return val.Int()
	case reflect.Uint8, reflect.Uint16, reflect.Uint32:
		return int64(val.Uint())
	case reflect.Uint, reflect.Uint64:
		tv := val.Uint()
		if tv <= math.MaxInt64 {
			return int64(tv)
		}
		// TODO: What is the sensible thing to do here?
		return math.MaxInt64
	case reflect.Float32, reflect.Float64:
		return int64(val.Float())
	case reflect.Bool:
		if val.Bool() == true {
			return 1
		}
		return 0
	default:
		return 0
	}
}
143211
func (v Value) Float64() (float64, error) {
	f, err := strconv.ParseFloat(v.String(), 64)
	if err != nil {
		return 0.0, v.invalidTypeError(err, "float64")
	}
	return f, nil
}
29631
func (d *Datum) ToInt64(sc *stmtctx.StatementContext) (int64, error) {
	return d.toSignedInteger(sc, mysql.TypeLonglong)
}
14653
func (s *Session) Int64(key string) int64 {
	s.RLock()
	defer s.RUnlock()

	v, ok := s.data[key]
	if !ok {
		return 0
	}

	value, ok := v.(int64)
	if !ok {
		return 0
	}
	return value
}
1072129
func (r *Reply) Int64() (int64, error) {
	if r.Type == ErrorReply {
		return 0, r.Err
	}
	if r.Type != IntegerReply {
		s, err := r.Str()
		if err == nil {
			i64, err := strconv.ParseInt(s, 10, 64)
			if err != nil {
				return 0, errors.New("failed to parse integer value from string value")
			} else {
				return i64, nil
			}
		}

		return 0, errors.New("integer value is not available for this reply type")
	}

	return r.int, nil
}
475807
func TryToInt64(i interface{}) (r int64, err error) {
	switch t := i.(type) {
	case nil:
		r = 0
	case int:
		r = int64(t)
	case int8:
		r = int64(t)
	case int16:
		r = int64(t)
	case int32:
		r = int64(t)
	case int64:
		r = t
	case *int64:
		r = *t
	case uint:
		r = int64(t)
	case uint8:
		r = int64(t)
	case uint16:
		r = int64(t)
	case uint32:
		r = int64(t)
	case uint64:
		r = int64(t)
	case float32:
		r = int64(t)
	case float64:
		r = int64(t)
	case bool:
		if t {
			r = 1
		}
	case string:
		r, err = strconv.ParseInt(t, 10, 64)
	default:
		err = castError(i, "int64")
	}
	return
}
916138
func UInt64Value(val string) (*wrappers.UInt64Value, error) {
	parsedVal, err := Uint64(val)
	return &wrappers.UInt64Value{Value: parsedVal}, err
}
112036
func ConvertIntToInt(val int64, lowerBound int64, upperBound int64, tp byte) (int64, error) {
	if val < lowerBound {
		return lowerBound, overflow(val, tp)
	}

	if val > upperBound {
		return upperBound, overflow(val, tp)
	}

	return val, nil
}
1276404
func (f *FlagSet) Int64(name string, value int64, usage string) *int64 {
	p := new(int64)
	f.Int64Var(p, name, value, usage)
	return p
}
1101991
func (enc *Encoder) WriteInt64(nn int64) error {
	n := binary.PutVarint(enc.bytebuf, nn)
	_, enc.err = enc.buf.Write(enc.bytebuf[:n])
	return enc.err
}