Skip to content

Commit aa07755

Browse files
mipo256mp911de
authored andcommitted
Correctly apply TableOptions through CassandraAdminTemplate.createTable(…).
Closes: #359 Original pull request: #1385
1 parent 97e359a commit aa07755

File tree

9 files changed

+73
-10
lines changed

9 files changed

+73
-10
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminOperations.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public interface CassandraAdminOperations extends CassandraOperations {
5555
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
5656
Map<String, Object> optionsByName);
5757

58+
5859
/**
5960
* Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple
6061
* name of the {@link Class entity class} or name of the table specified with the {@link Table} mapping annotation.

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification;
3030
import org.springframework.data.cassandra.core.cql.keyspace.DropTableSpecification;
3131
import org.springframework.data.cassandra.core.cql.keyspace.DropUserTypeSpecification;
32+
import org.springframework.data.cassandra.core.cql.keyspace.TableOption;
3233
import org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity;
3334
import org.springframework.util.Assert;
35+
import org.springframework.util.CollectionUtils;
3436

3537
import com.datastax.oss.driver.api.core.CqlIdentifier;
3638
import com.datastax.oss.driver.api.core.CqlSession;
@@ -44,6 +46,7 @@
4446
* @author Fabio J. Mendes
4547
* @author John Blum
4648
* @author Vagif Zeynalov
49+
* @author Mikhail Polivakha
4750
*/
4851
public class CassandraAdminTemplate extends CassandraTemplate implements CassandraAdminOperations {
4952

@@ -108,13 +111,23 @@ public SchemaFactory getSchemaFactory() {
108111
* @see org.springframework.data.cassandra.core.CassandraAdminOperations#createTable(boolean, org.springframework.data.cassandra.core.cql.CqlIdentifier, java.lang.Class, java.util.Map)
109112
*/
110113
@Override
111-
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
112-
Map<String, Object> optionsByName) {
113-
114+
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass, Map<String, Object> optionsByName) {
114115
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
115116

116117
CreateTableSpecification createTableSpecification = this.schemaFactory
117-
.getCreateTableSpecificationFor(entity, tableName).ifNotExists(ifNotExists);
118+
.getCreateTableSpecificationFor(entity, tableName)
119+
.ifNotExists(ifNotExists);
120+
121+
if (!CollectionUtils.isEmpty(optionsByName)) {
122+
optionsByName.forEach((key, value) -> {
123+
TableOption tableOption = TableOption.valueOfIgnoreCase(key);
124+
if (tableOption.requiresValue()) {
125+
createTableSpecification.with(tableOption, value);
126+
} else {
127+
createTableSpecification.with(tableOption);
128+
}
129+
});
130+
}
118131

119132
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
120133
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/EntityOperations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ interface AdaptibleEntity<T> extends Entity<T> {
196196
StatementBuilder<Delete> appendVersionCondition(StatementBuilder<Delete> delete);
197197

198198
/**
199-
* Initializes the version property of the of the current entity if available.
199+
* Initializes the version property of the current entity if available.
200200
*
201201
* @return the entity with the version property updated if available.
202202
*/

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/StatementFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ StatementBuilder<RegularInsert> insert(Object objectToInsert, WriteOptions optio
295295
Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
296296

297297
boolean insertNulls;
298+
298299
if (options instanceof InsertOptions) {
299300

300301
InsertOptions insertOptions = (InsertOptions) options;

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/MappingCassandraConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,12 @@ public ColumnTypeResolver getColumnTypeResolver() {
298298
@SuppressWarnings("unchecked")
299299
private <S> ConvertingPropertyAccessor<S> newConvertingPropertyAccessor(S source,
300300
CassandraPersistentEntity<?> entity) {
301-
302301
PersistentPropertyAccessor<S> propertyAccessor = source instanceof PersistentPropertyAccessor
303-
? (PersistentPropertyAccessor<S>) source
304-
: entity.getPropertyAccessor(source);
302+
? (PersistentPropertyAccessor<S>) source
303+
: entity.getPropertyAccessor(source);
305304

306305
return new ConvertingPropertyAccessor<>(propertyAccessor, getConversionService());
306+
307307
}
308308

309309
private <S> CassandraPersistentEntityParameterValueProvider newParameterValueProvider(ConversionContext context,

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/convert/SchemaFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public CreateTableSpecification getCreateTableSpecificationFor(CassandraPersiste
147147
if (property.isCompositePrimaryKey()) {
148148

149149
CassandraPersistentEntity<?> primaryKeyEntity = mappingContext
150-
.getRequiredPersistentEntity(property.getRawType());
150+
.getRequiredPersistentEntity(property.getRawType());
151151

152152
for (CassandraPersistentProperty primaryKeyProperty : primaryKeyEntity) {
153153

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/keyspace/TableOption.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Map;
1919

2020
import org.springframework.lang.Nullable;
21+
import org.springframework.util.StringUtils;
2122

2223
/**
2324
* Enumeration that represents all known table options. If a table option is not listed here, but is supported by
@@ -26,6 +27,7 @@
2627
*
2728
* @author Matthew T. Adams
2829
* @author Mark Paluch
30+
* @author Mikhail Polivakha
2931
* @see CompactionOption
3032
* @see CompressionOption
3133
* @see CachingOption
@@ -84,6 +86,15 @@ public enum TableOption implements Option {
8486
this.delegate = new DefaultOption(name, type, requiresValue, escapesValue, quotesValue);
8587
}
8688

89+
public static TableOption valueOfIgnoreCase(String optionName) {
90+
for (TableOption value : values()) {
91+
if (value.getName().equalsIgnoreCase(optionName)) {
92+
return value;
93+
}
94+
}
95+
throw new IllegalArgumentException(String.format("Unable to recognize specified Table option '%s'", optionName));
96+
}
97+
8798
@Override
8899
public Class<?> getType() {
89100
return this.delegate.getType();

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/keyspace/TableSpecification.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ protected T column(CqlIdentifier name, DataType type, Optional<PrimaryKeyType> o
296296

297297
this.columns.add(column);
298298

299-
if (!optionalKeyType.isPresent()) {
299+
if (optionalKeyType.isEmpty()) {
300300
this.nonKeyColumns.add(column);
301301
}
302302

spring-data-cassandra/src/test/java/org/springframework/data/cassandra/core/CassandraAdminTemplateIntegrationTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.time.LocalDate;
2021
import java.util.Collection;
22+
import java.util.Map;
2123

24+
import org.assertj.core.api.Assertions;
2225
import org.junit.jupiter.api.BeforeEach;
2326
import org.junit.jupiter.api.Test;
27+
import org.springframework.data.annotation.Id;
2428
import org.springframework.data.cassandra.core.convert.MappingCassandraConverter;
2529
import org.springframework.data.cassandra.core.cql.generator.DropTableCqlGenerator;
2630
import org.springframework.data.cassandra.core.cql.keyspace.DropTableSpecification;
31+
import org.springframework.data.cassandra.core.cql.keyspace.TableOption;
32+
import org.springframework.data.cassandra.core.mapping.Table;
2733
import org.springframework.data.cassandra.domain.User;
2834
import org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTests;
2935

@@ -36,6 +42,7 @@
3642
* Integration tests for {@link CassandraAdminTemplate}.
3743
*
3844
* @author Mark Paluch
45+
* @author Mikhail Polivakha
3946
*/
4047
class CassandraAdminTemplateIntegrationTests extends AbstractKeyspaceCreatingIntegrationTests {
4148

@@ -59,6 +66,27 @@ private KeyspaceMetadata getKeyspaceMetadata() {
5966
return getSession().getKeyspace().flatMap(metadata::getKeyspace).get();
6067
}
6168

69+
@Test
70+
void givenAdminTemplate_whenCreateTableWithOptions_ThenCreatedTableContainsTheseOptions() {
71+
cassandraAdminTemplate.createTable(
72+
true,
73+
CqlIdentifier.fromCql("someTable"),
74+
SomeTable.class,
75+
Map.of(
76+
TableOption.COMMENT.getName(), "This is comment for table",
77+
TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3"
78+
)
79+
);
80+
81+
TableMetadata someTable = getKeyspaceMetadata().getTables().values().stream().findFirst().orElse(null);
82+
83+
Assertions.assertThat(someTable).isNotNull();
84+
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName())))
85+
.isEqualTo("This is comment for table");
86+
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName())))
87+
.isEqualTo(0.3);
88+
}
89+
6290
@Test // DATACASS-173
6391
void testCreateTables() {
6492

@@ -85,4 +113,13 @@ void testDropTable() {
85113

86114
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
87115
}
116+
117+
@Table("someTable")
118+
private static class SomeTable {
119+
120+
@Id
121+
private String name;
122+
private Integer number;
123+
private LocalDate createdAt;
124+
}
88125
}

0 commit comments

Comments
 (0)