package com.codvision.db.versionctl.task;


import com.codvision.db.versionctl.chain.DbVersionCtlAbstractTask;
import com.codvision.db.versionctl.chain.DbVersionCtlContext;

import java.io.InputStream;
import java.util.List;

/**
 * 任务:修改数据库版本控制表
 *
 * @author lingee
 */
@SuppressWarnings("unused")
public class ModifyVersionTblTask extends DbVersionCtlAbstractTask {
    public ModifyVersionTblTask(DbVersionCtlContext context) {
        super(context);
    }

    @Override
    public boolean runTask() {
        logger.info("ModifyVersionTblTask begin...");
        // 读取修改SQL文
        List<String> sqls = readModifySql();
        // 执行建表文
        this.jdbcUtil.executeWithConnection(this.connection, sqls);
        logger.info("ModifyVersionTblTask end...");
        return true;
    }

    @SuppressWarnings("squid:S112")
    private List<String> readModifySql() {
        String modifyDbVersionTableSqlPath = this.context.getDbVersionCtlProps().getModifyDbVersionTableSqlPath();
        String dbVersionTableName = this.context.getDbVersionCtlProps().getDbVersionTableName();
        boolean needReplaceTblName =
                "classpath:db/versionctl/modify_cvbp_db_version_ctl.sql".equals(modifyDbVersionTableSqlPath)
                        && !"cvbp_db_version_ctl".equals(dbVersionTableName);

        InputStream inputStream;
        logger.debug("readModifySql : {}", modifyDbVersionTableSqlPath);
        if (modifyDbVersionTableSqlPath.startsWith("classpath:")) {
            logger.debug("readModifySql in classpath.");
            inputStream = loadInputStreamFromClassPath(modifyDbVersionTableSqlPath);
        } else {
            logger.debug("readCreateSql in filesystem.");
            inputStream = loadInputStreamFromFile(modifyDbVersionTableSqlPath);
        }
        if (inputStream != null) {
            return readSqlFromInputStream(dbVersionTableName, needReplaceTblName, inputStream);
        } else {
            throw new RuntimeException(modifyDbVersionTableSqlPath + " not found!");
        }
    }
}
