Search in sources :

Example 1 with SQLDatabasePersistActionComment

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

the class PostgreTableManagerBase method addObjectExtraActions.

protected void addObjectExtraActions(List<DBEPersistAction> actions, NestedObjectCommand<PostgreTableBase, PropertyHandler> command, Map<String, Object> options) {
    boolean isDDL = CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SOURCE);
    PostgreTableBase table = command.getObject();
    // Add comments
    if ((!table.isPersisted() || command.getProperty(DBConstants.PROP_ID_DESCRIPTION) != null) && table.getDescription() != null) {
        actions.add(new SQLDatabasePersistAction("Comment table", "COMMENT ON " + (table.isView() ? "VIEW" : "TABLE") + " " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + " IS " + SQLUtils.quoteString(table, table.getDescription())));
    }
    DBRProgressMonitor monitor = new VoidProgressMonitor();
    if (isDDL) {
        try {
            {
                // Column comments
                boolean hasComments = false;
                for (PostgreTableColumn column : table.getAttributes(monitor)) {
                    if (!CommonUtils.isEmpty(column.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Column comments"));
                        }
                        PostgreTableColumnManager.addColumnCommentAction(actions, column);
                        hasComments = true;
                    }
                }
            }
            {
                // Constraint comments
                boolean hasComments = false;
                for (PostgreTableConstraintBase constr : table.getConstraints(monitor)) {
                    if (!CommonUtils.isEmpty(constr.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Constraint comments"));
                        }
                        PostgreConstraintManager.addConstraintCommentAction(actions, constr);
                        hasComments = true;
                    }
                }
                for (DBSEntityAssociation fk : table.getAssociations(monitor)) {
                    if (fk instanceof PostgreTableForeignKey && !CommonUtils.isEmpty(fk.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Foreign key comments"));
                        }
                        PostgreConstraintManager.addConstraintCommentAction(actions, (PostgreTableForeignKey) fk);
                        hasComments = true;
                    }
                }
            }
            // Triggers
            if (table instanceof PostgreTableReal) {
                Collection<PostgreTrigger> triggers = ((PostgreTableReal) table).getTriggers(monitor);
                if (!CommonUtils.isEmpty(triggers)) {
                    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Triggers"));
                    for (PostgreTrigger trigger : triggers) {
                        actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getObjectDefinitionText(monitor, options)));
                    }
                }
            }
            getTableGrantPermissionActions(monitor, table, actions, options);
        } catch (DBException e) {
            log.error(e);
        }
    }
}
Also used : SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 2 with SQLDatabasePersistActionComment

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

the class SQLiteTableColumnManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) throws DBException {
    final GenericTableColumn column = command.getObject();
    final GenericTableBase table = column.getTable();
    final List<? extends GenericTableColumn> attributes = table.getAttributes(monitor);
    if (CommonUtils.isEmpty(attributes)) {
        throw new DBException("Table was deleted");
    }
    final String tableColumns = attributes.stream().filter(x -> !x.getName().equals(column.getName()) && x.isPersisted()).map(DBUtils::getQuotedIdentifier).collect(Collectors.joining(",\n  "));
    final String tableName = DBUtils.getQuotedIdentifier(table);
    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop column " + DBUtils.getQuotedIdentifier(column)));
    actions.add(new SQLDatabasePersistAction("Create temporary table from original table", "CREATE TEMPORARY TABLE temp AS\nSELECT\n  " + tableColumns + "\nFROM " + tableName));
    actions.add(new SQLDatabasePersistAction("Drop original table", "\nDROP TABLE " + tableName + ";\n"));
    actions.add(new SQLDatabasePersistAction("Create new table", DBStructUtils.generateTableDDL(monitor, table, Collections.emptyMap(), false)));
    actions.add(new SQLDatabasePersistAction("Insert values from temporary table to new table", "INSERT INTO " + tableName + "\n (" + tableColumns + ")\nSELECT\n  " + tableColumns + "\nFROM temp"));
    actions.add(new SQLDatabasePersistAction("Drop temporary table", "\nDROP TABLE temp"));
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) GenericTableColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Example 3 with SQLDatabasePersistActionComment

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

the class PostgreTableManagerBase method addObjectExtraActions.

