示例#1
0
// encodeTableKey encodes a single element of a table key, appending the
// encoded value to b.
func encodeTableKey(b []byte, v reflect.Value) ([]byte, error) {
	switch t := v.Interface().(type) {
	case []byte:
		return roachencoding.EncodeBytes(b, t), nil
	case string:
		return roachencoding.EncodeBytes(b, []byte(t)), nil
	}

	switch v.Kind() {
	case reflect.Bool:
		if v.Bool() {
			return roachencoding.EncodeVarint(b, 1), nil
		}
		return roachencoding.EncodeVarint(b, 0), nil

	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return roachencoding.EncodeVarint(b, v.Int()), nil

	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		return roachencoding.EncodeUvarint(b, v.Uint()), nil

	case reflect.Float32, reflect.Float64:
		return roachencoding.EncodeNumericFloat(b, v.Float()), nil

	case reflect.String:
		return roachencoding.EncodeBytes(b, []byte(v.String())), nil
	}

	return nil, fmt.Errorf("unable to encode key: %s", v)
}
示例#2
0
func encodeTableKey(b []byte, v parser.Datum) ([]byte, error) {
	switch t := v.(type) {
	case parser.DBool:
		if t {
			return encoding.EncodeVarint(b, 1), nil
		}
		return encoding.EncodeVarint(b, 0), nil
	case parser.DInt:
		return encoding.EncodeVarint(b, int64(t)), nil
	case parser.DFloat:
		return encoding.EncodeNumericFloat(b, float64(t)), nil
	case parser.DString:
		return encoding.EncodeBytes(b, []byte(t)), nil
	}
	return nil, fmt.Errorf("unable to encode table key: %T", v)
}
示例#3
0
func encodeTableKey(b []byte, v sqlwire.Datum) ([]byte, error) {
	if v.BoolVal != nil {
		if *v.BoolVal {
			return encoding.EncodeVarint(b, 1), nil
		}
		return encoding.EncodeVarint(b, 0), nil
	} else if v.IntVal != nil {
		return encoding.EncodeVarint(b, *v.IntVal), nil
	} else if v.FloatVal != nil {
		return encoding.EncodeNumericFloat(b, *v.FloatVal), nil
	} else if v.BytesVal != nil {
		return encoding.EncodeBytes(b, v.BytesVal), nil
	} else if v.StringVal != nil {
		return encoding.EncodeBytes(b, []byte(*v.StringVal)), nil
	}
	return nil, fmt.Errorf("unable to encode table key: %T", v)
}
示例#4
0
func encodeTableKey(b []byte, v parser.Datum) ([]byte, error) {
	switch t := v.(type) {
	case parser.DBool:
		if t {
			return encoding.EncodeVarint(b, 1), nil
		}
		return encoding.EncodeVarint(b, 0), nil
	case parser.DInt:
		return encoding.EncodeVarint(b, int64(t)), nil
	case parser.DFloat:
		return encoding.EncodeNumericFloat(b, float64(t)), nil
	case parser.DString:
		return encoding.EncodeBytes(b, []byte(t)), nil
	case parser.DNull:
		// TODO(tamird,pmattis): This is a hack; we should have proper nil encoding.
		return encoding.EncodeBytes(b, nil), nil
	}
	return nil, fmt.Errorf("unable to encode table key: %T", v)
}
示例#5
0
文件: conn.go 项目: Jaekyun/cockroach
func encodeTableKey(b []byte, v driver.Value) ([]byte, error) {
	switch t := v.(type) {
	case int64:
		return encoding.EncodeVarint(b, t), nil
	case float64:
		return encoding.EncodeNumericFloat(b, t), nil
	case bool:
		if t {
			return encoding.EncodeVarint(b, 1), nil
		}
		return encoding.EncodeVarint(b, 0), nil
	case []byte:
		return encoding.EncodeBytes(b, t), nil
	case string:
		return encoding.EncodeBytes(b, []byte(t)), nil
	case time.Time:
		return nil, fmt.Errorf("TODO(pmattis): encode index key: time.Time")
	}
	return nil, fmt.Errorf("unable to encode table key: %T", v)
}