Search in sources :

Example 16 with DBEPersistAction

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

the class ExasolTableColumnManager method addObjectModifyActions.

// -----
// Alter
// -----
@Override
protected void addObjectModifyActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options) {
    ExasolTableColumn exasolColumn = command.getObject();
    Map<Object, Object> props = command.getProperties();
    if (props.containsKey("defaultValue") || props.containsKey("dataType") || props.containsKey("scale") || props.containsKey("maxLength") || props.containsKey("autoGenerated") || props.containsKey("identityValue") || props.containsKey("required")) {
        // build nullability string
        String nullability = "";
        if (exasolColumn.isOriRequired() != null && exasolColumn.isOriRequired() != exasolColumn.isRequired())
            nullability = exasolColumn.isRequired() ? "NOT NULL" : "NULL";
        final String deltaSQL = DBUtils.getQuotedIdentifier(exasolColumn) + " " + exasolColumn.getFormatType() + " " + (exasolColumn.getDefaultValue() == null ? "" : " DEFAULT " + exasolColumn.getDefaultValue()) + " " + formatIdentiy(exasolColumn.isAutoGenerated(), exasolColumn.getIdentityValue()) + " " + nullability;
        if (!deltaSQL.isEmpty()) {
            String sqlAlterColumn = String.format(SQL_ALTER, exasolColumn.getTable().getFullyQualifiedName(DBPEvaluationContext.DDL), deltaSQL);
            actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sqlAlterColumn));
        }
    }
    // Comment
    DBEPersistAction commentAction = buildCommentAction(exasolColumn);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
    if (command.getProperties().containsKey("distKey")) {
        try {
            actionList.addAll(modifyDistKey(monitor, exasolColumn));
        } catch (DBException e) {
            log.error("Failed to modify distkey settings", e);
        }
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ExasolTableColumn(org.jkiss.dbeaver.ext.exasol.model.ExasolTableColumn) DBSObject(org.jkiss.dbeaver.model.struct.DBSObject) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 17 with DBEPersistAction

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

the class ExasolTableManager method addObjectModifyActions.

// ------
// Alter
// ------
@Override
public void addObjectModifyActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actionList, ObjectChangeCommand command, Map<String, Object> options) {
    ExasolTable exasolTable = command.getObject();
    if (command.getProperties().size() > 0) {
        if (command.getProperties().containsKey("hasPartitionKey") && ((command.getProperties().get("hasPartitionKey").toString()).equals("false"))) {
            actionList.add(new SQLDatabasePersistAction("ALTER TABLE " + exasolTable.getFullyQualifiedName(DBPEvaluationContext.DDL) + " DROP PARTITION KEYS"));
        } else if (command.getProperties().size() > 1) {
            StringBuilder sb = new StringBuilder(128);
            sb.append(SQL_ALTER);
            sb.append(exasolTable.getFullyQualifiedName(DBPEvaluationContext.DDL));
            sb.append(" ");
            appendTableModifiers(monitor, command.getObject(), command, sb, true);
            actionList.add(new SQLDatabasePersistAction(CMD_ALTER, sb.toString()));
        }
    }
    DBEPersistAction commentAction = buildCommentAction(exasolTable);
    if (commentAction != null) {
        actionList.add(commentAction);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 18 with DBEPersistAction

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

the class SQLUtils method generateComments.

@NotNull
public static String generateComments(DBPDataSource dataSource, DBEPersistAction[] persistActions, boolean addComments) {
    final SQLDialect sqlDialect = SQLUtils.getDialectFromDataSource(dataSource);
    final String lineSeparator = GeneralUtils.getDefaultLineSeparator();
    StringBuilder script = new StringBuilder(64);
    if (addComments) {
        script.append(DBEAVER_DDL_COMMENT).append(Platform.getProduct().getName()).append(lineSeparator).append(DBEAVER_DDL_WARNING).append(lineSeparator);
    }
    if (persistActions != null) {
        String slComment;
        String[] slComments = sqlDialect.getSingleLineComments();
        if (ArrayUtils.isEmpty(slComments)) {
            slComment = "--";
        } else {
            slComment = slComments[0];
        }
        for (DBEPersistAction action : persistActions) {
            if (action.getType() != DBEPersistAction.ActionType.COMMENT) {
                String scriptLine = action.getTitle();
                if (CommonUtils.isEmpty(scriptLine)) {
                    continue;
                }
                script.append(slComment).append(" ").append(scriptLine);
            } else {
                String scriptLine = action.getScript();
                if (CommonUtils.isEmpty(scriptLine)) {
                    continue;
                }
                script.append(scriptLine);
            }
            script.append(lineSeparator);
        }
    }
    return script.toString();
}
Also used : BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) NotNull(org.jkiss.code.NotNull)

Example 19 with DBEPersistAction

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

the class SQLUtils method generateScript.

@NotNull
public static String generateScript(DBPDataSource dataSource, DBEPersistAction[] persistActions, boolean addComments) {
    final SQLDialect sqlDialect = SQLUtils.getDialectFromDataSource(dataSource);
    final String lineSeparator = GeneralUtils.getDefaultLineSeparator();
    StringBuilder script = new StringBuilder(64);
    if (addComments) {
        script.append(DBEAVER_DDL_COMMENT).append(Platform.getProduct().getName()).append(lineSeparator).append(DBEAVER_DDL_WARNING).append(lineSeparator);
    }
    if (persistActions != null) {
        String redefiner = sqlDialect.getScriptDelimiterRedefiner();
        for (DBEPersistAction action : persistActions) {
            String scriptLine = action.getScript();
            if (CommonUtils.isEmpty(scriptLine)) {
                continue;
            }
            String delimiter = getScriptLineDelimiter(sqlDialect);
            if (action.isComplex() && redefiner != null) {
                script.append(lineSeparator).append(redefiner).append(" ").append(DBEAVER_SCRIPT_DELIMITER).append(lineSeparator);
                delimiter = DBEAVER_SCRIPT_DELIMITER;
                script.append(delimiter).append(lineSeparator);
            } else if (action.getType() == DBEPersistAction.ActionType.COMMENT) {
                if (script.length() > 2) {
                    int lfCount = 0;
                    for (int i = script.length() - 1; i >= 0; i--) {
                        if (!Character.isWhitespace(script.charAt(i))) {
                            break;
                        }
                        if (script.charAt(i) == '\n')
                            lfCount++;
                    }
                    if (lfCount < 2) {
                        // Add line feed if we do not have empty line before
                        script.append(lineSeparator);
                    }
                }
            }
            script.append(scriptLine);
            if (action.getType() != DBEPersistAction.ActionType.COMMENT) {
                String testLine = scriptLine.trim();
                if (testLine.lastIndexOf(delimiter) != (testLine.length() - delimiter.length())) {
                    script.append(delimiter);
                }
            } else {
                script.append(lineSeparator);
            }
            script.append(lineSeparator);
            if (action.isComplex() && redefiner != null) {
                script.append(redefiner).append(" ").append(getScriptLineDelimiter(sqlDialect)).append(lineSeparator);
            }
        }
    }
    return script.toString();
}
Also used : BasicSQLDialect(org.jkiss.dbeaver.model.impl.sql.BasicSQLDialect) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) NotNull(org.jkiss.code.NotNull)

Example 20 with DBEPersistAction

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

the class DB2SchemaManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) {
    String schemaName = command.getObject().getName();
    DBEPersistAction action = new SQLDatabasePersistAction("Drop schema (SQL)", String.format(SQL_DROP_SCHEMA, DBUtils.getQuotedIdentifier(command.getObject())));
    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