Search in sources :

Example 31 with UserDefinedType

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

the class DefaultUdtValueTest method should_support_null_items_when_setting_in_bulk.

@Test
public void should_support_null_items_when_setting_in_bulk() throws UnsupportedEncodingException {
    UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
    when(codecRegistry.<Integer>codecFor(DataTypes.INT)).thenReturn(TypeCodecs.INT);
    when(codecRegistry.codecFor(DataTypes.TEXT, "foo")).thenReturn(TypeCodecs.TEXT);
    UdtValue value = type.newValue(null, "foo");
    assertThat(value.isNull(0)).isTrue();
    assertThat(value.getString(1)).isEqualTo("foo");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 32 with UserDefinedType

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

the class DefaultUdtValueTest method should_serialize_and_deserialize.

@Test
public void should_serialize_and_deserialize() {
    UserDefinedType type = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), DataTypes.TEXT).build();
    UdtValue in = type.newValue();
    in = in.setBytesUnsafe(0, Bytes.fromHexString("0x00000001"));
    in = in.setBytesUnsafe(1, Bytes.fromHexString("0x61"));
    UdtValue out = SerializationHelper.serializeAndDeserialize(in);
    assertThat(out.getType()).isEqualTo(in.getType());
    assertThat(out.getType().isDetached()).isTrue();
    assertThat(Bytes.toHexString(out.getBytesUnsafe(0))).isEqualTo("0x00000001");
    assertThat(Bytes.toHexString(out.getBytesUnsafe(1))).isEqualTo("0x61");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Example 33 with UserDefinedType

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

the class DefaultUdtValueTest method newInstance.

@Override
protected UdtValue newInstance(List<DataType> dataTypes, List<Object> values, AttachmentPoint attachmentPoint) {
    UserDefinedTypeBuilder builder = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type"));
    for (int i = 0; i < dataTypes.size(); i++) {
        builder.withField(CqlIdentifier.fromInternal("field" + i), dataTypes.get(i));
    }
    UserDefinedType userDefinedType = builder.build();
    userDefinedType.attach(attachmentPoint);
    return userDefinedType.newValue(values.toArray());
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) AttachmentPoint(com.datastax.oss.driver.api.core.detach.AttachmentPoint)

Example 34 with UserDefinedType

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

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

the class GraphDataTypesTest method complexUdtTests.

@Test
public void complexUdtTests() throws IOException {
    UserDefinedType type1 = new UserDefinedTypeBuilder("ks", "udt1").withField("a", INT).withField("b", TEXT).build();
    verifySerDeBinary(type1.newValue(1, "2"));
    TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
    TupleType firstNested = tupleOf(TEXT, secondNested);
    UserDefinedType type2 = new UserDefinedTypeBuilder("ks", "udt2").withField("a", INT).withField("b", TEXT).withField("c", type1).withField("mylist", listOf(BIGINT)).withField("mytuple_withlist", firstNested).build();
    verifySerDeBinary(type2.newValue(1, "2", type1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L)))));
    UserDefinedType type3 = new UserDefinedTypeBuilder("ks", "udt3").withField("a", listOf(INT)).withField("b", setOf(FLOAT)).withField("c", mapOf(TEXT, BIGINT)).withField("d", listOf(listOf(DOUBLE))).withField("e", setOf(setOf(FLOAT))).withField("f", listOf(tupleOf(INT, TEXT))).build();
    verifySerDeBinary(type3.newValue(ImmutableList.of(1), ImmutableSet.of(2.1f), ImmutableMap.of("3", 4L), ImmutableList.of(ImmutableList.of(5.1d, 6.1d), ImmutableList.of(7.1d)), ImmutableSet.of(ImmutableSet.of(8.1f), ImmutableSet.of(9.1f)), ImmutableList.of(tupleOf(INT, TEXT).newValue(10, "11"))));
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) UserDefinedTypeBuilder(com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4