more CQL generation refactoring

This commit is contained in:
Matthew Adams
2013-11-20 15:01:44 -06:00
parent 515f55c4f7
commit cea05c659b
13 changed files with 83 additions and 66 deletions

View File

@@ -1,4 +1,4 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.escapeSingle;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
@@ -11,14 +11,14 @@ import org.springframework.cassandra.core.keyspace.Option;
import org.springframework.util.Assert;
/**
* Base class that contains behavior common to table operations.
* Base class that contains behavior common to CQL generation for table operations.
*
* @author Matthew T. Adams
* @param T The subtype of AbstractTableSpecification for which this is a CQL generator.
* @param T The subtype of this class for which this is a CQL generator.
*/
public abstract class AbstractTableOperationCqlGenerator<T extends AbstractTableSpecification<T>> {
protected abstract StringBuilder toCql(StringBuilder cql);
public abstract StringBuilder toCql(StringBuilder cql);
private AbstractTableSpecification<T> specification;

View File

@@ -1,16 +1,20 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import org.springframework.cassandra.core.keyspace.AddColumnSpecification;
/**
* CQL generator for generating an <code>ADD</code> clause of an <code>ALTER TABLE</code> statement.
*
* @author Matthew T. Adams
*/
public class AddColumnCqlGenerator extends ColumnChangeCqlGenerator<AddColumnSpecification> {
public AddColumnCqlGenerator(AddColumnSpecification specification) {
super(specification);
}
@Override
public StringBuilder toCql(StringBuilder cql) {
return noNull(cql).append("ADD ").append(spec().getNameAsIdentifier()).append(" TYPE ")
.append(spec().getType().getName());

View File

@@ -1,9 +1,14 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import org.springframework.cassandra.core.keyspace.AlterColumnSpecification;
/**
* CQL generator for generating an <code>ALTER</code> column clause of an <code>ALTER TABLE</code> statement.
*
* @author Matthew T. Adams
*/
public class AlterColumnCqlGenerator extends ColumnChangeCqlGenerator<AlterColumnSpecification> {
public AlterColumnCqlGenerator(AlterColumnSpecification specification) {

View File

@@ -1,4 +1,4 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
@@ -11,13 +11,17 @@ import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification;
import org.springframework.cassandra.core.keyspace.DropColumnSpecification;
import org.springframework.cassandra.core.keyspace.Option;
/**
* CQL generator for generating <code>ALTER TABLE</code> statements.
*
* @author Matthew T. Adams
*/
public class AlterTableCqlGenerator extends AbstractTableOperationCqlGenerator<AlterTableSpecification> {
public AlterTableCqlGenerator(AlterTableSpecification specification) {
super(specification);
}
@Override
public StringBuilder toCql(StringBuilder cql) {
cql = noNull(cql);

View File

@@ -1,8 +1,14 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification;
import org.springframework.util.Assert;
/**
* Base class for column change CQL generators.
*
* @author Matthew T. Adams
* @param <T> The corresponding {@link ColumnChangeSpecification} type for this CQL generator.
*/
public abstract class ColumnChangeCqlGenerator<T extends ColumnChangeSpecification> {
public abstract StringBuilder toCql(StringBuilder cql);

View File

@@ -1,4 +1,4 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import static org.springframework.data.cassandra.mapping.KeyType.PARTITION;
@@ -13,7 +13,7 @@ import org.springframework.cassandra.core.keyspace.CreateTableSpecification;
import org.springframework.cassandra.core.keyspace.Option;
/**
* Builder class to construct CQL for a <code>CREATE TABLE</code> statement. Not threadsafe.
* CQL generator for generating a <code>CREATE TABLE</code> statement.
*
* @author Matthew T. Adams
*/

View File

@@ -1,9 +1,14 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import org.springframework.cassandra.core.keyspace.DropColumnSpecification;
/**
* CQL generator for generating a <code>DROP</code> column clause of an <code>ALTER TABLE</code> statement.
*
* @author Matthew T. Adams
*/
public class DropColumnCqlGenerator extends ColumnChangeCqlGenerator<DropColumnSpecification> {
public DropColumnCqlGenerator(DropColumnSpecification specification) {

View File

@@ -1,10 +1,15 @@
package org.springframework.cassandra.core.cql.builder;
package org.springframework.cassandra.core.cql.generator;
import static org.springframework.cassandra.core.cql.CqlStringUtils.noNull;
import org.springframework.cassandra.core.keyspace.DropTableSpecification;
import org.springframework.util.Assert;
/**
* CQL generator for generating a <code>DROP TABLE</code> statement.
*
* @author Matthew T. Adams
*/
public class DropTableCqlGenerator {
protected DropTableSpecification specification;

View File

@@ -1,46 +0,0 @@
package org.springframework.cassandra.core.keyspace;
public class CqlBuilder {
/**
* Entry point into the {@link CqlBuilder}'s fluent API to create a table. Convenient if imported statically.
*/
public static CreateTableSpecification createTable() {
return new CreateTableSpecification();
}
/**
* Entry point into the {@link CqlBuilder}'s fluent API to create a table. Convenient if imported statically.
*/
public static CreateTableSpecification createTable(String name) {
return new CreateTableSpecification().name(name);
}
/**
* Entry point into the {@link CqlBuilder}'s fluent API to alter a table. Convenient if imported statically.
*/
public static AlterTableSpecification alterTable() {
return new AlterTableSpecification();
}
/**
* Entry point into the {@link CqlBuilder}'s fluent API to alter a table. Convenient if imported statically.
*/
public static AlterTableSpecification alterTable(String name) {
return new AlterTableSpecification().name(name);
}
/**
* Entry point into the {@link CqlBuilder}'s fluent API to drop a table. Convenient if imported statically.
*/
public static DropTableSpecification dropTable() {
return new DropTableSpecification();
}
/**
* Entry point into the {@link CqlBuilder}'s fluent API to drop a table. Convenient if imported statically.
*/
public static DropTableSpecification dropTable(String name) {
return new DropTableSpecification().name(name);
}
}

View File

@@ -0,0 +1,34 @@
package org.springframework.cassandra.core.keyspace;
/**
* Class that offers static methods as entry points into the fluent API for building create, drop and alter table
* specifications. These methods are most convenient when imported statically.
*
* @author Matthew T. Adams
*/
public class TableOperations {
/**
* Entry point into the {@link CreateTableSpecification}'s fluent API to create a table. Convenient if imported
* statically.
*/
public static CreateTableSpecification createTable() {
return new CreateTableSpecification();
}
/**
* Entry point into the {@link DropTableSpecification}'s fluent API to drop a table. Convenient if imported
* statically.
*/
public static DropTableSpecification dropTable() {
return new DropTableSpecification();
}
/**
* Entry point into the {@link AlterTableSpecification}'s fluent API to alter a table. Convenient if imported
* statically.
*/
public static AlterTableSpecification alterTable() {
return new AlterTableSpecification();
}
}

View File

@@ -1,9 +1,9 @@
package org.springframework.cassandra.cql.builder;
import static org.springframework.cassandra.core.keyspace.CqlBuilder.alterTable;
import static org.springframework.cassandra.core.keyspace.TableOperations.alterTable;
import org.junit.Test;
import org.springframework.cassandra.core.cql.builder.AlterTableCqlGenerator;
import org.springframework.cassandra.core.cql.generator.AlterTableCqlGenerator;
import org.springframework.cassandra.core.keyspace.AlterTableSpecification;
import com.datastax.driver.core.DataType;

View File

@@ -1,7 +1,7 @@
package org.springframework.cassandra.cql.builder;
import static junit.framework.Assert.assertEquals;
import static org.springframework.cassandra.core.keyspace.CqlBuilder.createTable;
import static org.springframework.cassandra.core.keyspace.TableOperations.createTable;
import static org.springframework.cassandra.core.keyspace.MapBuilder.map;
import static org.springframework.cassandra.core.keyspace.TableOption.BLOOM_FILTER_FP_CHANCE;
import static org.springframework.cassandra.core.keyspace.TableOption.CACHING;
@@ -10,7 +10,7 @@ import static org.springframework.cassandra.core.keyspace.TableOption.COMPACTION
import static org.springframework.cassandra.core.keyspace.TableOption.CompactionOption.TOMBSTONE_THRESHOLD;
import org.junit.Test;
import org.springframework.cassandra.core.cql.builder.CreateTableCqlGenerator;
import org.springframework.cassandra.core.cql.generator.CreateTableCqlGenerator;
import org.springframework.cassandra.core.keyspace.CreateTableSpecification;
import org.springframework.cassandra.core.keyspace.TableOption.CachingOption;

View File

@@ -1,9 +1,9 @@
package org.springframework.cassandra.cql.builder;
import static org.springframework.cassandra.core.keyspace.CqlBuilder.dropTable;
import static org.springframework.cassandra.core.keyspace.TableOperations.dropTable;
import org.junit.Test;
import org.springframework.cassandra.core.cql.builder.DropTableCqlGenerator;
import org.springframework.cassandra.core.cql.generator.DropTableCqlGenerator;
import org.springframework.cassandra.core.keyspace.DropTableSpecification;
public class DropTableBuilderTest {