Search in sources :

Example 21 with UserDefinedType

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

the class DataTypeDetachableTest method attaching_udt_should_attach_all_of_its_subtypes.

@Test
public void attaching_udt_should_attach_all_of_its_subtypes() {
    TupleType tuple = DataTypes.tupleOf(DataTypes.INT);
    UserDefinedType udt = new UserDefinedTypeBuilder(CqlIdentifier.fromInternal("ks"), CqlIdentifier.fromInternal("type")).withField(CqlIdentifier.fromInternal("field1"), DataTypes.INT).withField(CqlIdentifier.fromInternal("field2"), tuple).build();
    assertThat(tuple.isDetached()).isTrue();
    assertThat(udt.isDetached()).isTrue();
    udt.attach(attachmentPoint);
    assertThat(tuple.isDetached()).isFalse();
}
Also used : TupleType(com.datastax.oss.driver.api.core.type.TupleType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Test(org.junit.Test)

Example 22 with UserDefinedType

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

the class CassandraSchemaParser method parseVirtualKeyspace.

private KeyspaceMetadata parseVirtualKeyspace(AdminRow keyspaceRow) {
    CqlIdentifier keyspaceId = CqlIdentifier.fromInternal(keyspaceRow.getString("keyspace_name"));
    boolean durableWrites = MoreObjects.firstNonNull(keyspaceRow.getBoolean("durable_writes"), false);
    Map<String, String> replicationOptions = Collections.emptyMap();
    ;
    Map<CqlIdentifier, UserDefinedType> types = parseTypes(keyspaceId);
    return new DefaultKeyspaceMetadata(keyspaceId, durableWrites, true, replicationOptions, types, parseVirtualTables(keyspaceId, types), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
}
Also used : DefaultKeyspaceMetadata(com.datastax.oss.driver.internal.core.metadata.schema.DefaultKeyspaceMetadata) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 23 with UserDefinedType

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

the class CoreGraphDataTypeITBase method should_insert_and_retrieve_nested_UDTS_and_tuples.

@Test
public void should_insert_and_retrieve_nested_UDTS_and_tuples() {
    CqlSession session = session();
    // use CQL to create type for now because DSP-17567 is not in yet, so this is more stable
    session.execute(String.format("CREATE TYPE %s.udt1(a int, b text)", graphName()));
    session.execute(String.format("CREATE TYPE %s.udt2(" + "a int" + ", b text" + ", c frozen<udt1>" + ", mylist list<bigint>" + ", mytuple_withlist tuple<varchar, tuple<bigint, frozen<list<bigint>>>>" + ")", graphName()));
    session.execute(String.format("CREATE TYPE %s.udt3(" + "a list<int>" + ", b set<float>" + ", c map<text, bigint>" + ", d list<frozen<list<double>>>" + ", e set<frozen<set<float>>>" + ", f list<frozen<tuple<int, text>>>" + ")", graphName()));
    UserDefinedType udt1 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt1")).orElseThrow(IllegalStateException::new);
    UdtValue udtValue1 = udt1.newValue(1, "2");
    UserDefinedType udt2 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt2")).orElseThrow(IllegalStateException::new);
    TupleType secondNested = tupleOf(BIGINT, listOf(BIGINT));
    TupleType firstNested = tupleOf(TEXT, secondNested);
    UdtValue udtValue2 = udt2.newValue(1, "2", udt1.newValue(3, "4"), ImmutableList.of(5L), firstNested.newValue("6", secondNested.newValue(7L, ImmutableList.of(8L))));
    UserDefinedType udt3 = session.getMetadata().getKeyspace(graphName()).flatMap(keyspace -> keyspace.getUserDefinedType("udt3")).orElseThrow(IllegalStateException::new);
    UdtValue udtValue3 = udt3.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")));
    Map<String, Object> properties = ImmutableMap.<String, Object>builder().put("frozen(typeOf('udt1'))", udtValue1).put("frozen(typeOf('udt2'))", udtValue2).put("frozen(typeOf('udt3'))", udtValue3).build();
    int vertexID = 1;
    String vertexLabel = "graphBinaryNestedTypes";
    runTest(properties, vertexLabel, vertexID);
}
Also used : Arrays(java.util.Arrays) INT(com.datastax.oss.driver.api.core.type.DataTypes.INT) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) ByteBuffer(java.nio.ByteBuffer) BIGINT(com.datastax.oss.driver.api.core.type.DataTypes.BIGINT) Point(com.datastax.dse.driver.api.core.data.geometry.Point) BigDecimal(java.math.BigDecimal) ImmutableList(com.google.common.collect.ImmutableList) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) LocalTime(java.time.LocalTime) BigInteger(java.math.BigInteger) DseDataTypes(com.datastax.dse.driver.api.core.type.DseDataTypes) Polygon(com.datastax.dse.driver.api.core.data.geometry.Polygon) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Geo(com.datastax.dse.driver.api.core.graph.predicates.Geo) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Test(org.junit.Test) Instant(java.time.Instant) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) Sets(com.google.common.collect.Sets) ZoneId(java.time.ZoneId) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ChronoUnit(java.time.temporal.ChronoUnit) DataTypes.listOf(com.datastax.oss.driver.api.core.type.DataTypes.listOf) LocalDate(java.time.LocalDate) CqlDuration(com.datastax.oss.driver.api.core.data.CqlDuration) DataTypes.tupleOf(com.datastax.oss.driver.api.core.type.DataTypes.tupleOf) TEXT(com.datastax.oss.driver.api.core.type.DataTypes.TEXT) 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) LineString(com.datastax.dse.driver.api.core.data.geometry.LineString) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Point(com.datastax.dse.driver.api.core.data.geometry.Point) Test(org.junit.Test)

