Search in sources :

Example 6 with DBECommand

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

the class EntityEditor method saveCommandContext.

private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
    monitor.beginTask("Save entity", 1);
    Throwable error = null;
    final DBECommandContext commandContext = getCommandContext();
    if (commandContext == null) {
        log.warn("Null command context");
        return true;
    }
    DBCExecutionContext executionContext = getExecutionContext();
    if (executionContext == null) {
        log.warn("Null execution context");
        return true;
    }
    boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
    if (!isNewObject) {
        // Check for any new nested objects
        for (DBECommand cmd : commandContext.getFinalCommands()) {
            if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
                isNewObject = true;
                break;
            }
        }
    }
    try {
        DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
            try {
                commandContext.saveChanges(monitor, options);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (DBException e) {
        error = e;
    }
    if (getDatabaseObject() instanceof DBPStatefulObject) {
        try {
            ((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
        } catch (DBCException e) {
            // Just report an error
            log.error(e);
        }
    }
    if (error == null) {
        // Refresh underlying node
        // It'll refresh database object and all it's descendants
        // So we'll get actual data from database
        final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
        try {
            boolean doRefresh = isNewObject;
            UIUtils.runInProgressService(monitor1 -> {
                try {
                    treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            });
        } catch (InvocationTargetException e) {
            error = e.getTargetException();
        } catch (InterruptedException e) {
        // ok
        }
    }
    monitor.done();
    if (error == null) {
        return true;
    } else {
        // Try to handle error in nested editors
        final Throwable vError = error;
        UIUtils.syncExec(() -> {
            final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
            if (errorVisualizer != null) {
                errorVisualizer.visualizeError(monitor, vError);
            }
        });
        // Show error dialog
        UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
        return false;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) 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)

Example 7 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) {
            DBSObject object = node.getObject();
            if (object != null) {
                DBEObjectRenamer objectRenamer = DBWorkbench.getPlatform().getEditorsRegistry().getObjectManager(object.getClass(), DBEObjectRenamer.class);
                if (objectRenamer != null) {
                    CommandTarget commandTarget = getCommandTarget(workbenchWindow, node.getParentNode(), 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(true);
                            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) {
        DBWorkbench.getPlatformUI().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 8 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();
    try {
        saveInProgress = true;
        UIUtils.runInProgressService(monitor -> {
            monitor.beginTask("Generate SQL script", commands.size());
            Map<String, Object> validateOptions = new HashMap<>();
            for (DBECommand command : commands) {
                monitor.subTask(command.getTitle());
                try {
                    command.validateCommand(monitor, validateOptions);
                } catch (final DBException e) {
                    throw new InvocationTargetException(e);
                }
                Map<String, Object> options = new HashMap<>();
                options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
                DBPDataSource dataSource = getDatabaseObject().getDataSource();
                try {
                    DBEPersistAction[] persistActions = command.getPersistActions(monitor, getExecutionContext(), options);
                    script.append(SQLUtils.generateScript(dataSource, persistActions, false));
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
                monitor.worked(1);
            }
            monitor.done();
        });
    } catch (InterruptedException e) {
        return IDialogConstants.CANCEL_ID;
    } catch (InvocationTargetException e) {
        log.error(e);
        DBWorkbench.getPlatformUI().showError("Script generate error", "Couldn't generate alter script", e.getTargetException());
        return IDialogConstants.CANCEL_ID;
    } finally {
        saveInProgress = false;
    }
    if (script.length() == 0) {
        return IDialogConstants.PROCEED_ID;
    }
    ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
    UIUtils.syncExec(changesPreviewer);
    return changesPreviewer.getResult();
}
Also used : DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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)

Example 9 with DBECommand

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

the class EntityEditor method saveCommandContext.

private boolean saveCommandContext(final DBRProgressMonitor monitor, Map<String, Object> options) {
    monitor.beginTask("Save entity", 1);
    Throwable error = null;
    final DBECommandContext commandContext = getCommandContext();
    if (commandContext == null) {
        log.warn("Null command context");
        return true;
    }
    DBCExecutionContext executionContext = getExecutionContext();
    if (executionContext == null) {
        log.warn("Null execution context");
        return true;
    }
    boolean isNewObject = getDatabaseObject() == null || !getDatabaseObject().isPersisted();
    if (!isNewObject) {
        // Check for any new nested objects
        for (DBECommand cmd : commandContext.getFinalCommands()) {
            if (cmd.getObject() instanceof DBSObject && !((DBSObject) cmd.getObject()).isPersisted()) {
                isNewObject = true;
                break;
            }
        }
    }
    try {
        DBExecUtils.tryExecuteRecover(monitor, executionContext.getDataSource(), param -> {
            try {
                commandContext.saveChanges(monitor, options);
            } catch (DBException e) {
                throw new InvocationTargetException(e);
            }
        });
    } catch (DBException e) {
        error = e;
    }
    if (getDatabaseObject() instanceof DBPStatefulObject) {
        try {
            ((DBPStatefulObject) getDatabaseObject()).refreshObjectState(monitor);
        } catch (DBCException e) {
            // Just report an error
            log.error(e);
        }
    }
    if (error == null) {
        // Refresh underlying node
        // It'll refresh database object and all it's descendants
        // So we'll get actual data from database
        final DBNDatabaseNode treeNode = getEditorInput().getNavigatorNode();
        try {
            boolean doRefresh = isNewObject;
            UIUtils.runInProgressService(monitor1 -> {
                try {
                    treeNode.refreshNode(monitor1, doRefresh ? DBNEvent.FORCE_REFRESH : DBNEvent.UPDATE_ON_SAVE);
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
            });
        } catch (InvocationTargetException e) {
            error = e.getTargetException();
        } catch (InterruptedException e) {
        // ok
        }
    }
    monitor.done();
    if (error == null) {
        return true;
    } else {
        // Try to handle error in nested editors
        final Throwable vError = error;
        UIUtils.syncExec(() -> {
            final IErrorVisualizer errorVisualizer = getAdapter(IErrorVisualizer.class);
            if (errorVisualizer != null) {
                errorVisualizer.visualizeError(monitor, vError);
            }
        });
        // Show error dialog
        UIUtils.asyncExec(() -> DBWorkbench.getPlatformUI().showError("Can't save '" + getDatabaseObject().getName() + "'", null, vError));
        return false;
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBCException(org.jkiss.dbeaver.model.exec.DBCException) 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)

Example 10 with DBECommand

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

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();
    try {
        saveInProgress = true;
        UIUtils.runInProgressService(monitor -> {
            monitor.beginTask("Generate SQL script", commands.size());
            Map<String, Object> validateOptions = new HashMap<>();
            for (DBECommand command : commands) {
                monitor.subTask(command.getTitle());
                try {
                    command.validateCommand(monitor, validateOptions);
                } catch (final DBException e) {
                    throw new InvocationTargetException(e);
                }
                Map<String, Object> options = new HashMap<>();
                options.put(DBPScriptObject.OPTION_OBJECT_SAVE, true);
                DBPDataSource dataSource = getDatabaseObject().getDataSource();
                try {
                    DBEPersistAction[] persistActions = command.getPersistActions(monitor, getExecutionContext(), options);
                    script.append(SQLUtils.generateScript(dataSource, persistActions, false));
                } catch (DBException e) {
                    throw new InvocationTargetException(e);
                }
                monitor.worked(1);
            }
            monitor.done();
        });
    } catch (InterruptedException e) {
        return IDialogConstants.CANCEL_ID;
    } catch (InvocationTargetException e) {
        log.error(e);
        DBWorkbench.getPlatformUI().showError("Script generate error", "Couldn't generate alter script", e.getTargetException());
        return IDialogConstants.CANCEL_ID;
    } finally {
        saveInProgress = false;
    }
    if (script.length() == 0) {
        return IDialogConstants.PROCEED_ID;
    }
    ChangesPreviewer changesPreviewer = new ChangesPreviewer(script, allowSave);
    UIUtils.syncExec(changesPreviewer);
    return changesPreviewer.getResult();
}
Also used : DBException(org.jkiss.dbeaver.DBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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)14 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 DBException (org.jkiss.dbeaver.DBException)8 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)6 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)6 DBEObjectRenamer (org.jkiss.dbeaver.model.edit.DBEObjectRenamer)4 DBNContainer (org.jkiss.dbeaver.model.navigator.DBNContainer)4 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)3 DBEObjectMaker (org.jkiss.dbeaver.model.edit.DBEObjectMaker)2 DBCException (org.jkiss.dbeaver.model.exec.DBCException)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)2 UIServiceSQL (org.jkiss.dbeaver.runtime.ui.UIServiceSQL)2 ViewSQLDialog (org.jkiss.dbeaver.ui.dialogs.sql.ViewSQLDialog)2 DatabaseNavigatorView (org.jkiss.dbeaver.ui.navigator.database.DatabaseNavigatorView)2