use of org.jkiss.dbeaver.model.struct.rdb.DBSSchema in project dbeaver by serge-rider.
the class ContextDefaultObjectsReader method run.
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
DBSObjectContainer objectContainer = DBUtils.getAdapter(DBSObjectContainer.class, dataSource);
if (objectContainer == null) {
return;
}
DBCExecutionContextDefaults contextDefaults = null;
if (executionContext != null) {
contextDefaults = executionContext.getContextDefaults();
}
if (contextDefaults == null) {
return;
}
try {
monitor.beginTask("Read default objects", 1);
currentDatabaseInstanceName = null;
Class<? extends DBSObject> childType = objectContainer.getPrimaryChildType(monitor);
if (childType == null || !DBSObjectContainer.class.isAssignableFrom(childType)) {
enabled = false;
} else {
enabled = true;
DBSObjectContainer defObject = null;
if (DBSCatalog.class.isAssignableFrom(childType)) {
defObject = contextDefaults.getDefaultCatalog();
}
if (defObject != null) {
Class<? extends DBSObject> catalogChildrenType = defObject.getPrimaryChildType(monitor);
if (catalogChildrenType != null && DBSSchema.class.isAssignableFrom(catalogChildrenType)) {
currentDatabaseInstanceName = defObject.getName();
if (contextDefaults.supportsSchemaChange()) {
objectContainer = defObject;
} else if (!contextDefaults.supportsCatalogChange()) {
// Nothing can be changed
objectContainer = null;
}
DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
if (defaultSchema != null) {
defObject = defaultSchema;
}
}
}
objectList = objectContainer == null ? (defObject == null ? Collections.emptyList() : Collections.singletonList(defObject)) : objectContainer.getChildren(monitor);
defaultObject = defObject;
if (readNodes) {
// Cache navigator nodes
if (objectList != null) {
for (DBSObject child : objectList) {
if (DBUtils.getAdapter(DBSObjectContainer.class, child) != null) {
DBNDatabaseNode node = navigatorModel.getNodeByObject(monitor, child, false);
if (node != null) {
nodeList.add(node);
}
}
}
}
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSSchema in project dbeaver by serge-rider.
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.struct.rdb.DBSSchema in project dbeaver by serge-rider.
the class NavigatorHandlerSetDefaultObject method markObjectAsActive.
@SuppressWarnings("unchecked")
private void markObjectAsActive(final DBNDatabaseNode databaseNode, IEditorPart activeEditor) {
DBNNode parentNode = databaseNode.getParentNode();
if (parentNode instanceof DBNDatabaseItem) {
markObjectAsActive((DBNDatabaseItem) parentNode, activeEditor);
return;
}
DBSObject object = databaseNode.getObject();
DBPDataSource dataSource = object.getDataSource();
final DBCExecutionContext editorContext;
if (activeEditor instanceof DBPContextProvider) {
editorContext = ((DBPContextProvider) activeEditor).getExecutionContext();
} else {
editorContext = null;
}
TasksJob.runTask("Change default object", monitor -> {
try {
DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
try {
DBCExecutionContext defaultContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
DBCExecutionContext[] contextsToChange;
if (editorContext != null && editorContext != defaultContext && editorContext.getDataSource() == defaultContext.getDataSource()) {
contextsToChange = new DBCExecutionContext[] { defaultContext, editorContext };
} else {
contextsToChange = new DBCExecutionContext[] { defaultContext };
}
for (DBCExecutionContext executionContext : contextsToChange) {
DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
if (contextDefaults != null) {
if (object instanceof DBSCatalog && contextDefaults.supportsCatalogChange()) {
contextDefaults.setDefaultCatalog(monitor, (DBSCatalog) object, null);
} else if (object instanceof DBSSchema && contextDefaults.supportsSchemaChange()) {
contextDefaults.setDefaultSchema(monitor, (DBSSchema) object);
} else {
throw new DBCException("Internal error: active object change not supported");
}
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (Exception e) {
throw new InvocationTargetException(e);
}
});
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSSchema in project dbeaver by serge-rider.
the class DBExecUtils method changeDefaultObject.
@SuppressWarnings("unchecked")
public static void changeDefaultObject(@NotNull DBRProgressMonitor monitor, @NotNull DBSObjectContainer rootContainer, @NotNull DBCExecutionContextDefaults contextDefaults, @Nullable String newCatalogName, @Nullable String curCatalogName, @Nullable String newObjectName) throws DBException {
DBSCatalog newCatalog = null;
DBSSchema newSchema = null;
if (newCatalogName != null) {
DBSObject newInstance = rootContainer.getChild(monitor, newCatalogName);
if (newInstance instanceof DBSCatalog) {
newCatalog = (DBSCatalog) newInstance;
}
}
DBSObject newObject;
if (newObjectName != null) {
if (newCatalog == null) {
newObject = rootContainer.getChild(monitor, newObjectName);
} else {
newObject = newCatalog.getChild(monitor, newObjectName);
}
if (newObject instanceof DBSSchema) {
newSchema = (DBSSchema) newObject;
} else if (newObject instanceof DBSCatalog) {
newCatalog = (DBSCatalog) newObject;
}
}
boolean changeCatalog = (curCatalogName != null ? !CommonUtils.equalObjects(curCatalogName, newCatalogName) : newCatalog != null);
if (newCatalog != null && newSchema != null && changeCatalog) {
contextDefaults.setDefaultCatalog(monitor, newCatalog, newSchema);
} else if (newSchema != null) {
contextDefaults.setDefaultSchema(monitor, newSchema);
} else if (newCatalog != null && changeCatalog) {
contextDefaults.setDefaultCatalog(monitor, newCatalog, null);
}
}
Aggregations