Search in sources :

Example 1 with GetAllSchemaTask

use of com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask 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 2 with GetAllSchemaTask

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

the class DataCompareEditorPart method doRefresh.

private void doRefresh(boolean collectRecordCount) {
    // FIXME logic code move to core module
    Map<String, DataCompare> dataCompareMap = new HashMap<String, DataCompare>();
    if (compareList != null) {
        for (DataCompare dataCompare : compareList) {
            dataCompareMap.put(dataCompare.getTableName(), dataCompare);
        }
    }
    DatabaseInfo sourceDB = ((DataCompareEditorInput) getEditorInput()).getSourceDB();
    DatabaseInfo targetDB = ((DataCompareEditorInput) getEditorInput()).getTargetDB();
    if (logFileBaseName != null) {
        FileUtil.delete(logFileBasePath + File.separatorChar + logFileBaseName);
        logFileBaseName = null;
    }
    logFileBaseName = sourceDB.getDbName() + "_" + System.currentTimeMillis();
    GetAllSchemaTask sourceSchemaTask = new GetAllSchemaTask(sourceDB);
    GetAllSchemaTask targetSchemaTask = new GetAllSchemaTask(targetDB);
    TaskExecutor taskExecutor = new CommonTaskExec(Messages.loadEntireSchemaComparison);
    taskExecutor.addTask(sourceSchemaTask);
    taskExecutor.addTask(targetSchemaTask);
    new ExecTaskWithProgress(taskExecutor).exec();
    if (taskExecutor.isSuccess()) {
        synchronized (compareList) {
            compareList.clear();
            Set<String> partitions = new HashSet<String>();
            List<SchemaInfo> sourceList = sourceSchemaTask.getSchemaList();
            List<SchemaInfo> targetList = targetSchemaTask.getSchemaList();
            for (SchemaInfo schemaInfo : sourceList) {
                if (schemaInfo.getPartitionList() != null) {
                    for (PartitionInfo partition : schemaInfo.getPartitionList()) {
                        String partClassName = partition.getPartitionClassName();
                        partitions.add(partClassName);
                    }
                }
            }
            for (SchemaInfo schemaInfo : sourceList) {
                DataCompare dataCompare = dataCompareMap.get(schemaInfo.getClassname());
                if (dataCompare == null) {
                    dataCompare = new DataCompare();
                    dataCompare.setTableName(schemaInfo.getClassname());
                    dataCompare.setSchemaInfo(schemaInfo);
                    dataCompare.setRefreshed(false);
                } else {
                    dataCompare.setMatches(0);
                    dataCompare.setNotExists(0);
                    dataCompare.setNotMatches(0);
                    dataCompare.setProgressPosition(0);
                }
                if (schemaInfo.hasPK() && !partitions.contains(schemaInfo.getClassname())) {
                    SchemaInfo targetSchemeInfo = getSchemeInfoByName(schemaInfo.getClassname(), targetList);
                    boolean isSameSchema = canCompareData(schemaInfo, targetSchemeInfo);
                    dataCompare.setSameSchema(isSameSchema);
                    compareList.add(dataCompare);
                }
            }
            Collections.sort(compareList, new Comparator<DataCompare>() {

                public int compare(DataCompare o1, DataCompare o2) {
                    if (o1 == null || o1.getTableName() == null) {
                        return -1;
                    } else if (o2 == null || o2.getTableName() == null) {
                        return 1;
                    }
                    return o1.getTableName().compareToIgnoreCase(o2.getTableName());
                }
            });
        }
        compareTableViewer.refresh();
    }
    if (!collectRecordCount) {
        return;
    }
    totalRecords = 0;
    completedRecords = 0;
    refreshRecordCount();
    refreshedRecordCounts = true;
}
Also used : CommonTaskExec(com.cubrid.common.ui.spi.progress.CommonTaskExec) HashMap(java.util.HashMap) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) TaskExecutor(com.cubrid.common.ui.spi.progress.TaskExecutor) ExecTaskWithProgress(com.cubrid.common.ui.spi.progress.ExecTaskWithProgress) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) DataCompare(com.cubrid.common.ui.compare.data.model.DataCompare) HashSet(java.util.HashSet) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 3 with GetAllSchemaTask

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

the class ExportERwinAction method run.

