Search in sources :

Example 6 with Constraint

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

the class EditVirtualTableDialog method createFkIndexTabItem.

/**
	 * Create foreign key/Index tab item.
	 *
	 * @param tabFolder the object of TabFolder
	 */
private void createFkIndexTabItem(final TabFolder tabFolder) {
    final TabItem foreignKeyTabItem = new TabItem(tabFolder, SWT.NONE);
    foreignKeyTabItem.setText(Messages.infoIndexesTab);
    final Composite composite = new Composite(tabFolder, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    composite.setLayout(gridLayout);
    foreignKeyTabItem.setControl(composite);
    // create the fk table viewer
    final Label fkLabel = new Label(composite, SWT.NONE);
    fkLabel.setText(Messages.lblFK);
    fkTableView = new TableViewer(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
    fkTable = fkTableView.getTable();
    final GridData gdFkTable = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
    fkTable.setLayoutData(gdFkTable);
    fkTable.setLinesVisible(true);
    fkTable.setHeaderVisible(true);
    TableColumn tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(100);
    tblCol.setText(Messages.tblColumnFK);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(119);
    tblCol.setText(Messages.tblColumnColumnName);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(93);
    tblCol.setText(Messages.tblColumnForeignTable);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(143);
    tblCol.setText(Messages.tblColumnForeignColumnName);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(84);
    tblCol.setText(Messages.tblColumnUpdateRule);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(86);
    tblCol.setText(Messages.tblColumnDeleteRule);
    tblCol = new TableColumn(fkTable, SWT.NONE);
    tblCol.setWidth(100);
    tblCol.setText(Messages.tblColumnCacheColumn);
    FKTableViewerContentProvider fkContentProvider = new FKTableViewerContentProvider();
    FKTableViewerLabelProvider fkLabelProvider = new FKTableViewerLabelProvider(database.getDatabaseInfo(), erSchema);
    fkTableView.setContentProvider(fkContentProvider);
    fkTableView.setLabelProvider(fkLabelProvider);
    fkTableView.setInput(getNewSchemaInfo());
    final Composite fkBtnComposite = new Composite(composite, SWT.NONE);
    final GridData gdBtnComposite = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
    fkBtnComposite.setLayoutData(gdBtnComposite);
    gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    fkBtnComposite.setLayout(gridLayout);
    final GridData gdFKBTN = new GridData(SWT.LEFT, SWT.CENTER, false, false);
    final Button addFKBTN = new Button(fkBtnComposite, SWT.NONE);
    addFKBTN.setLayoutData(gdFKBTN);
    addFKBTN.setText(Messages.btnAddFk);
    addFKBTN.addSelectionListener(new AddFkBtnListenerOnFkIndexTab());
    final Button editFKBTN = new Button(fkBtnComposite, SWT.NONE);
    editFKBTN.setLayoutData(gdFKBTN);
    editFKBTN.setEnabled(false);
    editFKBTN.setText(Messages.btnEditFk);
    editFKBTN.addSelectionListener(new EditFkBtnListenerOnFkIndexTab());
    final Button deleteFKBTN = new Button(fkBtnComposite, SWT.NONE);
    deleteFKBTN.setLayoutData(gdFKBTN);
    deleteFKBTN.setEnabled(false);
    deleteFKBTN.setText(Messages.btnDelFk);
    deleteFKBTN.addSelectionListener(new DelFkBtnListenerOnFkIndexTab());
    fkTable.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            SchemaInfo newSchemaInfo = getNewSchemaInfo();
            if (database == null || database.getDatabaseInfo() == null || newSchemaInfo == null) {
                return;
            }
            TableItem[] items = fkTable.getSelection();
            if (items == null || items.length == 0) {
                deleteFKBTN.setEnabled(false);
                editFKBTN.setEnabled(false);
            } else {
                deleteFKBTN.setEnabled(true);
                editFKBTN.setEnabled(items.length == 1);
                for (TableItem item : items) {
                    String fkName = item.getText(0);
                    List<SchemaInfo> superList = SuperClassUtil.getSuperClasses(database.getDatabaseInfo(), newSchemaInfo.getSuperClasses());
                    if (newSchemaInfo.isInSuperClasses(superList, fkName)) {
                        deleteFKBTN.setEnabled(false);
                        editFKBTN.setEnabled(false);
                    }
                }
            }
        }
    });
    // create index table view
    final Label indexLabel = new Label(composite, SWT.NONE);
    indexLabel.setText(Messages.lblIndexes);
    indexTableView = new TableViewer(composite, SWT.FULL_SELECTION | SWT.MULTI | SWT.BORDER);
    indexTable = indexTableView.getTable();
    indexTable.setLinesVisible(true);
    indexTable.setHeaderVisible(true);
    final GridData gdIndexTable = new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1);
    indexTable.setLayoutData(gdIndexTable);
    tblCol = new TableColumn(indexTable, SWT.NONE);
    tblCol.setWidth(150);
    tblCol.setText(Messages.tblColumnIndexName);
    tblCol = new TableColumn(indexTable, SWT.NONE);
    tblCol.setWidth(78);
    tblCol.setText(Messages.tblColumnIndexType);
    tblCol = new TableColumn(indexTable, SWT.NONE);
    tblCol.setWidth(218);
    tblCol.setText(Messages.tblColumnOnColumns);
    tblCol = new TableColumn(indexTable, SWT.NONE);
    tblCol.setWidth(282);
    tblCol.setText(Messages.tblColumnIndexRule);
    IndexTableViewerContentProvider indexContentProvider = new IndexTableViewerContentProvider();
    IndexTableViewerLabelProvider indexLabelProvider = new IndexTableViewerLabelProvider();
    indexTableView.setContentProvider(indexContentProvider);
    indexTableView.setLabelProvider(indexLabelProvider);
    indexTableView.setInput(getNewSchemaInfo());
    indexTableView.addDoubleClickListener(new IDoubleClickListener() {

        public void doubleClick(DoubleClickEvent event) {
            StructuredSelection selected = (StructuredSelection) event.getSelection();
            if (selected == null) {
                return;
            }
            Constraint constraint = (Constraint) selected.getFirstElement();
            openEditIndexDialog(constraint);
        }
    });
    final Composite indexBtnComposite = new Composite(composite, SWT.NONE);
    final GridData gdIndexBtnComposite = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
    indexBtnComposite.setLayoutData(gdIndexBtnComposite);
    gridLayout = new GridLayout();
    gridLayout.numColumns = 3;
    indexBtnComposite.setLayout(gridLayout);
    final GridData gdIndexBTN = new GridData(SWT.LEFT, SWT.CENTER, false, false);
    final Button addIndexBTN = new Button(indexBtnComposite, SWT.NONE);
    addIndexBTN.setLayoutData(gdIndexBTN);
    addIndexBTN.setText(Messages.btnAddIndex);
    addIndexBTN.addSelectionListener(new AddIndexBtnListenerOnFkIndexTab());
    final Button editIndexBTN = new Button(indexBtnComposite, SWT.NONE);
    editIndexBTN.setLayoutData(gdIndexBTN);
    editIndexBTN.setEnabled(false);
    editIndexBTN.setText(Messages.btnEditIndex);
    editIndexBTN.addSelectionListener(new EditIndexBtnListenerOnFkIndexTab());
    final Button deleteIndexBTN = new Button(indexBtnComposite, SWT.NONE);
    deleteIndexBTN.setLayoutData(gdIndexBTN);
    deleteIndexBTN.setEnabled(false);
    deleteIndexBTN.setText(Messages.btnDelIndex);
    deleteIndexBTN.addSelectionListener(new DelIndexBtnListenerOnFkIndexTab());
    indexTable.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            TableItem[] items = indexTable.getSelection();
            if (items == null || items.length == 0) {
                deleteIndexBTN.setEnabled(false);
                editIndexBTN.setEnabled(false);
            } else {
                deleteIndexBTN.setEnabled(true);
                editIndexBTN.setEnabled(items.length == 1);
            }
        }
    });
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) TableItem(org.eclipse.swt.widgets.TableItem) Label(org.eclipse.swt.widgets.Label) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) FKTableViewerLabelProvider(com.cubrid.common.ui.cubrid.table.control.FKTableViewerLabelProvider) GridLayout(org.eclipse.swt.layout.GridLayout) Button(org.eclipse.swt.widgets.Button) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) ArrayList(java.util.ArrayList) List(java.util.List) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) Composite(org.eclipse.swt.widgets.Composite) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IndexTableViewerContentProvider(com.cubrid.common.ui.cubrid.table.control.IndexTableViewerContentProvider) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) TableColumn(org.eclipse.swt.widgets.TableColumn) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) IndexTableViewerLabelProvider(com.cubrid.common.ui.cubrid.table.control.IndexTableViewerLabelProvider) TabItem(org.eclipse.swt.widgets.TabItem) FKTableViewerContentProvider(com.cubrid.common.ui.cubrid.table.control.FKTableViewerContentProvider) GridData(org.eclipse.swt.layout.GridData) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 7 with Constraint

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

