Search in sources :

Example 96 with DBAttribute

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

the class TableEditorPart method changeForEditAttribute.

/**
	 * Make a change log for editing attribute
	 *
	 * @param attrName
	 * @param lastAttr attribute previous changed by the user
	 * @param editAttr attribute to be changed by the user
	 * @param origAttr attribute on a server
	 * @return
	 */
public boolean changeForEditAttribute(String attrName, DBAttribute editAttr, DBAttribute origAttr) {
    // FIXME move this logic to core module
    if (database == null || editAttr == null) {
        return false;
    }
    String tableName = newSchemaInfo.getClassname();
    editAttr.setInherit(tableName);
    String newAttrName = editAttr.getName();
    // new attribute
    if (origAttr == null) {
        if (!StringUtil.isEmpty(attrName) && newSchemaInfo.replaceDBAttributeByName(attrName, editAttr)) {
            // replace
            addDropAttrLog(attrName, false);
            addNewAttrLog(newAttrName, false);
        }
    } else {
        // existed attribute to changed with a name
        addEditAttrLog(attrName, newAttrName, false);
    }
    if (origAttr == null) {
        origAttr = new DBAttribute();
    }
    if (!origAttr.isUnique() && newSchemaInfo.getUniqueByAttrName(editAttr.getName()) == null && editAttr.isUnique()) {
        Constraint unique = new Constraint(true);
        unique.setType(Constraint.ConstraintType.UNIQUE.getText());
        unique.addAttribute(newAttrName);
        unique.addRule(newAttrName + " ASC");
        unique.setName(ConstraintNamingUtil.getUniqueName(tableName, unique.getRules()));
        newSchemaInfo.addConstraint(unique);
        addNewUniqueLog(unique);
    } else if (origAttr.isUnique() && !editAttr.isUnique()) {
        if (oldSchemaInfo != null) {
            Constraint unique = oldSchemaInfo.getUniqueByAttrName(origAttr.getName());
            addDelUniqueLog(unique);
        }
        newSchemaInfo.removeUniqueByAttrName(attrName);
    } else if (!origAttr.isUnique() && !editAttr.isUnique()) {
        Constraint unique = newSchemaInfo.getUniqueByAttrName(attrName);
        if (unique != null) {
            addDelUniqueLog(unique);
        }
        newSchemaInfo.removeUniqueByAttrName(attrName);
    }
    indexTableView.setInput(newSchemaInfo);
    fkTableView.setInput(newSchemaInfo);
    if (database != null && database.getDatabaseInfo() != null && newSchemaInfo != null) {
        SuperClassUtil.fireSuperClassChanged(database.getDatabaseInfo(), oldSchemaInfo, newSchemaInfo, newSchemaInfo.getSuperClasses());
    }
    attrLabelProvider.setSchema(newSchemaInfo);
    loadColumnData();
    columnTableView.setSelection(new StructuredSelection(editAttr), true);
    columnsTable.setFocus();
    handleSelectionChangeInColumnTable();
    return true;
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 97 with DBAttribute

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

the class TableEditorPart method removeTempDBAttributeByName.

/**
	 * Remove temporary attribute by name
	 *
	 * @param name
	 */
public void removeTempDBAttributeByName(String name) {
    List<DBAttribute> removeList = new ArrayList<DBAttribute>();
    for (DBAttribute attr : tempDBAttributeList) {
        if (!StringUtil.isEmpty(name) && StringUtil.isEqual(name, attr.getName())) {
            removeList.add(attr);
        }
    }
    tempDBAttributeList.removeAll(removeList);
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) ArrayList(java.util.ArrayList)

Example 98 with DBAttribute

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

the class TableEditorPart method openEditIndexDialog.

/**
	 * Open the index edit dialog with the constraint object
	 *
	 * @param editedIndex Constraint
	 */
private void openEditIndexDialog(Constraint editedIndex) {
    boolean isNewConstraint = true;
    for (int i = 0, len = originalConstraints.size(); i < len; i++) {
        if (originalConstraints.get(i) == editedIndex) {
            isNewConstraint = false;
        }
    }
    AddIndexDialog dlg = new AddIndexDialog(getSite().getShell(), newSchemaInfo, database, editedIndex, isNewConstraint);
    int returnCode = dlg.open();
    if (returnCode == AddIndexDialog.OK) {
        Constraint newIndex = dlg.getIndexConstraint();
        if (newIndex == null) {
            return;
        }
        newSchemaInfo.removeConstraintByName(editedIndex.getName(), editedIndex.getType());
        newSchemaInfo.addConstraint(newIndex);
        // For bug TOOLS-2394 Unique index can't be added again
        if (Constraint.ConstraintType.UNIQUE.getText().equals(newIndex.getType()) && newIndex.getAttributes().size() == 1) {
            DBAttribute attr = newSchemaInfo.getDBAttributeByName(newIndex.getAttributes().get(0), false);
            attr.setUnique(true);
            loadColumnData();
        }
        boolean modifiedUK = Constraint.ConstraintType.UNIQUE.getText().equals(editedIndex.getType()) && editedIndex.getAttributes().size() == 1;
        boolean noNewUK = !Constraint.ConstraintType.UNIQUE.getText().equals(newIndex.getType()) || newIndex.getAttributes().size() != 1;
        if (modifiedUK && noNewUK) {
            String attrName = editedIndex.getAttributes().get(0);
            DBAttribute attr = newSchemaInfo.getDBAttributeByName(attrName, false);
            if (attr != null) {
                attr.setUnique(false);
                loadColumnData();
            }
        }
        String key1 = editedIndex.getDefaultName(newSchemaInfo.getClassname()) + "$" + editedIndex.getName();
        String key2 = newIndex.getDefaultName(newSchemaInfo.getClassname()) + "$" + newIndex.getName();
        SchemaChangeLog changeLog = new SchemaChangeLog(key1, key2, SchemeInnerType.TYPE_INDEX);
        schemaChangeMgr.addSchemeChangeLog(changeLog);
        indexTableView.setInput(newSchemaInfo);
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) AddIndexDialog(com.cubrid.common.ui.cubrid.table.dialog.AddIndexDialog) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaChangeLog(com.cubrid.cubridmanager.core.cubrid.table.model.SchemaChangeLog)

Example 99 with DBAttribute

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

the class TableEditorPart method addNewColumn.

public void addNewColumn() {
    // FIXME move this logic to core module
    if (newSchemaInfo == null) {
        return;
    }
    // boolean hasNotFinishedColumn = false;
    boolean hasDuplicatedColumn = false;
    List<DBAttribute> items = newSchemaInfo.getAttributes();
    if (items != null && items.size() > 0) {
        Set<String> matches = new HashSet<String>();
        // check whether there is no name column
        for (DBAttribute attr : items) {
            if (attr == null) {
                continue;
            }
            if (StringUtil.isEmpty(attr.getName())) {
                continue;
            }
            if (matches.contains(attr.getName())) {
                hasDuplicatedColumn = true;
                break;
            }
            matches.add(attr.getName());
        }
    }
    if (hasDuplicatedColumn) {
        CommonUITool.openErrorBox(Messages.errSameNameOnEditTableAddColumn);
        return;
    }
    String collation = null;
    if (newSchemaInfo != null && newSchemaInfo.getCollation() != null) {
        collation = newSchemaInfo.getCollation();
    } else {
        collation = Collation.DEFAULT_COLLATION;
    }
    String newAttrName = "";
    DBAttribute addAttribute = new DBAttribute(newAttrName, DataType.DATATYPE_CHAR, newSchemaInfo.getClassname(), false, false, false, false, null, collation);
    addAttribute.setNew(true);
    tempDBAttributeList.add(addAttribute);
    loadColumnData();
    columnTableView.setSelection(new StructuredSelection(addAttribute), true);
    columnsTable.setFocus();
    handleSelectionChangeInColumnTable();
}
Also used : DBAttribute(com.cubrid.common.core.common.model.DBAttribute) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) HashSet(java.util.HashSet)

Example 100 with DBAttribute

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

the class TableEditorPart method deleteColumn.

private void deleteColumn() {
    if (!CommonUITool.openConfirmBox(Messages.msgDeleteColumnConfirm)) {
        return;
    }
    TableItem[] tblItems = columnsTable.getSelection();
    if (tblItems.length > 0) {
        DBAttribute attr = (DBAttribute) tblItems[0].getData();
        List<DBAttribute> items = newSchemaInfo.getAttributes();
        if (!items.contains(attr)) {
            return;
        }
    }
    TableItem[] selection = columnsTable.getSelection();
    int selectionIndex = columnsTable.getSelectionIndex();
    if (selection != null && selection.length >= 1) {
        List<String> attrNames = new ArrayList<String>();
        for (int m = 0; m < selection.length; m++) {
            attrNames.add(m, selection[m].getText(1));
        }
        List<SchemaInfo> allSupers = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo);
        Constraint pk = newSchemaInfo.getPK(allSupers);
        List<String> pkAttributes = pk == null ? new ArrayList<String>() : pk.getAttributes();
        boolean hasPk = false;
        for (String pkAttribute : pkAttributes) {
            if (attrNames.contains(pkAttribute)) {
                hasPk = true;
                break;
            }
        }
        if (hasPk) {
            if (attrNames.containsAll(pkAttributes)) {
                newSchemaInfo.removeConstraintByName(pk.getName(), Constraint.ConstraintType.PRIMARYKEY.getText());
            } else {
                CommonUITool.openErrorBox(Messages.errColumnNotDropForPk);
                return;
            }
        }
        for (PartitionInfo partitionInfo : newSchemaInfo.getPartitionList()) {
            String partitionExpr = partitionInfo.getPartitionExpr();
            if (StringUtil.isNotEmpty(partitionExpr)) {
                if (partitionExpr.startsWith("[") && partitionExpr.endsWith("]")) {
                    partitionExpr = partitionExpr.substring(1, partitionExpr.length() - 1);
                    if (attrNames.contains(partitionExpr)) {
                        CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
                        return;
                    }
                } else {
                    if (attrNames.contains(partitionExpr)) {
                        CommonUITool.openErrorBox(Messages.errDropForPartitonColumn);
                        return;
                    }
                }
            }
        }
        for (TableItem selec : selection) {
            DBAttribute oldAttribute = (DBAttribute) selec.getData();
            if (oldAttribute == null) {
                continue;
            }
            if (!oldAttribute.getInherit().equals(newSchemaInfo.getClassname())) {
                CommonUITool.openErrorBox(Messages.errColumnNotDrop);
                return;
            }
            if (oldAttribute.isClassAttribute()) {
                newSchemaInfo.getClassAttributes().remove(oldAttribute);
            } else {
                newSchemaInfo.getAttributes().remove(oldAttribute);
                // newSchemaInfo.removeUniqueByAttrName(selec.getText(1));
                // For bug TOOLS-2390 After delete a column of a table,some
                // related index doesn't been deleted.
                newSchemaInfo.removeConstraintByAttrName(oldAttribute.getName());
                indexTableView.setInput(newSchemaInfo);
                fkTableView.setInput(newSchemaInfo);
            }
            SuperClassUtil.fireSuperClassChanged(database.getDatabaseInfo(), oldSchemaInfo, newSchemaInfo, newSchemaInfo.getSuperClasses());
            String oldAttrName = oldAttribute.getName();
            addDropAttrLog(oldAttrName, oldAttribute.isClassAttribute());
        }
        attrLabelProvider.setSchema(newSchemaInfo);
        loadColumnData();
        int itemCount = columnsTable.getItemCount();
        columnsTable.select(selectionIndex < itemCount ? selectionIndex : selectionIndex - 1);
        columnsTable.setFocus();
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) Constraint(com.cubrid.common.core.common.model.Constraint) DBAttribute(com.cubrid.common.core.common.model.DBAttribute) PartitionInfo(com.cubrid.common.core.common.model.PartitionInfo) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Aggregations

DBAttribute (com.cubrid.common.core.common.model.DBAttribute)130 SchemaInfo (com.cubrid.common.core.common.model.SchemaInfo)57 Constraint (com.cubrid.common.core.common.model.Constraint)53 ArrayList (java.util.ArrayList)46 HashMap (java.util.HashMap)16 List (java.util.List)15 SerialInfo (com.cubrid.common.core.common.model.SerialInfo)14 TableItem (org.eclipse.swt.widgets.TableItem)13 ERTableColumn (com.cubrid.common.ui.er.model.ERTableColumn)11 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)11 GetAllAttrTask (com.cubrid.cubridmanager.core.cubrid.table.task.GetAllAttrTask)10 SchemaComment (com.cubrid.common.core.schemacomment.model.SchemaComment)8 Map (java.util.Map)8 Point (org.eclipse.swt.graphics.Point)8 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)7 SQLException (java.sql.SQLException)7 PreparedStatement (java.sql.PreparedStatement)6 DBResolution (com.cubrid.common.core.common.model.DBResolution)5 PartitionInfo (com.cubrid.common.core.common.model.PartitionInfo)5 SqlFormattingStrategy (com.cubrid.common.ui.query.format.SqlFormattingStrategy)5