Search in sources :

Example 1 with WrappedDatabaseInfo

use of com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo in project cubrid-manager by CUBRID.

the class ImportERwinAction method createCompareModel.

/**
	 *
	 * @param tableSchema
	 * @param schemaInfos
	 */
private void createCompareModel(final String modelName, final Map<String, TableSchema> tableSchema, final Map<String, ERWinSchemaInfo> schemaInfos) {
    final List<TableSchemaCompareModelInputLazy> input = new ArrayList<TableSchemaCompareModelInputLazy>();
    ITask reportBugTask = new AbstractUITask() {

        private boolean success = false;

        public void cancel() {
        }

        public void finish() {
        }

        public boolean isCancel() {
            return false;
        }

        public boolean isSuccess() {
            return success;
        }

        public void execute(IProgressMonitor monitor) {
            List<TableDetailInfo> leftDbTableInfoList = TableSchemaCompareUtil.getTableInfoList(database);
            final CubridDatabase virtualDb = new CubridDatabase(modelName, modelName);
            virtualDb.setVirtual(true);
            DatabaseInfo info = database.getDatabaseInfo();
            virtualDb.setDatabaseInfo(info);
            WrappedDatabaseInfo wrappedDatabaseInfo = new WrappedDatabaseInfo(info);
            Map<String, SchemaInfo> dbSchemaInfos = new HashMap<String, SchemaInfo>();
            Collection<ERWinSchemaInfo> erwinSchemas = schemaInfos.values();
            for (ERWinSchemaInfo erwinSchema : erwinSchemas) {
                SchemaInfo schemaInfo = (SchemaInfo) erwinSchema;
                dbSchemaInfos.put(schemaInfo.getClassname(), schemaInfo);
            }
            wrappedDatabaseInfo.addSchemaInfos(dbSchemaInfos);
            wrappedDatabaseInfo.addTableSchemas(tableSchema);
            ERXmlDatabaseInfoMapper.addWrappedDatabaseInfo(info, wrappedDatabaseInfo);
            TableSchemaModel leftModel = TableSchemaCompareUtil.createTableSchemaModel(leftDbTableInfoList);
            TableSchemaModel rightModel = new TableSchemaModel();
            rightModel.getTableSchemaMap().putAll(tableSchema);
            TableSchemaComparator comparator = new TableSchemaComparator(database, virtualDb);
            TableSchemaCompareModel model = comparator.compare(leftModel, rightModel);
            model.setSourceDB(database);
            model.setTargetDB(virtualDb);
            // TODO rename class to ErwinCompareModelInput
            input.add(new TableSchemaCompareModelInputLazy(model));
            success = true;
        }
    };
    TaskExecutor taskExecutor = new CommonTaskExec(Messages.titleSchemaComparison);
    taskExecutor.addTask(reportBugTask);
    new ExecTaskWithProgress(taskExecutor).exec();
    if (taskExecutor.isSuccess()) {
        try {
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input.get(0), TableSchemaCompareInfoPart.ID);
        } catch (Exception e) {
        }
    }
}
Also used : TableSchemaCompareModelInputLazy(com.cubrid.common.ui.compare.schema.control.TableSchemaCompareModelInputLazy) CommonTaskExec(com.cubrid.common.ui.spi.progress.CommonTaskExec) AbstractUITask(com.cubrid.common.core.task.AbstractUITask) ITask(com.cubrid.common.core.task.ITask) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) HashMap(java.util.HashMap) TableSchemaComparator(com.cubrid.common.ui.compare.schema.TableSchemaComparator) ArrayList(java.util.ArrayList) TableDetailInfo(com.cubrid.common.core.common.model.TableDetailInfo) TableSchemaModel(com.cubrid.common.ui.compare.schema.model.TableSchemaModel) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) TableSchemaCompareModel(com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TaskExecutor(com.cubrid.common.ui.spi.progress.TaskExecutor) AbstractUITask(com.cubrid.common.core.task.AbstractUITask) ExecTaskWithProgress(com.cubrid.common.ui.spi.progress.ExecTaskWithProgress) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)

