Search in sources :

Example 36 with SchemaInfo

use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.

the class SuperClassUtil method fireSuperClassChanged.

/**
	 * reset attributes and resolutions when super classes change, eg: <li>when
	 * adding a super class, some naming conflicts maybe occurs, so some
	 * resolution would be added, attributes to the schema should be reset and
	 * ordered
	 * 
	 * @param database DatabaseInfo the given reference of a DatabaseInfo object
	 * @param oldSchemaInfo SchemaInfo the given reference of old SchemaInfo
	 *        object
	 * @param newSchemaInfo SchemaInfo the given reference of new SchemaInfo
	 *        object
	 * @param newSupers List<String>
	 * @return boolean whether is succeed
	 */
public static boolean fireSuperClassChanged(DatabaseInfo database, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<String> newSupers) {
    //checking attribute
    List<DBResolution> newResolutions = getCloneResolutions(newSchemaInfo.getResolutions());
    List<DBAttribute> localAttributes = newSchemaInfo.getLocalAttributes();
    Map<String, List<SchemaInfo>> columnInheritSchemaMap = new HashMap<String, List<SchemaInfo>>();
    boolean success = checkingOnSuperClassChanged(columnInheritSchemaMap, database, oldSchemaInfo, newSchemaInfo, newSupers, localAttributes, newResolutions, false);
    if (!success) {
        return false;
    }
    //checking class attributes
    List<DBResolution> newClassResolutions = getCloneResolutions(newSchemaInfo.getClassResolutions());
    List<DBAttribute> localClassAttributes = newSchemaInfo.getLocalClassAttributes();
    Map<String, List<SchemaInfo>> classColumnInheritSchemaMap = new HashMap<String, List<SchemaInfo>>();
    boolean classSuccess = checkingOnSuperClassChanged(classColumnInheritSchemaMap, database, oldSchemaInfo, newSchemaInfo, newSupers, localClassAttributes, newClassResolutions, true);
    if (!classSuccess) {
        return false;
    }
    if (database == null) {
        return false;
    }
    //reset resolution, super classes and resolution
    List<SchemaInfo> schemalist = new ArrayList<SchemaInfo>();
    for (String sup : newSupers) {
        schemalist.add(database.getSchemaInfo(sup).clone());
    }
    if (success && classSuccess) {
        //remove inherit constraint
        List<SchemaInfo> superList = getSuperClasses(database, newSchemaInfo);
        List<Constraint> constraints = newSchemaInfo.getConstraints();
        for (int j = constraints.size() - 1; j >= 0; j--) {
            Constraint constraint = constraints.get(j);
            String constraintType = constraint.getType();
            String constraintName = constraint.getName();
            boolean isConstraintInheritSupported = isConstraintTypeInherit(constraintType);
            if (isConstraintInheritSupported && newSchemaInfo.isInSuperClasses(superList, constraintName)) {
                constraints.remove(j);
            }
        }
        resetAttribute(newSchemaInfo, schemalist, newResolutions, localAttributes, columnInheritSchemaMap, false);
        newSchemaInfo.setResolutions(newResolutions);
        resetAttribute(newSchemaInfo, schemalist, newClassResolutions, localClassAttributes, classColumnInheritSchemaMap, true);
        newSchemaInfo.setClassResolutions(newClassResolutions);
        newSchemaInfo.setSuperClasses(newSupers);
        //add inherit constraint
        superList = getSuperClasses(database, newSupers);
        for (SchemaInfo schema : superList) {
            constraints = schema.getConstraints();
            for (int i = 0; i < constraints.size(); i++) {
                Constraint constraint = constraints.get(i);
                String constraintType = constraint.getType();
                boolean isConstraintInheritSupported = isConstraintTypeInherit(constraintType);
                if (isConstraintInheritSupported) {
                    newSchemaInfo.addConstraint(constraint);
                }
            }
        }
    }
    return true;
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) DBResolution(com.cubrid.common.core.common.model.DBResolution) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 37 with SchemaInfo

use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.

the class ConstraintComparator method getAlterDDL.

/**
	 * Return an alter DDL of schema, some changes stored in change
	 * logs(SchemaChangeManager), others are found by differing old and new
	 * schema objects
	 *
	 * @param oldSchemaInfo SchemaInfo the old reference of oldSchemaInfo
	 * @param newSchemaInfo SchemaInfo the new reference of oldSchemaInfo
	 * @return String a string indicates the info of DDL
	 */
