Search in sources :

Example 31 with Constraint

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

the class AddFKDialog method getPKTableData.

/**
	 * Get PK Table Data
	 * 
	 */
private void getPKTableData() {
    pkForeignTable.removeAll();
    if (oldCombo != null) {
        oldCombo.dispose();
    }
    String refTable = foreignTableCombo.getText();
    refSchema = getSchemaInfo(refTable);
    if (refSchema == null) {
        return;
    }
    List<SchemaInfo> supers = getRefedSupper(refSchema);
    Constraint pk = refSchema.getPK(supers);
    if (pk != null) {
        List<String> pkAttrs = pk.getAttributes();
        for (String attr : pkAttrs) {
            DBAttribute da = (DBAttribute) refSchema.getDBAttributeByName(attr, false);
            if (da == null) {
                continue;
            }
            TableItem item = new TableItem(pkForeignTable, SWT.NONE);
            item.setText(0, da.getName());
            item.setText(1, DataType.getShownType(da.getType()));
        }
    }
    if (fkTable.getItemCount() > 0) {
        TableItem[] items = fkTable.getItems();
        for (int i = 0, n = items.length; i < n; i++) {
            //$NON-NLS-1$
            items[i].setText(fkTableColCount - 1, "");
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) TableItem(org.eclipse.swt.widgets.TableItem) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 32 with Constraint

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

the class AddIndexDialog method buttonPressed.

/**
	 * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
	 * @param buttonId the id of the button that was pressed (see
	 *        <code>IDialogConstants.*_ID</code> constants)
	 */
protected void buttonPressed(int buttonId) {
    if (buttonId == IDialogConstants.OK_ID) {
        errorMsg = null;
        setErrorMessage(null);
        int count = columnTable.getItemCount();
        indexConstraint = new Constraint(true);
        String indexType = getSelectedIndexType();
        indexConstraint.setType(indexType);
        int indexColumnCount = 0;
        boolean isHasPrefixLength = false;
        for (int i = 0; i < count; i++) {
            TableItem item = columnTable.getItem(i);
            if (!item.getChecked()) {
                continue;
            }
            indexColumnCount++;
            indexConstraint.addAttribute(item.getText(1));
            String indexPrefixLength = getPrefixLength(item);
            if (errorMsg != null) {
                setErrorMessage(errorMsg);
                return;
            }
            if (indexPrefixLength.length() > 0) {
                isHasPrefixLength = true;
            }
            indexConstraint.addRule(item.getText(1) + indexPrefixLength + " " + item.getText(3));
        }
        if (indexConstraint.getAttributes().size() == 0) {
            setErrorMessage(Messages.errSelectMoreColumns);
            return;
        }
        if (indexColumnCount > 1 && isHasPrefixLength) {
            setErrorMessage(Messages.errMultColIndexPrefixLength);
            return;
        }
        String indexName = indexNameText.getText().trim();
        String tableName = schemaInfo.getClassname();
        if (("").equals(indexName)) {
            //$NON-NLS-1$
            indexName = indexConstraint.getDefaultName(tableName);
        }
        indexConstraint.setName(indexName);
        if (isCommentSupport) {
            String description = indexDescriptionText.getText().trim();
            indexConstraint.setDescription(description);
        }
        List<Constraint> constraintList = new ArrayList<Constraint>();
        constraintList.addAll(schemaInfo.getConstraints());
        if (editedIndex != null) {
            constraintList.remove(editedIndex);
        }
        /*For bug TOOLS-2394 Unique index can't be added again*/
        if (editedIndex == null && CUB_UNIQUE.equals(indexConstraint.getType()) && indexConstraint.getAttributes().size() == 1 && schemaInfo.getUniqueByAttrName(indexConstraint.getAttributes().get(0)) != null) {
            setErrorMessage(Messages.errExistUniqueSameRule);
            return;
        }
        for (Constraint constraint : constraintList) {
            if (CUB_INDEX.equals(constraint.getType()) && constraint.getType().equals(indexType)) {
                List<String> rules = constraint.getRules();
                if (rules.equals(indexConstraint.getRules())) {
                    setErrorMessage(Messages.errExistIndex);
                    return;
                }
            } else if (CUB_RINDEX.equals(constraint.getType()) && constraint.getType().equals(indexType)) {
                List<String> attrs = constraint.getAttributes();
                if (attrs.equals(indexConstraint.getAttributes())) {
                    setErrorMessage(Messages.errExistReverseIndex);
                    return;
                }
            } else if (CUB_UNIQUE.equals(constraint.getType()) && constraint.getType().equals(indexType)) {
                if (constraint.getName().equals(indexConstraint.getName())) {
                    setErrorMessage(Messages.bind(Messages.errExistUniqueIndex, constraint.getName()));
                    return;
                }
            } else if (CUB_RUNIQUE.equals(constraint.getType()) && constraint.getType().equals(indexType) && constraint.getName().equals(indexConstraint.getName())) {
                setErrorMessage(Messages.errExistReverseUniqueIndex);
                return;
            }
        }
    }
    super.buttonPressed(buttonId);
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Constraint(com.cubrid.common.core.common.model.Constraint) Point(org.eclipse.swt.graphics.Point)

Example 33 with Constraint

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

the class AttributeCellModifier method modify.

public void modify(Object element, String property, Object value) {
    // FIXME move this logic to core module
    final TableItem item = (TableItem) element;
    if (item == null) {
        return;
    }
    DBAttribute attr = (DBAttribute) item.getData();
    String attrName = attr.getName();
    DBAttribute oldAttribute = null;
    if (editor.getOldSchemaInfo() != null) {
        oldAttribute = editor.getOldSchemaInfo().getDBAttributeByName(attrName, false);
    }
    if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
        SchemaInfo schemaInfo = editor.getNewSchemaInfo();
        if (schemaInfo == null) {
            return;
        }
        boolean on = ((Boolean) value).booleanValue();
        if (on) {
            Constraint constraint = schemaInfo.getPK();
            if (constraint == null) {
                constraint = new Constraint("pk", Constraint.ConstraintType.PRIMARYKEY.getText());
                schemaInfo.addConstraint(constraint);
            }
            constraint.addAttribute(attr.getName());
        } else {
            Constraint constraint = schemaInfo.getPK();
            if (constraint == null) {
                return;
            }
            List<String> columns = constraint.getAttributes();
            if (columns == null || columns.size() == 0) {
                return;
            }
            boolean isContain = columns.remove(attr.getName());
            /*For bug TOOLS-3972 The collumn's setting in Edit Table Inconsistent with the setting in Set Primary Key*/
            if (isContain && columns.size() == 0) {
                schemaInfo.removeConstraintByName(constraint.getName(), constraint.getType());
            }
            /*For bug TOOLS-3046 : deal with edit column*/
            if (oldAttribute != null && isContain) {
                attr.setNotNull(false);
                editor.changeForEditElement(attrName, attr, oldAttribute);
            }
        }
        editor.makeChangeLogForIndex(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
        String newName = (String) value;
        SchemaInfo schemaInfo = editor.getNewSchemaInfo();
        if (schemaInfo == null) {
            // TODO Improve error message
            CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
            return;
        }
        if (!StringUtil.isEmpty(newName) && !ValidateUtil.isValidIdentifier(newName)) {
            CommonUITool.openErrorBox(Messages.errColumnName);
            return;
        }
        List<DBAttribute> lastAttrs = schemaInfo.getAttributes();
        if (StringUtil.isEmpty(newName) && lastAttrs.contains(attr)) {
            CommonUITool.openErrorBox(Messages.errEmptyNameOnEditTableColumn);
            return;
        }
        for (DBAttribute lastAttr : lastAttrs) {
            if (StringUtil.isEqualIgnoreCase(lastAttr.getName(), newName) && attr != lastAttr) {
                CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
                return;
            }
        }
        if (!StringUtil.isEqualIgnoreCase(attr.getName(), newName)) {
            replaceNewConstraintAttributeName(schemaInfo, attr.getName(), newName);
            DBAttribute newAttribute = attr.clone();
            if (newAttribute != null) {
                newAttribute.setName(newName);
            }
            if (attr != null) {
                attr.setName(newName);
            }
            if (!hasAddedToSchemaInfo(attr)) {
                attr.setName(newName);
                if (!StringUtil.isEmpty(newName)) {
                    if (!lastAttrs.contains(newAttribute)) {
                        newAttribute.setInherit(editor.getNewSchemaInfo().getClassname());
                        schemaInfo.addAttribute(newAttribute);
                        editor.removeElementByName(newName);
                    }
                    editor.addNewAttrLog(newName, newAttribute.isClassAttribute());
                    if (!StringUtil.isEmpty(newAttribute.getName())) {
                        editor.makeChangeLogForIndex(attrName, newAttribute, oldAttribute);
                    }
                }
            } else {
                editor.changeForEditElement(attrName, newAttribute, oldAttribute);
            }
        }
        DBAttribute lastDBAttribute = null;
        if (lastAttrs.size() > 0) {
            Table columnsTable = editor.getColumnsTable();
            lastDBAttribute = (DBAttribute) columnsTable.getItem(columnsTable.getItemCount() - 1).getData();
        }
        if (!StringUtil.isEmpty(newName) && (lastDBAttribute == null || !StringUtil.isEmpty(lastDBAttribute.getName()))) {
            editor.addNewColumn();
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
        String dataTypeRaw = (String) value;
        if (dataTypeRaw != null && dataTypeRaw.trim().toLowerCase().startsWith("enum")) {
            int sp = dataTypeRaw.indexOf("(");
            if (sp != -1) {
                String dataType = dataTypeRaw.substring(0, sp).toLowerCase().trim();
                attr.setType(dataType);
                String enumeration = dataTypeRaw.substring(sp).trim();
                attr.setEnumeration(enumeration);
            }
        } else {
            attr.setType(dataTypeRaw);
        }
        if (!DataType.isIntegerType(attr.getType())) {
            attr.setAutoIncrement(null);
        }
        if (!DataType.canUseCollation(attr.getType())) {
            attr.setCollation("");
        }
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
        String defaultVal = (String) value;
        boolean isStringType = DataType.isStringType(attr.getType());
        boolean isEmpty = StringUtil.isEmpty(defaultVal);
        boolean isNull = false;
        if (defaultVal == null || DataType.NULL_EXPORT_FORMAT.equals(defaultVal) || DataType.VALUE_NULL.equals(defaultVal) || (isEmpty && !isStringType)) {
            isNull = true;
        }
        if (isNull) {
            attr.setDefault(null);
        } else {
            if (attr.getAutoIncrement() != null) {
                attr.setDefault(null);
                CommonUITool.openErrorBox(Messages.errCanNotSetDefaultOnAI);
                return;
            }
            boolean isConfirmReset = "".equals(defaultVal) && oldAttribute != null && !"".equals(oldAttribute.getDefault());
            if (isConfirmReset) {
                String confirmResetDef = Messages.confirmResetDef;
                if (CommonUITool.openConfirmBox(confirmResetDef)) {
                    attr.setDefault(null);
                } else {
                    attr.setDefault(defaultVal);
                }
            } else {
                attr.setDefault(defaultVal);
            }
        }
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
        DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
        if (aiAttr != null && aiAttr != attr) {
            attr.setAutoIncrement(null);
            return;
        }
        String param = (String) value;
        if (StringUtil.isNotEmpty(param)) {
            if (!param.matches("\\s*[0-9]+\\s*,\\s*[0-9]+\\s*")) {
                CommonUITool.openErrorBox(Messages.errInvalidAutoIncrForm);
                return;
            }
            String defaultValue = attr.getDefault();
            if (StringUtil.isNotEmpty(defaultValue)) {
                CommonUITool.openErrorBox(Messages.errCanNotSetAIOnDefault);
                return;
            }
            String[] params = param.split(",");
            String startVal = params[0].trim();
            String incrVal = params[1].trim();
            SchemaInfo schemaInfo = editor.getNewSchemaInfo();
            SerialInfo serial = new SerialInfo();
            serial.setOwner(schemaInfo.getOwner());
            serial.setClassName(schemaInfo.getClassname());
            serial.setAttName(attrName);
            serial.setCacheCount("1");
            serial.setCurrentValue(startVal);
            serial.setCyclic(false);
            serial.setIncrementValue(incrVal);
            serial.setMaxValue(String.valueOf(Integer.MAX_VALUE));
            serial.setMinValue(startVal);
            serial.setStartedValue(startVal);
            if (attr.getAutoIncrement() != null && schemaInfo != null && schemaInfo.getAutoIncrementColumn() != null && schemaInfo.getAutoIncrementColumn().getAutoIncrement() != null) {
                String oldAI = attr.getAutoIncrement().getTableAutoIncrementString();
                String newAI = serial.getTableAutoIncrementString();
                if (StringUtil.isEqual(oldAI, newAI)) {
                    return;
                }
            }
            attr.setAutoIncrement(serial);
        } else {
            attr.setAutoIncrement(null);
        }
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
        boolean on = ((Boolean) value).booleanValue();
        attr.setNotNull(on);
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
        boolean on = ((Boolean) value).booleanValue();
        if (on && attr.isShared()) {
            CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
            return;
        }
        attr.setUnique(on);
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
        boolean on = ((Boolean) value).booleanValue();
        String defaultValue = attr.getDefault();
        if (on && StringUtil.isEmpty(defaultValue)) {
            CommonUITool.openErrorBox(Messages.msgInputSharedValue);
            return;
        }
        if (on && attr.isUnique()) {
            CommonUITool.openErrorBox(Messages.errCanNotUseUkAndSharedOnce);
            return;
        }
        attr.setShared(on);
        editor.changeForEditElement(attrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
        String orignCollation = attr.getCollation();
        Integer selection = StringUtil.intValue(value.toString(), 0);
        if (selection > -1) {
            String newCollation = editor.getCollationArray()[selection];
            if (!StringUtil.isEqualNotIgnoreNull(orignCollation, newCollation)) {
                attr.setCollation(newCollation);
            }
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
        attr.setDescription((String) value);
    }
    editor.loadColumnData();
}
Also used : Table(org.eclipse.swt.widgets.Table) Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) List(java.util.List) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 34 with Constraint

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

the class AttributeCellModifier method getValue.

public Object getValue(Object element, String property) {
    // FIXME move this logic to core module
    DBAttribute attr = (DBAttribute) element;
    if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
        SchemaInfo schemaInfo = editor.getNewSchemaInfo();
        if (schemaInfo == null) {
            return false;
        }
        Constraint constraint = schemaInfo.getPK();
        if (constraint == null) {
            return false;
        }
        List<String> columns = constraint.getAttributes();
        if (columns == null || columns.size() == 0) {
            return false;
        }
        return columns.contains(attr.getName());
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NAME)) {
        return attr.getName();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
        String dataType = attr.getType();
        if (dataType == null) {
            return "";
        }
        String physicalType = getShownPhysicalType(attr);
        return physicalType;
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DEFAULT)) {
        String defaultValue = attr.getDefault();
        if (defaultValue == null || attr.getAutoIncrement() != null || (StringUtil.isEmpty(defaultValue) && !DataType.isStringType(attr.getType()))) {
            return DataType.NULL_EXPORT_FORMAT;
        } else {
            return defaultValue;
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_AUTO_INCREMENT)) {
        DBAttribute aiAttr = editor.getNewSchemaInfo().getAutoIncrementColumn();
        if (aiAttr != null && aiAttr != attr) {
            CommonUITool.openErrorBox(Messages.errCanNotAddAutoincrementAlreadyExists);
            return "";
        }
        SerialInfo serial = attr.getAutoIncrement();
        if (serial == null) {
            return "";
        }
        String defaultValue = attr.getDefault();
        if (StringUtil.isNotEmpty(defaultValue)) {
            return "";
        }
        return serial.getTableAutoIncrementString();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
        return attr.isNotNull();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
        return attr.isUnique();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
        return attr.isShared();
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_COLLATION)) {
        if (DataType.canUseCollation(attr.getType())) {
            String collation = attr.getCollation();
            return editor.getCollationIndex(collation);
        }
        return "";
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_MEMO)) {
        return attr.getDescription();
    }
    return null;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 35 with Constraint

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

the class AttributeLabelProvider method getColumnImage.

public Image getColumnImage(Object element, int columnIndex) {
    if (element == null) {
        return null;
    }
    DBAttribute dbAttribute = (DBAttribute) element;
    if (dbAttribute == null || dbAttribute.getInherit() == null || schema == null) {
        return null;
    }
    String property = editorAdaptor.getColumnProperty(columnIndex);
    if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
        String attrName = dbAttribute.getName();
        Constraint pk = schema.getPK(supers);
        if (null != pk && pk.getAttributes().contains(attrName)) {
            return PK_IMAGE;
        }
        return editableMode ? UNCHECK_IMAGE : null;
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
        String attrName = dbAttribute.getName();
        Constraint pk = schema.getPK(supers);
        if (null != pk && pk.getAttributes().contains(attrName)) {
            return DISABLED_CHECK_IMAGE;
        }
        if (dbAttribute.isNotNull()) {
            return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
        } else {
            return editableMode ? UNCHECK_IMAGE : null;
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_UK)) {
        String attrName = dbAttribute.getName();
        Constraint pk = schema.getPK(supers);
        if (null != pk && pk.getAttributes().contains(attrName)) {
            return DISABLED_CHECK_IMAGE;
        }
        if (dbAttribute.isUnique() && schema.isAttributeUnique(dbAttribute, supers)) {
            return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
        } else {
            return editableMode ? UNCHECK_IMAGE : DISABLED_UNCHECK_IMAGE;
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_SHARED)) {
        if (dbAttribute.isShared()) {
            return editableMode ? CHECK_IMAGE : DISABLED_CHECK_IMAGE;
        } else {
            return editableMode ? UNCHECK_IMAGE : null;
        }
    }
    return null;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute)

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