DATACASS-172 - Support for UDTs.

We now support Cassandra User-defined types. UDTs can be created using CQL generators and used inside of mapped domain classes. User-defined types can be used either raw as UDTValue that is passed through or as mapped object. Mapped UDTs must be annotated with @UserDefinedType. Types are included into schema generation so known and defined UDTs are created before any tables are created. UDTs can be used with set and list collection types and in primary keys. UDTs can also be used in repository query methods as query predicates.
Updating UDTs will update the whole UDT.

@UserDefinedType
public class Address {
  String city;
  String country;
}

@Table
public class Person {

  @Id String id;
  Address address;
  UDTValue genericUdt;
}

The XML namespace support was extended with new schema versions to support provide a User Type resolver so UDTs can be resolved:

<cassandra:mapping>
  <cassandra:user-type-resolver keyspace-name="${cassandra.keyspace}" />
</cassandra:mapping>
This commit is contained in:
Mark Paluch
2016-09-07 13:12:46 +02:00
committed by John Blum
parent a36e7beecb
commit 2195374c68
82 changed files with 4922 additions and 627 deletions

View File

@@ -102,5 +102,4 @@ public class AlterUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspaceC
public void generationFailsWithoutFields() {
toCql(AlterUserTypeSpecification.alterType().name("hello"));
}
}

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.cassandra.core.cql.generator.AlterUserTypeCqlGenerator.*;
import org.junit.Test;
@@ -40,7 +39,7 @@ public class AlterUserTypeCqlGeneratorUnitTests {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.add("zip", DataType.varchar());
assertThat(toCql(spec), is(equalTo("ALTER TYPE address ADD zip varchar;")));
assertThat(toCql(spec)).isEqualTo("ALTER TYPE address ADD zip varchar;");
}
/**
@@ -52,7 +51,7 @@ public class AlterUserTypeCqlGeneratorUnitTests {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.alter("zip", DataType.varchar());
assertThat(toCql(spec), is(equalTo("ALTER TYPE address ALTER zip TYPE varchar;")));
assertThat(toCql(spec)).isEqualTo("ALTER TYPE address ALTER zip TYPE varchar;");
}
/**
@@ -64,7 +63,7 @@ public class AlterUserTypeCqlGeneratorUnitTests {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.rename("zip", "zap");
assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap;")));
assertThat(toCql(spec)).isEqualTo("ALTER TYPE address RENAME zip TO zap;");
}
/**
@@ -77,7 +76,7 @@ public class AlterUserTypeCqlGeneratorUnitTests {
.rename("zip", "zap") //
.rename("city", "county");
assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap AND city TO county;")));
assertThat(toCql(spec)).isEqualTo("ALTER TYPE address RENAME zip TO zap AND city TO county;");
}
/**

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*;
import org.junit.Before;
@@ -57,7 +56,7 @@ public class CreateUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspace
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
UserType address = keyspace.getUserType("address");
assertThat(address.getFieldNames(), contains("zip", "city"));
assertThat(address.getFieldNames()).contains("zip", "city");
}
/**
@@ -75,7 +74,7 @@ public class CreateUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspace
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
UserType address = keyspace.getUserType("address");
assertThat(address.getFieldNames(), contains("zip", "city"));
assertThat(address.getFieldNames()).contains("zip", "city");
}
/**

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*;
import org.junit.Test;
@@ -41,7 +40,7 @@ public class CreateUserTypeCqlGeneratorUnitTests {
.createType("address") //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE address (city varchar);")));
assertThat(toCql(spec)).isEqualTo("CREATE TYPE address (city varchar);");
}
/**
@@ -55,7 +54,7 @@ public class CreateUserTypeCqlGeneratorUnitTests {
.field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE address (zip ascii, city varchar);")));
assertThat(toCql(spec)).isEqualTo("CREATE TYPE address (zip ascii, city varchar);");
}
/**
@@ -69,7 +68,7 @@ public class CreateUserTypeCqlGeneratorUnitTests {
.name("address").ifNotExists().field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE IF NOT EXISTS address (zip ascii, city varchar);")));
assertThat(toCql(spec)).isEqualTo("CREATE TYPE IF NOT EXISTS address (zip ascii, city varchar);");
}
/**

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.cassandra.core.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.cassandra.core.cql.generator.DropUserTypeCqlGenerator.*;
import org.junit.Test;
@@ -29,19 +28,25 @@ import org.springframework.cassandra.core.keyspace.DropUserTypeSpecification;
*/
public class DropUserTypeCqlGeneratorUnitTests {
/**
* @see DATACASS-172
*/
@Test
public void shouldDropUserType() throws Exception {
public void shouldDropUserType() {
DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address");
assertThat(toCql(spec), is(equalTo("DROP TYPE address;")));
assertThat(toCql(spec)).isEqualTo("DROP TYPE address;");
}
/**
* @see DATACASS-172
*/
@Test
public void shouldDropUserTypeIfExists() throws Exception {
public void shouldDropUserTypeIfExists() {
DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address").ifExists();
assertThat(toCql(spec), is(equalTo("DROP TYPE IF EXISTS address;")));
assertThat(toCql(spec)).isEqualTo("DROP TYPE IF EXISTS address;");
}
}

View File

@@ -41,7 +41,7 @@ import com.datastax.driver.core.policies.AddressTranslator;
import com.datastax.driver.core.policies.SpeculativeExecutionPolicy;
/**
* Test XML namespace configuration using the spring-cql-1.0.xsd.
* Test XML namespace configuration using the spring-cql XSD.
*
* @author Matthews T. Adams
* @author Oliver Gierke