Search in sources :

Example 1 with UpdateDescriptionTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask 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)

Example 2 with UpdateDescriptionTask

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

the class TableEditorPart method getUpdateDescriptionTaskList.

/**
	 * Get UpdateDescriptionTaskList
	 *
	 * @param dbInfo
	 * @return
	 */
private List<UpdateDescriptionTask> getUpdateDescriptionTaskList(DatabaseInfo dbInfo) {
    // FIXME move this logic to core module
    List<UpdateDescriptionTask> updateDescriptionTaskList = new ArrayList<UpdateDescriptionTask>();
    if (isSupportTableComment) {
        for (DBAttribute newAttr : newSchemaInfo.getAttributes()) {
            DBAttribute oldAttr = null;
            if (oldSchemaInfo != null) {
                oldAttr = oldSchemaInfo.getDBAttributeByName(newAttr.getName(), newAttr.isClassAttribute());
            }
            if (oldAttr != null && StringUtil.isEqual(oldAttr.getDescription(), newAttr.getDescription())) {
                continue;
            } else {
                updateDescriptionTaskList.add(new UpdateDescriptionTask(Messages.updateDescriptionTask, dbInfo, tableName, newAttr.getName(), newAttr.getDescription()));
            }
        }
        boolean notSameDescription = false;
        if (oldSchemaInfo != null) {
            notSameDescription = !StringUtil.isEqualNotIgnoreNull(oldSchemaInfo.getDescription(), newSchemaInfo.getDescription());
        }
        if (oldSchemaInfo == null || notSameDescription) {
            UpdateDescriptionTask task = new UpdateDescriptionTask(Messages.updateDescriptionTask, dbInfo, tableName, null, newSchemaInfo.getDescription());
            updateDescriptionTaskList.add(task);
        }
    }
    return updateDescriptionTaskList;
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) UpdateDescriptionTask(com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask)

Example 3 with UpdateDescriptionTask

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

the class ERSchemaEditor method syncComments.

/**
	 * sync table comments to db tables.
	* 
	* @return void
	 */
public void syncComments() {
    DatabaseInfo info = database.getDatabaseInfo();
    if (info == null) {
        CommonUITool.openErrorBox(Messages.errNoDatabase);
        return;
    }
    if (!info.isLogined()) {
        CommonUITool.openErrorBox(Messages.msgDBNotLogin);
        return;
    }
    ServerInfo serverInfo = info.getServerInfo();
    if (serverInfo != null && !serverInfo.isConnected()) {
        CommonUITool.openErrorBox(Messages.msgDBNotLogin);
        return;
    }
    boolean isSupportTableComment = false;
    Connection conn = null;
    try {
        conn = JDBCConnectionManager.getConnection(info, false);
        isSupportTableComment = SchemaCommentHandler.isInstalledMetaTable(info, conn);
        if (!isSupportTableComment) {
            CommonUITool.openErrorBox(Messages.cannotSyncComments);
            return;
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    } finally {
        QueryUtil.freeQuery(conn);
    }
    if (!CommonUITool.openConfirmBox(getSite().getShell(), Messages.bind(Messages.msgConfirmSyncComments, database.getLabel()))) {
        return;
    }
    CommonTaskExec taskJobExec = new CommonTaskExec(null);
    List<UpdateDescriptionTask> updateDescTasks = getUpdateDescriptionTaskList(info);
    for (UpdateDescriptionTask task : updateDescTasks) {
        taskJobExec.addTask(task);
    }
    if (updateDescTasks.size() == 0) {
        CommonUITool.openInformationBox(Messages.msgNoComments);
        return;
    }
    new ExecTaskWithProgress(taskJobExec).exec();
    Set<String> failTables = new HashSet<String>();
    for (UpdateDescriptionTask task : updateDescTasks) {
        if (!task.isSuccess()) {
            failTables.add(task.getTableName());
        }
    }
    if (failTables.size() == 0) {
        CommonUITool.openInformationBox(Messages.msgSuccessSyncComments);
    } else {
        CommonUITool.openErrorBox(Messages.bind(Messages.errSyncComments, failTables.toString()));
    }
}
Also used : CommonTaskExec(com.cubrid.common.ui.spi.progress.CommonTaskExec) ERVirtualDatabaseInfo(com.cubrid.common.ui.er.model.ERVirtualDatabaseInfo) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) ServerInfo(com.cubrid.cubridmanager.core.common.model.ServerInfo) Connection(java.sql.Connection) ExecTaskWithProgress(com.cubrid.common.ui.spi.progress.ExecTaskWithProgress) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) UpdateDescriptionTask(com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask) HashSet(java.util.HashSet)