Example 2 with WrappedDatabaseInfo

use of com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo in project cubrid-manager by CUBRID.

the class TableSchemaComparator method compareTableSchema.

/**
	 * Compare table schemas between source and target databases
	 */
private List<TableSchemaCompareModel> compareTableSchema(Map<String, TableSchema> leftTableSchema, Map<String, TableSchema> rightTableSchema, Map<String, TableDetailInfo> leftTableDetail, Map<String, TableDetailInfo> rightTableDetail) {
    // FIXME logic code move to core module
    List<TableSchemaCompareModel> models = new ArrayList<TableSchemaCompareModel>();
    /**
		 * Setup databases connections
		 */
    DatabaseInfo sourceDBInfo = sourceDB.getDatabaseInfo();
    DatabaseInfo targetDBInfo = null;
    if (targetDB.isVirtual()) {
        targetDBInfo = ERXmlDatabaseInfoMapper.getWrappedDatabaseInfo(targetDB.getDatabaseInfo());
    } else {
        targetDBInfo = targetDB.getDatabaseInfo();
    }
    SchemaDDL sourceSchemaDDL = new SchemaDDL(null, sourceDB.getDatabaseInfo());
    SchemaDDL targetSchemaDDL = new SchemaDDL(null, targetDB.getDatabaseInfo());
    // collect schemas info left db
    // collect sources
    GetAllSchemaTask task = new GetAllSchemaTask(sourceDB.getDatabaseInfo());
    task.execute();
    sourceClasses.clear();
    sourceClasses.putAll(task.getSchemas());
    // collect target
    if (!targetDB.isVirtual()) {
        task = new GetAllSchemaTask(targetDB.getDatabaseInfo());
        task.execute();
        targetClasses.clear();
        targetClasses.putAll(task.getSchemas());
    } else {
        WrappedDatabaseInfo info = (WrappedDatabaseInfo) targetDBInfo;
        targetClasses.putAll(info.getSchemaInfos());
    }
    int compareStatus = TableSchemaCompareModel.SCHEMA_EQUAL;
    /**
		 * Compare table schemas from left to right
		 */
    Iterator<String> leftkeys = leftTableSchema.keySet().iterator();
    while (leftkeys.hasNext()) {
        compareStatus = TableSchemaCompareModel.SCHEMA_EQUAL;
        String key = (String) leftkeys.next().toLowerCase();
        TableSchema lTableSchema = leftTableSchema.get(key);
        TableDetailInfo lTableDetail = leftTableDetail.get(key);
        List<String> names = findDuplication(rightTableSchema, key);
        TableSchema rTableSchema = null;
        TableDetailInfo rTableDetail = null;
        if (names != null) {
            if (names.size() == 1) {
                rTableSchema = rightTableSchema.get(names.get(0));
                rTableDetail = rightTableDetail.get(names.get(0));
            } else {
                duplicateNameMap.put(key, names);
                for (String tableName : names) {
                    rightTableSchema.remove(tableName);
                }
                leftkeys.remove();
                continue;
            }
        }
        if (rTableSchema == null) {
            rTableSchema = new TableSchema(null, null);
            compareStatus = TableSchemaCompareModel.SCHEMA_TMISS;
        } else {
            String left = lTableSchema.getName().toLowerCase();
            String right = rTableSchema.getName().toLowerCase();
            if (valueEqual(left, right)) {
                // TODO refactoring
                boolean compScheInfo = compareSchemaInfo(sourceDBInfo, targetDBInfo, sourceSchemaDDL, targetSchemaDDL, lTableSchema, rTableSchema);
                if (compScheInfo == false) {
                    compareStatus = TableSchemaCompareModel.SCHEMA_DIFF;
                }
            }
        }
        leftkeys.remove();
        rightTableSchema.remove(rTableSchema.getName());
        TableSchemaCompareModel cm = new TableSchemaCompareModel(lTableSchema, rTableSchema, sourceClasses, targetClasses);
        cm.setCompareStatus(compareStatus);
        cm.setSourceTableDetailInfo(lTableDetail);
        cm.setTargetTableDetailInfo(rTableDetail);
        models.add(cm);
    }
    /**
		 * Compare schemas from right to left
		 */
    Iterator<String> rightkeys = rightTableSchema.keySet().iterator();
    Map<String, TableSchemaCompareModel> tempCompareMap = new HashMap<String, TableSchemaCompareModel>();
    String RIGHT_PATTERN = "__RIGHT_PATTERN";
    while (rightkeys.hasNext()) {
        compareStatus = TableSchemaCompareModel.SCHEMA_EQUAL;
        String key = (String) rightkeys.next();
        TableSchema lTableSchema = leftTableSchema.get(key);
        TableDetailInfo lTableDetail = leftTableDetail.get(key);
        if (!duplicateNameMap.containsKey(key.toLowerCase() + RIGHT_PATTERN)) {
            duplicateNameMap.put(key.toLowerCase() + RIGHT_PATTERN, new ArrayList<String>());
        }
        duplicateNameMap.get(key.toLowerCase() + RIGHT_PATTERN).add(key);
        TableSchema rTableSchema = rightTableSchema.get(key);
        TableDetailInfo rTableDetail = rightTableDetail.get(key);
        if (lTableSchema == null) {
            lTableSchema = new TableSchema();
            compareStatus = TableSchemaCompareModel.SCHEMA_SMISS;
        }
        rightkeys.remove();
        leftTableSchema.remove(key);
        TableSchemaCompareModel cm = new TableSchemaCompareModel(lTableSchema, rTableSchema, sourceClasses, targetClasses);
        cm.setCompareStatus(compareStatus);
        cm.setSourceTableDetailInfo(lTableDetail);
        cm.setTargetTableDetailInfo(rTableDetail);
        tempCompareMap.put(key, cm);
    }
    for (List<String> listKey : duplicateNameMap.values()) {
        if (listKey.size() > 1) {
            for (String string : listKey) {
                tempCompareMap.remove(string);
            }
        }
    }
    models.addAll(tempCompareMap.values());
    if (models.size() <= 0) {
        return new ArrayList<TableSchemaCompareModel>();
    }
    return models;
}
Also used : TableSchema(com.cubrid.common.ui.compare.schema.model.TableSchema) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TableDetailInfo(com.cubrid.common.core.common.model.TableDetailInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) TableSchemaCompareModel(com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask)