public String getAlterDDL(SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo) {
    DDLGenerator generator = new DDLGenerator();
    if (oldSchemaInfo == null) {
        return null;
    }
    List<SchemaInfo> oldSupers = SuperClassUtil.getSuperClasses(databaseInfo, oldSchemaInfo);
    if (oldSupers == null) {
        return null;
    }
    List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(databaseInfo, newSchemaInfo);
    if (newSupers == null) {
        return null;
    }
    // old --> new
    Map<String, String> attrMap = new HashMap<String, String>();
    //Generate the DDL for rename table
    String oldTableName = oldSchemaInfo.getClassname().toLowerCase();
    String newTableName = newSchemaInfo.getClassname().toLowerCase();
    String tableName = oldTableName;
    if (!oldTableName.equals(newTableName)) {
        String renameDDL = getRenameTableDDL(oldTableName, newTableName);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_REBANE_TABLE, newSchemaInfo, renameDDL);
        tableName = newTableName;
    }
    String oldCollation = oldSchemaInfo.getCollation();
    String newCollation = newSchemaInfo.getCollation();
    if (!StringUtil.isEmpty(newCollation) && !StringUtil.isEqualNotIgnoreNull(oldCollation, newCollation)) {
        String alterCollationDDL = getAlterTableCollationDDL(oldSchemaInfo, newSchemaInfo);
        generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_TABLE_COLLATE, newSchemaInfo, alterCollationDDL);
    }
    //Generate the DDL for column attribute change
    List<SchemaChangeLog> allAttrChanges = changeLogMgr.getClassAttrChangeLogs();
    allAttrChanges.addAll(changeLogMgr.getAttrChangeLogs());
    // only new added attribute and after version 8.4.0 support to reorder
    boolean isSupportReorderColumn = CompatibleUtil.isSupportReorderColumn(databaseInfo);
    if (isSupportReorderColumn) {
        /*For the bug TOOLS-1258 After add column, change column name, it will pop error.*/
        /*Sort the change log first*/
        Collections.sort(allAttrChanges, new ChangeLogCompartor(newSchemaInfo));
        for (SchemaChangeLog log : allAttrChanges) {
            boolean isClassAttr = false;
            if (log.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE) {
                isClassAttr = true;
            } else {
                isClassAttr = false;
            }
            appendChangeAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, isClassAttr, generator);
        }
        List<SchemaChangeLog> allPosChangeLogs = changeLogMgr.getPositionChangeLogs();
        for (SchemaChangeLog log : allPosChangeLogs) {
            if (!generator.hasProcessedAttr(log.getOldValue())) {
                for (DBAttribute attr : newSchemaInfo.getAttributes()) {
                    if (attr.getName().equals(log.getOldValue())) {
                        appendChangeAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, attr.isClassAttribute(), generator);
                        break;
                    }
                }
            }
        }
    } else {
        for (SchemaChangeLog log : allAttrChanges) {
            appendAlterAttributeDDL(oldSchemaInfo, newSchemaInfo, oldSupers, newSupers, attrMap, tableName, log, generator);
        }
        if (isSupportReorderColumn) {
            generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_POS, newSchemaInfo, getAddReorderColumnDDL(oldSchemaInfo, newSchemaInfo, newSupers, tableName));
        }
    }
    //Generate the DDL for super class change
    List<String> oldSuperClasses = oldSchemaInfo.getSuperClasses();
    List<String> newSuperClasses = newSchemaInfo.getSuperClasses();
    List<List<String>> superChanges = getSuperclassChanges(oldSuperClasses, newSuperClasses);
    generator.addSchemaDDLMode(DDLGenerator.TYPE_CHANGE_SUPER, newSchemaInfo, appendChangedSuperDDL(oldSchemaInfo, newSchemaInfo, tableName, oldSuperClasses, newSuperClasses, superChanges));
    //Generate the DDL for PK change
    List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, newSchemaInfo);
    allSupers.addAll(newSupers);
    allSupers.addAll(oldSupers);
    Constraint newPK = newSchemaInfo.getPK(allSupers);
    Constraint oldPK = oldSchemaInfo.getPK(oldSupers);
    if (oldPK == null && newPK != null) {
        // add pk
        List<String> pkAttributes = newPK.getAttributes();
        if (pkAttributes != null && pkAttributes.size() > 0) {
            String addPKDDL = getAddPKDDL(tableName, pkAttributes, newPK.getName()) + endLineChar + StringUtil.NEWLINE;
            generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_INDEX, newPK, addPKDDL);
        }
    } else if (oldPK != null && newPK == null) {
        // del pk
        String dropPKDDL = dropPK(tableName, oldPK.getName()) + endLineChar + StringUtil.NEWLINE;
        generator.addPreDDLMode(DDLGenerator.TYPE_DROP_INDEX, oldPK, dropPKDDL);
    } else if (oldPK != null && newPK != null) {
        appendChangedPkDDL(attrMap, tableName, newPK, oldPK, generator);
    }
    //Generate the DDL for FK change
    List<SchemaChangeLog> fkChanges = changeLogMgr.getFKChangeLogs();
    for (SchemaChangeLog log : fkChanges) {
        appendChangedFkDDL(oldSchemaInfo, newSchemaInfo, attrMap, tableName, log, generator);
    }
    List<SchemaChangeLog> indexChanges = changeLogMgr.getIndexChangeLogs();
    for (SchemaChangeLog log : indexChanges) {
        appendChanedIndexDDL(oldSchemaInfo, newSchemaInfo, tableName, log, generator);
    }
    // Partitioning
    boolean isPartitionChanged = isPartitonChange(oldSchemaInfo.getPartitionList(), newSchemaInfo.getPartitionList());
    if ("YES".equals(oldSchemaInfo.isPartitionGroup()) && isPartitionChanged) {
        String sql = getTransformToGenericDDL(tableName) + endLineChar + StringUtil.NEWLINE;
        generator.addSchemaDDLMode(DDLGenerator.TYPE_DROP_PARTITON, oldSchemaInfo.getPartitionList(), sql);
    }
    if (isPartitionChanged) {
        List<PartitionInfo> partitionInfoList = newSchemaInfo.getPartitionList();
        String sql = getTransformToPartitionDDL(partitionInfoList);
        if (sql != null) {
            sql = sql + endLineChar + StringUtil.NEWLINE;
            generator.addSchemaDDLMode(DDLGenerator.TYPE_ADD_PARTITON, oldSchemaInfo.getPartitionList(), sql);
        }
    }
    return generator.generatorDDL();
}
Also used : HashMap(java.util.HashMap) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList) List(java.util.List) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 38 with SchemaInfo

