Search in sources :

Example 1 with DBECommand

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

the class NavigatorHandlerObjectRename method renameDatabaseObject.

public static boolean renameDatabaseObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node, String newName) {
    try {
        if (node.getParentNode() instanceof DBNContainer) {
            final DBNContainer container = (DBNContainer) node.getParentNode();
            DBSObject object = node.getObject();
            if (object != null) {
                DBEObjectRenamer objectRenamer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectRenamer.class);
                if (objectRenamer != null) {
                    CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
                    objectRenamer.renameObject(commandTarget.getContext(), object, newName);
                    if (object.isPersisted() && commandTarget.getEditor() == null) {
                        if (!showScript(workbenchWindow, commandTarget.getContext(), "Rename script")) {
                            commandTarget.getContext().resetChanges();
                            return false;
                        } else {
                            ObjectSaver renamer = new ObjectSaver(commandTarget.getContext());
                            TasksJob.runTask("Rename object '" + object.getName() + "'", renamer);
                        }
                    } else {
                        for (DBECommand command : commandTarget.getContext().getFinalCommands()) {
                        //System.out.println(command);
                        }
                    }
                    return true;
                }
            }
        }
    } catch (Throwable e) {
        UIUtils.showErrorDialog(workbenchWindow.getShell(), "Rename object", "Can't rename object '" + node.getNodeName() + "'", e);
        return false;
    }
    return false;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBEObjectRenamer(org.jkiss.dbeaver.model.edit.DBEObjectRenamer)

Example 2 with DBECommand

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

the class NavigatorHandlerObjectBase method showScript.

protected static boolean showScript(IWorkbenchWindow workbenchWindow, DBECommandContext commandContext, String dialogTitle) {
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    StringBuilder script = new StringBuilder();
    for (DBECommand command : commands) {
        script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), command.getPersistActions(), false));
    }
    DatabaseNavigatorView view = UIUtils.findView(workbenchWindow, DatabaseNavigatorView.class);
    if (view != null) {
        ViewSQLDialog dialog = new ViewSQLDialog(view.getSite(), commandContext.getExecutionContext(), dialogTitle, UIIcon.SQL_PREVIEW, script.toString());
        dialog.setShowSaveButton(true);
        return dialog.open() == IDialogConstants.PROCEED_ID;
    } else {
        return false;
    }
}
Also used : DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DatabaseNavigatorView(org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorView) ViewSQLDialog(org.jkiss.dbeaver.ui.dialogs.sql.ViewSQLDialog)

Example 3 with DBECommand

use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by dbeaver.

the class EntityEditor method showChanges.

public int showChanges(boolean allowSave) {
    DBECommandContext commandContext = getCommandContext();
    if (commandContext == null) {
        return IDialogConstants.CANCEL_ID;
    }
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    if (CommonUtils.isEmpty(commands)) {
        return IDialogConstants.IGNORE_ID;
    }
    StringBuilder script = new StringBuilder();
    for (DBECommand command : commands) {
        try {
            command.validateCommand();
        } catch (final DBException e) {
            log.debug(e);
            DBeaverUI.syncExec(() -> DBUserInterface.getInstance().showError("Validation", e.getMessage()));
            return IDialogConstants.CANCEL_ID;
        }
        script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), command.getPersistActions(DBPScriptObject.EMPTY_OPTIONS), false));
    }
    if (script.length() == 0) {
        return IDialogConstants.PROCEED_ID;
    }
    ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
    DBeaverUI.syncExec(changesPreviewer);
    return changesPreviewer.getResult();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext)

Example 4 with DBECommand

use of org.jkiss.dbeaver.model.edit.DBECommand in project dbeaver by dbeaver.

the class NavigatorHandlerObjectRename method renameDatabaseObject.

