Search in sources :

Example 26 with ERTableColumn

use of com.cubrid.common.ui.er.model.ERTableColumn in project cubrid-manager by CUBRID.

the class EditVirtualTableDialog method syncLogicalNameAndPhysicalComment.

/**
	 * If the logical name is not the same with physical comment, then
	 * synchronize the logical name with physical comment on table and columns.
	 * 
	 * @return void
	 */
private void syncLogicalNameAndPhysicalComment() {
    boolean isPhysical = erSchema.isPhysicModel();
    SchemaInfo newSchemaInfo = getNewSchemaInfo();
    if (isPhysical) {
        //sync table desc to table logical name
        String newTableDesc = newSchemaInfo.getDescription();
        String oldTableDesc = oldSchemaInfo.getDescription();
        oldSchemaInfo.setDescription(newTableDesc);
        if (StringUtil.isEmpty(newTableDesc)) {
            oldERTable.setName(newSchemaInfo.getClassname(), false);
        }
        if (StringUtil.isNotEmpty(newTableDesc) && !StringUtil.isEqual(newTableDesc, oldTableDesc)) {
            oldERTable.setName(newTableDesc, false);
        }
        //sync column desc to column logical name
        List<DBAttribute> attrs = newSchemaInfo.getAttributes();
        for (DBAttribute attr : attrs) {
            String newColumnDesc = attr.getDescription();
            ERTableColumn column = oldERTable.getColumn(attr.getName());
            column.getAttr().setDescription(newColumnDesc);
            if (StringUtil.isEmpty(newColumnDesc)) {
                //set physical name to logical name
                column.setName(attr.getName(), false);
            } else {
                column.setName(newColumnDesc, false);
            }
        }
    } else {
        //sync table logical name to table desc
        String logicalName = oldERTable.getLogicalName();
        if (StringUtil.isEqual(logicalName, oldERTable.getName())) {
            oldERTable.setDescription("");
        } else {
            oldERTable.setDescription(logicalName);
        }
        //sync column logical name to column desc
        List<ERTableColumn> columns = oldERTable.getColumns();
        for (ERTableColumn column : columns) {
            String logicalColName = column.getName(false);
            String physicalColName = column.getName(true);
            if (StringUtil.isEqual(logicalColName, physicalColName)) {
                column.setDescription("");
            } else {
                column.setDescription(logicalColName);
            }
        }
    }
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 27 with ERTableColumn

use of com.cubrid.common.ui.er.model.ERTableColumn in project cubrid-manager by CUBRID.

the class EditVirtualTableDialog method okPressed.

protected void okPressed() {
    if (!checkValid()) {
        return;
    }
    String message = (oldSchemaInfo == null) ? Messages.msgCreateTableConfirm : Messages.msgAlterTableConfirm;
    if (!CommonUITool.openConfirmBox(message)) {
        return;
    }
    SchemaInfo newSchemaInfo = getNewSchemaInfo();
    newSchemaInfo.removeInvalidPKAndIndex(true);
    newERTable.setName(tableNameText.getText().trim(), isPhysical);
    if (erSchema.isPhysicModel()) {
        newSchemaInfo.setDescription(tableDescText.getText().trim());
    }
    //remove empty column
    List<ERTableColumn> columns = newERTable.getColumns();
    for (int i = columns.size() - 1; i >= 0; i--) {
        if (StringUtil.isEmpty(columns.get(i).getName())) {
            columns.remove(i);
        }
    }
    //check
    ERSchema tmpErSchema = new ERSchema(erSchema.getName() + "_tmp", erSchema.getInput());
    Map<String, SchemaInfo> schemaInfos = erSchema.getAllSchemaInfo();
    schemaInfos.put(newSchemaInfo.getClassname(), newSchemaInfo);
    CubridTableParser tableParser = new CubridTableParser(tmpErSchema);
    tableParser.buildERTables(schemaInfos.values(), -1, -1, false);
    Map<String, Exception> failedTables = tableParser.getFailedTables();
    Map<String, List<Constraint>> removedFKs = tableParser.getRemovedFKConstraints();
    if (failedTables.size() > 0) {
        Set<String> tables = failedTables.keySet();
        String tableName = tables.iterator().next();
        CommonUITool.openErrorBox(failedTables.get(tableName).getMessage());
        return;
    }
    if (removedFKs.size() > 0) {
        Set<String> tables = removedFKs.keySet();
        String tableName = tables.iterator().next();
        List<Constraint> constraints = removedFKs.get(tableName);
        CommonUITool.openErrorBox("Foreign relation is error. Please check the relation of " + constraints.get(0).getName() + ", in table of " + tableName);
        return;
    }
    try {
        if (isTableNameChanged() && erSchema.isContainsTable(newERTable.getName(isPhysical), isPhysical)) {
            throw new Exception(Messages.bind(Messages.errExistTable, newERTable.getName(isPhysical)));
        }
        newERTable.checkValidate();
    } catch (Exception e) {
        CommonUITool.openErrorBox(e.getMessage());
        return;
    }
    super.okPressed();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) CubridTableParser(com.cubrid.common.ui.er.model.CubridTableParser) Constraint(com.cubrid.common.core.common.model.Constraint) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) ERSchema(com.cubrid.common.ui.er.model.ERSchema) ArrayList(java.util.ArrayList) List(java.util.List) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 28 with ERTableColumn