use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.

the class ConstraintComparator method getIndexDDL.

/**
	 * Get the index DDL
	 *
	 * @param schemaInfo SchemaInfo
	 * @return The index related DDL
	 */
public String getIndexDDL(SchemaInfo schemaInfo) {
    String tableName = schemaInfo.getClassname();
    StringBuffer ddlBuffer = new StringBuffer();
    //Get the PK
    List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
    Constraint pk = schemaInfo.getPK(allSupers);
    if (pk != null) {
        List<String> pkAttributes = pk.getAttributes();
        if (pkAttributes != null && pkAttributes.size() > 0) {
            ddlBuffer.append(getAddPKDDL(tableName, pkAttributes, pk.getName()));
            ddlBuffer.append(endLineChar);
            ddlBuffer.append(StringUtil.NEWLINE);
            ddlBuffer.append(StringUtil.NEWLINE);
        }
    }
    //Get the FK
    List<Constraint> fkList = schemaInfo.getFKConstraints();
    if (fkList != null) {
        for (Constraint fk : fkList) {
            ddlBuffer.append(getAddFKDDL(tableName, fk));
            ddlBuffer.append(endLineChar);
            ddlBuffer.append(StringUtil.NEWLINE);
            ddlBuffer.append(StringUtil.NEWLINE);
        }
    }
    //Get the index
    List<Constraint> constaintList = schemaInfo.getConstraints();
    if (!constaintList.isEmpty()) {
        for (int i = 0; i < constaintList.size(); i++) {
            Constraint constraint = constaintList.get(i);
            List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
            if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
                String type = constraint.getType();
                if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE INDEX".equals(type) || "REVERSE UNIQUE".equals(type)) {
                    String indexDDL = getCreateIndexDDL(tableName, constraint);
                    if (StringUtil.isNotEmpty(indexDDL)) {
                        ddlBuffer.append(indexDDL);
                        ddlBuffer.append(endLineChar);
                        ddlBuffer.append(StringUtil.NEWLINE);
                        ddlBuffer.append(StringUtil.NEWLINE);
                    }
                }
            }
        }
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 39 with SchemaInfo

use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.

the class ConstraintComparator method getIndexsDDL.

/**
	 * get the pk DDL
	 *
	 * @param schemaInfo SchemaInfo
	 * @return pk DDL
	 */
