Search in sources :

Example 6 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by serge-rider.

the class AbstractCommandContext method getCommandQueues.

private List<CommandQueue> getCommandQueues() {
    if (commandQueues != null) {
        return commandQueues;
    }
    commandQueues = new ArrayList<>();
    CommandInfo aggregator = null;
    // Create queues from commands
    for (CommandInfo commandInfo : commands) {
        if (commandInfo.command instanceof DBECommandAggregator) {
            aggregator = commandInfo;
        }
        DBPObject object = commandInfo.command.getObject();
        CommandQueue queue = null;
        if (!commandQueues.isEmpty()) {
            for (CommandQueue tmpQueue : commandQueues) {
                if (tmpQueue.getObject() == object) {
                    queue = tmpQueue;
                    break;
                }
            }
        }
        if (queue == null) {
            DBEObjectManager<?> objectManager = executionContext.getDataSource().getContainer().getPlatform().getEditorsRegistry().getObjectManager(object.getClass());
            if (objectManager == null) {
                throw new IllegalStateException("Can't find object manager for '" + object.getClass().getName() + "'");
            }
            queue = new CommandQueue(objectManager, null, object);
            commandQueues.add(queue);
        }
        queue.addCommand(commandInfo);
    }
    // Merge commands
    for (CommandQueue queue : commandQueues) {
        final Map<DBECommand, CommandInfo> mergedByMap = new IdentityHashMap<>();
        final List<CommandInfo> mergedCommands = new ArrayList<>();
        for (int i = 0; i < queue.commands.size(); i++) {
            CommandInfo lastCommand = queue.commands.get(i);
            lastCommand.mergedBy = null;
            CommandInfo firstCommand = null;
            DBECommand<?> result = lastCommand.command;
            if (mergedCommands.isEmpty()) {
                result = lastCommand.command.merge(null, userParams);
            } else {
                boolean skipCommand = false;
                for (int k = mergedCommands.size(); k > 0; k--) {
                    firstCommand = mergedCommands.get(k - 1);
                    result = lastCommand.command.merge(firstCommand.command, userParams);
                    if (result == null) {
                        // Remove first and skip last command
                        mergedCommands.remove(firstCommand);
                        skipCommand = true;
                    } else if (result != lastCommand.command) {
                        break;
                    }
                }
                if (skipCommand) {
                    continue;
                }
            }
            mergedCommands.add(lastCommand);
            if (result == lastCommand.command) {
            // No changes
            // firstCommand.mergedBy = lastCommand;
            } else if (firstCommand != null && result == firstCommand.command) {
                // Remove last command from queue
                lastCommand.mergedBy = firstCommand;
            } else {
                // Some other command
                // May be it is some earlier command from queue or some new command (e.g. composite)
                CommandInfo mergedBy = mergedByMap.get(result);
                if (mergedBy == null) {
                    // Try to find in command stack
                    for (int k = i; k >= 0; k--) {
                        if (queue.commands.get(k).command == result) {
                            mergedBy = queue.commands.get(k);
                            break;
                        }
                    }
                    if (mergedBy == null) {
                        // Create new command info
                        mergedBy = new CommandInfo(result, null);
                    }
                    mergedByMap.put(result, mergedBy);
                }
                lastCommand.mergedBy = mergedBy;
                if (!mergedCommands.contains(mergedBy)) {
                    mergedCommands.add(mergedBy);
                }
            }
        }
        queue.commands = mergedCommands;
    }
    // Filter commands
    for (CommandQueue queue : commandQueues) {
        if (queue.objectManager instanceof DBECommandFilter) {
            ((DBECommandFilter) queue.objectManager).filterCommands(queue);
        }
    }
    // Aggregate commands
    if (aggregator != null) {
        ((DBECommandAggregator) aggregator.command).resetAggregatedCommands();
        for (CommandQueue queue : commandQueues) {
            for (CommandInfo cmd : queue.commands) {
                if (cmd.command != aggregator.command && cmd.mergedBy == null && ((DBECommandAggregator) aggregator.command).aggregateCommand(cmd.command)) {
                    cmd.mergedBy = aggregator;
                }
            }
        }
    }
    // Move rename commands in the head (#7512)
    for (CommandQueue queue : commandQueues) {
        int headIndex = 0;
        for (CommandInfo cmd : new ArrayList<>(queue.commands)) {
            if (cmd.mergedBy == null && cmd.command instanceof DBECommandRename) {
                queue.commands.remove(cmd);
                queue.commands.add(headIndex++, cmd);
            }
        }
    }
    return commandQueues;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 7 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class DataImportHandler method adaptTransferNode.

@Override
protected IDataTransferNode adaptTransferNode(Object object) {
    final DBSDataManipulator adapted = RuntimeUtils.getObjectAdapter(object, DBSDataManipulator.class);
    if (adapted != null) {
        return new DatabaseTransferConsumer(adapted);
    } else {
        IFile file = RuntimeUtils.getObjectAdapter(object, IFile.class);
        if (file != null) {
            return getNodeByFile(file);
        }
        DBSObjectContainer objectContainer = RuntimeUtils.getObjectAdapter(object, DBSObjectContainer.class);
        if (objectContainer == null) {
            if (object instanceof DBSWrapper) {
                object = ((DBSWrapper) object).getObject();
            }
            if (object instanceof DBPObject) {
                object = DBUtils.getPublicObject((DBSObject) object);
            }
            if (object instanceof DBSObjectContainer) {
                objectContainer = (DBSObjectContainer) object;
            }
        }
        if (objectContainer != null) {
            if (isObjectContainerSupportsImport(objectContainer)) {
                return new DatabaseTransferConsumer(objectContainer);
            } else {
                DBWorkbench.getPlatformUI().showError("Wrong container", objectContainer.getName() + " doesn't support direct data import");
            }
        }
        return null;
    }
}
Also used : DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) DBPObject(org.jkiss.dbeaver.model.DBPObject) IFile(org.eclipse.core.resources.IFile)

