Search in sources :

Example 1 with SchemaDDL

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL in project cubrid-manager by CUBRID.

the class ERXmlContainer method createSchemaDDL.

private void createSchemaDDL() {
    DatabaseInfo info = database.getDatabaseInfo();
    if (info == null) {
        LOGGER.error("The databaseInfo is a null.");
        return;
    }
    WrappedDatabaseInfo wrappedDatabaseInfo = new WrappedDatabaseInfo(info.getDbName(), info.getServerInfo());
    ERXmlDatabaseInfoMapper.addWrappedDatabaseInfo(info, wrappedDatabaseInfo);
    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(tableSchemas);
    SchemaDDL ddl = new SchemaDDL(null, wrappedDatabaseInfo);
    for (String tableName : tableSchemas.keySet()) {
        TableSchema tableSchema = tableSchemas.get(tableName);
        SchemaInfo schemaInfo = schemaInfos.get(tableName);
        if (schemaInfo == null) {
            continue;
        }
        String strDDL = "";
        if (schemaInfo.getVirtual().equals(ClassType.VIEW.getText())) {
            strDDL = createViewSchema(schemaInfo);
        } else {
            strDDL = ddl.getSchemaDDL(schemaInfo);
        }
        tableSchema.setSchemaInfo(strDDL);
    }
}
Also used : TableSchema(com.cubrid.common.ui.compare.schema.model.TableSchema) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) HashMap(java.util.HashMap) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)

Example 2 with SchemaDDL

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL 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 SchemaDDL

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL in project cubrid-manager by CUBRID.

the class TableSchemaCompareComposite method getTableSchema.

private String getTableSchema(CubridDatabase db, Map<String, SchemaInfo> schemas, String tableName) {
    // FIXME logic code move to core module
    String tableSchemaInfo = "";
    try {
        SchemaInfo schemaInfo = schemas.get(tableName);
        if (schemaInfo == null) {
            return "";
        }
        if (schemaInfo.getVirtual().equals(ClassType.VIEW.getText())) {
            GetAllClassListTask getAllClassListTask = new GetAllClassListTask(db.getDatabaseInfo());
            getAllClassListTask.setTableName(tableName);
            getAllClassListTask.getClassInfoTaskExcute();
            ClassInfo classInfo = getAllClassListTask.getClassInfo();
            GetAllAttrTask getAllAttrTask = new GetAllAttrTask(db.getDatabaseInfo());
            getAllAttrTask.setClassName(tableName);
            getAllAttrTask.getAttrList();
            List<DBAttribute> attrList = getAllAttrTask.getAllAttrList();
            List<Map<String, String>> viewColListData = GetInfoDataUtil.getViewColMapList(attrList);
            /*Get view column*/
            GetViewAllColumnsTask getAllDBVclassTask = new GetViewAllColumnsTask(db.getDatabaseInfo());
            getAllDBVclassTask.setClassName(tableName);
            getAllDBVclassTask.getAllVclassListTaskExcute();
            /*Get query list*/
            List<String> vclassList = getAllDBVclassTask.getAllVclassList();
            List<Map<String, String>> queryListData = new ArrayList<Map<String, String>>();
            for (String sql : vclassList) {
                Map<String, String> map = new HashMap<String, String>();
                map.put("0", sql);
                queryListData.add(map);
            }
            tableSchemaInfo = GetInfoDataUtil.getViewCreateSQLScript(true, db, classInfo, tableName, viewColListData, queryListData);
        } else {
            SchemaDDL schemaDDL = null;
            schemaDDL = new SchemaDDL(null, db.getDatabaseInfo());
            tableSchemaInfo = schemaDDL.getSchemaDDL(schemaInfo);
        }
    } catch (Exception e) {
        LOGGER.error("", e);
        return "";
    }
    return tableSchemaInfo;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GetAllClassListTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllClassListTask) GetViewAllColumnsTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetViewAllColumnsTask) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) HashMap(java.util.HashMap) Map(java.util.Map) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ClassInfo(com.cubrid.cubridmanager.core.cubrid.table.model.ClassInfo) GetAllAttrTask(com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)

Example 4 with SchemaDDL

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL 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 5 with SchemaDDL

use of com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL in project cubrid-manager by CUBRID.

the class AbsExportDataHandler method exportSchemaToOBSFile.

/**
	 * Export the schema and index DDL to file
	 *
	 * @param databaseInfo DatabaseInfo
	 * @param exportDataEventHandler IExportDataEventHandler
	 * @param tableNameList Set<String>
	 * @param schemaFile String
	 * @param indexFile String
	 * @param fileCharset String
	 * @param exportStartValue whether keep start value
	 * @throws SQLException The SQL exception
	 * @throws IOException The IO exception
	 */
