From 577506f8b0d6abaf815b6ac8ea6abd151c736db9 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 18 Jan 2018 11:23:55 +0100 Subject: [PATCH] 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. --- .../generator/CreateTableCqlGenerator.java | 24 ++++++++------- .../CreateTableCqlGeneratorUnitTests.java | 29 +++++++++++++++++++ 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java index 78283d81e..8c0f1561a 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/core/cql/generator/CreateTableCqlGenerator.java @@ -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 CREATE TABLE statement. * * @author Matthew T. Adams * @author Alex Shvid + * @author Mark Paluch */ public class CreateTableCqlGenerator extends TableCqlGenerator { @@ -113,17 +114,18 @@ public class CreateTableCqlGenerator extends TableCqlGenerator 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 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