Example 4 with UpdateDescriptionTask

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

the class ERSchemaEditor method getUpdateDescriptionTaskList.

/**
	 * Get UpdateDescriptionTaskList
	 *
	 * @param dbInfo
	 * @return
	 */
private List<UpdateDescriptionTask> getUpdateDescriptionTaskList(DatabaseInfo dbInfo) {
    List<UpdateDescriptionTask> updateDescriptionTaskList = new ArrayList<UpdateDescriptionTask>();
    Map<String, SchemaInfo> schemaInfos = erSchema.getAllSchemaInfo();
    Collection<SchemaInfo> erdSchemaInfos = schemaInfos.values();
    GetAllSchemaTask task = new GetAllSchemaTask(dbInfo);
    task.execute();
    Map<String, SchemaInfo> dbSchemaInfoMap = task.getSchemas();
    String taskName = com.cubrid.common.ui.cubrid.table.Messages.updateDescriptionTask;
    for (SchemaInfo newSchemaInfo : erdSchemaInfos) {
        String tableName = newSchemaInfo.getClassname();
        SchemaInfo dbSchemaInfo = dbSchemaInfoMap.get(tableName);
        if (dbSchemaInfo == null) {
            continue;
        }
        for (DBAttribute newAttr : newSchemaInfo.getAttributes()) {
            DBAttribute oldAttr = dbSchemaInfo.getDBAttributeByName(newAttr.getName(), newAttr.isClassAttribute());
            if (oldAttr == null || StringUtil.isEqual(oldAttr.getDescription(), newAttr.getDescription())) {
                continue;
            }
            updateDescriptionTaskList.add(new UpdateDescriptionTask(taskName, dbInfo, tableName, newAttr.getName(), newAttr.getDescription()));
        }
        if (!StringUtil.isEqual(dbSchemaInfo.getDescription(), newSchemaInfo.getDescription())) {
            updateDescriptionTaskList.add(new UpdateDescriptionTask(taskName, dbInfo, tableName, "", newSchemaInfo.getDescription()));
        }
    }
    return updateDescriptionTaskList;
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask) UpdateDescriptionTask(com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

UpdateDescriptionTask (com.cubrid.cubridmanager.core.cubrid.table.task.UpdateDescriptionTask)4 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)3 ArrayList (java.util.ArrayList)3 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)2 Constraint (com.cubrid.common.core.common.model.Constraint)1 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)1 WrappedDatabaseInfo (com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo)1 ERVirtualDatabaseInfo (com.cubrid.common.ui.er.model.ERVirtualDatabaseInfo)1 CommonTaskExec (com.cubrid.common.ui.spi.progress.CommonTaskExec)1 CommonTaskJobExec (com.cubrid.common.ui.spi.progress.CommonTaskJobExec)1 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)1 JobFamily (com.cubrid.common.ui.spi.progress.JobFamily)1 TaskJobExecutor (com.cubrid.common.ui.spi.progress.TaskJobExecutor)1 ServerInfo (com.cubrid.cubridmanager.core.common.model.ServerInfo)1 CommonSQLExcuterTask (com.cubrid.cubridmanager.core.common.task.CommonSQLExcuterTask)1 FormatDataResult (com.cubrid.cubridmanager.core.cubrid.table.model.FormatDataResult)1 GetAllSchemaTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask)1 UpdateNullToDefault (com.cubrid.cubridmanager.core.cubrid.table.task.UpdateNullToDefault)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1