Search in sources :

Example 1 with CodecRegistry

use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.

the class UdtCodec method format.

@NonNull
@Override
public String format(@Nullable UdtValue value) {
    if (value == null) {
        return "NULL";
    }
    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
    StringBuilder sb = new StringBuilder("{");
    int size = cqlType.getFieldTypes().size();
    boolean first = true;
    for (int i = 0; i < size; i++) {
        if (first) {
            first = false;
        } else {
            sb.append(",");
        }
        CqlIdentifier elementName = cqlType.getFieldNames().get(i);
        sb.append(elementName.asCql(true));
        sb.append(":");
        DataType elementType = cqlType.getFieldTypes().get(i);
        TypeCodec<Object> codec = registry.codecFor(elementType);
        sb.append(codec.format(value.get(i, codec)));
    }
    sb.append("}");
    return sb.toString();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 2 with CodecRegistry

use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project thingsboard by thingsboard.

the class AbstractBufferedRateExecutor method toStringWithValues.

private static String toStringWithValues(BoundStatement boundStatement, ProtocolVersion protocolVersion) {
    CodecRegistry codecRegistry = boundStatement.codecRegistry();
    PreparedStatement preparedStatement = boundStatement.getPreparedStatement();
    String query = preparedStatement.getQuery();
    ColumnDefinitions defs = preparedStatement.getVariableDefinitions();
    int index = 0;
    for (ColumnDefinition def : defs) {
        DataType type = def.getType();
        TypeCodec<Object> codec = codecRegistry.codecFor(type);
        if (boundStatement.getBytesUnsafe(index) != null) {
            Object value = codec.decode(boundStatement.getBytesUnsafe(index), protocolVersion);
            String replacement = Matcher.quoteReplacement(codec.format(value));
            query = query.replaceFirst("\\?", replacement);
        }
        index++;
    }
    return query;
}
Also used : ColumnDefinitions(com.datastax.oss.driver.api.core.cql.ColumnDefinitions) DataType(com.datastax.oss.driver.api.core.type.DataType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) ColumnDefinition(com.datastax.oss.driver.api.core.cql.ColumnDefinition)

Example 3 with CodecRegistry

use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.

the class UdtCodec method parse.

@Nullable
@Override
public UdtValue parse(@Nullable String value) {
    if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
        return null;
    }
    UdtValue udt = cqlType.newValue();
    int length = value.length();
    int position = ParseUtils.skipSpaces(value, 0);
    if (value.charAt(position) != '{') {
        throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting '{' but got '%c'", value, position, value.charAt(position)));
    }
    position++;
    position = ParseUtils.skipSpaces(value, position);
    if (position == length) {
        throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at character %d: expecting CQL identifier or '}', got EOF", value, position));
    }
    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
    CqlIdentifier id = null;
    while (position < length) {
        if (value.charAt(position) == '}') {
            position = ParseUtils.skipSpaces(value, position + 1);
            if (position == length) {
                return udt;
            }
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
        }
        int n;
        try {
            n = ParseUtils.skipCQLId(value, position);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", cannot parse a CQL identifier at character %d", value, position), e);
        }
        id = CqlIdentifier.fromInternal(value.substring(position, n));
        position = n;
        if (!cqlType.contains(id)) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", unknown CQL identifier at character %d: \"%s\"", value, position, id));
        }
        position = ParseUtils.skipSpaces(value, position);
        if (position == length) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got EOF", value, id, position));
        }
        if (value.charAt(position) != ':') {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ':', but got '%c'", value, id, position, value.charAt(position)));
        }
        position++;
        position = ParseUtils.skipSpaces(value, position);
        try {
            n = ParseUtils.skipCQLValue(value, position);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d)", value, id, position), e);
        }
        String fieldValue = value.substring(position, n);
        // This works because ids occur at most once in UDTs
        DataType fieldType = cqlType.getFieldTypes().get(cqlType.firstIndexOf(id));
        TypeCodec<Object> codec = registry.codecFor(fieldType);
        Object parsed;
        try {
            parsed = codec.parse(fieldValue);
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", invalid CQL value at field %s (character %d): %s", value, id, position, e.getMessage()), e);
        }
        udt = udt.set(id, parsed, codec);
        position = n;
        position = ParseUtils.skipSpaces(value, position);
        if (position == length) {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' or '}', but got EOF", value, id, position));
        }
        if (value.charAt(position) == '}') {
            continue;
        }
        if (value.charAt(position) != ',') {
            throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\", at field %s (character %d) expecting ',' but got '%c'", value, id, position, value.charAt(position)));
        }
        // skip ','
        ++position;
        position = ParseUtils.skipSpaces(value, position);
    }
    throw new IllegalArgumentException(String.format("Cannot parse UDT value from \"%s\" at field %s (character %d): expecting CQL identifier or '}', got EOF", value, id, position));
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) BufferUnderflowException(java.nio.BufferUnderflowException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Example 4 with CodecRegistry

use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.

the class TupleCodec method format.

@NonNull
@Override
public String format(@Nullable TupleValue value) {
    if (value == null) {
        return "NULL";
    }
    if (!value.getType().equals(cqlType)) {
        throw new IllegalArgumentException(String.format("Invalid tuple type, expected %s but got %s", cqlType, value.getType()));
    }
    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
    StringBuilder sb = new StringBuilder("(");
    boolean first = true;
    for (int i = 0; i < value.size(); i++) {
        if (first) {
            first = false;
        } else {
            sb.append(",");
        }
        DataType elementType = cqlType.getComponentTypes().get(i);
        TypeCodec<Object> codec = registry.codecFor(elementType);
        sb.append(codec.format(value.get(i, codec)));
    }
    sb.append(")");
    return sb.toString();
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 5 with CodecRegistry

use of com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry in project java-driver by datastax.

the class TupleCodec method parse.

@Nullable
@Override
public TupleValue parse(@Nullable String value) {
    if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) {
        return null;
    }
    TupleValue tuple = cqlType.newValue();
    int length = value.length();
    int position = ParseUtils.skipSpaces(value, 0);
    if (value.charAt(position) != '(') {
        throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting '(' but got '%c'", value, position, value.charAt(position)));
    }
    position++;
    position = ParseUtils.skipSpaces(value, position);
    CodecRegistry registry = cqlType.getAttachmentPoint().getCodecRegistry();
    int field = 0;
    while (position < length) {
        if (value.charAt(position) == ')') {
            position = ParseUtils.skipSpaces(value, position + 1);
            if (position == length) {
                return tuple;
            }
            throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at character %d expecting EOF or blank, but got \"%s\"", value, position, value.substring(position)));
        }
        int n;
        try {
            n = ParseUtils.skipCQLValue(value, position);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d)", value, field, position), e);
        }
        String fieldValue = value.substring(position, n);
        DataType elementType = cqlType.getComponentTypes().get(field);
        TypeCodec<Object> codec = registry.codecFor(elementType);
        Object parsed;
        try {
            parsed = codec.parse(fieldValue);
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", invalid CQL value at field %d (character %d): %s", value, field, position, e.getMessage()), e);
        }
        tuple = tuple.set(field, parsed, codec);
        position = n;
        position = ParseUtils.skipSpaces(value, position);
        if (position == length) {
            throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' or ')', but got EOF", value, field, position));
        }
        if (value.charAt(position) == ')') {
            continue;
        }
        if (value.charAt(position) != ',') {
            throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting ',' but got '%c'", value, field, position, value.charAt(position)));
        }
        // skip ','
        ++position;
        position = ParseUtils.skipSpaces(value, position);
        field += 1;
    }
    throw new IllegalArgumentException(String.format("Cannot parse tuple value from \"%s\", at field %d (character %d) expecting CQL value or ')', got EOF", value, field, position));
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) CodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) BufferUnderflowException(java.nio.BufferUnderflowException) Nullable(edu.umd.cs.findbugs.annotations.Nullable)

