Search in sources :

Example 51 with Constraint

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

the class TableSchemaCompareUpdateDDL method setFKAlterDDL.

/**
	 * Compare table FK constraints
	 */
public void setFKAlterDDL() {
    // FIXME logic code move to core module
    List<Constraint> sourceFKConstraints = sourceTableSchema.getFKConstraints();
    List<Constraint> targetFKConstraints = targetTableSchema.getFKConstraints();
    for (Constraint targetFk : targetFKConstraints) {
        String targetFkName = targetFk.getName();
        Constraint sourceFk = findConstraint(sourceTableSchema, targetFkName);
        if (sourceFk != null) {
            if (!targetFk.equals(sourceFk)) {
                changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceFk.getName(), targetFk.getName(), SchemeInnerType.TYPE_FK));
            }
        } else {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(null, targetFk.getName(), SchemeInnerType.TYPE_FK));
        }
    }
    for (Constraint sourceFk : sourceFKConstraints) {
        String sourceFkName = sourceFk.getName();
        Constraint targetFk = findConstraint(targetTableSchema, sourceFkName);
        if (targetFk == null) {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceFk.getName(), null, SchemeInnerType.TYPE_FK));
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 52 with Constraint

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

the class TableSchemaCompareUpdateDDL method setIndexAlterDDL.

/**
	 * Compare table index constraints
	 */