public void run() {
    // FIXME logic code move to core module
    int selected = 0;
    int logined = 0;
    Object[] objects = getSelectedObj();
    if (objects instanceof Object[]) {
        for (Object object : objects) {
            if (object instanceof CubridDatabase) {
                selected++;
                CubridDatabase database = (CubridDatabase) object;
                if (database.isLogined()) {
                    logined++;
                }
            }
        }
    }
    if (selected > 1) {
        CommonUITool.openWarningBox(com.cubrid.common.ui.cubrid.database.erwin.Messages.errERwinSelectLeastOneDb);
        return;
    }
    if (selected <= 0) {
        CommonUITool.openWarningBox(com.cubrid.common.ui.cubrid.database.erwin.Messages.errERwinSelectExportDb);
        return;
    }
    if (logined <= 0) {
        CommonUITool.openWarningBox(com.cubrid.common.ui.cubrid.database.erwin.Messages.errERwinSelectLoginedDb);
        return;
    }
    FileDialog dialog = new FileDialog(getShell(), SWT.SAVE | SWT.APPLICATION_MODAL);
    dialog.setFilterExtensions(new String[] { "*.xml" });
    String filename = dialog.open();
    if (filename == null) {
        return;
    }
    if (filename.trim().equals("")) {
        CommonUITool.openErrorBox(Messages.errFileNameIsEmpty);
        return;
    }
    for (Object obj : objects) {
        if (!(obj instanceof CubridDatabase)) {
            continue;
        }
        CubridDatabase database = (CubridDatabase) obj;
        final Map<String, SchemaInfo> allSchemaInfos = new HashMap<String, SchemaInfo>();
        TaskExecutor executor = new TaskExecutor() {

            public boolean exec(IProgressMonitor monitor) {
                for (ITask task : taskList) {
                    if (task instanceof ExportSchemaTask) {
                        ExportSchemaTask eTask = (ExportSchemaTask) task;
                        try {
                            eTask.initMarshaller();
                        } catch (JAXBException e) {
                            e.printStackTrace();
                            eTask.cancel();
                            return false;
                        }
                        monitor.setTaskName(Messages.msgGenerateInfo);
                        monitor.worked(50);
                        eTask.execute();
                        monitor.setTaskName(Messages.msgFinished);
                        monitor.worked(100);
                        monitor.done();
                    } else if (task instanceof GetAllSchemaTask) {
                        monitor.beginTask(Messages.msgGenerateInfo, 100);
                        GetAllSchemaTask gTask = (GetAllSchemaTask) task;
                        gTask.execute();
                        if (task.getErrorMsg() == null) {
                            allSchemaInfos.putAll(gTask.getSchemas());
                        }
                        if (allSchemaInfos.size() == 0) {
                            continue;
                        }
                    }
                }
                return true;
            }
        };
        ExportSchemaTask task = new ExportSchemaTask(allSchemaInfos, filename);
        GetAllSchemaTask schemaTask = new GetAllSchemaTask(database.getDatabaseInfo());
        executor.addTask(schemaTask);
        executor.addTask(task);
        new ExecTaskWithProgress(executor).busyCursorWhile();
        if (executor.isSuccess()) {
            CommonUITool.openInformationBox(Messages.titleExportSchema, Messages.msgExportSuccess);
        }
    }
}
Also used : ITask(com.cubrid.common.core.task.ITask) HashMap(java.util.HashMap) JAXBException(javax.xml.bind.JAXBException) ExportSchemaTask(com.cubrid.common.ui.cubrid.database.erwin.task.ExportSchemaTask) TaskExecutor(com.cubrid.common.ui.spi.progress.TaskExecutor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ExecTaskWithProgress(com.cubrid.common.ui.spi.progress.ExecTaskWithProgress) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase) FileDialog(org.eclipse.swt.widgets.FileDialog) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 4 with GetAllSchemaTask

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

the class ColumnProposalAdvisor method loadProposal.

/**
	 * load proposal for database
	 * 
	 * @param databaseInfo
	 */
