Search in sources :

Example 71 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class DBVUtils method getVirtualEntity.

public static DBVEntity getVirtualEntity(@NotNull DBSDataContainer dataContainer, boolean create) {
    if (dataContainer instanceof IAdaptable) {
        // Data container can be a wrapper around another data container (e.g. ResultSetDataContainer). Virtual entity is linked to the nested one.
        DBSDataContainer nestedDC = ((IAdaptable) dataContainer).getAdapter(DBSDataContainer.class);
        if (nestedDC != null) {
            dataContainer = nestedDC;
        }
    }
    if (dataContainer instanceof DBSEntity) {
        return getVirtualEntity((DBSEntity) dataContainer, create);
    }
    // Not an entity. Most likely a custom query. Use local cache for such attributes.
    // There shouldn't be too many such settings as they are defined by user manually
    // so we shouldn't eay too much memory for that
    String attrKey = DBUtils.getObjectFullId(dataContainer);
    synchronized (orphanVirtualEntities) {
        DBPDataSource dataSource = dataContainer.getDataSource();
        if (dataSource == null) {
            return null;
        }
        DBVEntity vEntity = orphanVirtualEntities.get(attrKey);
        if (vEntity == null && create) {
            vEntity = new DBVEntity(dataSource.getContainer().getVirtualModel(), dataContainer.getName(), "");
            orphanVirtualEntities.put(attrKey, vEntity);
        }
        return vEntity;
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource)

Example 72 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class ConnectionTestJob method run.

@Override
public IStatus run(DBRProgressMonitor monitor) {
    if (ownerMonitor != null) {
        monitor = ownerMonitor;
    }
    Thread.currentThread().setName(ModelMessages.dialog_connection_wizard_start_connection_monitor_thread);
    try {
        container.setName(container.getConnectionConfiguration().getUrl());
        long startTime = System.currentTimeMillis();
        super.run(monitor);
        connectTime = (System.currentTimeMillis() - startTime);
        if (connectError != null || monitor.isCanceled()) {
            return Status.OK_STATUS;
        }
        if (connectStatus == Status.CANCEL_STATUS) {
            return Status.CANCEL_STATUS;
        }
        // Start monitor task here becaue actual connection makes its own begin/end sequence
        monitor.beginTask(ModelMessages.dialog_connection_wizard_start_connection_monitor_start, 3);
        DBPDataSource dataSource = container.getDataSource();
        if (dataSource == null) {
            throw new DBException(ModelMessages.error_not_connected_to_database);
        }
        monitor.subTask(ModelMessages.dialog_connection_wizard_start_connection_monitor_subtask_test);
        DBPDataSourceInfo info = dataSource.getInfo();
        if (info != null) {
            try {
                productName = info.getDatabaseProductName();
                productVersion = info.getDatabaseProductVersion();
                driverName = info.getDriverName();
                driverVersion = info.getDriverVersion();
            } catch (Exception e) {
                log.error("Can't obtain connection metadata", e);
            }
        } else {
            try (DBCSession session = DBUtils.openUtilSession(monitor, dataSource, "Test connection")) {
                if (session instanceof Connection) {
                    try {
                        Connection connection = (Connection) session;
                        DatabaseMetaData metaData = connection.getMetaData();
                        productName = metaData.getDatabaseProductName();
                        productVersion = metaData.getDatabaseProductVersion();
                        driverName = metaData.getDriverName();
                        driverVersion = metaData.getDriverVersion();
                    } catch (Exception e) {
                        log.error("Can't obtain connection metadata", e);
                    }
                }
            }
        }
        if (driverName == null || driverVersion == null) {
            try {
                if (driverName == null) {
                    driverName = container.getDriver().getDriverClassName();
                }
                if (driverVersion == null) {
                    // Try to get driver version from driver instance
                    Object driverInstance = container.getDriver().getDriverInstance(monitor);
                    if (driverInstance instanceof Driver) {
                        driverVersion = ((Driver) driverInstance).getMajorVersion() + "." + ((Driver) driverInstance).getMinorVersion();
                    }
                }
            } catch (DBException e) {
                log.debug(e);
            }
        }
        monitor.worked(1);
        monitor.subTask("Load connection info");
        try {
            try (DBCSession session = DBUtils.openUtilSession(monitor, dataSource, "Call connection testers")) {
                onTest.run(session);
            }
        } catch (InvocationTargetException e) {
            log.error(e.getTargetException());
        } catch (InterruptedException e) {
        // ignore
        }
        monitor.worked(1);
        new DisconnectJob(container).schedule();
        monitor.worked(1);
        monitor.subTask(ModelMessages.dialog_connection_wizard_start_connection_monitor_success);
        if (!CommonUtils.isEmpty(this.productName)) {
            serverVersion = this.productName + " " + this.productVersion;
        }
        if (!CommonUtils.isEmpty(this.driverName)) {
            clientVersion = this.driverName + " " + this.driverVersion;
        }
    } catch (DBException ex) {
        connectError = ex;
    }
    monitor.done();
    return Status.OK_STATUS;
}
Also used : DBException(org.jkiss.dbeaver.DBException) Connection(java.sql.Connection) Driver(java.sql.Driver) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceInfo(org.jkiss.dbeaver.model.DBPDataSourceInfo) DatabaseMetaData(java.sql.DatabaseMetaData) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBCSession(org.jkiss.dbeaver.model.exec.DBCSession)

Example 73 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class DatabaseLazyEditorInput method initializeRealInput.

public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
    // Get the node path.
    if (project != null) {
        dataSourceContainer = project.getDataSourceRegistry().getDataSource(dataSourceId);
    }
    if (dataSourceContainer == null) {
        // $NON-NLS-2$
        log.error("Can't find data source '" + dataSourceId + "'");
        return null;
    }
    if (project == null) {
        project = dataSourceContainer.getRegistry().getProject();
    }
    final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
    navigatorModel.ensureProjectLoaded(project);
    // dataSourceContainer, project, nodePath, nodeName, activePageId, activeFolderId
    DBPDataSource dataSource;
    while (!dataSourceContainer.isConnected()) {
        try {
            dataSourceContainer.connect(monitor, true, true);
        } catch (final DBException e) {
            // Connection error
            final Integer result = new UITask<Integer>() {

                @Override
                protected Integer runTask() {
                    ConnectionLostDialog clDialog = new ConnectionLostDialog(UIUtils.getActiveWorkbenchShell(), dataSourceContainer, e, "Close");
                    return clDialog.open();
                }
            }.execute();
            if (result == IDialogConstants.STOP_ID) {
                // Close editor
                return null;
            } else if (result == IDialogConstants.RETRY_ID) {
                continue;
            } else {
                return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
            }
        }
        break;
    }
    try {
        dataSource = dataSourceContainer.getDataSource();
        if (dataSource == null) {
            throw new DBException("Connection to '" + dataSourceContainer.getName() + "' canceled");
        }
        final DBNNode[] editorNodeResult = new DBNNode[1];
        DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
            try {
                DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, this.dataSourceContainer, true);
                if (dsNode == null) {
                    throw new DBException("Datasource '" + this.dataSourceContainer.getName() + "' navigator node not found");
                }
                dsNode.initializeNode(monitor, null);
                editorNodeResult[0] = navigatorModel.getNodeByPath(monitor, project, nodePath);
            } catch (Exception e) {
                throw new InvocationTargetException(e);
            }
        });
        DBNNode node = editorNodeResult[0];
        if (node == null) {
            throw new DBException("Navigator node '" + nodePath + "' not found");
        }
        if (node instanceof DBNDatabaseNode) {
            DatabaseNodeEditorInput realInput = new DatabaseNodeEditorInput((DBNDatabaseNode) node);
            realInput.setDefaultFolderId(activeFolderId);
            realInput.setDefaultPageId(activePageId);
            return realInput;
        } else {
            throw new DBException("Database node has bad type: " + node.getClass().getName());
        }
    } catch (DBException e) {
        return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) UITask(org.jkiss.dbeaver.ui.UITask) ConnectionLostDialog(org.jkiss.dbeaver.ui.dialogs.ConnectionLostDialog) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) DBNDataSource(org.jkiss.dbeaver.model.navigator.DBNDataSource) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBNModel(org.jkiss.dbeaver.model.navigator.DBNModel) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 74 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class SelectActiveSchemaHandler method changeDataBaseSelection.