public static void exportSchemaToOBSFile(DatabaseInfo databaseInfo, IExportDataEventHandler exportDataEventHandler, Set<String> tableNameList, String schemaFile, String indexFile, String fileCharset, boolean exportStartValue) throws SQLException, IOException {
    // FIXME move this logic to core module
    if (schemaFile == null && indexFile == null) {
        return;
    }
    Connection conn = null;
    BufferedWriter schemaWriter = null;
    BufferedWriter indexWriter = null;
    LinkedList<SchemaInfo> schemaInfoList = null;
    boolean hasDataInIndexFile = false;
    try {
        if (schemaFile != null) {
            schemaWriter = getBufferedWriter(schemaFile, fileCharset);
        }
        if (indexFile != null) {
            indexWriter = getBufferedWriter(indexFile, fileCharset);
            schemaInfoList = new LinkedList<SchemaInfo>();
        }
        SchemaDDL schemaDDL = new SchemaDDL(new SchemaChangeManager(databaseInfo, true), databaseInfo);
        conn = JDBCConnectionManager.getConnection(databaseInfo, false);
        for (String tableName : tableNameList) {
            SchemaInfo schemaInfo = databaseInfo.getSchemaInfo(conn, tableName);
            if (schemaInfo == null) {
                continue;
            }
            //write the create DDL
            if (schemaWriter == null) {
                continue;
            }
            String ddl = schemaDDL.getSchemaDDL(schemaInfo, false);
            if (ddl != null && ddl.trim().length() > 0) {
                schemaWriter.write(ddl);
                schemaWriter.write(StringUtil.NEWLINE);
            }
            schemaWriter.flush();
            if (exportStartValue) {
                List<SerialInfo> autoIncrementList = schemaDDL.getAutoIncrementList(schemaInfo);
                if (autoIncrementList.size() > 0) {
                    for (SerialInfo serialInfo : autoIncrementList) {
                        String serialName = getSerailName(tableName, serialInfo.getAttName());
                        String currentValue = getCurrentValueFromSystemTable(serialName, databaseInfo, conn);
                        String alterStartValueDDL = schemaDDL.getAlterSerialStartValueDDL(serialName, currentValue);
                        schemaWriter.write(alterStartValueDDL);
                        schemaWriter.write(StringUtil.NEWLINE);
                    }
                }
            }
            if (indexWriter == null) {
                continue;
            }
            schemaInfoList.add(schemaInfo);
        }
        // write PKs, indexes to a file
        if (schemaInfoList != null) {
            String ddl = null;
            // write pk
            for (SchemaInfo schemaInfo : schemaInfoList) {
                ddl = schemaDDL.getPKsDDL(schemaInfo);
                if (ddl != null && ddl.trim().length() > 0) {
                    indexWriter.write(ddl.trim());
                    indexWriter.newLine();
                    hasDataInIndexFile = true;
                }
            }
            if (hasDataInIndexFile) {
                indexWriter.newLine();
            }
            // write index
            for (SchemaInfo schemaInfo : schemaInfoList) {
                ddl = schemaDDL.getIndexsDDL(schemaInfo);
                if (ddl != null && ddl.trim().length() > 0) {
                    indexWriter.write(ddl.trim());
                    indexWriter.newLine();
                    hasDataInIndexFile = true;
                }
            }
            if (hasDataInIndexFile) {
                indexWriter.newLine();
            }
            //write fk
            for (SchemaInfo schemaInfo : schemaInfoList) {
                ddl = schemaDDL.getFKsDDL(schemaInfo);
                if (ddl != null && ddl.trim().length() > 0) {
                    indexWriter.write(ddl.trim());
                    indexWriter.newLine();
                    hasDataInIndexFile = true;
                }
            }
            if (hasDataInIndexFile) {
                indexWriter.flush();
            }
        }
    } finally {
        QueryUtil.freeQuery(conn);
        FileUtil.close(schemaWriter);
        FileUtil.close(indexWriter);
        if (!hasDataInIndexFile) {
            FileUtil.delete(indexFile);
        }
    }
}
Also used : SchemaChangeManager(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager) Connection(java.sql.Connection) SchemaDDL(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) BufferedWriter(java.io.BufferedWriter) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)10 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)8 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)5 WrappedDatabaseInfo (com.cubrid.common.ui.cubrid.database.erwin.WrappedDatabaseInfo)4 SchemaChangeManager (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeManager)4 HashMap (java.util.HashMap)4 TableSchema (com.cubrid.common.ui.compare.schema.model.TableSchema)3 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)2 TableDetailInfo (com.cubrid.common.core.common.model.TableDetailInfo)2 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 PartInitException (org.eclipse.ui.PartInitException)2 Constraint (com.cubrid.common.core.common.model.Constraint)1 IDatabaseSpec (com.cubrid.common.core.common.model.IDatabaseSpec)1 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)1 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)1 TableSchemaCompareModel (com.cubrid.common.ui.compare.schema.model.TableSchemaCompareModel)1 TableSchemaCompareUpdateDDL (com.cubrid.common.ui.compare.schema.model.TableSchemaCompareUpdateDDL)1 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)1 ERVirtualDatabaseInfo (com.cubrid.common.ui.er.model.ERVirtualDatabaseInfo)1