Search in sources :

Example 36 with Constraint

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

the class TableEditorPart method makeChangeLogForIndex.

public boolean makeChangeLogForIndex(String attrName, DBAttribute editAttr, DBAttribute origAttr) {
    List<Constraint> constrainList = newSchemaInfo.getConstraints();
    if (constrainList.size() == 0) {
        // FIXME move this logic to core module
        return false;
    }
    List<Constraint> removedConstrainList = new ArrayList<Constraint>();
    List<Constraint> addedConstrainList = new ArrayList<Constraint>();
    for (Constraint cons : constrainList) {
        if (cons == null) {
            continue;
        }
        if (ConstraintType.INDEX.getText().equals(cons.getType()) || ConstraintType.REVERSEINDEX.getText().equals(cons.getType()) || ConstraintType.UNIQUE.getText().equals(cons.getType()) || ConstraintType.REVERSEUNIQUE.getText().equals(cons.getType())) {
            List<String> attrs = cons.getAttributes();
            if (!(attrs != null && attrs.contains(attrName))) {
                continue;
            }
            removedConstrainList.add(cons);
            Constraint newCons = new Constraint(true);
            newCons.setType(cons.getType());
            newCons.setName(cons.getName());
            cons.replaceAttribute(attrName, editAttr.getName());
            newCons.setAttributes(cons.getAttributes());
            for (String origRule : cons.getRules()) {
                if (cons.getRules().size() == 1) {
                    newCons.addRule(editAttr.getName() + origRule.substring(attrName.length()));
                    break;
                }
                int spaceIndex = origRule.indexOf(" ");
                String attrNameFromRule = origRule.substring(0, spaceIndex);
                if (attrName.equals(attrNameFromRule)) {
                    newCons.addRule(editAttr.getName() + origRule.substring(attrName.length()));
                } else {
                    newCons.addRule(origRule);
                }
            }
            addedConstrainList.add(newCons);
            String key = cons.getDefaultName(newSchemaInfo.getClassname()) + "$" + cons.getName();
            SchemaChangeLog changeLog = new SchemaChangeLog(key, key, SchemeInnerType.TYPE_INDEX);
            schemaChangeMgr.addSchemeChangeLog(changeLog);
        }
    }
    if (removedConstrainList.size() == 0) {
        return false;
    }
    constrainList.removeAll(removedConstrainList);
    constrainList.addAll(addedConstrainList);
    indexTableView.setInput(newSchemaInfo);
    return true;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 37 with Constraint

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

the class TableEditorPart method getNotNullChangedColumn.

/**
	 * Get all not null changed column
	 *
	 * @return
	 */
private List<String[]> getNotNullChangedColumn() {
    // FIXME move this logic to core module
    List<String[]> notNullChangedColumn = new ArrayList<String[]>();
    List<SchemaChangeLog> allAttrChanges = schemaChangeMgr.getAttrChangeLogs();
    List<SchemaInfo> oldSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), oldSchemaInfo);
    if (oldSupers == null) {
        return notNullChangedColumn;
    }
    List<SchemaInfo> newSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo);
    if (newSupers == null) {
        return notNullChangedColumn;
    }
    for (SchemaChangeLog changeLog : allAttrChanges) {
        if (changeLog.getOldValue() == null || changeLog.getNewValue() == null) {
            continue;
        }
        boolean isClassAttr = changeLog.getType() == SchemeInnerType.TYPE_CLASSATTRIBUTE;
        DBAttribute oldAttr = oldSchemaInfo.getDBAttributeByName(changeLog.getOldValue(), isClassAttr);
        DBAttribute newAttr = newSchemaInfo.getDBAttributeByName(changeLog.getNewValue(), isClassAttr);
        if (oldAttr == null || newAttr == null) {
            continue;
        }
        boolean oldNotNull = oldAttr.isNotNull();
        boolean newNotNull = newAttr.isNotNull();
        Constraint newPK = newSchemaInfo.getPK(newSupers);
        List<String> pkAttributes = newPK == null ? new ArrayList<String>() : newPK.getAttributes();
        if (oldNotNull == newNotNull) {
            continue;
        }
        boolean isChangedByPK = false;
        if (newNotNull) {
            // add a new PK
            if (pkAttributes.contains(newAttr.getName())) {
                isChangedByPK = true;
            }
        } else {
            // drop an old PK
            Constraint oldPK = oldSchemaInfo.getPK(oldSupers);
            if (oldPK != null) {
                List<String> oldPKAttrs = oldPK.getAttributes();
                if (oldPKAttrs != null && oldPKAttrs.contains(newAttr.getName())) {
                    isChangedByPK = true;
                }
            }
        }
        if (!isChangedByPK) {
            String[] newColumn = new String[] { newAttr.getName(), String.valueOf(newNotNull) };
            notNullChangedColumn.add(newColumn);
        }
    }
    return notNullChangedColumn;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 38 with Constraint

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

the class ERTable method buildColumns.

/**
	 * Build the columns in the schemaInfo to {@code ERTable}, and add to the er
	 * table.
	 *
	 * @param schemaInfo
	 */
public void buildColumns(SchemaInfo schemaInfo) {
    List<DBAttribute> attributesList = schemaInfo.getAttributes();
    Constraint pk = schemaInfo.getPK();
    for (DBAttribute attribute : attributesList) {
        if (attribute == null || attribute.getInherit() == null) {
            continue;
        }
        boolean isPK = pk == null ? false : pk.contains(attribute.getName(), false);
        ERTableColumn column = new ERTableColumn(this, attribute, isPK);
        this.addColumn(column);
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

Example 39 with Constraint

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

the class ERTableColumn method setName.

public void setName(String newName) {
    if (StringUtil.isEqual(name, newName)) {
        return;
    }
    attr.setName(newName);
    SchemaInfo schemaInfo = this.getTable().getSchemaInfo();
    Constraint pk = schemaInfo.getPK();
    if (pk != null && pk.contains(name, false)) {
        pk.replaceAttribute(name, newName);
    }
    schemaInfo.updateAttrNameInIndex(name, newName);
    name = newName;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 40 with Constraint

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

the class CubridTableParser method getReferredColumns.

/**
	 * Get the other table columns name which is referred on the foreign key
	 * constraint
	 * 
	 * @param schemaInfo
	 * @param fkConstaint
	 * @return The results is all columns of pk in the primary table actually.
	 * @throws Exception
	 */
private List<String> getReferredColumns(SchemaInfo schemaInfo, Constraint fkConstaint) throws Exception {
    List<String> resultList = new ArrayList<String>();
    String refTable = getReferencedTableName(fkConstaint);
    SchemaInfo referedSchemaInfo = getReferencedTable(refTable);
    if (referedSchemaInfo != null) {
        Constraint pkConstaint = referedSchemaInfo.getPK();
        if (pkConstaint == null) {
            throw new ERException(Messages.bind(Messages.errFKColumnMatch, new String[] { schemaInfo.getClassname(), fkConstaint.getName() }));
        }
        List<String> pklist = pkConstaint.getAttributes();
        for (int i = 0; i < pklist.size(); i++) {
            String referedKey = pklist.get(i).replace(" ASC", "").replace(" DESC", "");
            resultList.add(referedKey);
        }
    }
    return resultList;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ArrayList(java.util.ArrayList) ERException(com.cubrid.common.ui.er.ERException) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) ERWinSchemaInfo(com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)

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