Search in sources :

Example 61 with Constraint

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

the class CubridTableParser method getRemovedFKCount.

public int getRemovedFKCount() {
    int count = 0;
    Iterator<String> it = removedFKConstraint.keySet().iterator();
    while (it.hasNext()) {
        List<Constraint> fks = removedFKConstraint.get(it.next());
        count += fks.size();
    }
    return count;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) Constraint(com.cubrid.common.core.common.model.Constraint)

Example 62 with Constraint

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

the class SchemaAlterDDLTest method indexTest2.

//drop exist index
private void indexTest2() {
    changeList = new SchemaChangeManager(databaseInfo, false);
    ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaInfo newSchema = sup2.clone();
    String indexName = null;
    String indexType = null;
    indexName = "u_sup2_date_d";
    indexType = "UNIQUE";
    Constraint index = newSchema.getConstraintByName(indexName, indexType);
    newSchema.removeConstraintByName(indexName, indexType);
    changeList.addSchemeChangeLog(new SchemaChangeLog(index.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    index.getName(), //$NON-NLS-1$
    null, SchemeInnerType.TYPE_INDEX));
    indexName = "i_sup2_bigint_numeric1";
    indexType = "INDEX";
    index = newSchema.getConstraintByName(indexName, indexType);
    newSchema.removeConstraintByName(indexName, indexType);
    changeList.addSchemeChangeLog(new SchemaChangeLog(index.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    index.getName(), //$NON-NLS-1$
    null, SchemeInnerType.TYPE_INDEX));
    indexName = "ru_sup2_numeric2_float";
    indexType = "REVERSE UNIQUE";
    index = newSchema.getConstraintByName(indexName, indexType);
    newSchema.removeConstraintByName(indexName, indexType);
    changeList.addSchemeChangeLog(new SchemaChangeLog(index.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    index.getName(), //$NON-NLS-1$
    null, SchemeInnerType.TYPE_INDEX));
    indexName = "ri_sup2_numeric1_float";
    indexType = "REVERSE INDEX";
    index = newSchema.getConstraintByName(indexName, indexType);
    newSchema.removeConstraintByName(indexName, indexType);
    changeList.addSchemeChangeLog(new SchemaChangeLog(index.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    index.getName(), //$NON-NLS-1$
    null, SchemeInnerType.TYPE_INDEX));
    String expected = "ALTER TABLE sup2 DROP UNIQUE INDEX u_sup2_date_d;" + StringUtil.NEWLINE;
    expected += "ALTER TABLE sup2 DROP INDEX i_sup2_bigint_numeric1;" + StringUtil.NEWLINE;
    expected += "ALTER TABLE sup2 DROP REVERSE UNIQUE INDEX ru_sup2_numeric2_float;" + StringUtil.NEWLINE;
    expected += "ALTER TABLE sup2 DROP REVERSE INDEX ri_sup2_numeric1_float;";
    String alterDDL = ddl.getAlterDDL(sup2, newSchema);
    assertEquals(expected, alterDDL.trim());
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 63 with Constraint

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

the class SchemaAlterDDLTest method indexTest1.

//add indexes, then drop them
private void indexTest1() {
    changeList = new SchemaChangeManager(databaseInfo, false);
    ddl = new SchemaDDL(changeList, databaseInfo);
    SchemaInfo newSchema = sup3.clone();
    Constraint constraint = null;
    String indexType = null;
    String indexName = "";
    String tableName = newSchema.getClassname();
    //add unique
    constraint = new Constraint(false);
    indexType = "UNIQUE";
    constraint.setType(indexType);
    constraint.addAttribute("integer");
    constraint.addRule("integer DESC");
    constraint.addAttribute("bigint");
    constraint.addRule("bigint ASC");
    if (indexName.equals("")) {
        //$NON-NLS-1$
        indexName = constraint.getDefaultName(tableName);
    }
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add index
    constraint = new Constraint(false);
    indexType = "INDEX";
    constraint.setType(indexType);
    constraint.addAttribute("numeric1");
    constraint.addRule("numeric1 ASC");
    constraint.addAttribute("numeric2");
    constraint.addRule("numeric2 DESC");
    constraint.addAttribute("float");
    constraint.addRule("float ASC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add reverse unique
    constraint = new Constraint(false);
    indexType = "REVERSE UNIQUE";
    constraint.setType(indexType);
    constraint.addAttribute("numeric2");
    constraint.addRule("numeric2 DESC");
    constraint.addAttribute("float");
    constraint.addRule("float DESC");
    constraint.addAttribute("setint");
    constraint.addRule("setint DESC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    //add reverse index
    constraint = new Constraint(false);
    indexType = "REVERSE INDEX";
    constraint.setType(indexType);
    constraint.addAttribute("integer");
    constraint.addRule("integer DESC");
    constraint.addAttribute("bigint");
    constraint.addRule("bigint DESC");
    indexName = constraint.getDefaultName(tableName);
    constraint.setName(indexName);
    newSchema.addConstraint(constraint);
    changeList.addSchemeChangeLog(new SchemaChangeLog(null, constraint.getDefaultName(newSchema.getClassname()) + "$" + //$NON-NLS-1$
    constraint.getName(), SchemeInnerType.TYPE_INDEX));
    String expected = "CREATE UNIQUE INDEX u_sup3_integer_d_bigint ON sup3([integer] DESC,[bigint]);" + StringUtil.NEWLINE;
    expected += "CREATE INDEX i_sup3_numeric1_numeric2_d_float ON sup3(numeric1,numeric2 DESC,[float]);" + StringUtil.NEWLINE;
    expected += "CREATE REVERSE UNIQUE INDEX ru_sup3_numeric2_float_setint ON sup3(numeric2 DESC,[float] DESC,setint DESC);" + StringUtil.NEWLINE;
    expected += "CREATE REVERSE INDEX ri_sup3_integer_bigint ON sup3([integer] DESC,[bigint] DESC);";
    String alterDDL = ddl.getAlterDDL(sup3, newSchema);
    assertEquals(expected, alterDDL.trim());
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 64 with Constraint

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

the class SchemaAlterDDLTest method editColumn.

/**
	 * edit a column
	 * 
	 * @param newSchema
	 * @param superList
	 * @param editAttribute
	 * @param oldAttribute
	 * @param isEditAll
	 */
private void editColumn(SchemaInfo newSchema, DBAttribute editAttribute, DBAttribute oldAttribute, boolean isEditAll) {
    if (editAttribute != null) {
        editAttribute.setInherit(newSchema.getClassname());
    } else {
        return;
    }
    String newAttrName = editAttribute.getName();
    boolean isNewAttrClass = editAttribute.isClassAttribute();
    boolean isOldAttrClass = oldAttribute.isClassAttribute();
    String attrName = oldAttribute.getName();
    String tableName = newSchema.getClassname();
    if (isEditAll) {
        if (isOldAttrClass != isNewAttrClass) {
            // attribute
            // type
            // changed
            newSchema.removeDBAttributeByName(attrName, isOldAttrClass);
            addDropAttrLog(attrName, isOldAttrClass);
            newSchema.addDBAttribute(editAttribute, isNewAttrClass);
            addNewAttrLog(newAttrName, isNewAttrClass);
        } else {
            newSchema.replaceDBAttributeByName(oldAttribute, editAttribute, isNewAttrClass, superList);
            addEditAttrLog(attrName, newAttrName, isNewAttrClass);
        }
        if (!oldAttribute.isUnique() && editAttribute.isUnique()) {
            Constraint unique = new Constraint(false);
            unique.setType(Constraint.ConstraintType.UNIQUE.getText());
            unique.addAttribute(newAttrName);
            unique.addRule(newAttrName + " ASC");
            unique.setName(ConstraintNamingUtil.getUniqueName(tableName, unique.getRules()));
            newSchema.addConstraint(unique);
        } else if (oldAttribute.isUnique() && !editAttribute.isUnique()) {
            newSchema.removeUniqueByAttrName(attrName);
        }
    } else {
        newSchema.replaceDBAttributeByName(oldAttribute, editAttribute, isNewAttrClass, superList);
        addEditAttrLog(attrName, newAttrName, isNewAttrClass);
    }
    SuperClassUtil.fireSuperClassChanged(databaseInfo, testedSchemaInfo, newSchema, newSchema.getSuperClasses());
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint)

Example 65 with Constraint

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

the class ConstraintComparator method getAlterAttrDDL.

/**
	 * Get alter DBAttribute ddl
	 *
	 * @param oldAttr
	 * @param newAttr
	 * @param oldSchemaInfo
	 * @param newSchemaInfo
	 * @param oldSupers
	 * @param newSupers
	 * @param isClassAttr
	 * @param attrMap
	 * @param tableName
	 * @return
	 */
private String getAlterAttrDDL(DBAttribute oldAttr, DBAttribute newAttr, SchemaInfo oldSchemaInfo, SchemaInfo newSchemaInfo, List<SchemaInfo> oldSupers, List<SchemaInfo> newSupers, boolean isClassAttr, Map<String, String> attrMap, String tableName, DDLGenerator generator) {
    StringBuffer ddlBuffer = new StringBuffer();
    String oldColumnName = oldAttr.getName().toLowerCase();
    String columnName = oldColumnName;
    String newColumnName = newAttr.getName().toLowerCase();
    if (!newColumnName.equals(oldColumnName)) {
        String ddl = getRenameColumnNameDDL(tableName, oldColumnName, newColumnName, isClassAttr);
        ddlBuffer.append(ddl).append(endLineChar).append(StringUtil.NEWLINE);
        columnName = newColumnName;
        attrMap.put(oldColumnName, newColumnName);
    }
    boolean oldNotNull = oldAttr.isNotNull();
    boolean newNotNull = newAttr.isNotNull();
    boolean notNullChanged = oldNotNull != newNotNull;
    boolean hasNotNullDDL = false;
    if (notNullChanged) {
        boolean isChangedByPK = false;
        if (newNotNull) {
            // add a new PK
            Constraint pk = newSchemaInfo.getPK(newSupers);
            List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
            if (pkAttributes.contains(newColumnName)) {
                isChangedByPK = true;
            }
        } else {
            // drop an old PK
            Constraint pk = oldSchemaInfo.getPK(oldSupers);
            List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
            if (pkAttributes.contains(newColumnName)) {
                isChangedByPK = true;
            }
        }
        if (!isChangedByPK) {
            hasNotNullDDL = true;
            // null constraint changed,it is not support for 8.2.2
            String editDDL = null;
            if (isClassAttr) {
                editDDL = getChangeAttributeDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, newSupers);
            } else {
                editDDL = getChangeColumnDDL(tableName, oldAttr, newAttr, oldSchemaInfo, newSchemaInfo, oldSupers, newSupers);
            }
            return editDDL;
        }
    }
    String oldDefault = oldAttr.getDefault();
    String newDefault = newAttr.getDefault();
    boolean defaultChanged = oldDefault == null ? newDefault != null : !oldDefault.equals(newDefault);
    if (defaultChanged && !hasNotNullDDL) {
        if (newDefault == null) {
            newDefault = "null";
        } else {
            FormatDataResult result = DBAttrTypeFormatter.formatForInput(newAttr.getType(), newDefault, false);
            if (result.isSuccess()) {
                newDefault = result.getFormatResult();
            }
        }
        String ddl = getChangeDefaultValueDDL(tableName, columnName, newDefault, isClassAttr);
        ddlBuffer.append(ddl).append(endLineChar).append(StringUtil.NEWLINE);
    }
    SerialInfo oldAutoIncrement = oldAttr.getAutoIncrement();
    SerialInfo newAutoIncrement = newAttr.getAutoIncrement();
    if (null != newAutoIncrement && !newAutoIncrement.equals(oldAutoIncrement)) {
        String increment = getAlterAutoIncrementDDL(tableName, newColumnName);
        ddlBuffer.append(increment);
    }
    return ddlBuffer.toString();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SerialInfo(com.cubrid.common.core.common.model.SerialInfo)

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