Search in sources :

Example 6 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by serge-rider.

the class DataSourceManagementToolbar method changeDataBaseSelection.

private void changeDataBaseSelection() {
    if (databaseCombo == null || databaseCombo.isDisposed() || databaseCombo.getSelectionIndex() < 0) {
        return;
    }
    DBPDataSourceContainer dsContainer = getDataSourceContainer();
    final String newName = databaseCombo.getItemText(databaseCombo.getSelectionIndex());
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBSObjectContainer oc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
                    DBSObjectSelector os = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
                    if (os != null) {
                        final DBSObject defObject = os.getDefaultObject();
                        if (defObject instanceof DBSObjectContainer) {
                            // USe seconds level of active object
                            DBSObjectSelector os2 = DBUtils.getAdapter(DBSObjectSelector.class, defObject);
                            if (os2 != null && os2.supportsDefaultChange()) {
                                oc = (DBSObjectContainer) defObject;
                                os = os2;
                            }
                        }
                    }
                    if (oc != null && os != null && os.supportsDefaultChange()) {
                        DBSObject newChild = oc.getChild(monitor, newName);
                        if (newChild != null) {
                            os.setDefaultObject(monitor, newChild);
                        } else {
                            throw new DBException(MessageFormat.format(CoreMessages.toolbar_datasource_selector_error_database_not_found, newName));
                        }
                    } else {
                        throw new DBException(CoreMessages.toolbar_datasource_selector_error_database_change_not_supported);
                    }
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 7 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class DBVContainer method getRealContainer.

public DBSObjectContainer getRealContainer(DBRProgressMonitor monitor) throws DBException {
    DBSObjectContainer realParent = parent.getRealContainer(monitor);
    if (realParent == null) {
        return null;
    }
    DBSObject child = realParent.getChild(monitor, name);
    if (child instanceof DBSObjectContainer) {
        return (DBSObjectContainer) child;
    }
    log.warn("Child '" + name + "' of '" + realParent.getName() + "' is not an object container");
    return null;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer)

Example 8 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class DBVModel method getRealContainer.

@Override
public DBSObjectContainer getRealContainer(DBRProgressMonitor monitor) throws DBException {
    DBPDataSource dataSource = dataSourceContainer.getDataSource();
    if (dataSource instanceof DBSObjectContainer) {
        return (DBSObjectContainer) dataSource;
    }
    log.warn("Datasource '" + dataSource + "' is not an object container");
    return null;
}
Also used : DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 9 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class ExasolCreateForeignKeyDialog method createSchemaSelector.

private void createSchemaSelector(Composite tableGroup) throws DBException {
    // Here is a trick - we need to find schema/catalog container node and list its children
    DBNDatabaseNode schemaContainerNode = null;
    for (DBNNode node = ownerTableNode.getParentNode(); node != null; node = node.getParentNode()) {
        if (node instanceof DBNDatabaseNode) {
            DBSObject nodeObject = ((DBNDatabaseNode) node).getObject();
            if (nodeObject instanceof DBSSchema || nodeObject instanceof DBSCatalog) {
                if (node.getParentNode() instanceof DBNDatabaseNode) {
                    schemaContainerNode = (DBNDatabaseNode) node.getParentNode();
                    break;
                }
            }
        }
    }
    if (schemaContainerNode != null) {
        ILabelProvider labelProvider = new LabelProvider() {

            @Override
            public Image getImage(Object element) {
                return DBeaverIcons.getImage(((DBNDatabaseNode) element).getNodeIcon());
            }

            @Override
            public String getText(Object element) {
                return ((DBNDatabaseNode) element).getNodeName();
            }
        };
        boolean isSchema = (ownTable.getParentObject() instanceof DBSSchema);
        DBPDataSourceInfo dsInfo = ownTable.getDataSource().getInfo();
        UIUtils.createControlLabel(tableGroup, isSchema ? dsInfo.getSchemaTerm() : dsInfo.getCatalogTerm());
        final CSmartCombo<DBNDatabaseNode> schemaCombo = new CSmartCombo<>(tableGroup, SWT.BORDER, labelProvider);
        schemaCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING));
        DBNDatabaseNode selectedNode = null;
        for (DBNNode node : schemaContainerNode.getChildren(new VoidProgressMonitor())) {
            if (node instanceof DBNDatabaseNode && ((DBNDatabaseNode) node).getObject() instanceof DBSObjectContainer) {
                schemaCombo.addItem((DBNDatabaseNode) node);
                if (((DBNDatabaseNode) node).getObject() == ownTable.getParentObject()) {
                    selectedNode = (DBNDatabaseNode) node;
                }
            }
        }
        if (selectedNode != null) {
            schemaCombo.select(selectedNode);
        }
        schemaCombo.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                // Here is another trick
                // We need to find table container node
                // This node is a child of schema node and has the same meta as our original table parent node
                DBNDatabaseNode newContainerNode = null;
                DBXTreeNode tableContainerMeta = ((DBNDatabaseNode) ownerTableNode.getParentNode()).getMeta();
                DBNDatabaseNode schemaNode = schemaCombo.getSelectedItem();
                if (schemaNode.getMeta() == tableContainerMeta) {
                    newContainerNode = schemaNode;
                } else {
                    try {
                        for (DBNNode child : schemaNode.getChildren(new VoidProgressMonitor())) {
                            if (child instanceof DBNDatabaseNode && ((DBNDatabaseNode) child).getMeta() == tableContainerMeta) {
                                newContainerNode = (DBNDatabaseNode) child;
                                break;
                            }
                        }
                    } catch (DBException e1) {
                        log.debug(e1);
                    // Shouldn't be here
                    }
                }
                if (newContainerNode != null) {
                    tableList.setRootNode(newContainerNode);
                    tableList.loadData();
                }
            }
        });
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) DBPDataSourceInfo(org.jkiss.dbeaver.model.DBPDataSourceInfo) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBXTreeNode(org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode) CSmartCombo(org.jkiss.dbeaver.ui.controls.CSmartCombo) GridData(org.eclipse.swt.layout.GridData) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) LabelProvider(org.eclipse.jface.viewers.LabelProvider)

