Search in sources :

Example 1 with DBSTableForeignKey

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey 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<DBSTableConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSTableConstraint.class);
    SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
    SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
    if (isIncludeDropInDDL()) {
        actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), "Drop table"));
        for (DBEPersistAction delAction : new ObjectDeleteCommand(table, ModelMessages.model_jdbc_delete_object).getPersistActions(options)) {
            actions.add(new SQLDatabasePersistActionComment(table.getDataSource(), delAction.getScript()));
        }
    }
    StructCreateCommand command = makeCreateCommand(table);
    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));
        }
    }
    if (pkm != null) {
        try {
            for (DBSTableConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
                if (DBUtils.isHiddenObject(constraint) || DBUtils.isInheritedObject(constraint)) {
                    continue;
                }
                command.aggregateCommand(pkm.makeCreateCommand(constraint));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (fkm != null) {
        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));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (im != null) {
        try {
            for (DBSTableIndex index : CommonUtils.safeCollection(table.getIndexes(monitor))) {
                if (DBUtils.isHiddenObject(index) || DBUtils.isInheritedObject(index)) {
                    continue;
                }
                command.aggregateCommand(im.makeCreateCommand(index));
            }
        } catch (DBException e) {
            // Ignore indexes
            log.debug(e);
        }
    }
    Collections.addAll(actions, command.getPersistActions(options));
    return actions.toArray(new DBEPersistAction[actions.size()]);
}
Also used : SQLDatabasePersistActionComment(org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment) DBException(org.jkiss.dbeaver.DBException) DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBSTableIndex(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSTableConstraint(org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint) DBSTableForeignKey(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)

Example 2 with DBSTableForeignKey

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey in project dbeaver by serge-rider.

the class SQLTableManager method getTableDDL.

public DBEPersistAction[] getTableDDL(DBRProgressMonitor monitor, OBJECT_TYPE table) throws DBException {
    final DBERegistry editorsRegistry = table.getDataSource().getContainer().getPlatform().getEditorsRegistry();
    SQLObjectEditor<DBSEntityAttribute, OBJECT_TYPE> tcm = getObjectEditor(editorsRegistry, DBSEntityAttribute.class);
    SQLObjectEditor<DBSTableConstraint, OBJECT_TYPE> pkm = getObjectEditor(editorsRegistry, DBSTableConstraint.class);
    SQLObjectEditor<DBSTableForeignKey, OBJECT_TYPE> fkm = getObjectEditor(editorsRegistry, DBSTableForeignKey.class);
    SQLObjectEditor<DBSTableIndex, OBJECT_TYPE> im = getObjectEditor(editorsRegistry, DBSTableIndex.class);
    StructCreateCommand command = makeCreateCommand(table);
    if (tcm != null) {
        // Aggregate nested column, constraint and index commands
        for (DBSEntityAttribute column : CommonUtils.safeCollection(table.getAttributes(monitor))) {
            if (DBUtils.isHiddenObject(column)) {
                continue;
            }
            command.aggregateCommand(tcm.makeCreateCommand(column));
        }
    }
    if (pkm != null) {
        try {
            for (DBSTableConstraint constraint : CommonUtils.safeCollection(table.getConstraints(monitor))) {
                if (DBUtils.isHiddenObject(constraint)) {
                    continue;
                }
                command.aggregateCommand(pkm.makeCreateCommand(constraint));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (fkm != null) {
        try {
            for (DBSEntityAssociation foreignKey : CommonUtils.safeCollection(table.getAssociations(monitor))) {
                if (!(foreignKey instanceof DBSTableForeignKey) || DBUtils.isHiddenObject(foreignKey)) {
                    continue;
                }
                command.aggregateCommand(fkm.makeCreateCommand((DBSTableForeignKey) foreignKey));
            }
        } catch (DBException e) {
            // Ignore primary keys
            log.debug(e);
        }
    }
    if (im != null) {
        try {
            for (DBSTableIndex index : CommonUtils.safeCollection(table.getIndexes(monitor))) {
                if (DBUtils.isHiddenObject(index)) {
                    continue;
                }
                command.aggregateCommand(im.makeCreateCommand(index));
            }
        } catch (DBException e) {
            // Ignore indexes
            log.debug(e);
        }
    }
    return command.getPersistActions();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBERegistry(org.jkiss.dbeaver.model.edit.DBERegistry) DBSTableIndex(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex) DBSTableConstraint(org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint) DBSTableForeignKey(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)

Example 3 with DBSTableForeignKey

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey in project dbeaver by serge-rider.

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)

Aggregations

DBException (org.jkiss.dbeaver.DBException)3 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)3 DBSTableForeignKey (org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)3 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)3 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)2 SQLDatabasePersistActionComment (org.jkiss.dbeaver.model.impl.edit.SQLDatabasePersistActionComment)2 DBSTableConstraint (org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)1 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)1