use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.
the class NavigatorUtils method syncEditorWithNavigator.
public static boolean syncEditorWithNavigator(INavigatorModelView navigatorView, IEditorPart activeEditor) {
if (!(activeEditor instanceof IDataSourceContainerProviderEx)) {
return false;
}
IDataSourceContainerProviderEx dsProvider = (IDataSourceContainerProviderEx) activeEditor;
Viewer navigatorViewer = navigatorView.getNavigatorViewer();
if (navigatorViewer == null) {
return false;
}
DBNNode selectedNode = getSelectedNode(navigatorViewer.getSelection());
if (!(selectedNode instanceof DBNDatabaseNode)) {
return false;
}
final DBPDataSourceContainer ds = ((DBNDatabaseNode) selectedNode).getDataSourceContainer();
if (ds == null) {
return false;
}
if (dsProvider.getDataSourceContainer() != ds) {
dsProvider.setDataSourceContainer(ds);
}
if (activeEditor instanceof DBPContextProvider) {
// Now check if we can change default object
DBSObject dbObject = ((DBNDatabaseNode) selectedNode).getObject();
if (dbObject instanceof DBSCatalog || dbObject instanceof DBSSchema) {
DBCExecutionContext navExecutionContext = null;
try {
navExecutionContext = DBUtils.getOrOpenDefaultContext(dbObject, false);
} catch (DBCException ignored) {
}
DBCExecutionContext editorExecutionContext = ((DBPContextProvider) activeEditor).getExecutionContext();
if (navExecutionContext != null && editorExecutionContext != null) {
DBCExecutionContextDefaults editorContextDefaults = editorExecutionContext.getContextDefaults();
if (editorContextDefaults != null) {
RuntimeUtils.runTask(monitor -> {
try {
monitor.beginTask("Change default object", 1);
if (dbObject instanceof DBSCatalog && dbObject != editorContextDefaults.getDefaultCatalog()) {
monitor.subTask("Change default catalog");
editorContextDefaults.setDefaultCatalog(monitor, (DBSCatalog) dbObject, null);
} else if (dbObject instanceof DBSSchema && dbObject != editorContextDefaults.getDefaultSchema()) {
monitor.subTask("Change default schema");
editorContextDefaults.setDefaultSchema(monitor, (DBSSchema) dbObject);
}
monitor.worked(1);
monitor.done();
} catch (DBCException e) {
throw new InvocationTargetException(e);
}
}, "Set active object", dbObject.getDataSource().getContainer().getPreferenceStore().getInt(ModelPreferences.CONNECTION_OPEN_TIMEOUT));
}
}
}
}
return true;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.
the class NavigatorUtils method addSetDefaultObjectAction.
private static void addSetDefaultObjectAction(IWorkbenchSite workbenchSite, IMenuManager manager, DBNNode selectedNode) {
// Add "Set active object" menu
boolean addSetActive = false;
if (selectedNode.isPersisted() && selectedNode instanceof DBNDatabaseNode && !(selectedNode instanceof DBNDatabaseFolder) && ((DBNDatabaseNode) selectedNode).getObject() != null) {
DBSObject selectedObject = ((DBNDatabaseNode) selectedNode).getObject();
DBPDataSource dataSource = ((DBNDatabaseNode) selectedNode).getDataSource();
if (dataSource != null) {
DBCExecutionContext defaultContext = dataSource.getDefaultInstance().getDefaultContext(new VoidProgressMonitor(), false);
DBCExecutionContextDefaults contextDefaults = defaultContext.getContextDefaults();
if (contextDefaults != null) {
if ((selectedObject instanceof DBSCatalog && contextDefaults.supportsCatalogChange() && contextDefaults.getDefaultCatalog() != selectedObject) || (selectedObject instanceof DBSSchema && contextDefaults.supportsSchemaChange() && contextDefaults.getDefaultSchema() != selectedObject)) {
addSetActive = true;
}
}
}
}
if (addSetActive) {
manager.add(ActionUtils.makeCommandContribution(workbenchSite, NavigatorCommands.CMD_OBJECT_SET_DEFAULT));
}
manager.add(new Separator());
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.
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);
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by serge-rider.
the class SQLScriptExecuteHandler method runScripts.
private void runScripts(DBRProgressMonitor monitor, DBTTask task, SQLScriptExecuteSettings settings, Log log, PrintStream logStream) throws DBException {
List<DBPDataSourceContainer> dataSources = settings.getDataSources();
for (String filePath : settings.getScriptFiles()) {
IFile sqlFile = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
try (InputStream sqlStream = sqlFile.getContents(true)) {
try (Reader fileReader = new InputStreamReader(sqlStream, sqlFile.getCharset())) {
String sqlScriptContent = IOUtils.readToString(fileReader);
try {
for (DBPDataSourceContainer dataSourceContainer : dataSources) {
if (!dataSourceContainer.isConnected()) {
dataSourceContainer.connect(monitor, true, true);
}
DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
throw new DBException("Can't obtain data source connection");
}
DBCExecutionContext executionContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
log.debug("> Execute script [" + filePath + "] in [" + dataSourceContainer.getName() + "]");
DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
if (contextDefaults != null) {
DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
if (defaultCatalog != null) {
log.debug("> Default catalog: " + defaultCatalog.getName());
}
DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
if (defaultSchema != null) {
log.debug("> Default schema: " + defaultSchema.getName());
}
}
processScript(monitor, task, settings, executionContext, filePath, sqlScriptContent, log, logStream);
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
} catch (Throwable e) {
Throwable error = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getTargetException() : e;
throw new DBException("Error executing script '" + filePath + "'", error);
}
}
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by dbeaver.
the class ConnectionPageInitialization method loadDatabaseSettings.
private void loadDatabaseSettings(DBRProgressMonitor monitor, DBPDataSource dataSource) {
DBPDataSourceContainer dataSourceContainer = dataSource.getContainer();
Collection<DBPTransactionIsolation> txnLevels = CommonUtils.safeCollection(dataSource.getInfo().getSupportedTransactionsIsolation());
Integer levelCode = dataSourceContainer.getDefaultTransactionsIsolation();
UIUtils.syncExec(() -> {
autocommit.setSelection(dataSourceContainer.isDefaultAutoCommit());
// isolationLevel.setEnabled(!autocommit.getSelection());
supportedLevels.clear();
DBPTransactionIsolation defaultLevel = null;
{
if (levelCode != null && !CommonUtils.isEmpty(txnLevels)) {
for (DBPTransactionIsolation level : txnLevels) {
if (level.getCode() == levelCode) {
defaultLevel = level;
break;
}
}
}
}
isolationLevel.removeAll();
supportedLevels.clear();
for (DBPTransactionIsolation level : txnLevels) {
if (!level.isEnabled()) {
continue;
}
isolationLevel.add(level.getTitle());
supportedLevels.add(level);
if (level.equals(defaultLevel)) {
isolationLevel.select(isolationLevel.getItemCount() - 1);
}
}
});
if (dataSource instanceof DBSObjectContainer) {
DBCExecutionContext executionContext = DBUtils.getDefaultContext(dataSource, true);
DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
DBSObjectContainer catalogContainer = DBUtils.getChangeableObjectContainer(contextDefaults, (DBSObjectContainer) dataSource, DBSCatalog.class);
if (catalogContainer != null) {
loadSelectableObject(monitor, catalogContainer, defaultCatalog, contextDefaults, true);
}
DBSObjectContainer schemaContainer = DBUtils.getChangeableObjectContainer(contextDefaults, (DBSObjectContainer) dataSource, DBSSchema.class);
loadSelectableObject(monitor, schemaContainer, defaultSchema, contextDefaults, false);
}
txnOptionsLoaded = true;
}
Aggregations