Example 3 with WrappedDatabaseInfo

use of com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo in project cubrid-manager by CUBRID.

the class TableSchemaCompareInfoPart method getDBSchema.

private String getDBSchema(CubridDatabase db, Map<String, SchemaInfo> schemaInfos, List<SchemaInfo> commonTables) {
    // FIXME logic code move to core module
    if (schemaInfos == null) {
        return "";
    }
    Set<String> commonNames = new HashSet<String>();
    for (SchemaInfo table : commonTables) {
        commonNames.add(table.getClassname());
    }
    StringBuilder buf = new StringBuilder();
    if (!db.isVirtual()) {
        SchemaDDL schemaDDL = new SchemaDDL(null, db.getDatabaseInfo());
        List<TableDetailInfo> tableList = getTableInfoList(db);
        Collections.sort(tableList);
        try {
            for (SchemaInfo schemaInfo : commonTables) {
                addSchemaDDL(buf, schemaDDL, schemaInfo, true, false);
            }
            for (TableDetailInfo table : tableList) {
                if (commonNames.contains(table.getTableName())) {
                    continue;
                }
                SchemaInfo schemaInfo = schemaInfos.get(table.getTableName());
                addSchemaDDL(buf, schemaDDL, schemaInfo, true, false);
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        }
    } else {
        WrappedDatabaseInfo info = ERXmlDatabaseInfoMapper.getWrappedDatabaseInfo(db.getDatabaseInfo());
        SchemaDDL schemaDDL = new SchemaDDL(null, info);
        List<SchemaInfo> tables = Arrays.asList(schemaInfos.values().toArray(new SchemaInfo[0]));
        Collections.sort(tables);
        for (SchemaInfo schemaInfo : commonTables) {
            addSchemaDDL(buf, schemaDDL, schemaInfo, true, true);
        }
        for (SchemaInfo si : tables) {
            if (commonNames.contains(si.getClassname())) {
                continue;
            }
            addSchemaDDL(buf, schemaDDL, si, true, true);
        }
    }
    return buf.toString();
}
Also used : SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) TableDetailInfo(com.cubrid.common.core.common.model.TableDetailInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 4 with WrappedDatabaseInfo

use of com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo in project cubrid-manager by CUBRID.

the class ERSchemaEditor method compareTableSchemas.

public void compareTableSchemas(final String modelName, final Map<String, TableSchema> tableSchema, final Map<String, SchemaInfo> schemaInfos) {
    final List<TableSchemaCompareModelInputLazy> input = new ArrayList<TableSchemaCompareModelInputLazy>();
    ITask reportBugTask = new AbstractUITask() {

        private boolean success = false;

        public void cancel() {
        }

        public void finish() {
        }

        public boolean isCancel() {
            return false;
        }

        public boolean isSuccess() {
            return success;
        }

        public void execute(IProgressMonitor monitor) {
            CubridDatabase database = erSchema.getCubridDatabase();
            List<TableDetailInfo> leftDbTableInfoList = TableSchemaCompareUtil.getTableInfoList(database);
            final CubridDatabase virtualDb = new CubridDatabase(modelName, modelName);
            virtualDb.setVirtual(true);
            DatabaseInfo info = database.getDatabaseInfo();
            virtualDb.setDatabaseInfo(info);
            WrappedDatabaseInfo wrappedDatabaseInfo = new WrappedDatabaseInfo(info);
            wrappedDatabaseInfo.addSchemaInfos(schemaInfos);
            wrappedDatabaseInfo.addTableSchemas(tableSchema);
            ERXmlDatabaseInfoMapper.addWrappedDatabaseInfo(info, wrappedDatabaseInfo);
            TableSchemaModel leftModel = TableSchemaCompareUtil.createTableSchemaModel(leftDbTableInfoList);
            TableSchemaModel rightModel = new TableSchemaModel();
            rightModel.getTableSchemaMap().putAll(tableSchema);
            TableSchemaComparator comparator = new TableSchemaComparator(database, virtualDb);
            TableSchemaCompareModel model = comparator.compare(leftModel, rightModel);
            model.setSourceDB(database);
            model.setTargetDB(virtualDb);
            input.add(new TableSchemaCompareModelInputLazy(model));
            success = true;
        }
    };
    TaskExecutor taskExecutor = new CommonTaskExec(com.cubrid.common.ui.common.Messages.titleSchemaComparison);
    taskExecutor.addTask(reportBugTask);
    new ExecTaskWithProgress(taskExecutor).exec();
    if (taskExecutor.isSuccess()) {
        try {
            PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().openEditor(input.get(0), TableSchemaCompareInfoPart.ID);
        } catch (Exception e) {
        }
    }
}
Also used : TableSchemaCompareModelInputLazy(com.cubrid.common.ui.compare.schema.control.TableSchemaCompareModelInputLazy) CommonTaskExec(com.cubrid.common.ui.spi.progress.CommonTaskExec) AbstractUITask(com.cubrid.common.core.task.AbstractUITask) ITask(com.cubrid.common.core.task.ITask) 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) TableSchemaComparator(com.cubrid.common.ui.compare.schema.TableSchemaComparator) ArrayList(java.util.ArrayList) TableDetailInfo(com.cubrid.common.core.common.model.TableDetailInfo) TableSchemaModel(com.cubrid.common.ui.compare.schema.model.TableSchemaModel) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) PartInitException(org.eclipse.ui.PartInitException) SQLException(java.sql.SQLException) TableSchemaCompareModel(com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) TaskExecutor(com.cubrid.common.ui.spi.progress.TaskExecutor) AbstractUITask(com.cubrid.common.core.task.AbstractUITask) ExecTaskWithProgress(com.cubrid.common.ui.spi.progress.ExecTaskWithProgress) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase)

