Package org.springframework.data.cassandra.util

Source Code of org.springframework.data.cassandra.util.CqlUtils

/*
* 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<String> alterTable(final String tableName, final CassandraPersistentEntity<?> entity,
      final TableMetadata table) {
    final List<String> result = new ArrayList<String>();

    entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
      @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;
  }
}
TOP

Related Classes of org.springframework.data.cassandra.util.CqlUtils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.