@Override
protected void addObjectExtraActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, NestedObjectCommand<PostgreTableBase, PropertyHandler> command, Map<String, Object> options) {
    boolean isDDL = CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SOURCE);
    PostgreTableBase table = command.getObject();
    // Add comments
    String comment;
    if (!table.isPersisted()) {
        Object descProp = command.getProperty(DBConstants.PROP_ID_DESCRIPTION);
        comment = descProp != null ? descProp.toString() : null;
    } else {
        comment = table.getDescription();
    }
    boolean showComments = CommonUtils.getOption(options, DBPScriptObject.OPTION_INCLUDE_COMMENTS) || CommonUtils.getOption(options, DBPScriptObject.OPTION_OBJECT_SAVE);
    if (showComments && !CommonUtils.isEmpty(comment)) {
        actions.add(new SQLDatabasePersistAction("Comment table", "COMMENT ON " + (table.isView() ? ((PostgreViewBase) table).getViewType() : "TABLE") + " " + table.getFullyQualifiedName(DBPEvaluationContext.DDL) + " IS " + SQLUtils.quoteString(table, CommonUtils.notEmpty(comment))));
    }
    if (isDDL || !table.isPersisted()) {
        // show comment commands for DDL and new objects
        try {
            if (showComments) {
                // Column comments
                boolean hasComments = false;
                for (PostgreTableColumn column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
                    if (!CommonUtils.isEmpty(column.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Column comments"));
                        }
                        PostgreTableColumnManager.addColumnCommentAction(actions, column);
                        hasComments = true;
                    }
                }
            }
            if (showComments) {
                // Constraint comments
                boolean hasComments = false;
                for (PostgreTableConstraintBase constr : CommonUtils.safeCollection(table.getConstraints(monitor))) {
                    if (!CommonUtils.isEmpty(constr.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Constraint comments"));
                        }
                        PostgreConstraintManager.addConstraintCommentAction(actions, constr);
                        hasComments = true;
                    }
                }
                for (DBSEntityAssociation fk : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                    if (fk instanceof PostgreTableForeignKey && !CommonUtils.isEmpty(fk.getDescription())) {
                        if (!hasComments) {
                            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Foreign key comments"));
                        }
                        PostgreConstraintManager.addConstraintCommentAction(actions, (PostgreTableForeignKey) fk);
                        hasComments = true;
                    }
                }
            }
            // Triggers
            if (table instanceof PostgreTableReal) {
                Collection<PostgreTrigger> triggers = ((PostgreTableReal) table).getTriggers(monitor);
                if (!CommonUtils.isEmpty(triggers)) {
                    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Triggers"));
                    for (PostgreTrigger trigger : triggers) {
                        actions.add(new SQLDatabasePersistAction("Create trigger", trigger.getObjectDefinitionText(monitor, options)));
                    }
                }
            }
            // Rules
            if (table instanceof PostgreTableReal) {
                Collection<PostgreRule> rules = ((PostgreTableReal) table).getRules(monitor);
                if (!CommonUtils.isEmpty(rules)) {
                    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Table Rules"));
                    for (PostgreRule rule : rules) {
                        actions.add(new SQLDatabasePersistAction("Create rule", rule.getObjectDefinitionText(monitor, options)));
                    }
                }
            }
            if (isDDL) {
                PostgreUtils.getObjectGrantPermissionActions(monitor, table, actions, options);
            }
        } catch (DBException e) {
            log.error(e);
        }
    }
}
Also used : SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction) DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBPScriptObject(org.jkiss.dbeaver.model.DBPScriptObject)

Example 4 with SQLDatabasePersistActionComment

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

the class SQLTableManager method getTableDDL.