Example 5 with WrappedDatabaseInfo

use of com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo in project cubrid-manager by CUBRID.

the class TableSchemaCompareComposite method getTableAlterScript.

/**
	 * Returns table schema update/alter script
	 *
	 * @param sourceDatabase
	 * @param targetDatabase
	 * @param tableCompare
	 */
public String getTableAlterScript(CubridDatabase sourceDatabase, CubridDatabase targetDatabase, String tableCompare, SchemaInfo sourceSchemaInfo, SchemaInfo targetSchemaInfo) {
    // FIXME logic code move to core module
    String alterDDL = null;
    try {
        DatabaseInfo source_dbInfo = sourceDatabase.getDatabaseInfo();
        DatabaseInfo target_dbInfo = targetDatabase.getDatabaseInfo();
        if (sourceSchemaInfo == null || !"normal".equals(sourceSchemaInfo.getVirtual()))
            sourceSchemaInfo = null;
        if (targetSchemaInfo == null || !"normal".equals(targetSchemaInfo.getVirtual()))
            targetSchemaInfo = null;
        SchemaChangeManager sourceChangeManger = new SchemaChangeManager(source_dbInfo, false);
        SchemaDDL sourceSchemaDDL = new SchemaDDL(sourceChangeManger, sourceDatabase.getDatabaseInfo());
        SchemaChangeManager targetChangeManger = new SchemaChangeManager(target_dbInfo, false);
        SchemaDDL targetSchemaDDL = null;
        if (targetDatabase.isVirtual()) {
            WrappedDatabaseInfo info = ERXmlDatabaseInfoMapper.getWrappedDatabaseInfo(target_dbInfo);
            targetSchemaDDL = new SchemaDDL(targetChangeManger, info);
        } else {
            targetSchemaDDL = new SchemaDDL(targetChangeManger, targetDatabase.getDatabaseInfo());
        }
        TableSchemaCompareUpdateDDL tableCompareDDL = new TableSchemaCompareUpdateDDL(sourceChangeManger, sourceSchemaDDL, targetSchemaDDL, sourceSchemaInfo, targetSchemaInfo);
        alterDDL = tableCompareDDL.getTableSchemaAlterDDL();
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return alterDDL;
}
Also used : DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) WrappedDatabaseInfo(com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo) TableSchemaCompareUpdateDDL(com.cubrid.common.ui.compare.schema.model.TableSchemaCompareUpdateDDL)

Aggregations

WrappedDatabaseInfo (com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo)6 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)5 TableDetailInfo (com.cubrid.common.core.common.model.TableDetailInfo)4 SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)4 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)3 TableSchemaCompareModel (com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 AbstractUITask (com.cubrid.common.core.task.AbstractUITask)2 ITask (com.cubrid.common.core.task.ITask)2 TableSchemaComparator (com.cubrid.common.ui.compare.schema.TableSchemaComparator)2 TableSchemaCompareModelInputLazy (com.cubrid.common.ui.compare.schema.control.TableSchemaCompareModelInputLazy)2 TableSchema (com.cubrid.common.ui.compare.schema.model.TableSchema)2 TableSchemaModel (com.cubrid.common.ui.compare.schema.model.TableSchemaModel)2 ERVirtualDatabaseInfo (com.cubrid.common.ui.er.model.ERVirtualDatabaseInfo)2 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)2 CommonTaskExec (com.cubrid.common.ui.spi.progress.CommonTaskExec)2 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)2 TaskExecutor (com.cubrid.common.ui.spi.progress.TaskExecutor)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2