Search in sources :

Example 46 with DBEPersistAction

use of org.jkiss.dbeaver.model.edit.DBEPersistAction 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 47 with DBEPersistAction

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

the class DB2TableColumnManager method addObjectModifyActions.

// -----
// Alter
// -----
@Override
protected void addObjectModifyActions(List<DBEPersistAction> actionList, ObjectChangeCommand command) {
    DB2TableColumn db2Column = command.getObject();
    boolean hasColumnChanges = false;
    if (!command.getProperties().isEmpty()) {
        final String deltaSQL = computeDeltaSQL(command);
        if (!deltaSQL.isEmpty()) {
            hasColumnChanges = true;
            String sqlAlterColumn = String.format(SQL_ALTER, db2Column.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL), deltaSQL);
            actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sqlAlterColumn));
        }
    }
    // Comment
    DBEPersistAction commentAction = buildCommentAction(db2Column);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
    if (hasColumnChanges) {
        // Be Safe, Add a reorg action
        actionList.add(buildReorgAction(db2Column));
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DB2TableColumn(org.jkiss.dbeaver.ext.db2.model.DB2TableColumn) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 48 with DBEPersistAction

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

the class DB2TableManager method addObjectModifyActions.

// ------
// Alter
// ------
@Override
public void addObjectModifyActions(List<DBEPersistAction> actionList, ObjectChangeCommand command) {
    DB2Table db2Table = command.getObject();
    if (command.getProperties().size() > 1) {
        StringBuilder sb = new StringBuilder(128);
        sb.append(SQL_ALTER);
        sb.append(db2Table.getFullyQualifiedName(DBPEvaluationContext.DDL));
        sb.append(" ");
        appendTableModifiers(command.getObject(), command, sb);
        actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sb.toString()));
    }
    DBEPersistAction commentAction = buildCommentAction(db2Table);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 49 with DBEPersistAction

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

the class CompileHandler method compileUnit.

public static boolean compileUnit(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSourceObject unit) throws DBCException {
    final DBEPersistAction[] compileActions = unit.getCompileActions();
    if (ArrayUtils.isEmpty(compileActions)) {
        return true;
    }
    try (JDBCSession session = DBUtils.openUtilSession(monitor, unit.getDataSource(), "Compile '" + unit.getName() + "'")) {
        boolean success = true;
        for (DBEPersistAction action : compileActions) {
            final String script = action.getScript();
            compileLog.trace(script);
            if (monitor.isCanceled()) {
                break;
            }
            try {
                try (DBCStatement dbStat = session.prepareStatement(DBCStatementType.QUERY, script, false, false, false)) {
                    dbStat.executeStatement();
                }
                action.handleExecute(session, null);
            } catch (DBCException e) {
                action.handleExecute(session, e);
                throw e;
            }
            if (action instanceof OracleObjectPersistAction) {
                if (!logObjectErrors(session, compileLog, unit, ((OracleObjectPersistAction) action).getObjectType())) {
                    success = false;
                }
            }
        }
        final DBSObjectState oldState = unit.getObjectState();
        unit.refreshObjectState(monitor);
        if (unit.getObjectState() != oldState) {
            unit.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, unit));
        }
        return success;
    }
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBSObjectState(org.jkiss.dbeaver.model.struct.DBSObjectState) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBCException(org.jkiss.dbeaver.model.exec.DBCException) DBPEvent(org.jkiss.dbeaver.model.DBPEvent) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) OracleObjectPersistAction(org.jkiss.dbeaver.ext.oracle.model.OracleObjectPersistAction)

Example 50 with DBEPersistAction

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

the class OracleSequenceManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(List<DBEPersistAction> actions, ObjectDeleteCommand command) {
    String sql = "DROP SEQUENCE " + command.getObject().getFullyQualifiedName(DBPEvaluationContext.DDL);
    DBEPersistAction action = new SQLDatabasePersistAction("Drop Sequence", sql);
    actions.add(action);
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Aggregations

DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)94 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)44 DBException (org.jkiss.dbeaver.DBException)24 InvocationTargetException (java.lang.reflect.InvocationTargetException)16 ArrayList (java.util.ArrayList)14 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)10 UIServiceSQL (org.jkiss.dbeaver.runtime.ui.UIServiceSQL)10 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)8 OracleObjectPersistAction (org.jkiss.dbeaver.ext.oracle.model.OracleObjectPersistAction)6 DBPEvent (org.jkiss.dbeaver.model.DBPEvent)6 DBECommand (org.jkiss.dbeaver.model.edit.DBECommand)6 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)6 DBCException (org.jkiss.dbeaver.model.exec.DBCException)6 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)6 DBCStatement (org.jkiss.dbeaver.model.exec.DBCStatement)6 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)6 SQLObjectEditor (org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)6 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)6 DBSObjectState (org.jkiss.dbeaver.model.struct.DBSObjectState)6 SQLException (java.sql.SQLException)4