Search in sources :

Example 1 with DBSTableConstraint

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint 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)

Aggregations

DBException (org.jkiss.dbeaver.DBException)1 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)1 DBSTableConstraint (org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint)1 DBSTableForeignKey (org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)1 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)1