Search in sources :

Example 1 with DBSTableIndex

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

the class ResultSetUtils method getBestIdentifier.

private static DBSEntityReferrer getBestIdentifier(@NotNull DBRProgressMonitor monitor, @NotNull DBSEntity table, DBDAttributeBindingMeta[] bindings) throws DBException {
    List<DBSEntityReferrer> identifiers = new ArrayList<>(2);
    // Check for pseudo attrs (ROWID)
    for (DBDAttributeBindingMeta column : bindings) {
        DBDPseudoAttribute pseudoAttribute = column.getPseudoAttribute();
        if (pseudoAttribute != null && pseudoAttribute.getType() == DBDPseudoAttributeType.ROWID) {
            identifiers.add(new DBDPseudoReferrer(table, column));
            break;
        }
    }
    if (table instanceof DBSTable && ((DBSTable) table).isView()) {
    // Skip physical identifiers for views. There are nothing anyway
    } else if (identifiers.isEmpty()) {
        // Check indexes first.
        if (table instanceof DBSTable) {
            try {
                Collection<? extends DBSTableIndex> indexes = ((DBSTable) table).getIndexes(monitor);
                if (!CommonUtils.isEmpty(indexes)) {
                    for (DBSTableIndex index : indexes) {
                        if (DBUtils.isIdentifierIndex(monitor, index)) {
                            identifiers.add(index);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                // Indexes are not supported or not available
                // Just skip them
                log.debug(e);
            }
        }
        if (identifiers.isEmpty()) {
            // Check constraints
            Collection<? extends DBSEntityConstraint> constraints = table.getConstraints(monitor);
            if (constraints != null) {
                for (DBSEntityConstraint constraint : constraints) {
                    if (DBUtils.isIdentifierConstraint(monitor, constraint)) {
                        identifiers.add((DBSEntityReferrer) constraint);
                    }
                }
            }
        }
    }
    if (CommonUtils.isEmpty(identifiers)) {
        // No physical identifiers
        // Make new or use existing virtual identifier
        DBVEntity virtualEntity = DBVUtils.findVirtualEntity(table, true);
        identifiers.add(virtualEntity.getBestIdentifier());
    }
    if (!CommonUtils.isEmpty(identifiers)) {
        // Find PK or unique key
        DBSEntityReferrer uniqueId = null;
        for (DBSEntityReferrer referrer : identifiers) {
            if (isGoodReferrer(monitor, bindings, referrer)) {
                if (referrer.getConstraintType() == DBSEntityConstraintType.PRIMARY_KEY) {
                    return referrer;
                } else if (referrer.getConstraintType().isUnique() || (referrer instanceof DBSTableIndex && ((DBSTableIndex) referrer).isUnique())) {
                    uniqueId = referrer;
                }
            }
        }
        return uniqueId;
    }
    return null;
}
Also used : DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBSTableIndex(org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex) DBException(org.jkiss.dbeaver.DBException) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 2 with DBSTableIndex

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex 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)2 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)2 DBERegistry (org.jkiss.dbeaver.model.edit.DBERegistry)1 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)1 DBSTableConstraint (org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint)1 DBSTableForeignKey (org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKey)1 DBVEntity (org.jkiss.dbeaver.model.virtual.DBVEntity)1