Search in sources :

Example 6 with DBSCatalog

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;
}
Also used : DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Example 7 with DBSCatalog

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());
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBCExecutionContextDefaults(org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 8 with DBSCatalog

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();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog) SQLDataSource(org.jkiss.dbeaver.model.sql.SQLDataSource) DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema)

Example 9 with DBSCatalog

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);
                }
            }
        }
    }
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)

Example 10 with DBSCatalog

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);
    }
}
Also used : DBSSchema(org.jkiss.dbeaver.model.struct.rdb.DBSSchema) DBSCatalog(org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)

Aggregations

DBSCatalog (org.jkiss.dbeaver.model.struct.rdb.DBSCatalog)12 DBSSchema (org.jkiss.dbeaver.model.struct.rdb.DBSSchema)12 DBCExecutionContextDefaults (org.jkiss.dbeaver.model.exec.DBCExecutionContextDefaults)6 DBException (org.jkiss.dbeaver.DBException)5 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)5 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)5 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)2 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)2 SQLDataSource (org.jkiss.dbeaver.model.sql.SQLDataSource)2 DBSObjectContainer (org.jkiss.dbeaver.model.struct.DBSObjectContainer)2 ExecutionException (org.eclipse.core.commands.ExecutionException)1 IFile (org.eclipse.core.resources.IFile)1 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)1 LabelProvider (org.eclipse.jface.viewers.LabelProvider)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1