Example 8 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class PropertySourceEditable method setPropertyValue.

@Override
@SuppressWarnings("unchecked")
public void setPropertyValue(@Nullable DBRProgressMonitor monitor, Object editableValue, ObjectPropertyDescriptor prop, Object newValue) throws IllegalArgumentException {
    if (prop.getValueTransformer() != null) {
        newValue = prop.getValueTransformer().transform(editableValue, newValue);
    }
    final Object oldValue = getPropertyValue(monitor, editableValue, prop, true);
    if (!updatePropertyValue(monitor, editableValue, prop, newValue, false)) {
        return;
    }
    if (commandContext != null) {
        if (lastCommand == null || lastCommand.getObject() != editableValue || lastCommand.property != prop || !commandContext.isDirty()) {
            // Last command is not applicable (check for isDirty because command queue might be reverted)
            final DBEObjectEditor<DBPObject> objectEditor = getObjectEditor(DBEObjectEditor.class);
            if (objectEditor == null) {
                log.error("Can't obtain object editor for " + getEditableValue());
                return;
            }
            final DBEPropertyHandler<DBPObject> propertyHandler = objectEditor.makePropertyHandler((DBPObject) editableValue, prop);
            PropertyChangeCommand curCommand = new PropertyChangeCommand((DBPObject) editableValue, prop, propertyHandler, oldValue, newValue);
            commandContext.addCommand(curCommand, commandReflector);
            lastCommand = curCommand;
        } else {
            lastCommand.setNewValue(newValue);
            commandContext.updateCommand(lastCommand, commandReflector);
        }
    }
    // To update name-based cache
    if (prop.isNameProperty() && editableValue instanceof DBSObject) {
        DBEObjectMaker objectManager = getObjectEditor(DBEObjectMaker.class);
        if (objectManager != null) {
            DBSObjectCache cache = objectManager.getObjectsCache((DBSObject) editableValue);
            if (cache != null && cache.isFullyCached()) {
                List<? extends DBSObject> cachedObjects = CommonUtils.copyList(cache.getCachedObjects());
                cache.setCache(cachedObjects);
            }
        }
    }
/*
        // Notify listeners
        for (IPropertySourceListener listener : listeners) {
            listener.handlePropertyChange(editableValue, prop, newValue);
        }
*/
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBSObjectCache(org.jkiss.dbeaver.model.struct.cache.DBSObjectCache) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject) DBPNamedObject(org.jkiss.dbeaver.model.DBPNamedObject) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker)

Example 9 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class SQLGeneratorContributor method getContributionItems.

// ////////////////////////////////////////////////////////
// Contributors
@Override
protected IContributionItem[] getContributionItems() {
    IWorkbenchPart part = UIUtils.getActiveWorkbenchWindow().getActivePage().getActivePart();
    IStructuredSelection structuredSelection = NavigatorUtils.getSelectionFromPart(part);
    if (structuredSelection == null || structuredSelection.isEmpty()) {
        return new IContributionItem[0];
    }
    List<IContributionItem> menu = new ArrayList<>();
    if (structuredSelection instanceof IResultSetSelection) {
        // Results
        makeResultSetContributions(menu, (IResultSetSelection) structuredSelection);
    } else {
        List<DBPObject> objects = new ArrayList<>();
        for (Object obj : structuredSelection.toList()) {
            DBSObject adaptedObject = GeneralUtils.adapt(obj, DBSObject.class);
            if (adaptedObject != null) {
                objects.add(adaptedObject);
            } else if (obj instanceof DBSWrapper) {
                objects.add(((DBSWrapper) obj).getObject());
            } else if (obj instanceof DBPObject) {
                objects.add((DBPObject) obj);
            }
        }
        List<SQLGeneratorDescriptor> generators = SQLGeneratorConfigurationRegistry.getInstance().getApplicableGenerators(objects, structuredSelection);
        int lastGrand = 0;
        for (SQLGeneratorDescriptor gen : generators) {
            int order = gen.getOrder();
            if (order > 0 && order / 1000 > lastGrand) {
                menu.add(new Separator());
            }
            lastGrand = order / 1000;
            menu.add(makeAction(gen.getLabel(), gen, objects));
        }
    }
    return menu.toArray(new IContributionItem[0]);
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject) DBSWrapper(org.jkiss.dbeaver.model.struct.DBSWrapper) ArrayList(java.util.ArrayList) IResultSetSelection(org.jkiss.dbeaver.ui.controls.resultset.IResultSetSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) SQLGeneratorDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLGeneratorDescriptor) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPObject(org.jkiss.dbeaver.model.DBPObject)

Example 10 with DBPObject

use of org.jkiss.dbeaver.model.DBPObject in project dbeaver by dbeaver.

the class AbstractCommandContext method getCommandQueues.

private List<CommandQueue> getCommandQueues() {
    if (commandQueues != null) {
        return commandQueues;
    }
    commandQueues = new ArrayList<>();
    CommandInfo aggregator = null;
    // Create queues from commands
    for (CommandInfo commandInfo : commands) {
        if (commandInfo.command instanceof DBECommandAggregator) {
            aggregator = commandInfo;
        }
        DBPObject object = commandInfo.command.getObject();
        CommandQueue queue = null;
        if (!commandQueues.isEmpty()) {
            for (CommandQueue tmpQueue : commandQueues) {
                if (tmpQueue.getObject() == object) {
                    queue = tmpQueue;
                    break;
                }
            }
        }
        if (queue == null) {
            DBEObjectManager<?> objectManager = executionContext.getDataSource().getContainer().getPlatform().getEditorsRegistry().getObjectManager(object.getClass());
            if (objectManager == null) {
                throw new IllegalStateException("Can't find object manager for '" + object.getClass().getName() + "'");
            }
            queue = new CommandQueue(objectManager, null, object);
            commandQueues.add(queue);
        }
        queue.addCommand(commandInfo);
    }
    // Merge commands
    for (CommandQueue queue : commandQueues) {
        final Map<DBECommand, CommandInfo> mergedByMap = new IdentityHashMap<>();
        final List<CommandInfo> mergedCommands = new ArrayList<>();
        for (int i = 0; i < queue.commands.size(); i++) {
            CommandInfo lastCommand = queue.commands.get(i);
            lastCommand.mergedBy = null;
            CommandInfo firstCommand = null;
            DBECommand<?> result = lastCommand.command;
            if (mergedCommands.isEmpty()) {
                result = lastCommand.command.merge(null, userParams);
            } else {
                boolean skipCommand = false;
                for (int k = mergedCommands.size(); k > 0; k--) {
                    firstCommand = mergedCommands.get(k - 1);
                    result = lastCommand.command.merge(firstCommand.command, userParams);
                    if (result == null) {
                        // Remove first and skip last command
                        mergedCommands.remove(firstCommand);
                        skipCommand = true;
                    } else if (result != lastCommand.command) {
                        break;
                    }
                }
                if (skipCommand) {
                    continue;
                }
            }
            mergedCommands.add(lastCommand);
            if (result == lastCommand.command) {
            // No changes
            // firstCommand.mergedBy = lastCommand;
            } else if (firstCommand != null && result == firstCommand.command) {
                // Remove last command from queue
                lastCommand.mergedBy = firstCommand;
            } else {
                // Some other command
                // May be it is some earlier command from queue or some new command (e.g. composite)
                CommandInfo mergedBy = mergedByMap.get(result);
                if (mergedBy == null) {
                    // Try to find in command stack
                    for (int k = i; k >= 0; k--) {
                        if (queue.commands.get(k).command == result) {
                            mergedBy = queue.commands.get(k);
                            break;
                        }
                    }
                    if (mergedBy == null) {
                        // Create new command info
                        mergedBy = new CommandInfo(result, null);
                    }
                    mergedByMap.put(result, mergedBy);
                }
                lastCommand.mergedBy = mergedBy;
                if (!mergedCommands.contains(mergedBy)) {
                    mergedCommands.add(mergedBy);
                }
            }
        }
        queue.commands = mergedCommands;
    }
    // Filter commands
    for (CommandQueue queue : commandQueues) {
        if (queue.objectManager instanceof DBECommandFilter) {
            ((DBECommandFilter) queue.objectManager).filterCommands(queue);
        }
    }
    // Aggregate commands
    if (aggregator != null) {
        ((DBECommandAggregator) aggregator.command).resetAggregatedCommands();
        for (CommandQueue queue : commandQueues) {
            for (CommandInfo cmd : queue.commands) {
                if (cmd.command != aggregator.command && cmd.mergedBy == null && ((DBECommandAggregator) aggregator.command).aggregateCommand(cmd.command)) {
                    cmd.mergedBy = aggregator;
                }
            }
        }
    }
    // Move rename commands in the head (#7512)
    for (CommandQueue queue : commandQueues) {
        int headIndex = 0;
        for (CommandInfo cmd : new ArrayList<>(queue.commands)) {
            if (cmd.mergedBy == null && cmd.command instanceof DBECommandRename) {
                queue.commands.remove(cmd);
                queue.commands.add(headIndex++, cmd);
            }
        }
    }
    return commandQueues;
}
Also used : DBPObject(org.jkiss.dbeaver.model.DBPObject)

Aggregations

DBPObject (org.jkiss.dbeaver.model.DBPObject)21 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)7 ArrayList (java.util.ArrayList)6 DBException (org.jkiss.dbeaver.DBException)5 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)5 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 DBSWrapper (org.jkiss.dbeaver.model.struct.DBSWrapper)4 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)3 DBPRefreshableObject (org.jkiss.dbeaver.model.DBPRefreshableObject)3 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 Action (org.eclipse.jface.action.Action)2 IContributionManager (org.eclipse.jface.action.IContributionManager)2 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2