DATACASS-522 - Allow for conditionally dropping a table iff the table exists.

This commit is contained in:
Mark Paluch
2018-01-26 11:37:03 +01:00
committed by John Blum
parent f6f8da97ca
commit 7411dfee55
4 changed files with 48 additions and 3 deletions

View File

@@ -39,7 +39,7 @@ public interface CassandraAdminOperations extends CassandraOperations {
* doesn't exist, parameter {@code ifNotExists} is ignored, the table is created and {@literal true} is returned.
*
* @param ifNotExists If true, will only create the table if it doesn't exist, else the create operation will be
* ignored and the method will return {@literal false}.
* ignored.
* @param tableName The name of the table.
* @param entityClass The class whose fields determine the columns created.
* @param optionsByName Table options, given by the string option name and the appropriate option value.
@@ -54,6 +54,15 @@ public interface CassandraAdminOperations extends CassandraOperations {
*/
void dropTable(CqlIdentifier tableName);
/**
* Drops the named table.
*
* @param ifExists If true, will only drop the table if it exists, else the create operation will be ignored.
* @param tableName The name of the table.
* @since 2.1
*/
void dropTable(boolean ifExists, CqlIdentifier tableName);
/**
* Lookup {@link TableMetadata}.
*

View File

@@ -90,7 +90,13 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
*/
@Override
public void dropTable(CqlIdentifier tableName) {
getCqlOperations().execute(DropTableCqlGenerator.toCql(DropTableSpecification.dropTable(tableName)));
dropTable(false, tableName);
}
@Override
public void dropTable(boolean ifExists, CqlIdentifier tableName) {
getCqlOperations()
.execute(DropTableCqlGenerator.toCql(DropTableSpecification.dropTable(tableName).ifExists(ifExists)));
}
/*

View File

@@ -21,6 +21,7 @@ import org.springframework.data.cassandra.core.cql.keyspace.DropTableSpecificati
* CQL generator for generating a {@code DROP TABLE} statement.
*
* @author Matthew T. Adams
* @author Mark Paluch
*/
public class DropTableCqlGenerator extends TableNameCqlGenerator<DropTableSpecification> {
@@ -35,7 +36,7 @@ public class DropTableCqlGenerator extends TableNameCqlGenerator<DropTableSpecif
@Override
public StringBuilder toCql(StringBuilder cql) {
return cql.append("DROP TABLE ")
// .append(spec().getIfExists() ? "IF EXISTS " : "")
.append(spec().getIfExists() ? "IF EXISTS " : "")
.append(spec().getName()).append(";");
}
}

View File

@@ -25,6 +25,8 @@ import org.springframework.data.cassandra.core.cql.CqlIdentifier;
*/
public class DropTableSpecification extends TableNameSpecification {
private boolean ifExists = false;
private DropTableSpecification(CqlIdentifier name) {
super(name);
}
@@ -50,4 +52,31 @@ public class DropTableSpecification extends TableNameSpecification {
public static DropTableSpecification dropTable(CqlIdentifier tableName) {
return new DropTableSpecification(tableName);
}
/**
* Causes the inclusion of an {@code IF EXISTS} clause.
*
* @return this
* @since 2.1
*/
public DropTableSpecification ifExists() {
return ifExists(true);
}
/**
* Toggles the inclusion of an {@code IF EXISTS} clause.
*
* @return this
* @since 2.1
*/
public DropTableSpecification ifExists(boolean ifExists) {
this.ifExists = ifExists;
return this;
}
public boolean getIfExists() {
return ifExists;
}
}