Search in sources :

Example 21 with Constraint

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

the class ConstraintComparator method getAddPKDDL.

/**
	 * Return the DDL of adding PK
	 *
	 * @param tableName String the given table name
	 * @param pkAttributes List<String> the given list includes the pk
	 *        attributes
	 * @return String a string indicates the add primary key statement
	 */
private String getAddPKDDL(String tableName, List<String> pkAttributes, String pkName) {
    StringBuffer bf = new StringBuffer();
    if (pkName != null) {
        bf.append("ALTER TABLE ").append(QuerySyntax.escapeKeyword(tableName)).append(" ADD CONSTRAINT ").append(QuerySyntax.escapeKeyword(pkName)).append(" PRIMARY KEY (");
    } else {
        bf.append("ALTER TABLE ").append(QuerySyntax.escapeKeyword(tableName)).append(" ADD PRIMARY KEY(");
    }
    int count = 0;
    for (String column : pkAttributes) {
        if (count > 0) {
            bf.append(",");
        }
        bf.append(QuerySyntax.escapeKeyword(column));
        count++;
    }
    bf.append(")");
    return bf.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Example 22 with Constraint

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

the class ConstraintComparator method getPKDDL.

/**
	 * DDL of PK in creating a schema
	 *
	 * @param tableName String the given table name
	 * @param constaint Constraint the given reference of a Constraint object,
	 *        which includes the info of primary key
	 * @return String a string that indicates the info of primary key
	 */
private String getPKDDL(String tableName, Constraint constaint) {
    if (!(constaint.getAttributes() != null && constaint.getAttributes().size() > 0)) {
        return "";
    }
    StringBuffer bf = new StringBuffer();
    bf.append("CONSTRAINT ");
    if (constaint.getName() == null || "".equals(constaint.getName().trim())) {
        bf.append(QuerySyntax.escapeKeyword(constaint.getDefaultName(tableName).toLowerCase()));
    } else {
        bf.append(QuerySyntax.escapeKeyword(constaint.getName().toLowerCase()));
    }
    bf.append(" ");
    bf.append("PRIMARY KEY(");
    List<String> list = constaint.getAttributes();
    for (int i = 0; i < list.size(); i++) {
        if (i != 0) {
            bf.append(",");
        }
        if (list.get(i).indexOf(" DESC") != -1 || list.get(i).indexOf(" ASC") != -1) {
            bf.append(list.get(i));
        } else {
            bf.append(QuerySyntax.escapeKeyword(list.get(i)));
        }
    }
    bf.append(")");
    return bf.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Example 23 with Constraint

use of com.cubrid.common.core.common.model.Constraint 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 24 with Constraint

use of com.cubrid.common.core.common.model.Constraint 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 25 with Constraint

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

the class ConstraintComparator method getCreateIndexDDL.

/**
	 * Return DDL of adding index, unique, reverse index or reverse unique
	 *
	 * @param tableName String the given table name
	 * @param indexConstaint Constraint the give reference of the Constraint
	 *        object
	 * @return String a string indicates the info of index
	 */
private String getCreateIndexDDL(String tableName, Constraint indexConstaint) {
    String type = indexConstaint.getType();
    String defaultName = indexConstaint.getDefaultName(tableName);
    StringBuffer bf = new StringBuffer();
    bf.append("CREATE ");
    if ("INDEX".equals(type)) {
        bf.append("INDEX");
    } else if ("REVERSE INDEX".equals(type)) {
        bf.append("REVERSE INDEX");
    } else if ("UNIQUE".equals(type)) {
        bf.append("UNIQUE INDEX");
    } else if ("REVERSE UNIQUE".equals(type)) {
        bf.append("REVERSE UNIQUE INDEX");
    }
    //TODO: it is needed to handle an exception if the type is not in above conditions.
    if (StringUtil.isNotEmpty(indexConstaint.getName())) {
        bf.append(" ").append(QuerySyntax.escapeKeyword(indexConstaint.getName()));
    } else {
        bf.append(" ").append(QuerySyntax.escapeKeyword(defaultName));
    }
    List<String> rules = indexConstaint.getRules();
    String[][] columnsRuleArray = new String[rules.size()][2];
    if ("UNIQUE".equals(type) || "INDEX".equals(type) || "REVERSE UNIQUE".equals(type) || "REVERSE INDEX".equals(type)) {
        for (int i = 0; i < rules.size(); i++) {
            String rule = rules.get(i);
            String[] strs = rule.trim().split(" ");
            if (strs[0].indexOf("(") > 0) {
                strs[0] = QuerySyntax.escapeKeyword(strs[0].substring(0, strs[0].indexOf("("))) + strs[0].substring(strs[0].indexOf("("));
            } else {
                strs[0] = QuerySyntax.escapeKeyword(strs[0]);
            }
            if (strs.length > 1) {
                columnsRuleArray[i][0] = strs[0];
                columnsRuleArray[i][1] = strs[1];
            } else {
                columnsRuleArray[i][0] = strs[0];
                columnsRuleArray[i][1] = "";
            }
        }
    }
    makeIndexColumns(bf, tableName, columnsRuleArray);
    String description = indexConstaint.getDescription();
    if (description != null) {
        description = StringUtil.escapeQuotes("'" + description + "'");
        bf.append(String.format(" COMMENT %s", description));
    }
    return bf.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Aggregations

Constraint (com.cubrid.common.core.common.model.Constraint)90 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)47 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)37 ArrayList (java.util.ArrayList)27 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)11 TableItem (org.eclipse.swt.widgets.TableItem)10 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)9 List (java.util.List)9 SchemaChangeLog (com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)7 HashMap (java.util.HashMap)6 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)4 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)4 Node (org.w3c.dom.Node)4 DBResolution (com.cubrid.common.core.common.model.DBResolution)3 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)3 ERSchema (com.cubrid.common.ui.er.model.ERSchema)3 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 NodeList (org.w3c.dom.NodeList)3