Search in sources :

Example 11 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, Map<String, Object> options, String dialogTitle) {
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    StringBuilder script = new StringBuilder();
    try {
        UIUtils.runInProgressService(monitor -> {
            try {
                for (DBECommand command : commands) {
                    DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), options);
                    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);
            }
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Script generation error", "Error generating alter script", e.getTargetException());
    } catch (InterruptedException e) {
        return false;
    }
    if (script.length() > 0) {
        UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
        if (serviceSQL != null) {
            return serviceSQL.openSQLViewer(commandContext.getExecutionContext(), dialogTitle, UIIcon.SQL_PREVIEW, script.toString(), true, false) == IDialogConstants.PROCEED_ID;
        }
    } else {
        return UIUtils.confirmAction(workbenchWindow.getShell(), dialogTitle, "No SQL script available.\nAre you sure you want to proceed?");
    }
    return false;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 12 with DBECommand

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

the class NavigatorHandlerObjectBase method showScript.

protected static boolean showScript(IWorkbenchWindow workbenchWindow, DBECommandContext commandContext, Map<String, Object> options, String dialogTitle) {
    Collection<? extends DBECommand> commands = commandContext.getFinalCommands();
    StringBuilder script = new StringBuilder();
    try {
        UIUtils.runInProgressService(monitor -> {
            try {
                for (DBECommand command : commands) {
                    DBEPersistAction[] persistActions = command.getPersistActions(monitor, commandContext.getExecutionContext(), options);
                    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);
            }
        });
    } catch (InvocationTargetException e) {
        DBWorkbench.getPlatformUI().showError("Script generation error", "Error generating alter script", e.getTargetException());
    } catch (InterruptedException e) {
        return false;
    }
    if (script.length() > 0) {
        UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
        if (serviceSQL != null) {
            return serviceSQL.openSQLViewer(commandContext.getExecutionContext(), dialogTitle, UIIcon.SQL_PREVIEW, script.toString(), true, false) == IDialogConstants.PROCEED_ID;
        }
    } else {
        return UIUtils.confirmAction(workbenchWindow.getShell(), dialogTitle, "No SQL script available.\nAre you sure you want to proceed?");
    }
    return false;
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBECommand(org.jkiss.dbeaver.model.edit.DBECommand) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 13 with DBECommand

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

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)

Example 14 with DBECommand

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

the class NavigatorHandlerObjectBase method showScript.

protected static boolean showScript(IWorkbenchWindow workbenchWindow, DBECommandContext commandContext, Map<String, Object> options, 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(options), 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)

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