Search in sources :

Example 6 with SetType

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

the class DataTypeDetachableTest method set_should_be_attached_if_its_element_is.

@Test
public void set_should_be_attached_if_its_element_is() {
    TupleType tuple = DataTypes.tupleOf(DataTypes.INT);
    SetType set = DataTypes.setOf(tuple);
    assertThat(tuple.isDetached()).isTrue();
    assertThat(set.isDetached()).isTrue();
    tuple.attach(attachmentPoint);
    assertThat(set.isDetached()).isFalse();
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test)

Example 7 with SetType

use of com.datastax.oss.driver.api.core.type.SetType in project jnosql-diana-driver by eclipse.

the class QueryUtils method getUdtValue.

private static Object getUdtValue(UserDefinedType userType, Iterable elements, DataType type) {
    Collection<Object> udtValues = getCollectionUdt(type);
    UdtValue udtValue = userType.newValue();
    final List<String> udtNames = userType.getFieldNames().stream().map(CqlIdentifier::asInternal).collect(Collectors.toList());
    for (Object object : elements) {
        if (Column.class.isInstance(object)) {
            Column column = Column.class.cast(object);
            Object convert = ValueUtil.convert(column.getValue());
            final int index = udtNames.indexOf(column.getName());
            if (index < 0) {
                throw new CommunicationException("This field has not been found: " + column.getName() + " the fields available are " + udtNames + " in the UDT type " + userType.getName().asCql(true) + " at the keyspace " + userType.getKeyspace());
            }
            DataType fieldType = userType.getFieldTypes().get(index);
            TypeCodec<Object> objectTypeCodec = CodecRegistry.DEFAULT.codecFor(fieldType);
            if (fieldType instanceof SetType) {
                udtValue.set(getName(column), new HashSet<Object>((Collection<?>) convert), objectTypeCodec);
            } else {
                udtValue.set(getName(column), convert, objectTypeCodec);
            }
        } else if (Iterable.class.isInstance(object)) {
            udtValues.add(getUdtValue(userType, Iterable.class.cast(Iterable.class.cast(object)), type));
        }
    }
    if (udtValues.isEmpty()) {
        return udtValue;
    }
    return udtValues;
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) CommunicationException(jakarta.nosql.CommunicationException) Column(jakarta.nosql.column.Column) SetType(com.datastax.oss.driver.api.core.type.SetType) DataType(com.datastax.oss.driver.api.core.type.DataType) Collection(java.util.Collection)

Example 8 with SetType

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

the class TableParser method buildLegacyIndexTarget.

private static String buildLegacyIndexTarget(ColumnMetadata column, Map<String, String> options) {
    String columnName = column.getName().asCql(true);
    DataType columnType = column.getType();
    if (options.containsKey("index_keys")) {
        return String.format("keys(%s)", columnName);
    }
    if (options.containsKey("index_keys_and_values")) {
        return String.format("entries(%s)", columnName);
    }
    if ((columnType instanceof ListType && ((ListType) columnType).isFrozen()) || (columnType instanceof SetType && ((SetType) columnType).isFrozen()) || (columnType instanceof MapType && ((MapType) columnType).isFrozen())) {
        return String.format("full(%s)", columnName);
    }
    // Note: the keyword 'values' is not accepted as a valid index target function until 3.0
    return columnName;
}
Also used : SetType(com.datastax.oss.driver.api.core.type.SetType) ListType(com.datastax.oss.driver.api.core.type.ListType) DataType(com.datastax.oss.driver.api.core.type.DataType) MapType(com.datastax.oss.driver.api.core.type.MapType)

Example 9 with SetType

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

the class CachingCodecRegistry method createCodec.

