Exemplo n.º 1
0
// Proto3ToBindVariables converts a proto.BinVariable map to internal data structure
func Proto3ToBindVariables(bv map[string]*querypb.BindVariable) (map[string]interface{}, error) {
	if len(bv) == 0 {
		return nil, nil
	}
	result := make(map[string]interface{})
	for k, v := range bv {
		if v == nil {
			continue
		}
		if v.Type == sqltypes.Tuple {
			list := make([]interface{}, len(v.Values))
			for i, lv := range v.Values {
				v, err := sqltypes.ValueFromBytes(lv.Type, lv.Value)
				if err != nil {
					return nil, err
				}
				// TODO(sougou): Change this to just v.
				list[i] = v.ToNative()
			}
			result[k] = list
		} else {
			v, err := sqltypes.ValueFromBytes(v.Type, v.Value)
			if err != nil {
				return nil, err
			}
			// TODO(sougou): Change this to just v.
			result[k] = v.ToNative()
		}
	}
	return result, nil
}
Exemplo n.º 2
0
// ProtoToEntityIds converts an array of EntityId from proto3
func ProtoToEntityIds(l []*vtgatepb.ExecuteEntityIdsRequest_EntityId) []EntityId {
	if len(l) == 0 {
		return nil
	}
	result := make([]EntityId, len(l))
	for i, e := range l {
		v, err := sqltypes.ValueFromBytes(e.Type, e.Value)
		if err != nil {
			panic(err)
		}
		result[i].KeyspaceID = e.KeyspaceId
		result[i].ExternalID = v.ToNative()
	}
	return result
}
Exemplo n.º 3
0
func mapEntityIdsToShards(ctx context.Context, topoServ topo.SrvTopoServer, cell, keyspace string, entityIds []*vtgatepb.ExecuteEntityIdsRequest_EntityId, tabletType topodatapb.TabletType) (string, map[string][]interface{}, error) {
	keyspace, _, allShards, err := getKeyspaceShards(ctx, topoServ, cell, keyspace, tabletType)
	if err != nil {
		return "", nil, err
	}
	var shards = make(map[string][]interface{})
	for _, eid := range entityIds {
		shard, err := getShardForKeyspaceID(allShards, eid.KeyspaceId)
		if err != nil {
			return "", nil, err
		}
		v, err := sqltypes.ValueFromBytes(eid.Type, eid.Value)
		if err != nil {
			return "", nil, err
		}
		shards[shard] = append(shards[shard], v.ToNative())
	}
	return keyspace, shards, nil
}
Exemplo n.º 4
0
// bigRatToValue converts 'number' to an SQL value with SQL type: valueType.
// If valueType is integral it truncates 'number' to the integer part according to the
// semantics of the big.Rat.Int method.
func bigRatToValue(number *big.Rat, valueType querypb.Type) sqltypes.Value {
	var numberAsBytes []byte
	switch {
	case sqltypes.IsIntegral(valueType):
		// 'number.Num()' returns a reference to the numerator of 'number'.
		// We copy it here to avoid changing 'number'.
		truncatedNumber := new(big.Int).Set(number.Num())
		truncatedNumber.Quo(truncatedNumber, number.Denom())
		numberAsBytes = bigIntToSliceOfBytes(truncatedNumber)
	case sqltypes.IsFloat(valueType):
		// Truncate to the closest 'float'.
		// There's not much we can do if there isn't an exact representation.
		numberAsFloat64, _ := number.Float64()
		numberAsBytes = strconv.AppendFloat([]byte{}, numberAsFloat64, 'f', -1, 64)
	default:
		panic(fmt.Sprintf("Unsupported type: %v", valueType))
	}
	result, err := sqltypes.ValueFromBytes(valueType, numberAsBytes)
	if err != nil {
		panic(fmt.Sprintf("sqltypes.ValueFromBytes failed with: %v", err))
	}
	return result
}