private void loadProposal(final DatabaseInfo databaseInfo) {
    final String key = makeKey(databaseInfo);
    Job job = new Job("Load database schema information job") {

        protected IStatus run(IProgressMonitor monitor) {
            List<String> tableNames = new ArrayList<String>();
            Map<String, List<ColumnProposalDetailInfo>> columns = new HashMap<String, List<ColumnProposalDetailInfo>>();
            GetAllSchemaTask task = null;
            try {
                task = new GetAllSchemaTask(databaseInfo, monitor);
                task.setNeedCollationInfo(false);
                task.execute();
                /*Check is canceled*/
                if (task.isCancel()) {
                    return Status.CANCEL_STATUS;
                }
                if (task.isSuccess()) {
                    Map<String, SchemaInfo> schemas = task.getSchemas();
                    Map<String, SchemaComment> descriptions = task.getComments();
                    List<String> fetchedTableNames = new ArrayList<String>();
                    for (SchemaInfo schemaInfo : schemas.values()) {
                        if (schemaInfo.isSystemClass()) {
                            continue;
                        }
                        String tableName = schemaInfo.getClassname();
                        if (ConstantsUtil.isExtensionalSystemTable(tableName)) {
                            continue;
                        }
                        fetchedTableNames.add(tableName);
                    }
                    Collections.sort(fetchedTableNames);
                    for (String tableName : fetchedTableNames) {
                        if (!tableNames.contains(tableName)) {
                            tableNames.add(tableName);
                        }
                        if (columns.containsKey(tableName)) {
                            continue;
                        }
                        SchemaInfo schemaInfo = schemas.get(tableName);
                        if (schemaInfo == null) {
                            continue;
                        }
                        if (descriptions != null) {
                            SchemaComment schemaComment = SchemaCommentHandler.find(descriptions, tableName, null);
                            if (schemaComment != null) {
                                String description = schemaComment.getDescription();
                                schemaInfo.setDescription(description);
                            }
                        }
                        List<ColumnProposalDetailInfo> colInfoList = new ArrayList<ColumnProposalDetailInfo>();
                        columns.put(tableName, colInfoList);
                        List<DBAttribute> dbClassAttrList = schemaInfo.getClassAttributes();
                        for (DBAttribute attr : dbClassAttrList) {
                            ColumnProposalDetailInfo colInfo = new ColumnProposalDetailInfo(schemaInfo, attr);
                            colInfoList.add(colInfo);
                        }
                        List<DBAttribute> attrList = schemaInfo.getAttributes();
                        for (DBAttribute attr : attrList) {
                            ColumnProposalDetailInfo colInfo = new ColumnProposalDetailInfo(schemaInfo, attr);
                            colInfoList.add(colInfo);
                        }
                        columns.put(schemaInfo.getClassname(), colInfoList);
                    }
                    /*Cache the data*/
                    ColumnProposal proposal = new ColumnProposal();
                    proposal.setTableNames(tableNames);
                    proposal.setColumns(columns);
                    synchronized (ColumnProposalAdvisor.class) {
                        cachedMap.put(key, proposal);
                    }
                }
            } catch (Exception e) {
                LOGGER.error(e.getMessage(), e);
            } finally {
                synchronized (ColumnProposalAdvisor.class) {
                    collectingKeys.remove(key);
                }
                task.finish();
            }
            return Status.OK_STATUS;
        }
    };
    /*Record collecting key*/
    synchronized (ColumnProposalAdvisor.class) {
        collectingKeys.add(key);
        job.schedule();
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask) SchemaComment(com.cubrid.common.core.schemacomment.model.SchemaComment) Job(org.eclipse.core.runtime.jobs.Job) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 5 with GetAllSchemaTask

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

the class ColumnProposalDetailInfo method fillInTableColumnInfo.

/**
	 * Fill in the table column information
	 *
	 * @param dbInfo DatabaseInfo
	 * @param tableNames List<String>
	 * @param columns Map<String, List<ColumnProposalDetailInfo>>
	 */
public static void fillInTableColumnInfo(DatabaseInfo dbInfo, List<String> tableNames, Map<String, List<ColumnProposalDetailInfo>> columns) {
    try {
        GetAllSchemaTask task = new GetAllSchemaTask(dbInfo);
        task.setNeedCollationInfo(false);
        task.execute();
        Map<String, SchemaInfo> schemas = task.getSchemas();
        List<String> fetchedTableNames = new ArrayList<String>();
        for (SchemaInfo schemaInfo : schemas.values()) {
            String tableName = schemaInfo.getClassname();
            fetchedTableNames.add(tableName);
        }
        Collections.sort(fetchedTableNames);
        for (String tableName : fetchedTableNames) {
            if (!tableNames.contains(tableName)) {
                tableNames.add(tableName);
            }
            if (columns.containsKey(tableName)) {
                continue;
            }
            SchemaInfo schemaInfo = schemas.get(tableName);
            if (schemaInfo == null) {
                continue;
            }
            List<ColumnProposalDetailInfo> colInfoList = new ArrayList<ColumnProposalDetailInfo>();
            columns.put(tableName, colInfoList);
            List<DBAttribute> dbClassAttrList = schemaInfo.getClassAttributes();
            for (DBAttribute attr : dbClassAttrList) {
                ColumnProposalDetailInfo colInfo = new ColumnProposalDetailInfo(schemaInfo, attr);
                colInfoList.add(colInfo);
            }
            List<DBAttribute> attrList = schemaInfo.getAttributes();
            for (DBAttribute attr : attrList) {
                ColumnProposalDetailInfo colInfo = new ColumnProposalDetailInfo(schemaInfo, attr);
                colInfoList.add(colInfo);
            }
            columns.put(schemaInfo.getClassname(), colInfoList);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) GetAllSchemaTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

GetAllSchemaTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllSchemaTask)6 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)3 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)2 TaskExecutor (com.cubrid.common.ui.spi.progress.TaskExecutor)2 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)1 TableDetailInfo (com.cubrid.common.core.common.model.TableDetailInfo)1 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)1 ITask (com.cubrid.common.core.task.ITask)1 DataCompare (com.cubrid.common.ui.compare.data.model.DataCompare)1 TableSchema (com.cubrid.common.ui.compare.schema.model.TableSchema)1 TableSchemaCompareModel (com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel)1 WrappedDatabaseInfo (com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo)1 ExportSchemaTask (com.cubrid.common.ui.cubrid.database.erwin.task.ExportSchemaTask)1 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)1 CommonTaskExec (com.cubrid.common.ui.spi.progress.CommonTaskExec)1