org.springframework.data.cassandra.util.CqlUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-data-cassandra Show documentation
Show all versions of spring-data-cassandra Show documentation
Cassandra support for Spring Data
/*
* Copyright 2013-2014 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.data.cassandra.util;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cassandra.core.cql.CqlStringUtils;
import org.springframework.data.cassandra.mapping.CassandraPersistentEntity;
import org.springframework.data.cassandra.mapping.CassandraPersistentProperty;
import org.springframework.data.mapping.PropertyHandler;
import com.datastax.driver.core.ColumnMetadata;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TableMetadata;
/**
* Utilities to convert Cassandra Annotated objects to Queries and CQL.
*
* @author Alex Shvid
* @author David Webb
* @author Matthew T. Adams
*
* @deprecated need to find a better place for this
*/
@Deprecated
public abstract class CqlUtils {
/**
* Alter the table to reflect the entity annotations
*
* @param tableName
* @param entity
* @param table
* @return
*/
public static List alterTable(final String tableName, final CassandraPersistentEntity entity,
final TableMetadata table) {
final List result = new ArrayList();
entity.doWithProperties(new PropertyHandler() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
String columnName = prop.getColumnName().toCql();
DataType columnDataType = prop.getDataType();
ColumnMetadata columnMetadata = table.getColumn(columnName.toLowerCase());
if (columnMetadata != null && columnDataType.equals(columnMetadata.getType())) {
return;
}
final StringBuilder str = new StringBuilder();
str.append("ALTER TABLE ");
str.append(tableName);
if (columnMetadata == null) {
str.append(" ADD ");
} else {
str.append(" ALTER ");
}
str.append(columnName);
str.append(' ');
if (columnMetadata != null) {
str.append("TYPE ");
}
str.append(CqlStringUtils.toCql(columnDataType));
str.append(';');
result.add(str.toString());
}
});
return result;
}
}