Example 10 with DBSObjectContainer

use of org.jkiss.dbeaver.model.struct.DBSObjectContainer in project dbeaver by dbeaver.

the class DataSourceManagementToolbar method changeDataBaseSelection.

private void changeDataBaseSelection(DBNDatabaseNode node) {
    DBPDataSourceContainer dsContainer = getDataSourceContainer();
    final String newName = node.getNodeName();
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBSObjectContainer oc = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
                    DBSObjectSelector os = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
                    if (os != null) {
                        final DBSObject defObject = os.getDefaultObject();
                        if (defObject instanceof DBSObjectContainer) {
                            // USe seconds level of active object
                            DBSObjectSelector os2 = DBUtils.getAdapter(DBSObjectSelector.class, defObject);
                            if (os2 != null && os2.supportsDefaultChange()) {
                                oc = (DBSObjectContainer) defObject;
                                os = os2;
                            }
                        }
                    }
                    if (oc != null && os != null && os.supportsDefaultChange()) {
                        DBSObject newChild = oc.getChild(monitor, newName);
                        if (newChild != null) {
                            os.setDefaultObject(monitor, newChild);
                        } else {
                            throw new DBException(MessageFormat.format(CoreMessages.toolbar_datasource_selector_error_database_not_found, newName));
                        }
                    } else {
                        throw new DBException(CoreMessages.toolbar_datasource_selector_error_database_change_not_supported);
                    }
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBSObjectSelector(org.jkiss.dbeaver.model.struct.DBSObjectSelector) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Aggregations

DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)21 DBException (org.jkiss.dbeaver.DBException)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)9 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)5 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)5 DBSObjectSelector (org.jkiss.dbeaver.model.struct.DBSObjectSelector)4 DataSourceDescriptor (org.jkiss.dbeaver.registry.DataSourceDescriptor)4 GridData (org.eclipse.swt.layout.GridData)3 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)3 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)3 CSmartCombo (org.jkiss.dbeaver.ui.controls.CSmartCombo)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ExecutionException (org.eclipse.core.commands.ExecutionException)2 IStatus (org.eclipse.core.runtime.IStatus)2 BadLocationException (org.eclipse.jface.text.BadLocationException)2 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)2 Point (org.eclipse.swt.graphics.Point)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBPDataSourceInfo (org.jkiss.dbeaver.model.DBPDataSourceInfo)2 DBPSystemObject (org.jkiss.dbeaver.model.DBPSystemObject)2