Search in sources :

Example 56 with DataType

use of com.datastax.oss.driver.api.core.type.DataType 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 57 with DataType

use of com.datastax.oss.driver.api.core.type.DataType 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)

Example 58 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeCqlNameParserTest method should_parse_native_types.

@Test
public void should_parse_native_types() {
    for (Map.Entry<String, DataType> entry : DataTypeCqlNameParser.NATIVE_TYPES_BY_NAME.entrySet()) {
        String className = entry.getKey();
        DataType expectedType = entry.getValue();
        assertThat(parse(className)).isEqualTo(expectedType);
    }
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 59 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeClassNameParserTest method should_parse_native_types.

@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_native_types(Locale locale) {
    Locale def = Locale.getDefault();
    try {
        Locale.setDefault(locale);
        for (Map.Entry<String, DataType> entry : DataTypeClassNameParser.NATIVE_TYPES_BY_CLASS_NAME.entrySet()) {
            String className = entry.getKey();
            DataType expectedType = entry.getValue();
            assertThat(parse(className)).isEqualTo(expectedType);
        }
    } finally {
        Locale.setDefault(def);
    }
}
Also used : Locale(java.util.Locale) DataType(com.datastax.oss.driver.api.core.type.DataType) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 60 with DataType

use of com.datastax.oss.driver.api.core.type.DataType in project java-driver by datastax.

the class DataTypeClassNameParserTest method should_parse_user_type_when_definition_not_already_available.

@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_parse_user_type_when_definition_not_already_available(Locale locale) {
    Locale def = Locale.getDefault();
    try {
        Locale.setDefault(locale);
        UserDefinedType addressType = (UserDefinedType) parse("org.apache.cassandra.db.marshal.UserType(" + "foo,61646472657373," + ("737472656574:org.apache.cassandra.db.marshal.UTF8Type," + "7a6970636f6465:org.apache.cassandra.db.marshal.Int32Type," + ("70686f6e6573:org.apache.cassandra.db.marshal.SetType(" + "org.apache.cassandra.db.marshal.UserType(foo,70686f6e65," + "6e616d65:org.apache.cassandra.db.marshal.UTF8Type," + "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type)") + "))"));
        assertThat(addressType.getKeyspace().asInternal()).isEqualTo("foo");
        assertThat(addressType.getName().asInternal()).isEqualTo("address");
        assertThat(addressType.isFrozen()).isTrue();
        assertThat(addressType.getFieldNames().size()).isEqualTo(3);
        assertThat(addressType.getFieldNames().get(0).asInternal()).isEqualTo("street");
        assertThat(addressType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
        assertThat(addressType.getFieldNames().get(1).asInternal()).isEqualTo("zipcode");
        assertThat(addressType.getFieldTypes().get(1)).isEqualTo(DataTypes.INT);
        assertThat(addressType.getFieldNames().get(2).asInternal()).isEqualTo("phones");
        DataType phonesType = addressType.getFieldTypes().get(2);
        assertThat(phonesType).isInstanceOf(SetType.class);
        UserDefinedType phoneType = ((UserDefinedType) ((SetType) phonesType).getElementType());
        assertThat(phoneType.getKeyspace().asInternal()).isEqualTo("foo");
        assertThat(phoneType.getName().asInternal()).isEqualTo("phone");
        assertThat(phoneType.isFrozen()).isTrue();
        assertThat(phoneType.getFieldNames().size()).isEqualTo(2);
        assertThat(phoneType.getFieldNames().get(0).asInternal()).isEqualTo("name");
        assertThat(phoneType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
        assertThat(phoneType.getFieldNames().get(1).asInternal()).isEqualTo("number");
        assertThat(phoneType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
    } finally {
        Locale.setDefault(def);
    }
}
Also used : Locale(java.util.Locale) SetType(com.datastax.oss.driver.api.core.type.SetType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataType(com.datastax.oss.driver.api.core.type.DataType) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Aggregations

DataType (com.datastax.oss.driver.api.core.type.DataType)73 NonNull (edu.umd.cs.findbugs.annotations.NonNull)21 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)19 SetType (com.datastax.oss.driver.api.core.type.SetType)10 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)10 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)10 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)9 CheckReturnValue (edu.umd.cs.findbugs.annotations.CheckReturnValue)9 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)8 Map (java.util.Map)8 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)7 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)7 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)6 Nullable (edu.umd.cs.findbugs.annotations.Nullable)6 UUID (java.util.UUID)6 TupleType (com.datastax.oss.driver.api.core.type.TupleType)5 CodecRegistry (com.datastax.oss.driver.api.core.type.codec.registry.CodecRegistry)5 List (java.util.List)5