Search in sources :

Example 1 with SQLDatabasePersistActionAtomic

use of org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionAtomic in project dbeaver by dbeaver.

the class PostgreDatabaseManager method addObjectCreateActions.

@Override
protected void addObjectCreateActions(List<DBEPersistAction> actions, ObjectCreateCommand command, Map<String, Object> options) {
    final PostgreDatabase database = command.getObject();
    StringBuilder sql = new StringBuilder();
    sql.append("CREATE DATABASE ").append(DBUtils.getQuotedIdentifier(database));
    try {
        VoidProgressMonitor monitor = new VoidProgressMonitor();
        if (database.getDBA(monitor) != null) {
            sql.append("\nOWNER = ").append(database.getDBA(monitor).getName());
        }
        if (!CommonUtils.isEmpty(database.getTemplateName())) {
            sql.append("\nTEMPLATE = ").append(database.getTemplateName());
        }
        if (database.getDefaultEncoding(monitor) != null) {
            sql.append("\nENCODING = '").append(database.getDefaultEncoding(monitor).getName()).append("'");
        }
        if (database.getDefaultTablespace(monitor) != null) {
            sql.append("\nTABLESPACE = ").append(database.getDefaultTablespace(monitor).getName());
        }
    } catch (DBException e) {
        log.error(e);
    }
    actions.add(new SQLDatabasePersistActionAtomic("Create database", sql.toString()));
}
Also used : DBException(org.jkiss.dbeaver.DBException) PostgreDatabase(org.jkiss.dbeaver.ext.postgresql.model.PostgreDatabase) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor) SQLDatabasePersistActionAtomic(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionAtomic)

Example 2 with SQLDatabasePersistActionAtomic

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

the class PostgreTableColumnManager method addObjectModifyActions.

@Override
protected void addObjectModifyActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options) {
    final PostgreAttribute column = command.getObject();
    boolean isAtomic = column.getDataSource().getServerType().isAlterTableAtomic();
    // PostgreSQL can't perform all changes by one query
    // ALTER [ COLUMN ] column [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
    // ALTER [ COLUMN ] column SET DEFAULT expression
    // ALTER [ COLUMN ] column DROP DEFAULT
    // ALTER [ COLUMN ] column { SET | DROP } NOT NULL
    // ALTER [ COLUMN ] column SET STATISTICS integer
    // ALTER [ COLUMN ] column SET ( attribute_option = value [, ... ] )
    // ALTER [ COLUMN ] column RESET ( attribute_option [, ... ] )
    // ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    String prefix = "ALTER TABLE " + DBUtils.getObjectFullName(column.getTable(), DBPEvaluationContext.DDL) + " ALTER COLUMN " + DBUtils.getQuotedIdentifier(column) + " ";
    String typeClause = column.getFullTypeName();
    if (column.getDataSource().isServerVersionAtLeast(8, 0) && column.getDataType() != null) {
        typeClause += " USING " + DBUtils.getQuotedIdentifier(column) + "::" + column.getDataType().getName();
    }
    if (command.hasProperty(DBConstants.PROP_ID_DATA_TYPE) || command.hasProperty("maxLength") || command.hasProperty("precision") || command.hasProperty("scale")) {
        actionList.add(new SQLDatabasePersistActionAtomic("Set column type", prefix + "TYPE " + typeClause, isAtomic));
    }
    if (command.hasProperty(DBConstants.PROP_ID_REQUIRED)) {
        actionList.add(new SQLDatabasePersistActionAtomic("Set column nullability", prefix + (column.isRequired() ? "SET" : "DROP") + " NOT NULL", isAtomic));
    }
    if (command.hasProperty(DBConstants.PROP_ID_DEFAULT_VALUE)) {
        if (CommonUtils.isEmpty(column.getDefaultValue())) {
            actionList.add(new SQLDatabasePersistActionAtomic("Drop column default", prefix + "DROP DEFAULT", isAtomic));
        } else {
            actionList.add(new SQLDatabasePersistActionAtomic("Set column default", prefix + "SET DEFAULT " + column.getDefaultValue(), isAtomic));
        }
    }
    if (command.getProperty(DBConstants.PROP_ID_DESCRIPTION) != null) {
        addColumnCommentAction(actionList, column);
    }
}
Also used : SQLDatabasePersistActionAtomic(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionAtomic)

Aggregations

SQLDatabasePersistActionAtomic (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionAtomic)2 DBException (org.jkiss.dbeaver.DBException)1 PostgreDatabase (org.jkiss.dbeaver.ext.postgresql.model.PostgreDatabase)1 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)1