Skip to content

Commit b354eba

Browse files
mipo256mp911de
authored andcommitted
Create table using provided name.
Closes #1388
1 parent 7fa3334 commit b354eba

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @author Matthew T. Adams
3333
* @author Mark Paluch
3434
* @author Fabio J. Mendes
35+
* @author Mikhail Polivakha
3536
*/
3637
public interface CassandraAdminOperations extends CassandraOperations {
3738

@@ -55,6 +56,20 @@ public interface CassandraAdminOperations extends CassandraOperations {
5556
void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
5657
Map<String, Object> optionsByName);
5758

59+
/**
60+
* Create a table with the name, that is derived from the given {@code entityClass} and also fields corresponding to the
61+
* same class. If the table already exists and parameter {@code ifNotExists} is {@literal true}, this is a no-op and
62+
* {@literal false} is returned. If the table doesn't exist, parameter {@code ifNotExists} is ignored, the table is created
63+
* and {@literal true} is returned.
64+
*
65+
* @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be
66+
* ignored.
67+
* @param entityClass The class whose fields determine the columns created.
68+
* @param optionsByName Table options, given by the string option name and the appropriate option value.
69+
*/
70+
void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName);
71+
72+
5873
/**
5974
* Drops a table based on the given {@link Class entity type}. The name of the table is derived from either the simple
6075
* 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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ public SchemaFactory getSchemaFactory() {
108108
}
109109

110110
@Override
111-
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass, Map<String, Object> optionsByName) {
111+
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
112+
Map<String, Object> optionsByName) {
112113
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
113114

114115
CreateTableSpecification createTableSpecification = this.schemaFactory
115-
.getCreateTableSpecificationFor(entity, tableName)
116-
.ifNotExists(ifNotExists);
116+
.getCreateTableSpecificationFor(entity, tableName)
117+
.ifNotExists(ifNotExists);
117118

118119
if (!CollectionUtils.isEmpty(optionsByName)) {
119120
optionsByName.forEach((key, value) -> {
@@ -129,6 +130,12 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
129130
getCqlOperations().execute(CreateTableCqlGenerator.toCql(createTableSpecification));
130131
}
131132

133+
@Override
134+
public void createTable(boolean ifNotExists, Class<?> entityClass, Map<String, Object> optionsByName) {
135+
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
136+
this.createTable(ifNotExists, entity.getTableName(), entityClass, optionsByName);
137+
}
138+
132139
@Override
133140
public void dropTable(Class<?> entityClass) {
134141
dropTable(getTableName(entityClass));

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

Lines changed: 1 addition & 2 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
@@ -94,13 +95,11 @@ public enum TableOption implements Option {
9495
* @since 4.1.1
9596
*/
9697
public static TableOption valueOfIgnoreCase(String optionName) {
97-
9898
for (TableOption value : values()) {
9999
if (value.getName().equalsIgnoreCase(optionName)) {
100100
return value;
101101
}
102102
}
103-
104103
throw new IllegalArgumentException(String.format("Unable to recognize specified Table option '%s'", optionName));
105104
}
106105

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Map;
2323

24+
import org.assertj.core.api.Assertions;
2425
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627
import org.springframework.data.annotation.Id;
@@ -83,28 +84,49 @@ void shouldApplyTableOptions() {
8384
.isEqualTo(0.3);
8485
}
8586

87+
@Test
88+
void shouldCreateTableWithNameDerivedFromEntityClass() {
89+
cassandraAdminTemplate.createTable(
90+
true,
91+
SomeTable.class,
92+
Map.of(
93+
TableOption.COMMENT.getName(), "This is comment for table", TableOption.BLOOM_FILTER_FP_CHANCE.getName(), "0.3"
94+
)
95+
);
96+
97+
TableMetadata someTable = getKeyspaceMetadata().getTables()
98+
.values()
99+
.stream()
100+
.findFirst()
101+
.orElse(null);
102+
103+
Assertions.assertThat(someTable).isNotNull();
104+
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.COMMENT.getName()))).isEqualTo("This is comment for table");
105+
Assertions.assertThat(someTable.getOptions().get(CqlIdentifier.fromCql(TableOption.BLOOM_FILTER_FP_CHANCE.getName()))).isEqualTo(3);
106+
}
107+
86108
@Test // DATACASS-173
87109
void testCreateTables() {
88110

89111
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
90112

91-
cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
113+
cassandraAdminTemplate.createTable(true, User.class, null);
92114
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
93115

94-
cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
116+
cassandraAdminTemplate.createTable(true, User.class, null);
95117
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
96118
}
97119

98120
@Test
99121
void testDropTable() {
100122

101-
cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
123+
cassandraAdminTemplate.createTable(true, User.class, null);
102124
assertThat(getKeyspaceMetadata().getTables()).hasSize(1);
103125

104126
cassandraAdminTemplate.dropTable(User.class);
105127
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);
106128

107-
cassandraAdminTemplate.createTable(true, CqlIdentifier.fromCql("users"), User.class, null);
129+
cassandraAdminTemplate.createTable(true, User.class, null);
108130
cassandraAdminTemplate.dropTable(CqlIdentifier.fromCql("users"));
109131

110132
assertThat(getKeyspaceMetadata().getTables()).hasSize(0);

0 commit comments

Comments
 (0)