public void setIndexAlterDDL() {
    // FIXME logic code move to core module
    List<Constraint> sourceDBConstraints = sourceTableSchema.getConstraints();
    List<Constraint> targetDBConstraints = targetTableSchema.getConstraints();
    for (Constraint targetCons : targetDBConstraints) {
        String targetConsName = targetCons.getName().toLowerCase();
        Constraint sourceCons = findConstraint(sourceTableSchema, targetConsName);
        if (targetCons.getType().equals(Constraint.ConstraintType.INDEX.getText()) || targetCons.getType().equals(Constraint.ConstraintType.UNIQUE.getText())) {
            if (sourceCons != null) {
                if (!targetCons.equals(sourceCons)) {
                    changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceCons.getDefaultName(sourceTableSchema.getClassname()) + "$" + sourceCons.getName(), targetCons.getDefaultName(targetTableSchema.getClassname()) + "$" + targetCons.getName(), SchemeInnerType.TYPE_INDEX));
                }
            } else {
                changeManager.addSchemeChangeLog(new SchemaChangeLog(null, targetCons.getDefaultName(targetTableSchema.getClassname()) + "$" + targetCons.getName(), SchemeInnerType.TYPE_INDEX));
            }
        }
    }
    for (Constraint sourceCons : sourceDBConstraints) {
        String sourceConsName = sourceCons.getName();
        Constraint t_cons = findConstraint(targetTableSchema, sourceConsName);
        if (t_cons == null && (sourceCons.getType().equals(Constraint.ConstraintType.INDEX.getText()) || sourceCons.getType().equals(Constraint.ConstraintType.UNIQUE.getText()))) {
            changeManager.addSchemeChangeLog(new SchemaChangeLog(sourceCons.getDefaultName(sourceTableSchema.getClassname()) + "$" + sourceCons.getName(), null, SchemeInnerType.TYPE_INDEX));
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 53 with Constraint

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

the class ERXmlContainer method parseKeyGroupGroups.

/**
	 * parse &lt;Key_Group&gt; node
	 *
	 * @param schemaInfo
	 * @param schema
	 * @param property
	 */
private List<Constraint> parseKeyGroupGroups() {
    NodeList nodeList = doc.getElementsByTagName("Key_Group");
    // create constraint with name key
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node keyGroup = nodeList.item(i);
        // skip the default Keys without any column definition
        NodeList columns = handler.getChildNodeList(keyGroup, "Key_Group_Member_Groups");
        if (columns == null || columns.getLength() == 0) {
            continue;
        }
        Constraint constraint = new Constraint(false);
        if (keyGroup.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        String name = keyGroup.getAttributes().getNamedItem("Name").getNodeValue().trim();
        String id = keyGroup.getAttributes().getNamedItem("id").getNodeValue().trim();
        nodeMap.put(id, keyGroup);
        String physicalName = handler.getChildValueByProperty(keyGroup, "Key_GroupProps.Physical_Name");
        String keyGroupType = handler.getChildValueByProperty(keyGroup, "Key_GroupProps.Key_Group_Type");
        String relationShipId = handler.getChildValueByProperty(keyGroup, "Key_GroupProps.Key_Group_Relationship_Pointer");
        physicalNameMap.put(id, physicalName);
        // get the schemaInfo instance to set the constraint
        Node pNode = findPNode(keyGroup, "Entity");
        String pId = pNode.getAttributes().getNamedItem("id").getNodeValue().trim();
        nodeMap.put(pId, pNode);
        String tableName = physicalNameMap.get(pId);
        if (tableName == null) {
            tableName = pNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
        }
        if (physicalName == null) {
            physicalName = "I" + keyGroupType + "_" + tableName;
            int count = getNameCount(physicalName);
            physicalName = physicalName + "_" + count;
        }
        constraint.setName((physicalName != null) ? physicalName : name);
        SchemaInfo schemaInfo = schemaInfos.get(tableName);
        schemaInfo.addConstraint(constraint);
        if (keyGroupType != null && keyGroupType.length() > 0) {
            if (keyGroupType.equals("PK")) {
                constraint.setType(Constraint.ConstraintType.PRIMARYKEY.getText());
            } else if (keyGroupType.indexOf("IF") != -1) {
                constraint.setType(ConstraintType.FOREIGNKEY.getText());
            } else if (keyGroupType.indexOf("AK") != -1) {
                constraint.setType(Constraint.ConstraintType.UNIQUE.getText());
            } else if (keyGroupType.indexOf("IE") != -1) {
                constraint.setType(Constraint.ConstraintType.INDEX.getText());
            }
        }
        constraints.put(id, constraint);
        if (relationShipId != null) {
            relationConstrantMap.put(relationShipId, constraint);
        }
    }
    // add attributes of each constraint
    final NodeList keyGroupMememberColumns = doc.getElementsByTagName("Key_Group_Member_Column");
    List<Node> nodes = new ArrayList<Node>();
    for (int i = 0; i < keyGroupMememberColumns.getLength(); i++) {
        nodes.add(keyGroupMememberColumns.item(i));
    }
    if (keyGroupMememberColumns.getLength() > MAXCOUNT_LIMIT) {
        int colPerTask = keyGroupMememberColumns.getLength() / Runtime.getRuntime().availableProcessors();
        int taskcount = keyGroupMememberColumns.getLength() / colPerTask;
        int lastOffset = keyGroupMememberColumns.getLength() % colPerTask;
        if (lastOffset > 0) {
            taskcount++;
        }
        CountDownLatch latch = new CountDownLatch(taskcount);
        Executor threadPool = Executors.newCachedThreadPool();
        ParseKeyGroupColumnTask task = null;
        for (int i = 0; i < taskcount; i++) {
            List<Node> copy = new ArrayList<Node>();
            Collections.copy(nodes, copy);
            if (i == taskcount - 1) {
                task = new ParseKeyGroupColumnTask(copy, i * colPerTask, lastOffset);
            } else {
                task = new ParseKeyGroupColumnTask(copy, i * colPerTask, colPerTask);
            }
            task.setLatch(latch);
            threadPool.execute(task);
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
            return null;
        }
    } else {
        parseKeyGroupColumn(nodes, 0, keyGroupMememberColumns.getLength());
    }
    return new ArrayList<Constraint>(constraints.values());
}
Also used : Executor(java.util.concurrent.Executor) Constraint(com.cubrid.common.core.common.model.Constraint) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) 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)