public static boolean renameDatabaseObject(IWorkbenchWindow workbenchWindow, DBNDatabaseNode node, String newName) {
    try {
        if (node.getParentNode() instanceof DBNContainer) {
            final DBNContainer container = (DBNContainer) node.getParentNode();
            DBSObject object = node.getObject();
            if (object != null) {
                DBEObjectRenamer objectRenamer = EntityEditorsRegistry.getInstance().getObjectManager(object.getClass(), DBEObjectRenamer.class);
                if (objectRenamer != null) {
                    CommandTarget commandTarget = getCommandTarget(workbenchWindow, container, object.getClass(), false);
                    objectRenamer.renameObject(commandTarget.getContext(), object, newName);
                    if (object.isPersisted() && commandTarget.getEditor() == null) {
                        Map<String, Object> options = DBPScriptObject.EMPTY_OPTIONS;
                        if (!showScript(workbenchWindow, commandTarget.getContext(), options, "Rename script")) {
                            commandTarget.getContext().resetChanges();
                            return false;
                        } else {
                            ObjectSaver renamer = new ObjectSaver(commandTarget.getContext(), options);
                            TasksJob.runTask("Rename object '" + object.getName() + "'", renamer);
                        }
                    } else {
                        for (DBECommand command : commandTarget.getContext().getFinalCommands()) {
                        // System.out.println(command);
                        }
                    }
                    return true;
                }
            }
        }
    } catch (Throwable e) {
        DBUserInterface.getInstance().showError("Rename object", "Can't rename object '" + node.getNodeName() + "'", e);
        return false;
    }
    return false;
}
Also used : DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBNContainer(org.jkiss.dbeaver.model.navigator.DBNContainer) DBEObjectRenamer(org.jkiss.dbeaver.model.edit.DBEObjectRenamer) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject)

Example 5 with DBECommand

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

the class NavigatorObjectsDeleter method appendScript.

private void appendScript(final DBRProgressMonitor monitor, final StringBuilder sql, final DBNDatabaseNode node) throws InvocationTargetException {
    if (!(node.getParentNode() instanceof DBNContainer)) {
        return;
    }
    final DBSObject object = node.getObject();
    if (object == null) {
        return;
    }
    final DBEObjectMaker objectMaker = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectMaker.class);
    if (objectMaker == null) {
        return;
    }
    final boolean supportsCascade = (objectMaker.getMakerOptions(object.getDataSource()) & DBEObjectMaker.FEATURE_DELETE_CASCADE) != 0;
    final NavigatorHandlerObjectBase.CommandTarget commandTarget;
    try {
        commandTarget = NavigatorHandlerObjectBase.getCommandTarget(window, node.getParentNode(), object.getClass(), false);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    if (commandContext == null) {
        commandContext = commandTarget.getContext();
    }
    if (!object.isPersisted() || commandTarget.getEditor() != null) {
        return;
    }
    final Map<String, Object> deleteOptions;
    if (supportsCascade && deleteCascade) {
        deleteOptions = OPTIONS_CASCADE;
    } else {
        deleteOptions = Collections.emptyMap();
    }
    try {
        objectMaker.deleteObject(commandTarget.getContext(), node.getObject(), deleteOptions);
    } catch (DBException e) {
        log.warn(e);
        return;
    }
    final StringBuilder script = new StringBuilder();
    final DBECommandContext commandContext = commandTarget.getContext();
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    try {
        for (DBECommand command : commands) {
            final DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), deleteOptions);
            script.append(SQLUtils.generateScript(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            if (script.length() == 0) {
                script.append(SQLUtils.generateComments(commandContext.getExecutionContext().getDataSource(), persistActions, false));
            }
        }
    } catch (DBException e) {
        throw new InvocationTargetException(e);
    }
    commandTarget.getContext().resetChanges(true);
    if (sql.length() != 0) {
        sql.append("\n");
    }
    sql.append(script);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBEObjectMaker(org.jkiss.dbeaver.model.edit.DBEObjectMaker) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBECommandContext(org.jkiss.dbeaver.model.edit.DBECommandContext) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject)

Aggregations

DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)11 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)7 DBException (org.jkiss.dbeaver.DBException)5 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 DBEObjectRenamer (org.jkiss.dbeaver.model.edit.DBEObjectRenamer)3 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)3 DBNContainer (org.jkiss.dbeaver.model.navigator.DBNContainer)3 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)2 ViewSQLDialog (org.jkiss.dbeaver.ui.dialogs.sql.ViewSQLDialog)2 DatabaseNavigatorView (org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorView)2 Point (org.eclipse.swt.graphics.Point)1 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)1 DBCException (org.jkiss.dbeaver.model.exec.DBCException)1 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)1 DBECommandAdapter (org.jkiss.dbeaver.model.impl.edit.DBECommandAdapter)1 DBNNode (org.jkiss.dbeaver.model.navigator.DBNNode)1 EntityEditorDescriptor (org.jkiss.dbeaver.registry.editor.EntityEditorDescriptor)1 EntityEditorsRegistry (org.jkiss.dbeaver.registry.editor.EntityEditorsRegistry)1 UIServiceSQL (org.jkiss.dbeaver.runtime.ui.UIServiceSQL)1