use of com.cubrid.common.ui.er.model.ERTableColumn in project cubrid-manager by CUBRID.

the class ERAttributeCellModifier method getValue.

public Object getValue(Object element, String property) {
    // FIXME move this logic to core module
    ERTableColumn erColumn = (ERTableColumn) element;
    DBAttribute attr = erColumn.getAttr();
    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 erColumn.getName(isPhysical);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
        if (isPhysical) {
            String dataType = attr.getType();
            if (dataType.trim().toLowerCase().startsWith("enum")) {
                return DataType.getShownType(attr.getType()) + attr.getEnumeration();
            }
            return DataType.getShownType(attr.getType());
        } else {
            return erColumn.getShowType(false);
        }
    } 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) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 29 with ERTableColumn

use of com.cubrid.common.ui.er.model.ERTableColumn in project cubrid-manager by CUBRID.

the class ERAttributeCellModifier 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;
    }
    ERTableColumn erColumn = (ERTableColumn) item.getData();
    ERTable table = editor.getDialog().getNewERTable();
    if (!StringUtil.isEmpty(erColumn.getName())) {
        erColumn = table.getColumn(erColumn.getName());
    }
    DBAttribute attr = erColumn.getAttr();
    String oldAttrName = attr.getName();
    DBAttribute oldAttribute = null;
    if (editor.getOldSchemaInfo() != null) {
        oldAttribute = editor.getOldSchemaInfo().getDBAttributeByName(oldAttrName, false);
    }
    ERTableColumn oldColumn = editor.getDialog().getOldERTable().getColumn(oldAttrName);
    if (StringUtil.isEqual(property, IAttributeColumn.COL_PK)) {
        SchemaInfo schemaInfo = editor.getNewSchemaInfo();
        if (schemaInfo == null) {
            return;
        }
        boolean on = ((Boolean) value).booleanValue();
        erColumn.setIsPrimaryKey(on);
        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.makeChangeLogForIndex(oldAttrName, 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) && isPhysical && !ValidateUtil.isValidIdentifier(newName)) {
            CommonUITool.openErrorBox(Messages.errColumnName);
            return;
        }
        List<ERTableColumn> columns = table.getColumns();
        for (ERTableColumn col : columns) {
            if (StringUtil.isEqualIgnoreCase(col.getName(isPhysical), newName) && erColumn != col) {
                CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
                return;
            }
        }
        if (!StringUtil.isEqualIgnoreCase(erColumn.getName(isPhysical), newName)) {
            if (isPhysical) {
                replaceNewConstraintAttributeName(schemaInfo, attr.getName(), newName);
            }
            String oldName = erColumn.getName(isPhysical);
            erColumn.setName(newName, isPhysical);
            if (erColumn.isNew()) {
                erColumn.setName(newName, !isPhysical);
                for (ERTableColumn col : columns) {
                    if (StringUtil.isEqualIgnoreCase(col.getName(!isPhysical), newName) && erColumn != col) {
                        CommonUITool.openErrorBox(Messages.errSameNameOnEditTableColumn);
                        erColumn.setName(oldName, isPhysical);
                        erColumn.setName(oldName, !isPhysical);
                        return;
                    }
                }
            }
            if (!hasAddedToSchemaInfo(attr)) {
                if (!StringUtil.isEmpty(newName)) {
                    if (!columns.contains(erColumn)) {
                        erColumn.getAttr().setInherit(editor.getNewSchemaInfo().getClassname());
                        table.addColumn(erColumn);
                        editor.removeElementByName(newName);
                    }
                    if (!StringUtil.isEmpty(oldAttrName)) {
                        editor.makeChangeLogForIndex(oldAttrName, erColumn.getAttr(), oldAttribute);
                    }
                }
            } else {
                editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
            }
        }
        ERTableColumn col = null;
        if (columns.size() > 0) {
            Table columnsTable = editor.getColumnsTable();
            col = (ERTableColumn) columnsTable.getItem(columnsTable.getItemCount() - 1).getData();
        }
        if (!StringUtil.isEmpty(newName) && (col == null || !StringUtil.isEmpty(col.getName(isPhysical)))) {
            editor.addNewColumn();
        }
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_DATATYPE)) {
        String dataTypeRaw = (String) value;
        ERSchema schema = erColumn.getERSchema();
        if (isPhysical) {
            if (dataTypeRaw.equalsIgnoreCase(DataType.getUpperEnumType())) {
                dataTypeRaw += "('" + DataType.ENUM_DAFAULT_VALUE + "')";
            }
            erColumn.setPhysicalDataType(DataType.reviseDataType(dataTypeRaw));
            if (!DataType.isIntegerType(attr.getType())) {
                attr.setAutoIncrement(null);
            }
            if (!DataType.canUseCollation(attr.getType())) {
                attr.setCollation("");
            }
            String physicalRealType = erColumn.getRealType();
            if (erColumn.getERSchema().hasPhysicalTypeInMap(physicalRealType) || erColumn.isNew()) {
                String logicalType = schema.convert2LogicalShowType(physicalRealType);
                erColumn.setShowType(logicalType, false);
            }
        } else {
            if (!DataType.DATATYPE_STRING.equalsIgnoreCase(dataTypeRaw)) {
                dataTypeRaw = DataType.reviseDataType(dataTypeRaw);
            }
            erColumn.setShowType(dataTypeRaw, false);
            if (erColumn.getERSchema().hasLogicalTypeInMap(dataTypeRaw) || erColumn.isNew()) {
                String physicalType = schema.convert2UpPhysicalShowType(dataTypeRaw);
                if (DataType.DATATYPE_STRING.equalsIgnoreCase(dataTypeRaw)) {
                    physicalType = DataType.reviseDataType(physicalType);
                }
                erColumn.setPhysicalDataType(physicalType);
            }
        }
        editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
    } 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.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
    } 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(oldAttrName);
            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(oldAttrName, attr, oldAttribute);
    } else if (StringUtil.isEqual(property, IAttributeColumn.COL_NOT_NULL)) {
        boolean on = ((Boolean) value).booleanValue();
        attr.setNotNull(on);
        editor.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
    } 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.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
    } 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.getDialog().changeForEditElement(oldAttrName, erColumn, oldColumn);
    } 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();
    editor.afterModifyColumn(oldColumn == null ? null : oldColumn.getName(isPhysical), erColumn.getName(isPhysical));
}
Also used : Table(org.eclipse.swt.widgets.Table) ERTable(com.cubrid.common.ui.er.model.ERTable) Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) ERTable(com.cubrid.common.ui.er.model.ERTable) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) ERSchema(com.cubrid.common.ui.er.model.ERSchema) List(java.util.List) SerialInfo(com.cubrid.common.core.common.model.SerialInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 30 with ERTableColumn

use of com.cubrid.common.ui.er.model.ERTableColumn in project cubrid-manager by CUBRID.

the class EditVirtualTableDialog method afterModifyAttr.

/**
	 * 
	* 
	* @param oldAttrName old physical column name
	* @param newAttrName new physical column name
	* @return void
	 */
private void afterModifyAttr(String oldAttrName, String newAttrName) {
    if (isPhysical) {
        afterModifyColumn(oldAttrName, newAttrName);
    } else {
        ERTableColumn oldColumn = oldERTable.getColumn(oldAttrName);
        ERTableColumn newColumn = newERTable.getColumn(newAttrName);
        String oldName = oldColumn.getName(false);
        String newName = newColumn.getName(false);
        afterModifyColumn(oldName, newName);
    }
}
Also used : ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn)

Aggregations

ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)32 DBAttribute (com.cubrid.common.core.common.model.DBAttribute)11 ERTable (com.cubrid.common.ui.er.model.ERTable)11 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)10 Constraint (com.cubrid.common.core.common.model.Constraint)9 List (java.util.List)6 EditableLabel (com.cubrid.common.ui.er.figures.EditableLabel)5 CubridTableParser (com.cubrid.common.ui.er.model.CubridTableParser)4 ArrayList (java.util.ArrayList)4 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)3 ERSchema (com.cubrid.common.ui.er.model.ERSchema)3 Relationship (com.cubrid.common.ui.er.model.Relationship)3 EditPart (org.eclipse.gef.EditPart)3 TableItem (org.eclipse.swt.widgets.TableItem)3 TablePart (com.cubrid.common.ui.er.part.TablePart)2 HashSet (java.util.HashSet)2 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)2 ITask (com.cubrid.common.core.task.ITask)1 ERWinDBAttribute (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinDBAttribute)1 ERWinSchemaInfo (com.cubrid.common.ui.cubrid.database.erwin.model.ERWinSchemaInfo)1