All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.centit.support.database.ddl.PostgreSqlDDLOperations Maven / Gradle / Ivy

Go to download

数据库操作通用方法和函数,从以前的util包中分离出来,并且整合了部分sys-module中的函数

The newest version!
package com.centit.support.database.ddl;

import com.centit.support.database.metadata.TableField;
import com.centit.support.database.utils.QueryUtils;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;

public class PostgreSqlDDLOperations extends GeneralDDLOperations {

    public PostgreSqlDDLOperations() {

    }

    public PostgreSqlDDLOperations(Connection conn) {
        super(conn);
    }

    @Override
    public String makeCreateSequenceSql(final String sequenceName) {
        return "create sequence " + QueryUtils.cleanSqlStatement(sequenceName);
    }

    /**
     * 修改列定义 ,比如 修改 varchar 的长度
     *
     * @param tableCode 表代码
     * @param oldColumn 老的字段
     * @param column    字段
     * @return sql语句
     */
    @Override
    public String makeModifyColumnSql(String tableCode, TableField oldColumn, TableField column) {
        StringBuilder sbsql = new StringBuilder("alter table ");
        Boolean modify=false;
        sbsql.append(tableCode);
        sbsql.append(" ALTER ").append(column.getColumnName()).append(" ");
        if (!StringUtils.equalsIgnoreCase(oldColumn.getColumnType(), column.getColumnType())
            || !oldColumn.getMaxLength().equals(column.getMaxLength())
            || !oldColumn.getPrecision().equals(column.getPrecision())) {
            sbsql.append(" type ");
            appendColumnTypeSQL(column, sbsql);
            modify=true;
        }

        if (oldColumn.isMandatory() != column.isMandatory()) {
            if(modify) {
                sbsql.append(";alter table "+tableCode);
                sbsql.append(" ALTER ").append(column.getColumnName()).append(" ");
            }
            sbsql.append(column.isMandatory() ? " set not null" : " drop not null");
        }

        return sbsql.toString();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy