Search in sources :

Example 86 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class NavigatorHandlerObjectCreateCopy method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell activeShell = HandlerUtil.getActiveShell(event);
    Control focusControl = activeShell.getDisplay().getFocusControl();
    if (focusControl instanceof Text) {
        ((Text) focusControl).paste();
        return null;
    } else if (focusControl instanceof StyledText) {
        ((StyledText) focusControl).paste();
        return null;
    } else if (focusControl instanceof Combo) {
        ((Combo) focusControl).paste();
        return null;
    }
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    DBNNode curNode = NavigatorUtils.getSelectedNode(selection);
    if (curNode != null) {
        Clipboard clipboard = new Clipboard(Display.getDefault());
        try {
            @SuppressWarnings("unchecked") Collection<DBNNode> cbNodes = (Collection<DBNNode>) clipboard.getContents(TreeNodeTransfer.getInstance());
            if (cbNodes != null) {
                for (DBNNode nodeObject : cbNodes) {
                    if (nodeObject instanceof DBNDatabaseNode) {
                        createNewObject(HandlerUtil.getActiveWorkbenchWindow(event), curNode, ((DBNDatabaseNode) nodeObject));
                    } else if (nodeObject instanceof DBNResource && curNode instanceof DBNResource) {
                        pasteResource((DBNResource) nodeObject, (DBNResource) curNode);
                    }
                }
            } else if (curNode instanceof DBNResource) {
                String[] files = (String[]) clipboard.getContents(FileTransfer.getInstance());
                if (files != null) {
                    for (String fileName : files) {
                        final File file = new File(fileName);
                        if (file.exists()) {
                            pasteResource(file, (DBNResource) curNode);
                        }
                    }
                } else {
                    log.debug("Paste error: unsupported clipboard format. File or folder were expected.");
                    Display.getCurrent().beep();
                }
            } else {
                log.debug("Paste error: clipboard contains data in unsupported format");
                Display.getCurrent().beep();
            }
        } finally {
            clipboard.dispose();
        }
    }
    return null;
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) StyledText(org.eclipse.swt.custom.StyledText) DBNResource(org.jkiss.dbeaver.model.navigator.DBNResource) ISelection(org.eclipse.jface.viewers.ISelection) Collection(java.util.Collection) Clipboard(org.eclipse.swt.dnd.Clipboard) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) IFile(org.eclipse.core.resources.IFile)

Example 87 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class TabbedFolderPageNode method isRefreshingEvent.

private boolean isRefreshingEvent(DBNEvent event) {
    if (!(event.getSource() instanceof DBPEvent)) {
        return false;
    }
    DBPEvent dbEvent = (DBPEvent) event.getSource();
    if (dbEvent.getData() == DBPEvent.REORDER) {
        DBNNode rootNode = getRootNode();
        // Reorder of child elements
        return rootNode instanceof DBNDatabaseNode && dbEvent.getObject() == ((DBNDatabaseNode) rootNode).getValueObject();
    }
    Object itemsInput = itemControl.getItemsViewer().getInput();
    return itemsInput instanceof Collection && ((Collection) itemsInput).contains(dbEvent.getObject());
}
Also used : DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) Collection(java.util.Collection) DBPEvent(org.jkiss.dbeaver.model.DBPEvent) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 88 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class NavigatorHandlerSetDefaultObject method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final ISelection selection = HandlerUtil.getCurrentSelection(event);
    final IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
    if (selection instanceof IStructuredSelection) {
        IStructuredSelection structSelection = (IStructuredSelection) selection;
        Object element = structSelection.getFirstElement();
        if (element instanceof DBNDatabaseNode) {
            markObjectAsActive((DBNDatabaseNode) element, activeEditor);
        }
    }
    return null;
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IEditorPart(org.eclipse.ui.IEditorPart) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 89 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class DatabaseConsumerPageMapping method activatePage.

@Override
public void activatePage() {
    final DatabaseConsumerSettings settings = getDatabaseConsumerSettings();
    // Detect producer container (e.g. schema)
    DBSObjectContainer producerContainer = null;
    for (DataTransferPipe pipe : getWizard().getSettings().getDataPipes()) {
        if (pipe.getProducer() != null) {
            DBSObject producerObject = pipe.getProducer().getDatabaseObject();
            if (producerObject instanceof DBSDataContainer) {
                DBSObject container = producerObject.getParentObject();
                if (container instanceof DBSObjectContainer) {
                    producerContainer = (DBSObjectContainer) container;
                }
            }
        }
    }
    settings.loadNode(getWizard().getRunnableContext(), getWizard().getSettings(), producerContainer);
    DBNDatabaseNode containerNode = settings.getContainerNode();
    if (containerNode != null) {
        try {
            containerPanel.checkValidContainerNode(containerNode);
            containerPanel.setContainerInfo(containerNode);
        } catch (DBException e) {
            setErrorMessage(e.getMessage());
        }
    }
    boolean newMappings = false;
    {
        // Load columns model. Update it only if mapping have different set of source columns
        // Otherwise we keep current mappings (to allow wizard page navigation without loosing mappings)
        List<DatabaseMappingContainer> model = new ArrayList<>();
        for (DataTransferPipe pipe : getWizard().getSettings().getDataPipes()) {
            if (pipe.getProducer() == null || !(pipe.getProducer().getDatabaseObject() instanceof DBSDataContainer)) {
                continue;
            }
            DBSDataContainer sourceDataContainer = (DBSDataContainer) pipe.getProducer().getDatabaseObject();
            DatabaseMappingContainer mapping = settings.getDataMapping(sourceDataContainer);
            {
                // Create new mapping for source object
                DatabaseMappingContainer newMapping;
                if (pipe.getConsumer() instanceof DatabaseTransferConsumer && ((DatabaseTransferConsumer) pipe.getConsumer()).getTargetObject() != null) {
                    try {
                        newMapping = new DatabaseMappingContainer(getWizard().getRunnableContext(), getDatabaseConsumerSettings(), sourceDataContainer, ((DatabaseTransferConsumer) pipe.getConsumer()).getTargetObject());
                    } catch (DBException e) {
                        setMessage(e.getMessage(), IMessageProvider.ERROR);
                        newMapping = new DatabaseMappingContainer(getDatabaseConsumerSettings(), sourceDataContainer);
                    }
                } else {
                    newMapping = new DatabaseMappingContainer(getDatabaseConsumerSettings(), sourceDataContainer);
                }
                newMapping.getAttributeMappings(getWizard().getRunnableContext());
                // Update current mapping if it differs from new one
                if (mapping == null || !mapping.isSameMapping(newMapping)) {
                    mapping = newMapping;
                    settings.addDataMappings(getWizard().getRunnableContext(), sourceDataContainer, mapping);
                }
            }
            model.add(mapping);
            newMappings = mapping.getMappingType() == DatabaseMappingType.unspecified;
        }
        mappingViewer.setInput(model);
        if (!model.isEmpty()) {
            // Select first element
            mappingViewer.setSelection(new StructuredSelection(model.get(0)));
        }
        if (newMappings) {
            autoAssignMappings();
        }
        Tree table = mappingViewer.getTree();
        int totalWidth = table.getClientArea().width;
        TreeColumn[] columns = table.getColumns();
        columns[0].setWidth(totalWidth * 40 / 100);
        columns[1].setWidth(totalWidth * 40 / 100);
        columns[2].setWidth(totalWidth * 20 / 100);
    }
    updatePageCompletion();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DataTransferPipe(org.jkiss.dbeaver.tools.transfer.DataTransferPipe) ArrayList(java.util.ArrayList) List(java.util.List) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)

Example 90 with DBNDatabaseNode

use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by dbeaver.

the class PostgreFDWConfigWizard method generateScript.

