Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATACASS-172-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data for Apache Cassandra</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-cql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra-parent</artifactId>
<version>1.5.0.BUILD-SNAPSHOT</version>
<version>1.5.0.DATACASS-172-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cassandra.core.cql.generator;

import static org.springframework.cassandra.core.cql.CqlStringUtils.*;

import org.springframework.cassandra.core.keyspace.AddColumnSpecification;
import org.springframework.cassandra.core.keyspace.AlterColumnSpecification;
import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification;
import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification;
import org.springframework.cassandra.core.keyspace.RenameColumnSpecification;
import org.springframework.util.Assert;

/**
* CQL generator for generating {@code ALTER TYPE} statements.
*
* @author Fabio J. Mendes
* @author Mark Paluch
* @since 1.5
* @see AlterUserTypeSpecification
* @see AddColumnSpecification
* @see RenameColumnSpecification
* @see AlterColumnSpecification
*/
public class AlterUserTypeCqlGenerator extends UserTypeNameCqlGenerator<AlterUserTypeSpecification> {

public static String toCql(AlterUserTypeSpecification specification) {
return new AlterUserTypeCqlGenerator(specification).toCql();
}

/**
* Creates a new {@link AlterUserTypeCqlGenerator} for a {@link AlterUserTypeSpecification}.
*
* @param specification must not be {@literal null}.
*/
public AlterUserTypeCqlGenerator(AlterUserTypeSpecification specification) {
super(specification);
}

/*
* (non-Javadoc)
* @see org.springframework.cassandra.core.cql.generator.UserTypeNameCqlGenerator#toCql(java.lang.StringBuilder)
*/
@Override
public StringBuilder toCql(StringBuilder cql) {

Assert.notNull(getSpecification().getName(), "User type name must not be null");
Assert.isTrue(!getSpecification().getChanges().isEmpty(),
String.format("User type [%s] does not contain fields", getSpecification().getName()));

cql = noNull(cql);

preambleCql(cql);
changesCql(cql);

cql.append(";");

return cql;
}

private StringBuilder preambleCql(StringBuilder cql) {
return noNull(cql).append("ALTER TYPE ").append(spec().getName()).append(' ');
}

private StringBuilder changesCql(StringBuilder cql) {
cql = noNull(cql);

boolean first = true;
boolean lastChangeWasRename = false;

for (ColumnChangeSpecification change : spec().getChanges()) {
if (first) {
first = false;
} else {
cql.append(' ');
}
getCqlGeneratorFor(change, lastChangeWasRename).toCql(cql);

lastChangeWasRename = change instanceof RenameColumnSpecification;
}

return cql;
}

private ColumnChangeCqlGenerator<?> getCqlGeneratorFor(ColumnChangeSpecification change,
boolean lastChangeWasRename) {

if (change instanceof AddColumnSpecification) {
return new AddColumnCqlGenerator((AddColumnSpecification) change);
}

if (change instanceof RenameColumnSpecification) {
return new RenameColumnCqlGenerator(lastChangeWasRename ? "AND" : RenameColumnCqlGenerator.RENAME, change);
}

if (change instanceof AlterColumnSpecification) {
return new AlterColumnCqlGenerator((AlterColumnSpecification) change);
}

throw new IllegalArgumentException(
String.format("Unknown ColumnChangeSpecification type: %s", change.getClass().getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cassandra.core.cql.generator;

import static org.springframework.cassandra.core.cql.CqlStringUtils.*;

import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification;
import org.springframework.cassandra.core.keyspace.FieldSpecification;
import org.springframework.util.Assert;

/**
* CQL generator for generating a {@code CREATE TYPE} statement.
*
* @author Fabio J. Mendes
* @author Mark Paluch
* @since 1.5
* @see CreateUserTypeSpecification
*/
public class CreateUserTypeCqlGenerator extends UserTypeNameCqlGenerator<CreateUserTypeSpecification> {

public static String toCql(CreateUserTypeSpecification specification) {
return new CreateUserTypeCqlGenerator(specification).toCql();
}

/**
* Creates a new {@link CreateUserTypeCqlGenerator} for a given {@link CreateUserTypeSpecification}.
*
* @param specification must not be {@literal null}.
*/
public CreateUserTypeCqlGenerator(CreateUserTypeSpecification specification) {
super(specification);
}

/*
* (non-Javadoc)
* @see org.springframework.cassandra.core.cql.generator.UserTypeNameCqlGenerator#toCql(java.lang.StringBuilder)
*/
@Override
public StringBuilder toCql(StringBuilder cql) {

Assert.notNull(getSpecification().getName(), "User type name must not be null");
Assert.isTrue(!getSpecification().getFields().isEmpty(),
String.format("User type [%s] does not contain fields", getSpecification().getName()));

cql = noNull(cql);

preambleCql(cql);
columns(cql);

cql.append(";");

return cql;
}

private StringBuilder preambleCql(StringBuilder cql) {
return noNull(cql).append("CREATE TYPE ").append(spec().getIfNotExists() ? "IF NOT EXISTS " : "")
.append(spec().getName());
}

private StringBuilder columns(StringBuilder cql) {

cql = noNull(cql);

// begin columns
cql.append(" (");

boolean first = true;
for (FieldSpecification col : spec().getFields()) {

if (!first) {
cql.append(", ");
}

col.toCql(cql);

first = false;
}

cql.append(")");
// end columns

return cql;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cassandra.core.cql.generator;

import static org.springframework.cassandra.core.cql.CqlStringUtils.*;

import org.springframework.cassandra.core.keyspace.DropUserTypeSpecification;

/**
* CQL generator for generating a {@code DROP TYPE} statement.
*
* @author Fabio J. Mendes
* @author Mark Paluch
* @since 1.5
* @see DropUserTypeSpecification
*/
public class DropUserTypeCqlGenerator extends UserTypeNameCqlGenerator<DropUserTypeSpecification> {

public static String toCql(DropUserTypeSpecification specification) {
return new DropUserTypeCqlGenerator(specification).toCql();
}

/**
* Creates a new {@link DropUserTypeCqlGenerator} for a given {@link DropUserTypeSpecification}.
*
* @param specification must not be {@literal null}.
*/
public DropUserTypeCqlGenerator(DropUserTypeSpecification specification) {
super(specification);
}

/*
* (non-Javadoc)
* @see org.springframework.cassandra.core.cql.generator.UserTypeNameCqlGenerator#toCql(java.lang.StringBuilder)
*/
@Override
public StringBuilder toCql(StringBuilder cql) {
return noNull(cql).append("DROP TYPE ").append(spec().getIfExists() ? "IF EXISTS " : "").append(spec().getName())
.append(";");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.springframework.cassandra.core.cql.CqlStringUtils.*;

import org.springframework.cassandra.core.keyspace.ColumnChangeSpecification;
import org.springframework.cassandra.core.keyspace.RenameColumnSpecification;

/**
Expand All @@ -30,14 +31,29 @@
*/
public class RenameColumnCqlGenerator extends ColumnChangeCqlGenerator<RenameColumnSpecification> {

static final String RENAME = "RENAME";

private final String keyword;

RenameColumnCqlGenerator(RenameColumnSpecification specification) {
this(RENAME, specification);
}

/**
* @param keyword the keyword to use for {@code RENAME}.
* @param specification the specification.
*/
RenameColumnCqlGenerator(String keyword, ColumnChangeSpecification specification) {
super(specification);
this.keyword = keyword;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.springframework.cassandra.core.cql.generator.ColumnChangeCqlGenerator#toCql(java.lang.StringBuilder)
*/
public StringBuilder toCql(StringBuilder cql) {
return noNull(cql).append("RENAME ").append(spec().getName()).append(" TO ").append(spec().getTargetName());
return noNull(cql).append(keyword).append(' ').append(spec().getName()).append(" TO ")
.append(spec().getTargetName());
}
}
Loading