private static void changeDataBaseSelection(DBPDataSourceContainer dsContainer, DBCExecutionContext executionContext, @Nullable String curInstanceName, @Nullable String newInstanceName, @Nullable String newObjectName) {
    if (dsContainer != null && dsContainer.isConnected()) {
        final DBPDataSource dataSource = dsContainer.getDataSource();
        new AbstractJob("Change active database") {

            {
                setUser(true);
            }

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                try {
                    DBExecUtils.setExecutionContextDefaults(monitor, dataSource, executionContext, newInstanceName, curInstanceName, newObjectName);
                    return Status.OK_STATUS;
                } catch (DBException e) {
                    return GeneralUtils.makeExceptionStatus(e);
                }
            }
        }.schedule();
    }
}
Also used : AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 75 with DBPDataSource

use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.

the class SelectActiveSchemaHandler method updateElement.

@Override
public void updateElement(UIElement element, Map parameters) {
    if ("true".equals(parameters.get("noCustomLabel"))) {
        return;
    }
    IWorkbenchWindow workbenchWindow = element.getServiceLocator().getService(IWorkbenchWindow.class);
    if (workbenchWindow == null || workbenchWindow.getActivePage() == null) {
        return;
    }
    IEditorPart activeEditor = workbenchWindow.getActivePage().getActiveEditor();
    if (activeEditor == null) {
        return;
    }
    String schemaName = "< N/A >";
    DBIcon schemaIcon = DBIcon.TREE_SCHEMA;
    String schemaTooltip = UINavigatorMessages.toolbar_datasource_selector_combo_database_tooltip;
    DBPDataSourceContainer dataSource = DataSourceToolbarUtils.getCurrentDataSource(workbenchWindow);
    if (dataSource != null && dataSource.isConnected()) {
        // schemaName = "<no schema>";
        IEditorInput editorInput = activeEditor.getEditorInput();
        if (editorInput instanceof IDatabaseEditorInput) {
            if (editorInput instanceof DatabaseLazyEditorInput) {
                activeEditor.addPropertyListener(new IPropertyListener() {

                    @Override
                    public void propertyChanged(Object source, int propId) {
                        if (EntityEditor.PROP_TITLE == propId) {
                            DataSourceToolbarUtils.updateCommandsUI();
                            activeEditor.removePropertyListener(this);
                        }
                    }
                });
            }
            DBCExecutionContext executionContext = ((IDatabaseEditorInput) editorInput).getExecutionContext();
            if (executionContext != null) {
                DBSObject schemaObject = DBUtils.getSelectedObject(executionContext);
                if (schemaObject != null && DBUtils.getPublicObjectContainer(schemaObject) != dataSource) {
                    DBSObject schemaParent = schemaObject.getParentObject();
                    if (schemaParent instanceof DBSObjectContainer && !(schemaParent instanceof DBPDataSource)) {
                        schemaName = schemaObject.getName() + "@" + schemaParent.getName();
                    } else {
                        schemaName = schemaObject.getName();
                    }
                }
            }
        } else {
            DBCExecutionContext executionContext = getExecutionContextFromPart(activeEditor);
            DBCExecutionContextDefaults contextDefaults = null;
            if (executionContext != null) {
                contextDefaults = executionContext.getContextDefaults();
            }
            if (contextDefaults != null) {
                DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
                DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
                if (defaultCatalog != null && (defaultSchema != null || contextDefaults.supportsSchemaChange())) {
                    schemaName = defaultSchema == null ? "?" : defaultSchema.getName() + "@" + defaultCatalog.getName();
                    schemaIcon = DBIcon.TREE_SCHEMA;
                } else if (defaultCatalog != null) {
                    schemaName = defaultCatalog.getName();
                    schemaIcon = DBIcon.TREE_DATABASE;
                } else if (defaultSchema != null) {
                    schemaName = defaultSchema.getName();
                    schemaIcon = DBIcon.TREE_SCHEMA;
                }
            }
        }
    }
    element.setText(schemaName);
    element.setIcon(DBeaverIcons.getImageDescriptor(schemaIcon));
    element.setTooltip(schemaTooltip);
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DatabaseLazyEditorInput(org.jkiss.dbeaver.ui.editors.DatabaseLazyEditorInput) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) IEditorPart(org.eclipse.ui.IEditorPart) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) IPropertyListener(org.eclipse.ui.IPropertyListener) IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectContainer(org.jkiss.dbeaver.model.struct.DBSObjectContainer) DBIcon(org.jkiss.dbeaver.model.DBIcon) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) IEditorInput(org.eclipse.ui.IEditorInput)

Aggregations

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)154 DBException (org.jkiss.dbeaver.DBException)57 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)28 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)19 InvocationTargetException (java.lang.reflect.InvocationTargetException)15 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)15 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)14 ArrayList (java.util.ArrayList)12 GridData (org.eclipse.swt.layout.GridData)12 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)12 SQLException (java.sql.SQLException)10 IEditorPart (org.eclipse.ui.IEditorPart)10 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)10 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)8 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)8 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)8 DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)8 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)8 ISelection (org.eclipse.jface.viewers.ISelection)6