Exemplo n.º 1
0
// decodeOidDatum decodes bytes with specified Oid and format code into
// a datum.
func decodeOidDatum(id oid.Oid, code formatCode, b []byte) (driver.Datum, error) {
	var d driver.Datum
	switch id {
	case oid.T_bool:
		switch code {
		case formatText:
			v, err := strconv.ParseBool(string(b))
			if err != nil {
				return d, fmt.Errorf("unknown bool value")
			}
			d.Payload = &driver.Datum_BoolVal{BoolVal: v}
		default:
			return d, fmt.Errorf("unsupported: binary bool parameter")
		}
	case oid.T_int8:
		switch code {
		case formatText:
			i, err := strconv.ParseInt(string(b), 10, 64)
			if err != nil {
				return d, fmt.Errorf("unknown int value")
			}
			d.Payload = &driver.Datum_IntVal{IntVal: i}
		default:
			return d, fmt.Errorf("unsupported: binary int parameter")
		}
	case oid.T_float8:
		switch code {
		case formatText:
			f, err := strconv.ParseFloat(string(b), 64)
			if err != nil {
				return d, fmt.Errorf("unknown float value")
			}
			d.Payload = &driver.Datum_FloatVal{FloatVal: f}
		default:
			return d, fmt.Errorf("unsupported: binary float parameter")
		}
	case oid.T_text:
		switch code {
		case formatText:
			d.Payload = &driver.Datum_StringVal{StringVal: string(b)}
		default:
			return d, fmt.Errorf("unsupported: binary string parameter")
		}
	// TODO(mjibson): implement date/time types
	default:
		return d, fmt.Errorf("unsupported: %v", id)
	}
	return d, nil
}
Exemplo n.º 2
0
func unmarshalValue(col structured.ColumnDescriptor, kv client.KeyValue) driver.Datum {
	var d driver.Datum
	if !kv.Exists() {
		return d
	}
	switch col.Type.Kind {
	case structured.ColumnType_BIT, structured.ColumnType_INT:
		tmp := kv.ValueInt()
		d.IntVal = &tmp
	case structured.ColumnType_FLOAT:
		tmp := math.Float64frombits(uint64(kv.ValueInt()))
		d.FloatVal = &tmp
	case structured.ColumnType_CHAR, structured.ColumnType_TEXT,
		structured.ColumnType_BLOB:
		// TODO(pmattis): The conversion to string isn't strictly necessary, but
		// makes log messages more readable right now.
		tmp := string(kv.ValueBytes())
		d.StringVal = &tmp
	}
	return d
}
Exemplo n.º 3
0
// decodeOidDatum decodes bytes with specified Oid and format code into
// a datum.
func decodeOidDatum(id oid.Oid, code formatCode, b []byte) (driver.Datum, error) {
	var d driver.Datum
	switch id {
	case oid.T_bool:
		switch code {
		case formatText:
			v, err := strconv.ParseBool(string(b))
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_BoolVal{BoolVal: v}
		default:
			return d, fmt.Errorf("unsupported bool format code: %s", code)
		}
	case oid.T_int2:
		switch code {
		case formatText:
			i, err := strconv.ParseInt(string(b), 10, 64)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: i}
		case formatBinary:
			var i int16
			err := binary.Read(bytes.NewReader(b), binary.BigEndian, &i)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: int64(i)}
		default:
			return d, fmt.Errorf("unsupported int2 format code: %s", code)
		}
	case oid.T_int4:
		switch code {
		case formatText:
			i, err := strconv.ParseInt(string(b), 10, 64)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: i}
		case formatBinary:
			var i int32
			err := binary.Read(bytes.NewReader(b), binary.BigEndian, &i)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: int64(i)}
		default:
			return d, fmt.Errorf("unsupported int4 format code: %s", code)
		}
	case oid.T_int8:
		switch code {
		case formatText:
			i, err := strconv.ParseInt(string(b), 10, 64)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: i}
		case formatBinary:
			var i int64
			err := binary.Read(bytes.NewReader(b), binary.BigEndian, &i)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_IntVal{IntVal: i}
		default:
			return d, fmt.Errorf("unsupported int8 format code: %s", code)
		}
	case oid.T_float4:
		switch code {
		case formatText:
			f, err := strconv.ParseFloat(string(b), 64)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_FloatVal{FloatVal: f}
		case formatBinary:
			var f float32
			err := binary.Read(bytes.NewReader(b), binary.BigEndian, &f)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_FloatVal{FloatVal: float64(f)}
		default:
			return d, fmt.Errorf("unsupported float4 format code: %s", code)
		}
	case oid.T_float8:
		switch code {
		case formatText:
			f, err := strconv.ParseFloat(string(b), 64)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_FloatVal{FloatVal: f}
		case formatBinary:
			var f float64
			err := binary.Read(bytes.NewReader(b), binary.BigEndian, &f)
			if err != nil {
				return d, err
			}
			d.Payload = &driver.Datum_FloatVal{FloatVal: f}
		default:
			return d, fmt.Errorf("unsupported float8 format code: %s", code)
		}
	case oid.T_text:
		switch code {
		case formatText:
			d.Payload = &driver.Datum_StringVal{StringVal: string(b)}
		default:
			return d, fmt.Errorf("unsupported text format code: %s", code)
		}
	// TODO(mjibson): implement date/time types
	default:
		return d, fmt.Errorf("unsupported OID: %v", id)
	}
	return d, nil
}