public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table, Map<String, Object> options) throws DBException {
    List<DBEPersistAction> actions = new ArrayList<>();
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
    SQLObjectEditor<DBSEntityConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSEntityConstraint.class);
    SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
    SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
    DBCExecutionContext executionContext = DBUtils.getDefaultContext(table, true);
    if (CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_ONLY_FOREIGN_KEYS)) {
        if (fkm != null) {
            // Create only foreign keys
            try {
                for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                    if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
                        continue;
                    }
                    DBEPersistAction[] cmdActions = fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options).getPersistActions(monitor, executionContext, options);
                    if (cmdActions != null) {
                        Collections.addAll(actions, cmdActions);
                    }
                }
            } catch (DBException e) {
                // Ignore primary keys
                log.debug(e);
            }
        }
        return actions.toArray(new DBEPersistAction[0]);
    }
    if (isIncludeDropInDDL()) {
        actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
        for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(monitor, executionContext, options)) {
            String script = delAction.getScript();
            String delimiter = SQLUtils.getScriptLineDelimiter(SQLUtils.getDialectFromObject(table));
            if (!script.endsWith(delimiter)) {
                script += delimiter;
            }
            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), script));
        }
    }
    StructCreateCommand command = makeCreateCommand(table, options);
    if (tcm != null) {
        // Aggregate nested column, constraint and index commands
        for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
            if (DBUtils.isHiddenObject(column) || DBUtils.isInheritedObject(column)) {
                // Do not include hidden (pseudo?) and inherited columns in DDL
                continue;
            }
            command.aggregateCommand(tcm.makeCreateCommand(column, options));
        }
    }
    if (pkm != null) {
        try {
            for (DBSEntityConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
                if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
                    continue;
                }
                command.aggregateCommand(pkm.makeCreateCommand(constraint, options));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (fkm != null && !CommonUtils.getOption(options, DBPScriptObject.OPTION_DDL_SKIP_FOREIGN_KEYS)) {
        try {
            for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey) || DBUtils.isInheritedObject(foreignKey)) {
                    continue;
                }
                command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey, options));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (im != null && table instanceof DBSTable) {
        try {
            for (DBSTableIndex index : CommonUtils.safeCollection(((DBSTable) table).getIndexes(monitor))) {
                if (!isIncludeIndexInDDL(monitor, index)) {
                    continue;
                }
                command.aggregateCommand(im.makeCreateCommand(index, options));
            }
        } catch (DBException e) {
            // Ignore indexes
            log.debug(e);
        }
    }
    addExtraDDLCommands(monitor, table, options, command);
    Collections.addAll(actions, command.getPersistActions(monitor, executionContext, options));
    return actions.toArray(new DBEPersistAction[0]);
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBSTableIndex(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSTableForeignKey(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)

Example 5 with SQLDatabasePersistActionComment

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

the class SQLiteTableColumnManager method addObjectDeleteActions.

@Override
protected void addObjectDeleteActions(DBRProgressMonitor monitor, DBCExecutionContext executionContext, List<DBEPersistAction> actions, ObjectDeleteCommand command, Map<String, Object> options) throws DBException {
    final GenericTableColumn column = command.getObject();
    final GenericTableBase table = column.getTable();
    final List<? extends GenericTableColumn> attributes = table.getAttributes(monitor);
    if (CommonUtils.isEmpty(attributes)) {
        throw new DBException("Table was deleted");
    }
    final String tableColumns = attributes.stream().filter(x -> !x.getName().equals(column.getName()) && x.isPersisted()).map(DBUtils::getQuotedIdentifier).collect(Collectors.joining(",\n  "));
    final String tableName = DBUtils.getQuotedIdentifier(table);
    actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop column " + DBUtils.getQuotedIdentifier(column)));
    actions.add(new SQLDatabasePersistAction("Create temporary table from original table", "CREATE TEMPORARY TABLE temp AS\nSELECT\n  " + tableColumns + "\nFROM " + tableName));
    actions.add(new SQLDatabasePersistAction("Drop original table", "\nDROP TABLE " + tableName + ";\n"));
    actions.add(new SQLDatabasePersistAction("Create new table", DBStructUtils.generateTableDDL(monitor, table, Collections.emptyMap(), false)));
    actions.add(new SQLDatabasePersistAction("Insert values from temporary table to new table", "INSERT INTO " + tableName + "\n (" + tableColumns + ")\nSELECT\n  " + tableColumns + "\nFROM temp"));
    actions.add(new SQLDatabasePersistAction("Drop temporary table", "\nDROP TABLE temp"));
}
Also used : DBException(org.jkiss.dbeaver.DBException) SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) GenericTableColumn(org.jkiss.dbeaver.ext.generic.model.GenericTableColumn) GenericTableBase(org.jkiss.dbeaver.ext.generic.model.GenericTableBase) SQLDatabasePersistAction(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)

Aggregations

SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)14 DBException (org.jkiss.dbeaver.DBException)9 SQLDatabasePersistAction (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistAction)9 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)6 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 GenericTableBase (org.jkiss.dbeaver.ext.generic.model.GenericTableBase)2 GenericTableColumn (org.jkiss.dbeaver.ext.generic.model.GenericTableColumn)2 PostgreCommandGrantPrivilege (org.jkiss.dbeaver.ext.postgresql.edit.PostgreCommandGrantPrivilege)2 PostgreForeignTableManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreForeignTableManager)2 PostgreTableColumnManager (org.jkiss.dbeaver.ext.postgresql.edit.PostgreTableColumnManager)2 DBPScriptObject (org.jkiss.dbeaver.model.DBPScriptObject)2 DBECommandContext (org.jkiss.dbeaver.model.edit.DBECommandContext)2 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)2 DBNDatabaseNode (org.jkiss.dbeaver.model.navigator.DBNDatabaseNode)2 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)2 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)2