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;
}
}
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;
}
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));
}
}
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();
}
}
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);
}
Aggregations