List<DBEPersistAction> generateScript(DBRProgressMonitor monitor) throws DBException {
    PostgreDatabase database = getDatabase();
    PostgreDataSource curDataSource = database.getDataSource();
    List<DBEPersistAction> actions = new ArrayList<>();
    PostgreFDWConfigWizard.FDWInfo selectedFDW = getSelectedFDW();
    PropertySourceCustom propertySource = getFdwPropertySource();
    Map<String, Object> propValues = propertySource.getPropertiesWithDefaults();
    String serverId = getFdwServerId();
    actions.add(new SQLDatabasePersistActionComment(curDataSource, "CREATE EXTENSION " + selectedFDW.getId()));
    {
        StringBuilder script = new StringBuilder();
        script.append("CREATE SERVER ").append(serverId).append("\n\tFOREIGN DATA WRAPPER ").append(selectedFDW.getId()).append("\n\tOPTIONS(");
        boolean firstProp = true;
        for (Map.Entry<String, Object> pe : propValues.entrySet()) {
            String propName = CommonUtils.toString(pe.getKey());
            String propValue = CommonUtils.toString(pe.getValue());
            if (CommonUtils.isEmpty(propName) || CommonUtils.isEmpty(propValue)) {
                continue;
            }
            if (!firstProp)
                script.append(", ");
            script.append(propName).append(" '").append(propValue).append("'");
            firstProp = false;
        }
        script.append(")");
        actions.add(new SQLDatabasePersistAction("Create extension", script.toString()));
    }
    actions.add(new SQLDatabasePersistAction("CREATE USER MAPPING FOR CURRENT_USER SERVER " + serverId));
    // Now tables
    DBECommandContext commandContext = new SimpleCommandContext(getExecutionContext(), false);
    try {
        PostgreFDWConfigWizard.FDWInfo fdwInfo = getSelectedFDW();
        Map<String, Object> options = new HashMap<>();
        options.put(SQLObjectEditor.OPTION_SKIP_CONFIGURATION, true);
        PostgreForeignTableManager tableManager = new PostgreForeignTableManager();
        PostgreTableColumnManager columnManager = new PostgreTableColumnManager();
        for (DBNDatabaseNode tableNode : getSelectedEntities()) {
            DBSEntity entity = (DBSEntity) tableNode.getObject();
            PostgreTableForeign pgTable = (PostgreTableForeign) tableManager.createNewObject(monitor, commandContext, getSelectedSchema(), null, options);
            if (pgTable == null) {
                log.error("Internal error while creating new table");
                continue;
            }
            pgTable.setName(entity.getName());
            pgTable.setForeignServerName(serverId);
            pgTable.setForeignOptions(new String[0]);
            for (DBSEntityAttribute attr : CommonUtils.safeCollection(entity.getAttributes(monitor))) {
                // Cache data types
                PostgreSchema catalogSchema = database.getCatalogSchema(monitor);
                if (catalogSchema != null) {
                    catalogSchema.getDataTypes(monitor);
                }
                String defTypeName = DBStructUtils.mapTargetDataType(database, attr, true);
                String plainTargetTypeName = SQLUtils.stripColumnTypeModifiers(defTypeName);
                PostgreDataType dataType = database.getDataType(monitor, plainTargetTypeName);
                if (dataType == null) {
                    log.error("Data type '" + plainTargetTypeName + "' not found. Skip column mapping.");
                    continue;
                }
                PostgreTableColumn newColumn = columnManager.createNewObject(monitor, commandContext, pgTable, null, options);
                assert newColumn != null;
                newColumn.setName(attr.getName());
                newColumn.setDataType(dataType);
            }
            DBEPersistAction[] tableDDL = tableManager.getTableDDL(monitor, pgTable, options);
            Collections.addAll(actions, tableDDL);
        }
    } finally {
        commandContext.resetChanges(true);
    }
    // CREATE SERVER clickhouse_svr FOREIGN DATA WRAPPER clickhousedb_fdw OPTIONS(dbname 'default', driver '/usr/local/lib/odbc/libclickhouseodbc.so', host '46.101.202.143');
    return actions;
}
Also used : SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) PostgreForeignTableManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager) DBNDatabaseNode(org.jkiss.dbeaver.model.navigator.DBNDatabaseNode) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) PostgreTableColumnManager(org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager) PropertySourceCustom(org.jkiss.dbeaver.runtime.properties.PropertySourceCustom) SimpleCommandContext(org.jkiss.dbeaver.ui.editors.SimpleCommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)222 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)85 DBException (org.jkiss.dbeaver.DBException)83 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)81 InvocationTargetException (java.lang.reflect.InvocationTargetException)53 ArrayList (java.util.ArrayList)44 GridData (org.eclipse.swt.layout.GridData)36 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)29 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)28 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)27 DBNModel (org.jkiss.dbeaver.model.navigator.DBNModel)26 ISelection (org.eclipse.jface.viewers.ISelection)21 DBPDataSourceContainer (org.jkiss.dbeaver.model.DBPDataSourceContainer)21 DBNDatabaseFolder (org.jkiss.dbeaver.model.navigator.DBNDatabaseFolder)19 Collection (java.util.Collection)18 List (java.util.List)17 Composite (org.eclipse.swt.widgets.Composite)17 DBNDataSource (org.jkiss.dbeaver.model.navigator.DBNDataSource)17 DBXTreeNode (org.jkiss.dbeaver.model.navigator.meta.DBXTreeNode)17 SWT (org.eclipse.swt.SWT)15