Example 54 with Constraint

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

the class ERXmlContainer method setRelationType.

private void setRelationType(String tagName, boolean update) {
    NodeList relUpdates = doc.getElementsByTagName(tagName);
    if (relUpdates != null) {
        for (int i = 0; i < relUpdates.getLength(); i++) {
            Node relUpdate = relUpdates.item(i);
            int value = 0;
            if (relUpdate.getFirstChild().getNodeValue().trim() != null) {
                value = Integer.parseInt(relUpdate.getFirstChild().getNodeValue().trim());
            }
            Node pNode = findPNode(relUpdate, "Relationship");
            if (pNode == null) {
                continue;
            }
            String id = pNode.getAttributes().getNamedItem("id").getNodeValue().trim();
            Constraint constraint = foreignKeyMap.get(id);
            if (constraint == null) {
                continue;
            }
            if (update) {
                constraint.addRule("ON UPDATE " + text(value));
            } else {
                constraint.addRule("ON DELETE " + text(value));
            }
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Constraint(com.cubrid.common.core.common.model.Constraint)

Example 55 with Constraint

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

the class ERXmlContainer method parseRelationGroups.

private void parseRelationGroups() {
    NodeList relations = doc.getElementsByTagName("Relationship");
    if (relations == null || relations.getLength() == 0) {
        return;
    }
    for (int i = 0; i < relations.getLength(); i++) {
        Node node = relations.item(i);
        String id = node.getAttributes().getNamedItem("id").getNodeValue().trim();
        Constraint constraint = null;
        if (relationConstrantMap.containsKey(id)) {
            constraint = relationConstrantMap.get(id);
        }
        if (constraint == null) {
            continue;
        }
        String type = handler.getChildValueByProperty(node, "RelationshipProps.Type");
        String parentId = handler.getChildValueByProperty(node, "RelationshipProps.Relationship_Parent_Entity");
        Node pnode = handler.getNodeById("Entity", parentId);
        if (pnode == null) {
            continue;
        }
        String pName = pnode.getAttributes().getNamedItem("Name").getNodeValue().trim();
        String pId = pnode.getAttributes().getNamedItem("id").getNodeValue().trim();
        String pPhysicalName = physicalNameMap.get(pId);
        String childId = handler.getChildValueByProperty(node, "RelationshipProps.Relationship_Child_Entity");
        Node childNode = handler.getNodeById("Entity", childId);
        if (childNode == null) {
            continue;
        }
        String childName = childNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
        String childPhysicalName = physicalNameMap.get(childId);
        if (type != null && type.equals(ERXmlModelConstant.REL_VIEW)) {
            String physicalName = handler.getChildValueByProperty(node, "RelationshipProps.Physical_Name");
            ViewModel model = viewModelMap.get((childPhysicalName != null) ? childPhysicalName : childName);
            if (model == null) {
                model = new ViewModel();
                viewModelMap.put(childName, model);
            }
            model.addTableAlias((pPhysicalName != null) ? pPhysicalName : pName, (physicalName == null) ? (pPhysicalName != null) ? pPhysicalName : pName : physicalName);
            continue;
        }
        if (constraint.getType() != null)
            constraint.setType(ConstraintType.FOREIGNKEY.getText());
        // XML ID -> Constraint(FK)
        foreignKeyMap.put(id, constraint);
        Node tableNode = handler.getNodeById("Entity", parentId);
        if (tableNode == null) {
            continue;
        }
        String tableId = tableNode.getAttributes().getNamedItem("id").getNodeValue().trim();
        String tableName = physicalNameMap.get(tableId);
        if (tableName == null) {
            tableName = tableNode.getAttributes().getNamedItem("Name").getNodeValue().trim();
        }
        constraint.addRule("REFERENCES " + tableName);
    }
    setRelationType("Relationship_Parent_Update_Rule", true);
    setRelationType("Relationship_Parent_Delete_Rule", false);
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) 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