Search in sources :

Example 51 with UserDefinedType

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

the class DataTypeIT method typeFor.

private static String typeFor(DataType dataType) {
    String typeName = dataType.asCql(true, true);
    if (dataType instanceof UserDefinedType) {
        UserDefinedType udt = (UserDefinedType) dataType;
        // Create type if it doesn't already exist.
        List<String> fieldParts = new ArrayList<>();
        for (int i = 0; i < udt.getFieldNames().size(); i++) {
            String fieldName = udt.getFieldNames().get(i).asCql(false);
            String fieldType = typeFor(udt.getFieldTypes().get(i));
            fieldParts.add(fieldName + " " + fieldType);
        }
        SESSION_RULE.session().execute(SimpleStatement.builder(String.format("CREATE TYPE IF NOT EXISTS %s (%s)", udt.getName().asCql(false), String.join(",", fieldParts))).setExecutionProfile(SESSION_RULE.slowProfile()).build());
        // older versions of C* don't support non-frozen UDTs.
        if (!udt.isFrozen()) {
            typeName = "frozen<" + typeName + ">";
        }
    }
    return typeName;
}
Also used : ArrayList(java.util.ArrayList) DefaultUserDefinedType(com.datastax.oss.driver.internal.core.type.DefaultUserDefinedType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType)

Example 52 with UserDefinedType

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

the class CodecRegistryIT method should_register_and_use_custom_codec_for_user_defined_type.

@Test
public void should_register_and_use_custom_codec_for_user_defined_type() {
    Map<String, Coordinates> coordinatesMap = ImmutableMap.of("home", new Coordinates(12, 34));
    GenericType<Map<String, Coordinates>> coordinatesMapType = GenericType.mapOf(String.class, Coordinates.class);
    // Still create a separate session because we don't want to interfere with other tests
    try (CqlSession session = SessionUtils.newSession(CCM_RULE, SESSION_RULE.keyspace())) {
        // register the mapping codec for UDT coordinates
        UserDefinedType coordinatesUdt = session.getMetadata().getKeyspace(SESSION_RULE.keyspace()).flatMap(ks -> ks.getUserDefinedType("coordinates")).orElseThrow(IllegalStateException::new);
        MutableCodecRegistry codecRegistry = (MutableCodecRegistry) session.getContext().getCodecRegistry();
        // Retrieve the inner codec
        TypeCodec<UdtValue> innerCodec = codecRegistry.codecFor(coordinatesUdt);
        assertThat(innerCodec).isInstanceOf(UdtCodec.class);
        // Create the "outer" codec and register it
        CoordinatesCodec coordinatesCodec = new CoordinatesCodec(innerCodec);
        codecRegistry.register(coordinatesCodec);
        // Test that the codec will be used to create on-the-fly codecs
        assertThat(codecRegistry.codecFor(Coordinates.class)).isSameAs(coordinatesCodec);
        assertThat(codecRegistry.codecFor(coordinatesMapType).accepts(coordinatesMap)).isTrue();
        // test insertion
        PreparedStatement prepared = session.prepare("INSERT INTO test3 (k0, k1, v) values (?, ?, ?)");
        BoundStatement insert = prepared.boundStatementBuilder().setString(0, name.getMethodName()).setInt(1, 0).set(2, coordinatesMap, // use java type so has to be looked up in registry.
        coordinatesMapType).build();
        session.execute(insert);
        // test retrieval
        ResultSet result = session.execute(SimpleStatement.builder("SELECT v from test3 where k0 = ? AND k1 = ?").addPositionalValues(name.getMethodName(), 0).build());
        List<Row> rows = result.all();
        assertThat(rows).hasSize(1);
        Row row = rows.get(0);
        assertThat(row.get(0, coordinatesMapType)).isEqualTo(coordinatesMap);
        assertThat(row.getMap(0, String.class, Coordinates.class)).isEqualTo(coordinatesMap);
    }
}
Also used : Maps(org.assertj.core.util.Maps) BeforeClass(org.junit.BeforeClass) MappingCodec(com.datastax.oss.driver.api.core.type.codec.MappingCodec) TestRule(org.junit.rules.TestRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) CcmRule(com.datastax.oss.driver.api.testinfra.ccm.CcmRule) HashMap(java.util.HashMap) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) ByteBuffer(java.nio.ByteBuffer) CqlSession(com.datastax.oss.driver.api.core.CqlSession) TestName(org.junit.rules.TestName) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ClassRule(org.junit.ClassRule) ProtocolVersion(com.datastax.oss.driver.api.core.ProtocolVersion) Row(com.datastax.oss.driver.api.core.cql.Row) OptionalCodec(com.datastax.oss.driver.internal.core.type.codec.extras.OptionalCodec) Iterator(java.util.Iterator) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) ImmutableMap(com.google.common.collect.ImmutableMap) SessionUtils(com.datastax.oss.driver.api.testinfra.session.SessionUtils) DataType(com.datastax.oss.driver.api.core.type.DataType) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) TypeCodecs(com.datastax.oss.driver.api.core.type.codec.TypeCodecs) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) Objects(java.util.Objects) RuleChain(org.junit.rules.RuleChain) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) DataTypes(com.datastax.oss.driver.api.core.type.DataTypes) List(java.util.List) Rule(org.junit.Rule) CodecNotFoundException(com.datastax.oss.driver.api.core.type.codec.CodecNotFoundException) UdtCodec(com.datastax.oss.driver.internal.core.type.codec.UdtCodec) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) Nullable(edu.umd.cs.findbugs.annotations.Nullable) Optional(java.util.Optional) MutableCodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.MutableCodecRegistry) IntCodec(com.datastax.oss.driver.internal.core.type.codec.IntCodec) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) CqlSession(com.datastax.oss.driver.api.core.CqlSession) MutableCodecRegistry(com.datastax.oss.driver.api.core.type.codec.registry.MutableCodecRegistry) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Test(org.junit.Test)

Example 53 with UserDefinedType

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

the class TermTest method should_generate_literal_terms.

@Test
public void should_generate_literal_terms() {
    assertThat(literal(1)).hasCql("1");
    assertThat(literal("foo")).hasCql("'foo'");
    assertThat(literal(ImmutableList.of(1, 2, 3))).hasCql("[1,2,3]");
    TupleType tupleType = DataTypes.tupleOf(DataTypes.INT, DataTypes.TEXT);
    TupleValue tupleValue = tupleType.newValue().setInt(0, 1).setString(1, "foo");
    assertThat(literal(tupleValue)).hasCql("(1,'foo')");
    UserDefinedType udtType = new UserDefinedTypeBuilder(CqlIdentifier.fromCql("ks"), CqlIdentifier.fromCql("user")).withField(CqlIdentifier.fromCql("first_name"), DataTypes.TEXT).withField(CqlIdentifier.fromCql("last_name"), DataTypes.TEXT).build();
    UdtValue udtValue = udtType.newValue().setString("first_name", "Jane").setString("last_name", "Doe");
    assertThat(literal(udtValue)).hasCql("{first_name:'Jane',last_name:'Doe'}");
    assertThat(literal(null)).hasCql("NULL");
    assertThat(literal(Charsets.UTF_8, new CharsetCodec())).hasCql("'UTF-8'");
    assertThat(literal(Charsets.UTF_8, CharsetCodec.TEST_REGISTRY)).hasCql("'UTF-8'");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) 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) CharsetCodec(com.datastax.oss.driver.api.querybuilder.CharsetCodec) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) 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