Search in sources :

Example 1 with CreateUserTypeSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification in project spring-data-cassandra by spring-projects.

the class MappingCassandraConverterTupleIntegrationTests method setUp.

@BeforeEach
void setUp() {
    if (initialized.compareAndSet(false, true)) {
        this.session.execute("DROP TYPE IF EXISTS address;");
        this.session.execute("DROP TABLE IF EXISTS person;");
        CassandraMappingContext mappingContext = converter.getMappingContext();
        SchemaFactory schemaFactory = new SchemaFactory(converter);
        CreateUserTypeSpecification createAddress = schemaFactory.getCreateUserTypeSpecificationFor(mappingContext.getRequiredPersistentEntity(AddressUserType.class));
        this.session.execute(CreateUserTypeCqlGenerator.toCql(createAddress));
        String ddl = // 
        "CREATE TABLE person (id text, " + "tuplevalue tuple<text,int>," + // 
        "mapoftuples map<text, frozen<tuple<address, list<text>, text>>>, " + // 
        "mapoftuplevalues map<text, frozen<tuple<text, int>>>, " + // 
        "mappedtuple frozen<tuple<address, list<text>, text>>, " + // 
        "mappedtuplewithvalue frozen<tuple<address, list<text>, text>>, " + // 
        "mappedtuples list<frozen<tuple<address, list<text>, text>>>, " + "PRIMARY KEY (id));";
        this.session.execute(ddl);
    } else {
        this.session.execute("TRUNCATE person;");
    }
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CreateUserTypeSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification in project spring-data-cassandra by spring-projects.

the class CreateUserTypeCqlGeneratorIntegrationTests method createUserTypeIfNotExists.

// DATACASS-172
@Test
void createUserTypeIfNotExists() {
    CreateUserTypeSpecification spec = // 
    CreateUserTypeSpecification.createType("address").ifNotExists().field("zip", // 
    DataTypes.ASCII).field("city", DataTypes.TEXT);
    session.execute(toCql(spec));
    KeyspaceMetadata keyspace = session.getMetadata().getKeyspace(session.getKeyspace().get()).get();
    UserDefinedType address = keyspace.getUserDefinedType("address").get();
    assertThat(address.getFieldNames()).contains(CqlIdentifier.fromCql("zip"), CqlIdentifier.fromCql("city"));
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Test(org.junit.jupiter.api.Test)

Example 3 with CreateUserTypeSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification in project spring-data-cassandra by spring-projects.

the class CreateUserTypeCqlGeneratorIntegrationTests method createNestedUserType.

// DATACASS-172, DATACASS-424
@Test
void createNestedUserType() {
    CreateUserTypeSpecification addressSpec = // 
    CreateUserTypeSpecification.createType("address").ifNotExists().field("zip", // 
    DataTypes.ASCII).field("city", DataTypes.TEXT);
    session.execute(toCql(addressSpec));
    KeyspaceMetadata keyspace = session.getMetadata().getKeyspace(session.getKeyspace().get()).get();
    UserDefinedType address = keyspace.getUserDefinedType("address").get();
    CreateUserTypeSpecification personSpec = // 
    CreateUserTypeSpecification.createType("person").ifNotExists().field("address", // 
    address.copy(true)).field("city", DataTypes.TEXT);
    session.execute(toCql(personSpec));
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) KeyspaceMetadata(com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata) Test(org.junit.jupiter.api.Test)

Example 4 with CreateUserTypeSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification in project spring-data-cassandra by spring-projects.

the class CreateUserTypeCqlGeneratorUnitTests method createMultiFieldUserType.

// DATACASS-172
@Test
void createMultiFieldUserType() {
    CreateUserTypeSpecification spec = // 
    CreateUserTypeSpecification.createType(// 
    "address").field("zip", // 
    DataTypes.ASCII).field("city", DataTypes.TEXT);
    assertThat(toCql(spec)).isEqualTo("CREATE TYPE address (zip ascii, city text);");
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) Test(org.junit.jupiter.api.Test)

Example 5 with CreateUserTypeSpecification

use of org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification in project spring-data-cassandra by spring-projects.

the class SchemaTestUtils method potentiallyCreateUdtFor.

private static void potentiallyCreateUdtFor(CassandraPersistentEntity<?> persistentEntity, CassandraOperations operations, SchemaFactory schemaFactory) {
    if (persistentEntity.isUserDefinedType()) {
        CreateUserTypeSpecification udtspec = schemaFactory.getCreateUserTypeSpecificationFor(persistentEntity).ifNotExists();
        operations.getCqlOperations().execute(CreateUserTypeCqlGenerator.toCql(udtspec));
    } else {
        for (CassandraPersistentProperty property : persistentEntity) {
            if (!property.isEntity()) {
                continue;
            }
            if (property.isEmbedded()) {
                potentiallyCreateUdtFor(new EmbeddedEntityOperations(operations.getConverter().getMappingContext()).getEntity(property), operations, schemaFactory);
            } else {
                potentiallyCreateUdtFor(operations.getConverter().getMappingContext().getRequiredPersistentEntity(property), operations, schemaFactory);
            }
        }
    }
}
Also used : CreateUserTypeSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification) EmbeddedEntityOperations(org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)

Aggregations

CreateUserTypeSpecification (org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification)10 Test (org.junit.jupiter.api.Test)7 KeyspaceMetadata (com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata)3 UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)3 CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)2 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 DataType (com.datastax.oss.driver.api.core.type.DataType)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 EmbeddedEntityOperations (org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations)1 MappingException (org.springframework.data.mapping.MappingException)1