Example 24 with UserDefinedType

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

the class GeometryIT method should_insert_as_field_in_udt.

/**
 * Validates that a geometry value can be inserted as a field in a UDT and verifies that the UDT
 * is stored correctly by retrieving it and ensuring it matches.
 */
@Test
@Ignore
public void should_insert_as_field_in_udt() {
    UUID key = Uuids.random();
    UserDefinedType udtType = sessionRule.session().getMetadata().getKeyspace(sessionRule.session().getKeyspace().orElseThrow(AssertionError::new)).flatMap(ks -> ks.getUserDefinedType(CqlIdentifier.fromInternal("udt1"))).orElseThrow(AssertionError::new);
    assertThat(udtType).isNotNull();
    UdtValue value = udtType.newValue();
    value = value.set("g", sampleData.get(0), genericType);
    PreparedStatement prepared = sessionRule.session().prepare("INSERT INTO tbl (k, u) values (?, ?)");
    BoundStatement bs = prepared.boundStatementBuilder().setUuid(0, key).setUdtValue(1, value).build();
    sessionRule.session().execute(bs);
    ResultSet rs = sessionRule.session().execute(SimpleStatement.builder("SELECT k,u FROM tbl where k=?").addPositionalValues(key).build());
    Row row = rs.iterator().next();
    assertThat(row.getUuid("k")).isEqualTo(key);
    assertThat(row.getUdtValue("u")).isEqualTo(value);
    assertThat(row.getUdtValue(1)).isEqualTo(value);
}
Also used : BatchStatementBuilder(com.datastax.oss.driver.api.core.cql.BatchStatementBuilder) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GenericType(com.datastax.oss.driver.api.core.type.reflect.GenericType) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ParallelizableTests(com.datastax.oss.driver.categories.ParallelizableTests) ImmutableMap(com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Uuids(com.datastax.oss.driver.api.core.uuid.Uuids) Map(java.util.Map) Row(com.datastax.oss.driver.api.core.cql.Row) DefaultBatchType(com.datastax.oss.driver.api.core.cql.DefaultBatchType) Sets(com.datastax.oss.driver.shaded.guava.common.collect.Sets) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) TupleType(com.datastax.oss.driver.api.core.type.TupleType) Preconditions(org.assertj.core.util.Preconditions) Set(java.util.Set) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) TupleValue(com.datastax.oss.driver.api.core.data.TupleValue) Test(org.junit.Test) UUID(java.util.UUID) BatchStatement(com.datastax.oss.driver.api.core.cql.BatchStatement) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Category(org.junit.experimental.categories.Category) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) SessionRule(com.datastax.oss.driver.api.testinfra.session.SessionRule) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) List(java.util.List) Ignore(org.junit.Ignore) TypeCodec(com.datastax.oss.driver.api.core.type.codec.TypeCodec) UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) PreparedStatement(com.datastax.oss.driver.api.core.cql.PreparedStatement) Row(com.datastax.oss.driver.api.core.cql.Row) UUID(java.util.UUID) BoundStatement(com.datastax.oss.driver.api.core.cql.BoundStatement) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 25 with UserDefinedType

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

the class CassandraConverter method get.

static Object get(ColumnDefinition definition, Row row) {
    String name = definition.getName().asInternal();
    final DataType type = definition.getType();
    if (type instanceof UserDefinedType) {
        return getUDT(definition, row.getUdtValue(name));
    }
    final TypeCodec<Object> codec = row.codecRegistry().codecFor(type);
    return row.get(name, codec);
}
Also used : DataType(com.datastax.oss.driver.api.core.type.DataType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType)

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