use of org.jkiss.dbeaver.model.struct.rdb.DBSCatalog 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.struct.rdb.DBSCatalog 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.struct.rdb.DBSCatalog in project dbeaver by dbeaver.
the class DatabaseTransferConsumer method generateTargetTableDDL.
public static String generateTargetTableDDL(DBRProgressMonitor monitor, DBPDataSource dataSource, DBSObjectContainer schema, DatabaseMappingContainer containerMapping) throws DBException {
monitor.subTask("Create table " + containerMapping.getTargetName());
StringBuilder sql = new StringBuilder(500);
if (!(dataSource instanceof SQLDataSource)) {
throw new DBException("Data source doesn't support SQL");
}
SQLDataSource targetDataSource = (SQLDataSource) dataSource;
String tableName = DBObjectNameCaseTransformer.transformName(targetDataSource, containerMapping.getTargetName());
containerMapping.setTargetName(tableName);
sql.append("CREATE TABLE ");
if (schema instanceof DBSSchema || schema instanceof DBSCatalog) {
sql.append(DBUtils.getQuotedIdentifier(schema));
sql.append(targetDataSource.getSQLDialect().getCatalogSeparator());
}
sql.append(DBUtils.getQuotedIdentifier(targetDataSource, tableName)).append("(\n");
Map<DBSAttributeBase, DatabaseMappingAttribute> mappedAttrs = new HashMap<>();
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() != DatabaseMappingType.create) {
continue;
}
if (!mappedAttrs.isEmpty())
sql.append(",\n");
appendAttributeClause(dataSource, sql, attr);
mappedAttrs.put(attr.getSource(), attr);
}
if (containerMapping.getSource() instanceof DBSEntity) {
// Make primary key
Collection<? extends DBSEntityAttribute> identifier = DBUtils.getBestTableIdentifier(monitor, (DBSEntity) containerMapping.getSource());
if (!CommonUtils.isEmpty(identifier)) {
boolean idMapped = true;
for (DBSEntityAttribute idAttr : identifier) {
if (!mappedAttrs.containsKey(idAttr)) {
idMapped = false;
break;
}
}
if (idMapped) {
sql.append(",\nPRIMARY KEY (");
boolean hasAttr = false;
for (DBSEntityAttribute idAttr : identifier) {
DatabaseMappingAttribute mappedAttr = mappedAttrs.get(idAttr);
if (hasAttr)
sql.append(",");
sql.append(DBUtils.getQuotedIdentifier(dataSource, mappedAttr.getTargetName()));
hasAttr = true;
}
sql.append(")\n");
}
}
}
sql.append(")");
return sql.toString();
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSCatalog in project dbeaver by serge-rider.
the class DatabaseTransferConsumer method createTargetDatabaseObjects.
private boolean createTargetDatabaseObjects(DBRProgressMonitor monitor, DBSObject dbObject) throws DBException {
try (DBCSession session = DBUtils.openMetaSession(monitor, dbObject, "Create target metadata")) {
// We may need to change active catalog to create target object in the proper location
DBSCatalog oldCatalog = null;
DBSSchema oldSchema = null;
DBSCatalog catalog = dbObject instanceof DBSSchema ? DBUtils.getParentOfType(DBSCatalog.class, dbObject) : null;
if (catalog != null) {
DBCExecutionContextDefaults contextDefaults = session.getExecutionContext().getContextDefaults();
if (contextDefaults != null && contextDefaults.supportsCatalogChange() && contextDefaults.getDefaultCatalog() != catalog) {
oldCatalog = contextDefaults.getDefaultCatalog();
try {
contextDefaults.setDefaultCatalog(monitor, catalog, (DBSSchema) dbObject);
} catch (DBCException e) {
log.debug(e);
}
}
}
try {
switch(containerMapping.getMappingType()) {
case create:
createTargetTable(session, containerMapping);
return true;
case existing:
boolean hasNewObjects = false;
if (!(containerMapping.getTarget() instanceof DBSDocumentContainer)) {
for (DatabaseMappingAttribute attr : containerMapping.getAttributeMappings(monitor)) {
if (attr.getMappingType() == DatabaseMappingType.create) {
createTargetAttribute(session, attr);
hasNewObjects = true;
}
}
}
return hasNewObjects;
default:
return false;
}
} finally {
if (oldCatalog != null) {
// Revert to old catalog
try {
session.getExecutionContext().getContextDefaults().setDefaultCatalog(monitor, oldCatalog, oldSchema);
} catch (DBCException e) {
log.debug(e);
}
}
}
}
}
use of org.jkiss.dbeaver.model.struct.rdb.DBSCatalog 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