use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by dbeaver.
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 dbeaver.
the class DBTaskUtils method extractContext.
public static DBTTaskContext extractContext(@NotNull DBCExecutionContext executionContext) {
DBTTaskContext context = new DBTTaskContext();
DBCExecutionContextDefaults defaults = executionContext.getContextDefaults();
if (defaults != null) {
DBSCatalog defaultCatalog = defaults.getDefaultCatalog();
if (defaultCatalog != null) {
context.setDefaultCatalog(defaultCatalog.getName());
}
DBSSchema defaultSchema = defaults.getDefaultSchema();
if (defaultSchema != null) {
context.setDefaultSchema(defaultSchema.getName());
}
}
DBCTransactionManager txnManager = DBUtils.getTransactionManager(executionContext);
if (txnManager != null) {
try {
context.setAutoCommit(txnManager.isAutoCommit());
DBPTransactionIsolation isolation = txnManager.getTransactionIsolation();
if (isolation != null) {
context.setTransactionIsolation(isolation.getCode());
}
} catch (Throwable e) {
log.debug(e);
}
}
return context;
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults 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);
}
use of org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults in project dbeaver by dbeaver.
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 serge-rider.
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