diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/CqlIdentifier.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/CqlIdentifier.java index 6b6def5a8..79887c6b7 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/CqlIdentifier.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/CqlIdentifier.java @@ -26,19 +26,18 @@ import com.datastax.driver.core.TableMetadata; /** * This encapsulates the logic for CQL quoted and unquoted identifiers. - *

- * CQL identifiers, when unquoted, are converted to lower case. When quoted, they are returned as-is with no lower + * + *

CQL identifiers, when unquoted, are converted to lower case. When quoted, they are returned as-is with no lower * casing and encased in double quotes. To render, use any of the methods {@link #toCql()}, * {@link #toCql(StringBuilder)}, or {@link #toString()}. - * - * @see #CqlIdentifier(String) - * @see #CqlIdentifier(String, boolean) - * @see #toCql() - * @see #toCql(StringBuilder) - * @see #toString() + * * @author John McPeek * @author Matthew T. Adams * @author Mark Paluch + * @author John Blum + * @see #toCql() + * @see #toCql(StringBuilder) + * @see #toString() */ public final class CqlIdentifier implements Comparable, Serializable { @@ -52,8 +51,8 @@ public final class CqlIdentifier implements Comparable, Serializa /** * Factory method for {@link CqlIdentifier}. Convenient if imported statically. - * - * @see #CqlIdentifier(String) + * + * @see #CqlIdentifier(CharSequence) */ public static CqlIdentifier cqlId(CharSequence identifier) { return new CqlIdentifier(identifier); @@ -61,8 +60,8 @@ public final class CqlIdentifier implements Comparable, Serializa /** * Factory method for {@link CqlIdentifier}. Convenient if imported statically. - * - * @see #CqlIdentifier(String) + * + * @see #CqlIdentifier(CharSequence, boolean) */ public static CqlIdentifier cqlId(CharSequence identifier, boolean forceQuote) { return new CqlIdentifier(identifier, forceQuote); @@ -70,8 +69,8 @@ public final class CqlIdentifier implements Comparable, Serializa /** * Factory method for a force-quoted {@link CqlIdentifier}. Convenient if imported statically. - * - * @see #CqlIdentifier(String, boolean) + * + * @see #CqlIdentifier(CharSequence, boolean) */ public static CqlIdentifier quotedCqlId(CharSequence identifier) { return new CqlIdentifier(identifier, true); @@ -97,8 +96,8 @@ public final class CqlIdentifier implements Comparable, Serializa /** * Creates a new {@link CqlIdentifier} without force-quoting it. It may end up quoted, depending on its value. - * - * @see #cqlId(String) + * + * @see #cqlId(CharSequence) */ public CqlIdentifier(CharSequence identifier) { this(identifier, false); @@ -113,9 +112,9 @@ public final class CqlIdentifier implements Comparable, Serializa * plus the name will be converted to lower case and rendered as such. *

  • If the given identifier is illegal, an {@link IllegalArgumentException} is thrown.
  • * - * - * @see #cqlId(String) - * @see #quotedCqlId(String) + * + * @see #cqlId(CharSequence, boolean) + * @see #quotedCqlId(CharSequence) */ public CqlIdentifier(CharSequence identifier, boolean forceQuote) { setIdentifier(identifier, forceQuote); @@ -129,6 +128,7 @@ public final class CqlIdentifier implements Comparable, Serializa Assert.notNull(identifier, "Identifier must not be null"); String string = identifier.toString(); + Assert.hasText(string, "Identifier must not be empty"); if (forceQuoting || isQuotedIdentifier(string)) { @@ -167,17 +167,8 @@ public final class CqlIdentifier implements Comparable, Serializa * {@link StringBuilder}. If null is given, a new {@link StringBuilder} is created, appended to, and * returned. */ - public StringBuilder toCql(StringBuilder sb) { - sb = sb == null ? new StringBuilder() : sb; - return sb.append(toCql()); - } - - /** - * Alias for {@link #toCql()}. - */ - @Override - public String toString() { - return toCql(); + public StringBuilder toCql(StringBuilder builder) { + return (builder != null ? builder : new StringBuilder()).append(toCql()); } /** @@ -187,9 +178,16 @@ public final class CqlIdentifier implements Comparable, Serializa return quoted; } + /** + * Unquoted identifiers sort before quoted ones. Otherwise, they compare according to their identifiers. + */ @Override - public int hashCode() { - return ((Boolean) quoted).hashCode() ^ identifier.hashCode(); + @SuppressWarnings("all") + public int compareTo(CqlIdentifier identifier) { + + int comparison = ((Boolean) this.quoted).compareTo(identifier.quoted); + + return (comparison != 0 ? comparison : this.identifier.compareTo(identifier.identifier)); } /** @@ -198,32 +196,32 @@ public final class CqlIdentifier implements Comparable, Serializa * a {@link CqlIdentifier}. */ @Override - public boolean equals(Object that) { - if (this == that) { + public boolean equals(Object obj) { + + if (this == obj) { return true; } - if (that == null) { - return false; - } - if (!(that instanceof CqlIdentifier) && !(that instanceof CharSequence)) { + + if (!(obj instanceof CqlIdentifier || obj instanceof CharSequence)) { return false; } - CqlIdentifier other = (that instanceof CqlIdentifier) ? (CqlIdentifier) that : cqlId((CharSequence) that); + CqlIdentifier that = (obj instanceof CqlIdentifier) ? (CqlIdentifier) obj : cqlId((CharSequence) obj); - return this.quoted == other.quoted && this.identifier.equals(other.identifier); + return (this.quoted == that.quoted && this.identifier.equals(that.identifier)); + } + + @Override + // TODO hmmm, re-evaluate this since it is not a proper hash code matching equals! + public int hashCode() { + return ((Boolean) quoted).hashCode() ^ identifier.hashCode(); } /** - * Unquoted identifiers sort before quoted ones. Otherwise, they compare according to their identifiers. + * Alias for {@link #toCql()}. */ @Override - public int compareTo(CqlIdentifier that) { - - int comparison = ((Boolean) this.quoted).compareTo(that.quoted); - if (comparison != 0) { - return comparison; - } - return this.identifier.compareTo(that.identifier); + public String toString() { + return toCql(); } } diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AddColumnCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AddColumnCqlGenerator.java index c4f9c7b46..c4597cd51 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AddColumnCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AddColumnCqlGenerator.java @@ -1,12 +1,12 @@ /* * Copyright 2013-2014 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. @@ -21,10 +21,11 @@ import org.springframework.cassandra.core.keyspace.AddColumnSpecification; /** * CQL generator for generating an {@code ADD} clause of an {@code ALTER TABLE} statement. - * + * * @author Matthew T. Adams * @author Mark Paluch * @see AddColumnSpecification + * @see ColumnChangeCqlGenerator * @see org.springframework.cassandra.core.keyspace.AlterTableSpecification */ public class AddColumnCqlGenerator extends ColumnChangeCqlGenerator { diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterColumnCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterColumnCqlGenerator.java index c7dcd1dea..efaedaaaf 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterColumnCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/AlterColumnCqlGenerator.java @@ -1,12 +1,12 @@ /* * Copyright 2013-2014 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. @@ -21,8 +21,10 @@ import org.springframework.cassandra.core.keyspace.AlterColumnSpecification; /** * CQL generator for generating an ALTER column clause of an ALTER TABLE statement. - * + * * @author Matthew T. Adams + * @see AlterColumnSpecification + * @see ColumnChangeCqlGenerator */ public class AlterColumnCqlGenerator extends ColumnChangeCqlGenerator { @@ -31,6 +33,7 @@ public class AlterColumnCqlGenerator extends ColumnChangeCqlGenerator { /** * Generates a CQL statement from the given {@code specification}. - * + * * @param specification must not be {@literal null}. * @return the generated CQL statement. */ @@ -49,7 +50,7 @@ public class AlterTableCqlGenerator extends TableOptionsCqlGenerator options = spec().getOptions(); + if (options == null || options.isEmpty()) { return cql; } cql.append("WITH "); + boolean first = true; + for (String key : options.keySet()) { /* * Compact storage is illegal on alter table. - * * TODO - Is there a way to handle this in the specification? */ if (key.equals(TableOption.COMPACT_STORAGE.getName())) { @@ -150,9 +154,11 @@ public class AlterTableCqlGenerator extends TableOptionsCqlGenerator { diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/RenameColumnCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/RenameColumnCqlGenerator.java index 2c4fd8806..332ea41bc 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/RenameColumnCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/RenameColumnCqlGenerator.java @@ -21,9 +21,10 @@ import org.springframework.cassandra.core.keyspace.RenameColumnSpecification; /** * CQL generator for generating an {@code RENAME} column clause of an {@code ALTER TABLE} statement. - * + * * @author Mark Paluch * @since 1.5 + * @see ColumnChangeCqlGenerator * @see RenameColumnSpecification * @see org.springframework.cassandra.core.keyspace.AlterTableSpecification */ diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AddColumnSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AddColumnSpecification.java index ecf8724a8..0a46bbaf4 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AddColumnSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AddColumnSpecification.java @@ -1,12 +1,12 @@ /* * Copyright 2013-2014 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. @@ -21,15 +21,16 @@ import com.datastax.driver.core.DataType; /** * Specification to add a column. - * + * * @author Matthew Adams * @author Mark Paluch + * @see ColumnTypeChangeSpecification */ public class AddColumnSpecification extends ColumnTypeChangeSpecification { /** * Creates a new {@link AddColumnSpecification} for the given {@code name} and {@link type} - * + * * @param name must not be empty or {@literal null}. * @param type must not be {@literal null}. */ @@ -39,7 +40,7 @@ public class AddColumnSpecification extends ColumnTypeChangeSpecification { /** * Creates a new {@link AddColumnSpecification} for the given {@code name} and {@link type} - * + * * @param name must not be {@literal null}. * @param type must not be {@literal null}. */ diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterTableSpecification.java b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterTableSpecification.java index 01d63a867..bcf875d7e 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterTableSpecification.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/keyspace/AlterTableSpecification.java @@ -1,12 +1,12 @@ /* * Copyright 2013-2014 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. @@ -25,7 +25,7 @@ import com.datastax.driver.core.DataType; /** * Builder class to construct an {@code ALTER TABLE} specification. - * + * * @author Matthew T. Adams * @author Mark Paluch * @see AddColumnSpecification @@ -34,6 +34,7 @@ import com.datastax.driver.core.DataType; * @see RenameColumnSpecification * @see CreateTableSpecification * @see DropTableSpecification + * @see TableOptionsSpecification * @see org.springframework.cassandra.core.cql.generator.AlterTableCqlGenerator */ public class AlterTableSpecification extends TableOptionsSpecification { @@ -69,7 +70,7 @@ public class AlterTableSpecification extends TableOptionsSpecification);"); - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").alter("lastKnownLocation", - DataType.list(DataType.varchar())); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .alter("lastKnownLocation", DataType.list(DataType.varchar())); execute(spec); ColumnMetadata column = getTableMetadata("addamsFamily").getColumn("lastKnownLocation"); + assertThat(column.getType(), is(equalTo((DataType) DataType.list(DataType.varchar())))); } @@ -94,12 +96,13 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea session.execute( "CREATE TABLE addamsFamily (name varchar PRIMARY KEY, gender varchar,\n" + " lastknownlocation varchar);"); - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").add("gravesite", - DataType.varchar()); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .add("gravesite", DataType.varchar()); execute(spec); ColumnMetadata column = getTableMetadata("addamsFamily").getColumn("gravesite"); + assertThat(column.getType(), is(equalTo(DataType.varchar()))); } @@ -111,12 +114,13 @@ public class AlterTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCrea session.execute("CREATE TABLE users (user_name varchar PRIMARY KEY);"); - AlterTableSpecification spec = AlterTableSpecification.alterTable("users").add("top_places", - DataType.list(DataType.ascii())); + AlterTableSpecification spec = AlterTableSpecification.alterTable("users") + .add("top_places", DataType.list(DataType.ascii())); execute(spec); ColumnMetadata column = getTableMetadata("users").getColumn("top_places"); + assertThat(column.getType(), is(equalTo((DataType) DataType.list(DataType.ascii())))); } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGeneratorUnitTests.java b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGeneratorUnitTests.java index 98a41e36a..30643d814 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGeneratorUnitTests.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/core/cql/generator/AlterTableCqlGeneratorUnitTests.java @@ -44,8 +44,8 @@ public class AlterTableCqlGeneratorUnitTests { @Test public void alterTableAlterColumnType() { - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").alter("lastKnownLocation", - DataType.uuid()); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .alter("lastKnownLocation", DataType.uuid()); assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily ALTER lastknownlocation TYPE uuid;"))); } @@ -56,8 +56,8 @@ public class AlterTableCqlGeneratorUnitTests { @Test public void alterTableAlterListColumnType() { - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").alter("lastKnownLocation", - DataType.list(DataType.ascii())); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .alter("lastKnownLocation", DataType.list(DataType.ascii())); assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily ALTER lastknownlocation TYPE list;"))); } @@ -68,8 +68,8 @@ public class AlterTableCqlGeneratorUnitTests { @Test public void alterTableAddColumn() { - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").add("gravesite", - DataType.varchar()); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .add("gravesite", DataType.varchar()); assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily ADD gravesite varchar;"))); } @@ -80,8 +80,8 @@ public class AlterTableCqlGeneratorUnitTests { @Test public void alterTableAddListColumn() { - AlterTableSpecification spec = AlterTableSpecification.alterTable("users").add("top_places", - DataType.list(DataType.ascii())); + AlterTableSpecification spec = AlterTableSpecification.alterTable("users") + .add("top_places", DataType.list(DataType.ascii())); assertThat(toCql(spec), is(equalTo("ALTER TABLE users ADD top_places list;"))); } @@ -103,7 +103,8 @@ public class AlterTableCqlGeneratorUnitTests { @Test public void alterTableRenameColumn() { - AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily").rename("firstname", "lastname"); + AlterTableSpecification spec = AlterTableSpecification.alterTable("addamsFamily") + .rename("firstname", "lastname"); assertThat(toCql(spec), is(equalTo("ALTER TABLE addamsfamily RENAME firstname TO lastname;"))); } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTests.java b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTests.java index 1c69100c1..117efe299 100644 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTests.java +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/TableLifecycleIntegrationTests.java @@ -21,7 +21,6 @@ import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cassandra.core.cql.generator.AlterTableCqlGeneratorUnitTests; import org.springframework.cassandra.core.cql.generator.CreateTableCqlGeneratorUnitTests; import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator; import org.springframework.cassandra.core.cql.generator.DropTableCqlGeneratorUnitTests; @@ -39,7 +38,8 @@ public class TableLifecycleIntegrationTests extends AbstractKeyspaceCreatingInte private final static Logger log = LoggerFactory.getLogger(TableLifecycleIntegrationTests.class); - CreateTableCqlGeneratorUnitTests.MultipleOptionsTest createTableTest = new CreateTableCqlGeneratorUnitTests.MultipleOptionsTest(); + CreateTableCqlGeneratorUnitTests.MultipleOptionsTest createTableTest = + new CreateTableCqlGeneratorUnitTests.MultipleOptionsTest(); @Before public void setUp() throws Exception { @@ -47,7 +47,7 @@ public class TableLifecycleIntegrationTests extends AbstractKeyspaceCreatingInte } @Test - public void testDrop() { + public void dropIsSuccessful() { createTableTest.prepare(); @@ -79,7 +79,5 @@ public class TableLifecycleIntegrationTests extends AbstractKeyspaceCreatingInte public DropTableCqlGenerator generator() { return new DropTableCqlGenerator(specification); } - } - }