DATACASS-518 - Fix CQL generator when using ordered clustering columns with options.

CreateTableCqlGenerator now creates correct CQL when using ordered clustering columns with and without table options.
This commit is contained in:
Mark Paluch
2018-01-18 11:23:55 +01:00
parent 24147de713
commit 577506f8b0
2 changed files with 42 additions and 11 deletions

View File

@@ -15,9 +15,8 @@
*/
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import static org.springframework.cassandra.core.PrimaryKeyType.PARTITIONED;
import static org.springframework.cassandra.core.PrimaryKeyType.CLUSTERED;
import static org.springframework.cassandra.core.PrimaryKeyType.*;
import static org.springframework.cassandra.core.cql.CqlStringUtils.*;
import java.util.ArrayList;
import java.util.List;
@@ -26,12 +25,14 @@ import java.util.Map;
import org.springframework.cassandra.core.keyspace.ColumnSpecification;
import org.springframework.cassandra.core.keyspace.CreateTableSpecification;
import org.springframework.cassandra.core.keyspace.Option;
import org.springframework.util.StringUtils;
/**
* CQL generator for generating a <code>CREATE TABLE</code> statement.
*
* @author Matthew T. Adams
* @author Alex Shvid
* @author Mark Paluch
*/
public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecification> {
@@ -113,17 +114,18 @@ public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecif
// begin option clause
Map<String, Object> options = spec().getOptions();
if (ordering != null || !options.isEmpty()) {
if (!options.isEmpty() || ordering.length() != 0) {
// option preamble
boolean first = true;
cql.append(" WITH ");
// end option preamble
if (ordering != null) {
if (StringUtils.hasText(ordering)) {
cql.append(ordering);
first = false;
}
if (!options.isEmpty()) {
for (String name : options.keySet()) {
// append AND if we're not on first option
@@ -159,12 +161,13 @@ public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecif
}
private static StringBuilder createOrderingClause(List<ColumnSpecification> columns) {
StringBuilder ordering = null;
StringBuilder ordering = new StringBuilder();
boolean first = true;
for (ColumnSpecification col : columns) {
if (col.getOrdering() != null) { // then ordering specified
if (ordering == null) { // then initialize ordering clause
if (!StringUtils.hasText(ordering)) { // then initialize ordering clause
ordering = new StringBuilder().append("CLUSTERING ORDER BY (");
}
if (first) {
@@ -175,9 +178,11 @@ public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecif
ordering.append(col.getName()).append(" ").append(col.getOrdering().cql());
}
}
if (ordering != null) { // then end ordering option
if (StringUtils.hasText(ordering)) { // then end ordering option
ordering.append(")");
}
return ordering;
}
@@ -191,9 +196,6 @@ public class CreateTableCqlGenerator extends TableCqlGenerator<CreateTableSpecif
str.append(", ");
}
str.append(col.getName());
}
}
}

View File

@@ -17,6 +17,7 @@ package org.springframework.cassandra.core.cql.generator;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.cassandra.core.cql.CqlIdentifier.*;
import static org.springframework.cassandra.core.cql.generator.CreateTableCqlGenerator.*;
import java.util.ArrayList;
import java.util.Arrays;
@@ -28,6 +29,7 @@ import java.util.Map;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.ReservedKeyword;
import org.springframework.cassandra.core.cql.CqlIdentifier;
import org.springframework.cassandra.core.keyspace.CreateTableSpecification;
@@ -45,11 +47,38 @@ import com.datastax.driver.core.DataType;
*
* @author Matthew T. Adams
* @author David Webb
* @author Mark Paluch
*/
public class CreateTableCqlGeneratorUnitTests {
private static final Logger log = LoggerFactory.getLogger(CreateTableCqlGeneratorUnitTests.class);
@Test // DATACASS-518
public void createTableWithOrderedClustering() {
CreateTableSpecification table = CreateTableSpecification.createTable("person") //
.partitionKeyColumn("id", DataType.ascii()) //
.clusteredKeyColumn("date_of_birth", DataType.date(), Ordering.ASCENDING) //
.column("name", DataType.ascii());
assertThat(toCql(table)).isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC);");
}
@Test // DATACASS-518
public void createTableWithOrderedClusteringAndOptions() {
CreateTableSpecification table = CreateTableSpecification.createTable("person") //
.partitionKeyColumn("id", DataType.ascii()) //
.clusteredKeyColumn("date_of_birth", DataType.date(), Ordering.ASCENDING) //
.column("name", DataType.ascii()).with(TableOption.COMPACT_STORAGE);
assertThat(toCql(table)).isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC) AND COMPACT STORAGE;");
}
/**
* Asserts that the preamble is first & correctly formatted in the given CQL string.
*/