diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGenerator.java index 267ff5349..2784e4335 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGenerator.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,31 +15,52 @@ */ package org.springframework.cassandra.core.cql.generator; -import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; +import static org.springframework.cassandra.core.cql.CqlStringUtils.*; import org.springframework.cassandra.core.keyspace.AddColumnSpecification; import org.springframework.cassandra.core.keyspace.AlterColumnSpecification; import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification; import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification; -import org.springframework.cassandra.core.keyspace.DropColumnSpecification; +import org.springframework.cassandra.core.keyspace.RenameColumnSpecification; +import org.springframework.util.Assert; /** - * CQL generator for generating ALTER TYPE statements. - * + * CQL generator for generating {@code ALTER TYPE} statements. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see AlterUserTypeSpecification + * @see AddColumnSpecification + * @see RenameColumnSpecification + * @see AlterColumnSpecification */ -public class AlterUserTypeCqlGenerator extends UserTypeCqlGenerator { +public class AlterUserTypeCqlGenerator extends UserTypeNameCqlGenerator { public static String toCql(AlterUserTypeSpecification specification) { return new AlterUserTypeCqlGenerator(specification).toCql(); } + /** + * Creates a new {@link AlterUserTypeCqlGenerator} for a {@link AlterUserTypeSpecification}. + * + * @param specification must not be {@literal null}. + */ public AlterUserTypeCqlGenerator(AlterUserTypeSpecification specification) { super(specification); } + /* + * (non-Javadoc) + * @see org.springframework.cassandra.core.cql.generator.UserTypeNameCqlGenerator#toCql(java.lang.StringBuilder) + */ @Override public StringBuilder toCql(StringBuilder cql) { + + Assert.notNull(getSpecification().getName(), "User type name must not be null"); + Assert.isTrue(!getSpecification().getChanges().isEmpty(), + String.format("User type [%s] does not contain fields", getSpecification().getName())); + cql = noNull(cql); preambleCql(cql); @@ -50,37 +71,46 @@ public class AlterUserTypeCqlGenerator extends UserTypeCqlGenerator getCqlGeneratorFor(ColumnChangeSpecification change) { + private ColumnChangeCqlGenerator getCqlGeneratorFor(ColumnChangeSpecification change, + boolean lastChangeWasRename) { + if (change instanceof AddColumnSpecification) { return new AddColumnCqlGenerator((AddColumnSpecification) change); } - if (change instanceof DropColumnSpecification) { - return new DropColumnCqlGenerator((DropColumnSpecification) change); + + if (change instanceof RenameColumnSpecification) { + return new RenameColumnCqlGenerator(lastChangeWasRename ? "AND" : RenameColumnCqlGenerator.RENAME, change); } + if (change instanceof AlterColumnSpecification) { return new AlterColumnCqlGenerator((AlterColumnSpecification) change); } - throw new IllegalArgumentException("unknown ColumnChangeSpecification type: " + change.getClass().getName()); - } + throw new IllegalArgumentException( + String.format("Unknown ColumnChangeSpecification type: %s", change.getClass().getName())); + } } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGenerator.java index b7d79f737..ca668723d 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGenerator.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,29 +15,46 @@ */ package org.springframework.cassandra.core.cql.generator; -import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull; +import static org.springframework.cassandra.core.cql.CqlStringUtils.*; -import org.springframework.cassandra.core.keyspace.ColumnSpecification; import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification; +import org.springframework.cassandra.core.keyspace.FieldSpecification; +import org.springframework.util.Assert; /** - * CQL generator for generating a CREATE TYPE statement. - * + * CQL generator for generating a {@code CREATE TYPE} statement. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see CreateUserTypeSpecification */ -public class CreateUserTypeCqlGenerator extends UserTypeCqlGenerator { +public class CreateUserTypeCqlGenerator extends UserTypeNameCqlGenerator { public static String toCql(CreateUserTypeSpecification specification) { return new CreateUserTypeCqlGenerator(specification).toCql(); } + /** + * Creates a new {@link CreateUserTypeCqlGenerator} for a given {@link CreateUserTypeSpecification}. + * + * @param specification must not be {@literal null}. + */ public CreateUserTypeCqlGenerator(CreateUserTypeSpecification specification) { super(specification); } + /* + * (non-Javadoc) + * @see org.springframework.cassandra.core.cql.generator.UserTypeNameCqlGenerator#toCql(java.lang.StringBuilder) + */ @Override public StringBuilder toCql(StringBuilder cql) { + Assert.notNull(getSpecification().getName(), "User type name must not be null"); + Assert.isTrue(!getSpecification().getFields().isEmpty(), + String.format("User type [%s] does not contain fields", getSpecification().getName())); + cql = noNull(cql); preambleCql(cql); @@ -48,21 +65,29 @@ public class CreateUserTypeCqlGenerator extends UserTypeCqlGeneratorDROP TYPE statement. - * + * CQL generator for generating a {@code DROP TYPE} statement. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see DropUserTypeSpecification */ public class DropUserTypeCqlGenerator extends UserTypeNameCqlGenerator { @@ -30,14 +33,22 @@ public class DropUserTypeCqlGenerator extends UserTypeNameCqlGenerator { + static final String RENAME = "RENAME"; + + final String keyword; + RenameColumnCqlGenerator(RenameColumnSpecification specification) { + this(RENAME, specification); + } + + public RenameColumnCqlGenerator(String keyword, ColumnChangeSpecification specification) { super(specification); + this.keyword = keyword; } /* (non-Javadoc) * @see org.springframework.cassandra.core.cql.generator.ColumnChangeCqlGenerator#toCql(java.lang.StringBuilder) */ public StringBuilder toCql(StringBuilder cql) { - return noNull(cql).append("RENAME ").append(spec().getName()).append(" TO ").append(spec().getTargetName()); + return noNull(cql).append(keyword).append(' ').append(spec().getName()).append(" TO ") + .append(spec().getTargetName()); } } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/UserTypeNameCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/UserTypeNameCqlGenerator.java index e25991c68..ab62bca9d 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/UserTypeNameCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/UserTypeNameCqlGenerator.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,9 +19,13 @@ import org.springframework.cassandra.core.keyspace.UserTypeNameSpecification; import org.springframework.util.Assert; /** - * @author Fabio J. Mendes + * Abstract class to support User type CQL generation. * - * @param + * @author Fabio J. Mendes + * @author Mark Paluch + * @param specification type + * @since 1.5 + * @see UserTypeNameSpecification */ public abstract class UserTypeNameCqlGenerator> { @@ -29,12 +33,24 @@ public abstract class UserTypeNameCqlGenerator specification; + /** + * Creates a new {@link UserTypeNameCqlGenerator}. + * + * @param specification must not be {@literal null}. + */ public UserTypeNameCqlGenerator(UserTypeNameSpecification specification) { setSpecification(specification); } - protected void setSpecification(UserTypeNameSpecification specification) { - Assert.notNull(specification); + /** + * Sets the {@link UserTypeNameSpecification}. + * + * @param specification must not be {@literal null}. + */ + protected final void setSpecification(UserTypeNameSpecification specification) { + + Assert.notNull(specification, "UserTypeNameSpecification must not be null"); + this.specification = specification; } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterColumnSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterColumnSpecification.java index 98b77b1f0..33cf7d6f6 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterColumnSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterColumnSpecification.java @@ -19,6 +19,13 @@ import org.springframework.cassandra.core.cql.CqlIdentifier; import com.datastax.driver.core.DataType; +/** + * Value object for altering a column. + * + * @author Matthew T. Adams + * @author Mark Paluch + * @see CqlIdentifier + */ public class AlterColumnSpecification extends ColumnTypeChangeSpecification { public AlterColumnSpecification(String name, DataType type) { diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterUserTypeSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterUserTypeSpecification.java index 75b033fd1..9fae05a90 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterUserTypeSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterUserTypeSpecification.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -24,11 +24,16 @@ import org.springframework.cassandra.core.cql.CqlIdentifier; import com.datastax.driver.core.DataType; /** - * Builder class to construct an ALTER TYPE specification. - * + * Builder class to construct an {@code ALTER TYPE} specification. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see CqlIdentifier */ -public class AlterUserTypeSpecification extends UserTypeSpecification { +public class AlterUserTypeSpecification extends UserTypeNameSpecification { + + private final List changes = new ArrayList(); /** * Entry point into the {@link AlterUserTypeSpecification}'s fluent API to alter a type. Convenient if imported @@ -42,56 +47,92 @@ public class AlterUserTypeSpecification extends UserTypeSpecification changes = new ArrayList(); - - /* - * Adds a DROP to the list of column changes. + * Adds an {@literal ADD} to the list of field changes. * - * DW Removed as this only works in C* 2.0 + * @param field must not be empty or {@literal null}. + * @param type must not be {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. */ - // public AlterUserTypeSpecification drop(String column) { - // changes.add(new DropColumnSpecification(column)); - // return this; - // } + public AlterUserTypeSpecification add(String field, DataType type) { + return add(CqlIdentifier.cqlId(field), type); + } /** - * Adds an ADD to the list of column changes. + * Adds an {@literal ADD} to the list of field changes. + * + * @param field must not be {@literal null}. + * @param type must not be {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. */ - public AlterUserTypeSpecification add(String column, DataType type) { - changes.add(new AddColumnSpecification(column, type)); + public AlterUserTypeSpecification add(CqlIdentifier field, DataType type) { + + changes.add(new AddColumnSpecification(field, type)); return this; } /** - * Adds an ALTER to the list of column changes. + * Adds an {@literal ALTER} to the list of field changes. + * + * @param field must not be empty or {@literal null}. + * @param type must not be {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. */ - public AlterUserTypeSpecification alter(String column, DataType type) { - changes.add(new AlterColumnSpecification(column, type)); + public AlterUserTypeSpecification alter(String field, DataType type) { + return alter(CqlIdentifier.cqlId(field), type); + } + + /** + * Adds an {@literal ALTER} to the list of field changes. + * + * @param field must not be {@literal null}. + * @param type must not be {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. + */ + public AlterUserTypeSpecification alter(CqlIdentifier field, DataType type) { + + changes.add(new AlterColumnSpecification(field, type)); return this; } /** - * Returns an unmodifiable list of column changes. + * Adds an {@literal RENAME} to the list of field changes. + * + * @param from must not be empty or {@literal null}. + * @param to must not be empty or {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. + */ + public AlterUserTypeSpecification rename(String from, String to) { + return rename(CqlIdentifier.cqlId(from), CqlIdentifier.cqlId(to)); + } + + /** + * Adds an {@literal RENAME} to the list of field changes. + * + * @param from must not be {@literal null}. + * @param to must not be empty or {@literal null}. + * @return {@code this} {@link AlterUserTypeSpecification}. + */ + public AlterUserTypeSpecification rename(CqlIdentifier from, CqlIdentifier to) { + + changes.add(new RenameColumnSpecification(from, to)); + return this; + } + + /** + * @return an unmodifiable list of field changes. */ public List getChanges() { return Collections.unmodifiableList(changes); diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/CreateUserTypeSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/CreateUserTypeSpecification.java index b15068276..749ca1913 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/CreateUserTypeSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/CreateUserTypeSpecification.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,12 +18,17 @@ package org.springframework.cassandra.core.keyspace; import org.springframework.cassandra.core.cql.CqlIdentifier; /** - * Builder class to construct a CREATE TYPE specification. - * + * Builder class to construct a {@code CREATE TYPE} specification. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see CqlIdentifier */ public class CreateUserTypeSpecification extends UserTypeSpecification { + private boolean ifNotExists; + /** * Entry point into the {@link CreateUserTypeSpecification}'s fluent API to create a type. Convenient if imported * statically. @@ -37,7 +42,7 @@ public class CreateUserTypeSpecification extends UserTypeSpecificationIF NOT EXISTS clause. + * Enables the inclusion of an{@code IF NOT EXISTS} clause. * - * @return this + * @return this {@link CreateUserTypeSpecification}. */ public CreateUserTypeSpecification ifNotExists() { return ifNotExists(true); } /** - * Toggles the inclusion of an IF NOT EXISTS clause. - * - * @return this + * Sets the inclusion of an {@code IF NOT EXISTS} clause. + * + * @param ifNotExists {@literal true} to include an {@code IF NOT EXISTS} clause, {@literal false} to omit the + * {@code IF NOT EXISTS} clause. + * @return this {@link CreateUserTypeSpecification}. */ public CreateUserTypeSpecification ifNotExists(boolean ifNotExists) { + this.ifNotExists = ifNotExists; return this; } + /** + * @return {@literal true} if the {@code IF NOT EXISTS} clause is included. + */ public boolean getIfNotExists() { return ifNotExists; } - - @Override - public CreateUserTypeSpecification name(String name) { - return (CreateUserTypeSpecification) super.name(name); - } } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/DropUserTypeSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/DropUserTypeSpecification.java index a0e5123c8..e981d446d 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/DropUserTypeSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/DropUserTypeSpecification.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,28 +18,16 @@ package org.springframework.cassandra.core.keyspace; import org.springframework.cassandra.core.cql.CqlIdentifier; /** - * Builder class that supports the construction of DROP TYPE specifications. - * + * Builder class that supports the construction of {@code DROP TYPE} specifications. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see CqlIdentifier */ public class DropUserTypeSpecification extends UserTypeNameSpecification { - // private boolean ifExists; - - // Added in Cassandra 2.0. - - // public DropTableSpecification ifExists() { - // return ifExists(true); - // } - // - // public DropTableSpecification ifExists(boolean ifExists) { - // this.ifExists = ifExists; - // return this; - // } - // - // public boolean getIfExists() { - // return ifExists; - // } + private boolean ifExists; /** * Entry point into the {@link DropUserTypeSpecification}'s fluent API to drop a type. Convenient if imported @@ -51,23 +39,50 @@ public class DropUserTypeSpecification extends UserTypeNameSpecificationdropType().name(typeName). + * statically. * * @param typeName The name of the type to drop. */ public static DropUserTypeSpecification dropType(CqlIdentifier typeName) { - return new DropUserTypeSpecification().name(typeName); + return dropType().name(typeName); } /** * Entry point into the {@link DropUserTypeSpecification}'s fluent API to drop a type. Convenient if imported - * statically. This static method is shorter than the no-arg form, which would be - * dropType().name(typeName). + * statically. * * @param typeName The name of the type to drop. */ public static DropUserTypeSpecification dropType(String typeName) { - return new DropUserTypeSpecification().name(typeName); + return dropType(CqlIdentifier.cqlId(typeName)); + } + + /** + * Enables the inclusion of an{@code IF EXISTS} clause. + * + * @return this {@link DropUserTypeSpecification}. + */ + public DropUserTypeSpecification ifExists() { + return ifExists(true); + } + + /** + * Sets the inclusion of an {@code IF EXISTS} clause. + * + * @param ifNotExists {@literal true} to include an {@code IF EXISTS} clause, {@literal false} to omit the + * {@code IF NOT EXISTS} clause. + * @return this {@link DropUserTypeSpecification}. + */ + public DropUserTypeSpecification ifExists(boolean ifExists) { + + this.ifExists = ifExists; + return this; + } + + /** + * @return {@literal true} if the {@code IF EXISTS} clause is included. + */ + public boolean getIfExists() { + return ifExists; } } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/FieldSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/FieldSpecification.java new file mode 100644 index 000000000..3b997b49b --- /dev/null +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/FieldSpecification.java @@ -0,0 +1,94 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.keyspace; + +import static org.springframework.cassandra.core.cql.CqlIdentifier.*; +import static org.springframework.cassandra.core.cql.CqlStringUtils.*; + +import org.springframework.cassandra.core.cql.CqlIdentifier; +import org.springframework.util.Assert; + +import com.datastax.driver.core.DataType; + +/** + * Builder class to specify fields. + *

+ * A {@link FieldSpecification} consists of a name and a {@link DataType}. + * + * @author Mark Paluch + * @since 1.5 + * @see CqlIdentifier + */ +public class FieldSpecification { + + private CqlIdentifier name; + private DataType type; + + /** + * Sets the field name. + * + * @param name must not be empty or {@literal null}. + * @return {@code this} {@link FieldSpecification}. + */ + public FieldSpecification name(String name) { + return name(cqlId(name)); + } + + /** + * Sets the field name. + * + * @param name must not be {@literal null}. + * @return {@code this} {@link FieldSpecification}. + */ + public FieldSpecification name(CqlIdentifier name) { + + Assert.notNull(name, "CqlIdentifier must not be null"); + + this.name = name; + return this; + } + + /** + * Sets the column's type. + * + * @param type The data type of the field, must not be {@literal null}. + * @return {@code this} {@link FieldSpecification}. + */ + public FieldSpecification type(DataType type) { + + Assert.notNull(type, "DataType must not be null"); + + this.type = type; + return this; + } + + public String toCql() { + return toCql(null).toString(); + } + + public StringBuilder toCql(StringBuilder cql) { + return noNull(cql).append(name).append(" ").append(type); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return toCql(null).toString(); + } +} diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeNameSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeNameSpecification.java index b3cbdac44..5a452fbb0 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeNameSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeNameSpecification.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,40 +15,50 @@ */ package org.springframework.cassandra.core.keyspace; -import static org.springframework.cassandra.core.cql.CqlIdentifier.cqlId; - import org.springframework.cassandra.core.cql.CqlIdentifier; import org.springframework.util.Assert; /** * Abstract builder class to support the construction of user type specifications. - * + * * @author Fabio J. Mendes + * @author Mark Paluch * @param The subtype of the {@link UserTypeNameSpecification} + * @since 1.5 + * @see CqlIdentifier */ public abstract class UserTypeNameSpecification> { - /** - * The name of the type. - */ private CqlIdentifier name; /** * Sets the type name. * + * @param name must not be empty or {@literal null}. * @return this */ public T name(String name) { - return name(cqlId(name)); + return name(CqlIdentifier.cqlId(name)); } + /** + * Sets the type name. + * + * @param name must not be {@literal null}. + * @return this + */ @SuppressWarnings("unchecked") public T name(CqlIdentifier name) { - Assert.notNull(name); + + Assert.notNull(name, "Name must not be null"); + this.name = name; return (T) this; } + /** + * @return the user type name. + */ public CqlIdentifier getName() { return name; } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeSpecification.java index 8bfdd20b7..8b3d1689f 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/UserTypeSpecification.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -15,7 +15,7 @@ */ package org.springframework.cassandra.core.keyspace; -import static org.springframework.cassandra.core.cql.CqlIdentifier.cqlId; +import static org.springframework.cassandra.core.cql.CqlIdentifier.*; import java.util.ArrayList; import java.util.Collections; @@ -25,53 +25,48 @@ import org.springframework.cassandra.core.cql.CqlIdentifier; import com.datastax.driver.core.DataType; - /** - * Builder class to support the construction of user type specifications that have columns. This class can also be used as a - * standalone {@link UserTypeDescriptor}, independent of {@link CreateUserTypeSpecification}. - * + * Builder class to support the construction of user type specifications that have columns. This class can also be used + * as a standalone {@link UserTypeSpecification}. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 + * @see CqlIdentifier */ -public class UserTypeSpecification extends UserTypeNameSpecification> implements UserTypeDescriptor { +public class UserTypeSpecification> extends UserTypeNameSpecification { + + private final List fields = new ArrayList(); /** - * List of all columns. - */ - private List columns = new ArrayList(); - - /** - * Adds the given column to the type. + * Adds the given field to the type. * - * @param name The column name; must be a valid unquoted or quoted identifier without the surrounding double quotes. - * @param type The data type of the column. + * @param name must not be empty or {@literal null}. + * @param type The data type of the field. + * @return {@code this} specification. */ - public T column(String name, DataType type) { - return column(cqlId(name), type, false); + public T field(String name, DataType type) { + return field(cqlId(name), type); } /** - * Adds the given column to the type. - * - * @param name The column name; must be a valid unquoted or quoted identifier without the surrounding double quotes. - * @param type The data type of the column. + * Adds an {@literal ADD} to the list of field changes. + * + * @param name must not {@literal null}. + * @param type must not be {@literal null}. + * @return {@code this} specification. */ - public T column(CqlIdentifier name, DataType type) { - return column(name, type, false); - } - @SuppressWarnings("unchecked") - protected T column(CqlIdentifier name, DataType type, boolean frozen) { - ColumnSpecification column = new ColumnSpecification().name(name).type(type); - columns.add(column); + public T field(CqlIdentifier name, DataType type) { + + fields.add(new FieldSpecification().name(name).type(type)); return (T) this; } /** - * Returns an unmodifiable list of all columns. + * @return an unmodifiable list of all fields. */ - @Override - public List getColumns() { - return Collections.unmodifiableList(columns); + public List getFields() { + return Collections.unmodifiableList(fields); } - } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorIntegrationTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorIntegrationTests.java new file mode 100644 index 000000000..4d3c7f918 --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorIntegrationTests.java @@ -0,0 +1,106 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.cql.generator; + +import static org.springframework.cassandra.core.cql.generator.AlterUserTypeCqlGenerator.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; + +import com.datastax.driver.core.DataType; + +/** + * Integration tests for {@link AlterUserTypeCqlGenerator}. + * + * @author Mark Paluch + */ +public class AlterUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { + + @Before + public void setUp() throws Exception { + + session.execute("DROP TYPE IF EXISTS address;"); + session.execute("CREATE TYPE address (zip text, state text);"); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldAddField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")// + .add("street", DataType.varchar()); + + session.execute(toCql(spec)); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldAlterField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")// + .alter("zip", DataType.varchar()); + + session.execute(toCql(spec)); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldRenameField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")// + .rename("zip", "zap"); + + session.execute(toCql(spec)); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldRenameFields() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")// + .rename("zip", "zap") // + .rename("state", "county"); + + session.execute(toCql(spec)); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsIfNameIsNotSet() { + toCql(AlterUserTypeSpecification.alterType()); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsWithoutFields() { + toCql(AlterUserTypeSpecification.alterType().name("hello")); + } + +} diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorUnitTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorUnitTests.java new file mode 100644 index 000000000..738da7306 --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterUserTypeCqlGeneratorUnitTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.cql.generator; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.cassandra.core.cql.generator.AlterUserTypeCqlGenerator.*; + +import org.junit.Test; +import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification; + +import com.datastax.driver.core.DataType; + +/** + * Unit tests for {@link AlterUserTypeCqlGenerator}. + * + * @author Mark Paluch + */ +public class AlterUserTypeCqlGeneratorUnitTests { + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldAddField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") // + .add("zip", DataType.varchar()); + + assertThat(toCql(spec), is(equalTo("ALTER TYPE address ADD zip varchar;"))); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldAlterField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") // + .alter("zip", DataType.varchar()); + + assertThat(toCql(spec), is(equalTo("ALTER TYPE address ALTER zip TYPE varchar;"))); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldRenameField() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") // + .rename("zip", "zap"); + + assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap;"))); + } + + /** + * @see DATACASS-172 + */ + @Test + public void alterTypeShouldRenameFields() { + + AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") // + .rename("zip", "zap") // + .rename("city", "county"); + + assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap AND city TO county;"))); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsIfNameIsNotSet() { + toCql(AlterUserTypeSpecification.alterType()); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsWithoutFields() { + toCql(AlterUserTypeSpecification.alterType().name("hello")); + } +} diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorIntegrationTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorIntegrationTests.java new file mode 100644 index 000000000..29214597d --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorIntegrationTests.java @@ -0,0 +1,104 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.cql.generator; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification; +import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest; + +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.KeyspaceMetadata; +import com.datastax.driver.core.UserType; + +/** + * Integration tests for {@link CreateUserTypeCqlGenerator}. + * + * @author Mark Paluch + */ +public class CreateUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest { + + @Before + public void setUp() throws Exception { + + session.execute("DROP TYPE IF EXISTS person;"); + session.execute("DROP TYPE IF EXISTS address;"); + } + + /** + * @see DATACASS-172 + */ + @Test + public void createUserType() { + + CreateUserTypeSpecification spec = CreateUserTypeSpecification // + .createType("address") // + .field("zip", DataType.ascii()) // + .field("city", DataType.varchar()); + + session.execute(toCql(spec)); + + KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace()); + UserType address = keyspace.getUserType("address"); + assertThat(address.getFieldNames(), contains("zip", "city")); + } + + /** + * @see DATACASS-172 + */ + @Test + public void createUserTypeIfNotExists() { + + CreateUserTypeSpecification spec = CreateUserTypeSpecification // + .createType() // + .name("address").ifNotExists().field("zip", DataType.ascii()) // + .field("city", DataType.varchar()); + + session.execute(toCql(spec)); + + KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace()); + UserType address = keyspace.getUserType("address"); + assertThat(address.getFieldNames(), contains("zip", "city")); + } + + /** + * @see DATACASS-172 + */ + @Test + public void createNestedUserType() { + + CreateUserTypeSpecification addressSpec = CreateUserTypeSpecification // + .createType() // + .name("address").ifNotExists().field("zip", DataType.ascii()) // + .field("city", DataType.varchar()); + + session.execute(toCql(addressSpec)); + + KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace()); + UserType address = keyspace.getUserType("address"); + + CreateUserTypeSpecification personSpec = CreateUserTypeSpecification // + .createType() // + .name("person").ifNotExists().field("address", address) // + .field("city", DataType.varchar()); + + session.execute(toCql(personSpec)); + } +} diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorUnitTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorUnitTests.java new file mode 100644 index 000000000..29652ab00 --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/CreateUserTypeCqlGeneratorUnitTests.java @@ -0,0 +1,90 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.cql.generator; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*; + +import org.junit.Test; +import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification; + +import com.datastax.driver.core.DataType; + +/** + * Unit tests for {@link CreateUserTypeCqlGenerator}. + * + * @author Mark Paluch + */ +public class CreateUserTypeCqlGeneratorUnitTests { + + /** + * @see DATACASS-172 + */ + @Test + public void createUserType() { + + CreateUserTypeSpecification spec = CreateUserTypeSpecification // + .createType("address") // + .field("city", DataType.varchar()); + + assertThat(toCql(spec), is(equalTo("CREATE TYPE address (city varchar);"))); + } + + /** + * @see DATACASS-172 + */ + @Test + public void createMultiFieldUserType() { + + CreateUserTypeSpecification spec = CreateUserTypeSpecification // + .createType("address") // + .field("zip", DataType.ascii()) // + .field("city", DataType.varchar()); + + assertThat(toCql(spec), is(equalTo("CREATE TYPE address (zip ascii, city varchar);"))); + } + + /** + * @see DATACASS-172 + */ + @Test + public void createUserTypeIfNotExists() { + + CreateUserTypeSpecification spec = CreateUserTypeSpecification // + .createType() // + .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);"))); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsIfNameIsNotSet() { + toCql(CreateUserTypeSpecification.createType()); + } + + /** + * @see DATACASS-172 + */ + @Test(expected = IllegalArgumentException.class) + public void generationFailsWithoutFields() { + toCql(CreateUserTypeSpecification.createType().name("hello")); + } +} diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/DropUserTypeCqlGeneratorUnitTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/DropUserTypeCqlGeneratorUnitTests.java new file mode 100644 index 000000000..631d739ba --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/DropUserTypeCqlGeneratorUnitTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.core.cql.generator; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; +import static org.springframework.cassandra.core.cql.generator.DropUserTypeCqlGenerator.*; + +import org.junit.Test; +import org.springframework.cassandra.core.keyspace.DropUserTypeSpecification; + +/** + * Unit tests for {@link DropUserTypeCqlGenerator}. + * + * @author Mark Paluch + */ +public class DropUserTypeCqlGeneratorUnitTests { + + @Test + public void shouldDropUserType() throws Exception { + + DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address"); + + assertThat(toCql(spec), is(equalTo("DROP TYPE address;"))); + } + + @Test + public void shouldDropUserTypeIfExists() throws Exception { + + DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address").ifExists(); + + assertThat(toCql(spec), is(equalTo("DROP TYPE IF EXISTS address;"))); + } +} diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java index 109a700e0..199e3e544 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors - * + * Copyright 2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * + * http://www.apache.org/licenses/LICENSE-2.0 + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -27,6 +27,8 @@ import com.datastax.driver.core.UserType; * * @author David Webb * @author Matthew T. Adams + * @author Mark Paluch + * @author Fabio J. Mendes */ public interface CassandraAdminOperations extends CassandraOperations { @@ -42,7 +44,8 @@ public interface CassandraAdminOperations extends CassandraOperations { * @param entityClass The class whose fields determine the columns created. * @param optionsByName Table options, given by the string option name and the appropriate option value. */ - void createTable(boolean ifNotExists, CqlIdentifier tableName, Class entityClass, Map optionsByName); + void createTable(boolean ifNotExists, CqlIdentifier tableName, Class entityClass, + Map optionsByName); /** * Add columns to the given table from the given class. If parameter dropRemovedAttributColumns is true, then this @@ -73,16 +76,21 @@ public interface CassandraAdminOperations extends CassandraOperations { void dropTable(CqlIdentifier tableName); /** - * @param keyspace - * @param tableName - * @return + * Lookup {@link TableMetadata}. + * + * @param keyspace must not be empty or {@literal null}. + * @param tableName must not be {@literal null}. + * @return the {@link TableMetadata} or {@literal null}. */ TableMetadata getTableMetadata(String keyspace, CqlIdentifier tableName); /** - * @param keyspace - * @param userTypeName - * @return + * Lookup {@link UserType} metadata. + * + * @param keyspace must not be empty or {@literal null}. + * @param userTypeName must not be {@literal null}. + * @return the {@link UserType} or {@literal null}. + * @since 1.5 */ UserType getUserTypeMetadata(String keyspace, CqlIdentifier userTypeName); } diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java index 28a3dca14..8da134734 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java @@ -1,11 +1,11 @@ /* - * Copyright 2013-2016 the original author or authors + * Copyright 2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -38,6 +38,7 @@ import com.datastax.driver.core.UserType; * Default implementation of {@link CassandraAdminOperations}. * * @author Mark Paluch + * @author Fabio J. Mendes */ public class CassandraAdminTemplate extends CassandraTemplate implements CassandraAdminOperations { @@ -53,6 +54,10 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand super(session, converter); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#createTable(boolean, org.springframework.cassandra.core.cql.CqlIdentifier, java.lang.Class, java.util.Map) + */ @Override public void createTable(final boolean ifNotExists, final CqlIdentifier tableName, Class entityClass, Map optionsByName) { @@ -63,8 +68,8 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand @Override public Object doInSession(Session s) throws DataAccessException { - String cql = new CreateTableCqlGenerator(getCassandraMappingContext(). - getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists)).toCql(); + String cql = new CreateTableCqlGenerator( + getCassandraMappingContext().getCreateTableSpecificationFor(entity).ifNotExists(ifNotExists)).toCql(); log.debug(cql); @@ -74,11 +79,19 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand }); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#alterTable(org.springframework.cassandra.core.cql.CqlIdentifier, java.lang.Class, boolean) + */ @Override public void alterTable(CqlIdentifier tableName, Class entityClass, boolean dropRemovedAttributeColumns) { throw new UnsupportedOperationException("not yet implemented"); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#replaceTable(org.springframework.cassandra.core.cql.CqlIdentifier, java.lang.Class, java.util.Map) + */ @Override public void replaceTable(CqlIdentifier tableName, Class entityClass, Map optionsByName) { @@ -120,6 +133,10 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand dropTable(getTableName(entityClass)); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#dropTable(org.springframework.cassandra.core.cql.CqlIdentifier) + */ @Override public void dropTable(CqlIdentifier tableName) { @@ -128,10 +145,15 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand execute(DropTableSpecification.dropTable(tableName)); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#getTableMetadata(java.lang.String, org.springframework.cassandra.core.cql.CqlIdentifier) + */ @Override public TableMetadata getTableMetadata(final String keyspace, final CqlIdentifier tableName) { - Assert.notNull(tableName); + Assert.hasText(keyspace, "Keyspace name must not be empty"); + Assert.notNull(tableName, "Table name must not be null"); return execute(new SessionCallback() { @Override @@ -141,10 +163,15 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand }); } + /* + * (non-Javadoc) + * @see org.springframework.data.cassandra.core.CassandraAdminOperations#getUserTypeMetadata(java.lang.String, org.springframework.cassandra.core.cql.CqlIdentifier) + */ @Override public UserType getUserTypeMetadata(final String keyspace, final CqlIdentifier userTypeName) { - Assert.notNull(userTypeName); + Assert.hasText(keyspace, "Keyspace name must not be empty"); + Assert.notNull(userTypeName, "User type name must not be null"); return execute(new SessionCallback() { @Override diff --git a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/UserDefinedType.java b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/UserDefinedType.java index 6e7a3f007..aedead209 100644 --- a/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/UserDefinedType.java +++ b/spring-data-cassandra/src/main/java/org/springframework/data/cassandra/mapping/UserDefinedType.java @@ -22,7 +22,12 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** + * Identifies a domain object as Cassandra User Defined type (UDT). User defined types may contain nested user defined + * types. + * * @author Fabio J. Mendes + * @author Mark Paluch + * @since 1.5 */ @Inherited @Retention(RetentionPolicy.RUNTIME) @@ -30,7 +35,7 @@ import java.lang.annotation.Target; public @interface UserDefinedType { /** - * The name of the UDT; must be a valid CQL identifier or quoted identifier. + * The name of the UDT. Must be a valid CQL identifier or quoted identifier. */ String value() default "";