// Try to create a codec when we haven't found it in the cache
@NonNull
protected TypeCodec<?> createCodec(@Nullable DataType cqlType, @Nullable GenericType<?> javaType, boolean isJavaCovariant) {
    LOG.trace("[{}] Cache miss, creating codec", logPrefix);
    // Either type can be null, but not both.
    if (javaType == null) {
        assert cqlType != null;
        return createCodec(cqlType);
    } else if (cqlType == null) {
        return createCodec(javaType, isJavaCovariant);
    } else {
        // Both non-null
        TypeToken<?> token = javaType.__getToken();
        if (cqlType instanceof ListType && List.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((ListType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.listOf(elementCodec);
        } else if (cqlType instanceof SetType && Set.class.isAssignableFrom(token.getRawType())) {
            DataType elementCqlType = ((SetType) cqlType).getElementType();
            TypeCodec<Object> elementCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> elementJavaType = GenericType.of(typeArguments[0]);
                elementCodec = uncheckedCast(codecFor(elementCqlType, elementJavaType, isJavaCovariant));
            } else {
                elementCodec = codecFor(elementCqlType);
            }
            return TypeCodecs.setOf(elementCodec);
        } else if (cqlType instanceof MapType && Map.class.isAssignableFrom(token.getRawType())) {
            DataType keyCqlType = ((MapType) cqlType).getKeyType();
            DataType valueCqlType = ((MapType) cqlType).getValueType();
            TypeCodec<Object> keyCodec;
            TypeCodec<Object> valueCodec;
            if (token.getType() instanceof ParameterizedType) {
                Type[] typeArguments = ((ParameterizedType) token.getType()).getActualTypeArguments();
                GenericType<?> keyJavaType = GenericType.of(typeArguments[0]);
                GenericType<?> valueJavaType = GenericType.of(typeArguments[1]);
                keyCodec = uncheckedCast(codecFor(keyCqlType, keyJavaType, isJavaCovariant));
                valueCodec = uncheckedCast(codecFor(valueCqlType, valueJavaType, isJavaCovariant));
            } else {
                keyCodec = codecFor(keyCqlType);
                valueCodec = codecFor(valueCqlType);
            }
            return TypeCodecs.mapOf(keyCodec, valueCodec);
        } else if (cqlType instanceof TupleType && TupleValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.tupleOf((TupleType) cqlType);
        } else if (cqlType instanceof UserDefinedType && UdtValue.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.udtOf((UserDefinedType) cqlType);
        } else if (cqlType instanceof CustomType && ByteBuffer.class.isAssignableFrom(token.getRawType())) {
            return TypeCodecs.custom(cqlType);
        }
        throw new CodecNotFoundException(cqlType, javaType);
    }
}
Also used : CustomType(com.datastax.oss.driver.api.core.type.CustomType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) Set(java.util.Set) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ByteBuffer(java.nio.ByteBuffer) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) ParameterizedType(java.lang.reflect.ParameterizedType) ListType(com.datastax.oss.driver.api.core.type.ListType) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) MapType(com.datastax.oss.driver.api.core.type.MapType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) SetType(com.datastax.oss.driver.api.core.type.SetType) CustomType(com.datastax.oss.driver.api.core.type.CustomType) SetType(com.datastax.oss.driver.api.core.type.SetType) TypeToken(com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken) ListType(com.datastax.oss.driver.api.core.type.ListType) TupleType(com.datastax.oss.driver.api.core.type.TupleType) DataType(com.datastax.oss.driver.api.core.type.DataType) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 10 with SetType

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

SetType (com.datastax.oss.driver.api.core.type.SetType)12 DataType (com.datastax.oss.driver.api.core.type.DataType)8 ListType (com.datastax.oss.driver.api.core.type.ListType)7 MapType (com.datastax.oss.driver.api.core.type.MapType)7 TupleType (com.datastax.oss.driver.api.core.type.TupleType)6 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)5 Test (org.junit.Test)4 CustomType (com.datastax.oss.driver.api.core.type.CustomType)3 CodecNotFoundException (com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException)3 NonNull (edu.umd.cs.findbugs.annotations.NonNull)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)2 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)1 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)1 ColumnMetadata (com.datastax.oss.driver.api.core.metadata.schema.ColumnMetadata)1 TableMetadata (com.datastax.oss.driver.api.core.metadata.schema.TableMetadata)1 ViewMetadata (com.datastax.oss.driver.api.core.metadata.schema.ViewMetadata)1 GenericType (com.datastax.oss.driver.api.core.type.reflect.GenericType)1 ImmutableList (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList)1 ImmutableMap (com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap)1