Search in sources :

Example 1 with UpdateNullToDefault

use of com.cubrid.cubridmanager.core.cubrid.table.task.UpdateNullToDefault in project cubrid-manager by CUBRID.

the class TableEditorPart method okPressed.

protected void okPressed() {
    if (!verifyTableName()) {
        return;
    }
    if (columnsTable.getItemCount() == 0) {
        CommonUITool.openErrorBox(Messages.noAttributes);
        return;
    }
    String message = (oldSchemaInfo == null) ? Messages.msgCreateTableConfirm : Messages.msgAlterTableConfirm;
    if (!CommonUITool.openConfirmBox(message)) {
        return;
    }
    tableName = tableNameText.getText();
    owner = ownerCombo.getText();
    String tableDesc = tableDescText.getText();
    newSchemaInfo.setClassname(tableName);
    newSchemaInfo.setOwner(owner);
    newSchemaInfo.setDescription(tableDesc);
    if (reuseOIDBtn != null) {
        newSchemaInfo.setReuseOid(reuseOIDBtn.getSelection());
    }
    DatabaseInfo dbInfo = database.getDatabaseInfo();
    CommonSQLExcuterTask commonSqlTask = new CommonSQLExcuterTask(dbInfo);
    schemaDDL.setEndLineChar("$$$$");
    String ddlStr = null;
    if (isNewTableFlag) {
        ddlStr = schemaDDL.getSchemaDDL(newSchemaInfo);
    } else {
        ddlStr = schemaDDL.getSchemaDDL(oldSchemaInfo, newSchemaInfo);
    }
    boolean isExecuteCommonSqlTask = false;
    String[] sqlStr = ddlStr.split("\\$\\$\\$\\$");
    for (String sql : sqlStr) {
        String trimSql = sql.trim();
        if (trimSql.length() > 0 && !trimSql.startsWith("--")) {
            if (dbInfo.isShard()) {
                sql = dbInfo.wrapShardQuery(sql);
            }
            commonSqlTask.addSqls(sql);
            isExecuteCommonSqlTask = true;
        }
    }
    // do with table user change
    String changeOwnerDDL = getChangeOwnerDDL();
    if (StringUtil.isNotEmpty(changeOwnerDDL)) {
        changeOwnerDDL = dbInfo.wrapShardQuery(changeOwnerDDL);
        commonSqlTask.addCallSqls(changeOwnerDDL);
        isExecuteCommonSqlTask = true;
    }
    schemaDDL.setEndLineChar(";");
    // do with column null attribute change
    List<String[]> nullAttrChangedColumnList = getNotNullChangedColumn();
    // if the column is null value, when set this column for not null,need
    // change these null value for default value
    List<String> nullToDefaultChangedColumnList = new ArrayList<String>();
    List<String> defaultValList = new ArrayList<String>();
    if (ApplicationType.CUBRID_MANAGER.equals(PerspectiveManager.getInstance().getCurrentMode())) {
        for (Iterator<String[]> it = nullAttrChangedColumnList.iterator(); it.hasNext(); ) {
            String[] column = it.next();
            if (!Boolean.parseBoolean(column[1])) {
                continue;
            }
            nullToDefaultChangedColumnList.add(column[0]);
        }
        // if the column is null value, when set this column for not null,do
        // not need change these null value for default value
        List<String> keepNullValueColList = new ArrayList<String>();
        for (Iterator<String> it = nullToDefaultChangedColumnList.iterator(); it.hasNext(); ) {
            String nullColumn = it.next();
            DBAttribute dBAttribute = newSchemaInfo.getDBAttributeByName(nullColumn, false);
            if (dBAttribute == null) {
                continue;
            }
            String defaultVal = dBAttribute.getDefault();
            boolean isUnique = dBAttribute.isUnique();
            if (isUnique) {
                keepNullValueColList.add(nullColumn);
                it.remove();
            } else {
                if (defaultVal == null) {
                    keepNullValueColList.add(nullColumn);
                    it.remove();
                    continue;
                } else {
                    FormatDataResult result = DBAttrTypeFormatter.formatForInput(dBAttribute.getType(), defaultVal, false);
                    if (result.isSuccess()) {
                        defaultValList.add(result.getFormatResult());
                    }
                }
            }
        }
        String msg = Messages.bind(Messages.confirmSetDef, nullToDefaultChangedColumnList);
        if (!nullToDefaultChangedColumnList.isEmpty() && (!CommonUITool.openConfirmBox(msg))) {
            return;
        }
        msg = Messages.bind(Messages.confirmKeepNull, keepNullValueColList);
        if (!keepNullValueColList.isEmpty() && (!CommonUITool.openConfirmBox(msg))) {
            return;
        }
    }
    TaskJobExecutor taskJobExec = new CommonTaskJobExec(this);
    boolean hasChanges = isExecuteCommonSqlTask || !nullAttrChangedColumnList.isEmpty() || !nullToDefaultChangedColumnList.isEmpty();
    if (hasChanges) {
        if (isExecuteCommonSqlTask) {
            taskJobExec.addTask(commonSqlTask);
        }
        if (database == null || newSchemaInfo == null) {
            return;
        }
        // change all table data from null value to default value
        int nullColSize = nullToDefaultChangedColumnList.size();
        for (int colIndex = 0; colIndex < nullColSize; colIndex++) {
            UpdateNullToDefault updateNullToDefault = new UpdateNullToDefault(dbInfo);
            updateNullToDefault.setTable(tableName);
            updateNullToDefault.setColumn(nullToDefaultChangedColumnList.get(colIndex));
            updateNullToDefault.setDefaultValue(defaultValList.get(colIndex));
            taskJobExec.addTask(updateNullToDefault);
        }
    }
    List<UpdateDescriptionTask> updateDescriptionTaskList = getUpdateDescriptionTaskList(dbInfo);
    for (UpdateDescriptionTask task : updateDescriptionTaskList) {
        taskJobExec.addTask(task);
    }
    if (taskJobExec.getTaskCount() > 0) {
        String serverName = database.getServer().getName();
        String dbName = database.getDatabaseInfo().getDbName();
        String title = getSite().getShell().getText();
        jobName = title + " - " + tableName + "@" + dbName;
        JobFamily jobFamily = new JobFamily();
        jobFamily.setServerName(serverName);
        jobFamily.setDbName(dbName);
        taskJobExec.schedule(jobName, jobFamily, true, Job.SHORT);
    } else {
        getSite().getWorkbenchWindow().getActivePage().closeEditor(editor, false);
    }
}
Also used : TaskJobExecutor(com.cubrid.common.ui.spi.progress.TaskJobExecutor) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) CommonSQLExcuterTask(com.cubrid.cubridmanager.core.common.task.CommonSQLExcuterTask) ArrayList(java.util.ArrayList) JobFamily(com.cubrid.common.ui.spi.progress.JobFamily) Constraint(com.cubrid.common.core.common.model.Constraint) FormatDataResult(com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult) CommonTaskJobExec(com.cubrid.common.ui.spi.progress.CommonTaskJobExec) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) UpdateNullToDefault(com.cubrid.cubridmanager.core.cubrid.table.task.UpdateNullToDefault) UpdateDescriptionTask(com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask)

Aggregations

Constraint (com.cubrid.common.core.common.model.Constraint)1 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)1 CommonTaskJobExec (com.cubrid.common.ui.spi.progress.CommonTaskJobExec)1 JobFamily (com.cubrid.common.ui.spi.progress.JobFamily)1 TaskJobExecutor (com.cubrid.common.ui.spi.progress.TaskJobExecutor)1 CommonSQLExcuterTask (com.cubrid.cubridmanager.core.common.task.CommonSQLExcuterTask)1 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 UpdateDescriptionTask (com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask)1 UpdateNullToDefault (com.cubrid.cubridmanager.core.cubrid.table.task.UpdateNullToDefault)1 ArrayList (java.util.ArrayList)1