Search in sources :

Example 91 with DBEPersistAction

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

the class OracleSequenceManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) {
    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)

Example 92 with DBEPersistAction

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

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 93 with DBEPersistAction

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

the class DBStructUtils method generateObjectDDL.

public static String generateObjectDDL(@NotNull DBRProgressMonitor monitor, @NotNull DBSObject object, Map<String, Object> options, boolean addComments) throws DBException {
    final DBERegistry editorsRegistry = object.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    final SQLObjectEditor entityEditor = editorsRegistry.getObjectManager(object.getClass(), SQLObjectEditor.class);
    if (entityEditor != null) {
        SQLObjectEditor.ObjectCreateCommand createCommand = entityEditor.makeCreateCommand(object, options);
        DBEPersistAction[] ddlActions = createCommand.getPersistActions(monitor, DBUtils.getDefaultContext(object, true), options);
        return SQLUtils.generateScript(object.getDataSource(), ddlActions, addComments);
    }
    log.debug("Object editor not found for " + object.getClass().getName());
    return SQLUtils.generateCommentLine(object.getDataSource(), "Can't generate DDL: object editor not found for " + object.getClass().getName());
}
Also used : DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLObjectEditor(org.jkiss.dbeaver.model.impl.sql.edit.SQLObjectEditor)

Example 94 with DBEPersistAction

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

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