Aggregations

CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)7 DataType (com.datastax.oss.driver.api.core.type.DataType)5 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 PreparedStatement (com.datastax.oss.driver.api.core.cql.PreparedStatement)3 ConsistencyLevel (com.datastax.oss.driver.api.core.ConsistencyLevel)2 ProtocolVersion (com.datastax.oss.driver.api.core.ProtocolVersion)2 BoundStatement (com.datastax.oss.driver.api.core.cql.BoundStatement)2 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)2 ConsistencyLevelRegistry (com.datastax.oss.driver.internal.core.ConsistencyLevelRegistry)2 ProtocolVersionRegistry (com.datastax.oss.driver.internal.core.ProtocolVersionRegistry)2 Execute (com.datastax.oss.protocol.internal.request.Execute)2 Query (com.datastax.oss.protocol.internal.request.Query)2 NonNull (edu.umd.cs.findbugs.annotations.NonNull)2 Nullable (edu.umd.cs.findbugs.annotations.Nullable)2 BufferUnderflowException (java.nio.BufferUnderflowException)2 ByteBuffer (java.nio.ByteBuffer)2 ContinuousPagingOptions (com.datastax.dse.protocol.internal.request.query.ContinuousPagingOptions)1 DseQueryOptions (com.datastax.dse.protocol.internal.request.query.DseQueryOptions)1 BatchStatement (com.datastax.oss.driver.api.core.cql.BatchStatement)1 ColumnDefinition (com.datastax.oss.driver.api.core.cql.ColumnDefinition)1