public String getIndexsDDL(SchemaInfo schemaInfo) {
    String tableName = schemaInfo.getClassname();
    StringBuffer ddlBuffer = new StringBuffer();
    //Get the index
    List<Constraint> constaintList = schemaInfo.getConstraints();
    if (!constaintList.isEmpty()) {
        for (int i = 0; i < constaintList.size(); i++) {
            Constraint constraint = constaintList.get(i);
            List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(databaseInfo, schemaInfo);
            if (!schemaInfo.isInSuperClasses(superList, constraint.getName())) {
                String type = constraint.getType();
                if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE INDEX".equals(type) || "REVERSE UNIQUE".equals(type)) {
                    String indexDDL = getCreateIndexDDL(tableName, constraint);
                    if (StringUtil.isNotEmpty(indexDDL)) {
                        ddlBuffer.append(indexDDL);
                        ddlBuffer.append(endLineChar);
                        ddlBuffer.append(StringUtil.NEWLINE);
                        ddlBuffer.append(StringUtil.NEWLINE);
                    }
                }
            }
        }
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 40 with SchemaInfo

use of com.cubrid.common.core.common.model.SchemaInfo in project cubrid-manager by CUBRID.

the class DataCompareEditorPart method compareHashedCompareDataCompatible.

private void compareHashedCompareDataCompatible(Connection conn, DataCompare dataCompare, List<HashedCompareData> dataList) {
    // FIXME logic code move to core module
    if (dataList == null || dataList.size() == 0) {
        // TODO error
        return;
    }
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    SchemaInfo schemaInfo = dataCompare.getSchemaInfo();
    String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
    StringBuilder sql = new StringBuilder();
    sql.append(" SELECT * FROM ").append(escapedTableName);
    sql.append(" WHERE ");
    {
        HashedCompareData data = dataList.get(0);
        for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
            if (i > 0) {
                sql.append(" AND ");
            }
            String columnName = data.getPkColumns().get(i);
            sql.append(QuerySyntax.escapeKeyword(columnName)).append(" = ?");
        }
    }
    for (HashedCompareData data : dataList) {
        if (runningState != RUNNING_SATE_ONLINE) {
            return;
        }
        boolean exists = false;
        StringBuilder hash = new StringBuilder();
        try {
            pstmt = conn.prepareStatement(sql.toString());
            for (int i = 0, len = data.getPkColumns().size(); i < len; i++) {
                String columnValue = data.getPkValues().get(i);
                pstmt.setString(i + 1, columnValue);
            }
            rs = pstmt.executeQuery();
            if (!rs.next()) {
                exists = false;
            } else {
                exists = true;
                ResultSetMetaData meta = rs.getMetaData();
                for (int j = 1, len = meta.getColumnCount(); j <= len; j++) {
                    String columnName = meta.getColumnName(j);
                    boolean isPk = data.getPkColumns().contains(columnName);
                    if (isPk) {
                        continue;
                    }
                    String val = null;
                    if (DataType.isSetDataType(meta.getColumnTypeName(j))) {
                        String colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, meta.getColumnTypeName(j));
                        val = (String) FieldHandlerUtils.getRsValueForExport(colType, (CUBRIDResultSetProxy) rs, j, "{NULL}");
                    } else {
                        val = rs.getString(j);
                    }
                    if (val == null) {
                        val = "{NULL}";
                    }
                    hash.append(StringUtil.md5(val));
                }
            }
        } catch (SQLException e) {
            exists = false;
            printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
            printToConsoleOnWorkThread(e.getMessage(), true);
            LOGGER.error(sql.toString(), e);
        } finally {
            QueryUtil.freeQuery(pstmt, rs);
        }
        dataCompare.increaseProgress();
        if (!exists) {
            dataCompare.increaseNotExists();
            addLog(schemaInfo.getClassname(), data, charset);
        } else {
            if (data.getHash().equals(StringUtil.md5(hash.toString()))) {
                dataCompare.increaseMatches();
            } else {
                dataCompare.increaseNotMatches();
                addLog(schemaInfo.getClassname(), data, charset);
            }
        }
        completedRecords++;
        refreshProgressBar();
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                compareTableViewer.refresh();
            }
        });
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) HashedCompareData(com.cubrid.common.ui.compare.data.model.HashedCompareData) PreparedStatement(java.sql.PreparedStatement) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)136 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)57 Constraint (com.cubrid.common.core.common.model.Constraint)56 ArrayList (java.util.ArrayList)47 HashMap (java.util.HashMap)15 List (java.util.List)15 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)11 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)11 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)10 Connection (java.sql.Connection)10 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)9 SQLException (java.sql.SQLException)9 TableItem (org.eclipse.swt.widgets.TableItem)9 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)8 SchemaDDL (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaDDL)8 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)7 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 DBResolution (com.cubrid.common.core.common.model.DBResolution)6 Map (java.util.Map)5