Search in sources :

Example 71 with DBEPersistAction

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

Example 72 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(monitor);
    if (ArrayUtils.isEmpty(compileActions)) {
        throw new DBCException("No compile actions associated with " + unit.getSourceType().name());
    }
    try (JDBCSession session = DBUtils.openUtilSession(monitor, unit, "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)) {
                    action.beforeExecute(session);
                    dbStat.executeStatement();
                }
                action.afterExecute(session, null);
            } catch (DBCException e) {
                action.afterExecute(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 73 with DBEPersistAction

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

the class JobRunHandler method runJob.

public static boolean runJob(DBRProgressMonitor monitor, DBCCompileLog compileLog, OracleSchedulerJob job) throws DBCException {
    final DBEPersistAction[] compileActions = job.getRunActions();
    if (ArrayUtils.isEmpty(compileActions)) {
        return true;
    }
    try (JDBCSession session = DBUtils.openUtilSession(monitor, job, "Run '" + job.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.SCRIPT, script, false, false, false)) {
                    action.beforeExecute(session);
                    dbStat.executeStatement();
                }
                action.afterExecute(session, null);
            } catch (DBCException e) {
                action.afterExecute(session, e);
                throw e;
            }
            if (action instanceof OracleObjectPersistAction) {
                if (!logObjectErrors(session, compileLog, job, ((OracleObjectPersistAction) action).getObjectType())) {
                    success = false;
                }
            }
        }
        final DBSObjectState oldState = job.getObjectState();
        job.refreshObjectState(monitor);
        if (job.getObjectState() != oldState) {
            job.getDataSource().getContainer().fireEvent(new DBPEvent(DBPEvent.Action.OBJECT_UPDATE, job));
        }
        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 74 with DBEPersistAction

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

the class PostgreViewBase method getObjectDefinitionText.

@Override
@Property(hidden = true, editable = true, updatable = true, order = -1)
public String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> options) throws DBException {
    if (source == null) {
        if (isPersisted()) {
            try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read view definition")) {
                // Do not use view id as a parameter. For some reason it doesn't work for Redshift
                String definition = JDBCUtils.queryString(session, "SELECT pg_get_viewdef(" + getObjectId() + ", true)");
                if (definition == null) {
                    throw new DBException("View '" + getName() + "' doesn't exist");
                }
                this.source = PostgreUtils.getViewDDL(monitor, this, definition);
                String extDefinition = readExtraDefinition(session, options);
                if (extDefinition != null) {
                    this.source += "\n" + extDefinition;
                }
            } catch (SQLException e) {
                throw new DBException("Error reading view definition: " + e.getMessage(), e);
            }
        } else {
            source = "";
        }
    }
    List<DBEPersistAction> actions = new ArrayList<>();
    if (CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS)) {
        if (getDescription() != null) {
            actions.add(new SQLDatabasePersistAction("Comment", "COMMENT ON " + getViewType() + " " + getFullyQualifiedName(DBPEvaluationContext.DDL) + " IS " + SQLUtils.quoteString(this, getDescription())));
        }
        for (PostgreTableColumn column : CommonUtils.safeCollection(getAttributes(monitor))) {
            if (!CommonUtils.isEmpty(column.getDescription())) {
                PostgreTableColumnManager.addColumnCommentAction(actions, column);
            }
        }
    }
    if (isPersisted() && CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
        PostgreUtils.getObjectGrantPermissionActions(monitor, this, actions, options);
    }
    StringBuilder ddl = new StringBuilder(source);
    if (!actions.isEmpty()) {
        ddl.append("\n\n").append(SQLUtils.generateScript(getDataSource(), actions.toArray(new DBEPersistAction[0]), false));
    }
    return ddl.toString();
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ArrayList(java.util.ArrayList) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) Property(org.jkiss.dbeaver.model.meta.Property)

Example 75 with DBEPersistAction

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

the class PostgreProcedure method getObjectDefinitionText.

@Override
@Property(hidden = true, editable = true, updatable = true, order = -1)
public String getObjectDefinitionText(DBRProgressMonitor monitor, Map<String, Object> options) throws DBException {
    String procDDL;
    boolean omitHeader = CommonUtils.getOption(options, OPTION_DEBUGGER_SOURCE);
    if (isPersisted() && (!getDataSource().getServerType().supportsFunctionDefRead() || omitHeader)) {
        if (procSrc == null) {
            try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read procedure body")) {
                procSrc = JDBCUtils.queryString(session, "SELECT prosrc FROM pg_proc where oid = ?", getObjectId());
            } catch (SQLException e) {
                throw new DBException("Error reading procedure body", e);
            }
        }
        PostgreDataType returnType = getReturnType();
        String returnTypeName = returnType == null ? null : returnType.getFullTypeName();
        procDDL = omitHeader ? procSrc : generateFunctionDeclaration(getLanguage(monitor), returnTypeName, procSrc);
    } else {
        if (body == null) {
            if (!isPersisted()) {
                PostgreDataType returnType = getReturnType();
                String returnTypeName = returnType == null ? null : returnType.getFullTypeName();
                body = generateFunctionDeclaration(getLanguage(monitor), returnTypeName, "\n\t-- Enter function body here\n");
            } else if (oid == 0 || isAggregate) {
                // No OID so let's use old (bad) way
                body = this.procSrc;
            } else {
                if (isAggregate) {
                    body = "-- Aggregate function";
                } else {
                    try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read procedure body")) {
                        body = JDBCUtils.queryString(session, "SELECT pg_get_functiondef(" + getObjectId() + ")");
                    } catch (SQLException e) {
                        if (!CommonUtils.isEmpty(this.procSrc)) {
                            log.debug("Error reading procedure body", e);
                            // At least we have it
                            body = this.procSrc;
                        } else {
                            throw new DBException("Error reading procedure body", e);
                        }
                    }
                }
            }
        }
        procDDL = body;
    }
    if (this.isPersisted() && !omitHeader) {
        procDDL += ";\n";
        if (CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS) && !CommonUtils.isEmpty(getDescription())) {
            procDDL += "\nCOMMENT ON " + getProcedureTypeName() + " " + getFullQualifiedSignature() + " IS " + SQLUtils.quoteString(this, getDescription()) + ";\n";
        }
        if (CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_PERMISSIONS)) {
            List<DBEPersistAction> actions = new ArrayList<>();
            PostgreUtils.getObjectGrantPermissionActions(monitor, this, actions, options);
            procDDL += "\n" + SQLUtils.generateScript(getDataSource(), actions.toArray(new DBEPersistAction[0]), false);
        }
    }
    return procDDL;
}
Also used : JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) Property(org.jkiss.dbeaver.model.meta.Property)

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