the class EditVirtualTableDialog method makeChangeLogForIndex.

public boolean makeChangeLogForIndex(String attrName, DBAttribute editAttr, DBAttribute origAttr) {
    // FIXME move this logic to core module
    List<Constraint> constrainList = newERTable.getSchemaInfo().getConstraints();
    if (constrainList.size() == 0 || StringUtil.isEmpty(attrName)) {
        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(newERTable.getSchemaInfo().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(newERTable.getSchemaInfo());
    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 8 with Constraint

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

the class EditVirtualTableDialog method changeForEditElement.

/**
	 * Make a change log for editing attribute
	 *
	 * @param attrName
	 * @param editingCol ERD column to be changed by the user
	 * @param oldCol ERD column
	 * @return
	 */
public boolean changeForEditElement(String attrName, ERTableColumn editingCol, ERTableColumn oldCol) {
    // FIXME move this logic to core module
    SchemaInfo newSchemaInfo = getNewSchemaInfo();
    if (database == null || editingCol == null) {
        return false;
    }
    editingCol.getAttr().setInherit(newSchemaInfo.getClassname());
    String newAttrName = editingCol.getAttr().getName();
    if (oldCol == null) {
        oldCol = new ERTableColumn(newERTable, editingCol.getAttr(), false);
        oldCol.setIsNew(true);
    }
    DBAttribute oldAttr = oldCol.getAttr();
    DBAttribute editingAttr = editingCol.getAttr();
    if (!oldAttr.isUnique() && newSchemaInfo.getUniqueByAttrName(editingAttr.getName()) == null && editingAttr.isUnique()) {
        Constraint unique = new Constraint(true);
        unique.setType(Constraint.ConstraintType.UNIQUE.getText());
        unique.addAttribute(newAttrName);
        unique.addRule(newAttrName + " ASC");
        unique.setName(ConstraintNamingUtil.getUniqueName(newSchemaInfo.getClassname(), unique.getRules()));
        newSchemaInfo.addConstraint(unique);
    } else if (oldAttr.isUnique() && !editingAttr.isUnique()) {
        newSchemaInfo.removeUniqueByAttrName(attrName);
    } else if (!oldAttr.isUnique() && !editingAttr.isUnique()) {
        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(editingCol), true);
    columnsTable.setFocus();
    handleSelectionChangeInColumnTable();
    return true;
}
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) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 9 with Constraint

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

the class EditVirtualTableDialog method postEdittedTable.

/**
	 * Parse the table and add new table to the er schema
	 * 
	 * @param erSchema ERSchema
	 */
public void postEdittedTable(ERSchema erSchema) {
    ERTable tmpTable = (ERTable) oldERTable.clone();
    SchemaInfo newSchemaInfo = getNewSchemaInfo();
    //update table name
    if (!oldERTable.getName(isPhysical).equals(newERTable.getName(isPhysical))) {
        oldERTable.modifyNameAndFire(newERTable.getName(isPhysical), isPhysical);
    }
    //update table collation
    oldERTable.getSchemaInfo().setCollation(newSchemaInfo.getCollation());
    //deleted columns
    for (String oldColName : deletedColumns) {
        oldERTable.removeColumnAndFire(oldERTable.getColumn(oldColName, isPhysical));
    }
    //modified columns
    Set<String> oldNames = modifiedColumns.keySet();
    for (String oldColName : oldNames) {
        String newName = modifiedColumns.get(oldColName);
        ERTableColumn oldColumn = oldERTable.getColumn(oldColName, isPhysical);
        ERTableColumn newCol = newERTable.getColumn(newName, isPhysical);
        if (oldColumn == null) {
            continue;
        }
        ERTableColumn firedOldColumn = oldColumn.clone();
        if (newCol == null) {
            continue;
        }
        //will modify the old column to new
        oldERTable.modifyColumn(oldColName, isPhysical, newCol);
        oldColumn.firePropertyChange(ERTableColumn.TEXT_CHANGE, firedOldColumn, newCol);
    }
    //added columns
    for (String addedColumn : addedColumns) {
        ERTableColumn newColumn = newERTable.getColumn(addedColumn, isPhysical);
        if (newColumn == null) {
            continue;
        }
        //from new to old now
        newColumn.setIsNew(false);
        if (oldERTable.getColumn(addedColumn, isPhysical) != null) {
            continue;
        }
        oldERTable.addColumnAndFire(newColumn);
    }
    //update pk
    Constraint newPK = newSchemaInfo.getPK();
    Constraint oldPK = oldERTable.getSchemaInfo().getPK();
    if (oldPK != null) {
        oldERTable.getSchemaInfo().removeConstraintByName(oldPK.getName(), ConstraintType.PRIMARYKEY.getText());
    }
    if (newPK != null) {
        oldERTable.getSchemaInfo().addConstraint(newPK);
    }
    //update fk
    List<Constraint> oldFKs = oldERTable.getSchemaInfo().getFKConstraints();
    oldERTable.deleteAllFKShipsAndFire();
    for (Constraint fk : oldFKs) {
        oldERTable.getSchemaInfo().addConstraint(fk);
    }
    CubridTableParser tableParser = new CubridTableParser(erSchema);
    try {
        tableParser.buildReferenceShip(oldERTable, newSchemaInfo);
    } catch (Exception e) {
        CommonUITool.openErrorBox(e.getMessage());
        oldERTable = tmpTable;
        return;
    }
    syncLogicalNameAndPhysicalComment();
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ERTableColumn(com.cubrid.common.ui.er.model.ERTableColumn) ERTable(com.cubrid.common.ui.er.model.ERTable) CubridTableParser(com.cubrid.common.ui.er.model.CubridTableParser) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo)

Example 10 with Constraint

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

the class CreateRelationshipCommand method execute.

@Override
public void execute() {
    if (!check()) {
        return;
    }
    ERSchema erSchema = foreignTable.getERSchema();
    SchemaInfo fkSchemaInfo = erSchema.getSchemaInfo(foreignTable.getName());
    AddFKDialog dlg = new AddFKDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), erSchema.getCubridDatabase(), fkSchemaInfo, null, false, erSchema.getAllSchemaInfo());
    dlg.setDefaultTableName(primaryTable.getName());
    int returnCode = dlg.open();
    if (returnCode == AddFKDialog.OK) {
        Constraint fk = dlg.getRetFK();
        if (fk == null) {
            return;
        }
        CubridTableParser parser = new CubridTableParser(erSchema);
        try {
            parser.addFKShip(foreignTable, fkSchemaInfo, fk);
            fkSchemaInfo.addConstraint(fk);
        } catch (Exception e) {
            CommonUITool.openErrorBox(e.getMessage());
        }
    }
}
Also used : Constraint(com.cubrid.common.core.common.model.Constraint) ERSchema(com.cubrid.common.ui.er.model.ERSchema) CubridTableParser(com.cubrid.common.ui.er.model.CubridTableParser) Constraint(com.cubrid.common.core.common.model.Constraint) SchemaInfo(com.cubrid.common.core.common.model.SchemaInfo) AddFKDialog(com.cubrid.common.ui.cubrid.table.dialog.AddFKDialog)

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