Search in sources :

Example 1 with DBSEntityConstraint

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

the class SQLForeignKeyManager method getNestedDeclaration.

@Override
protected StringBuilder getNestedDeclaration(TABLE_TYPE owner, DBECommandAbstract<OBJECT_TYPE> command) {
    OBJECT_TYPE foreignKey = command.getObject();
    boolean legacySyntax = isLegacyForeignKeySyntax(owner);
    // Create column
    String constraintName = DBUtils.getQuotedIdentifier(foreignKey.getDataSource(), foreignKey.getName());
    StringBuilder decl = new StringBuilder(40);
    if (!legacySyntax || !foreignKey.isPersisted()) {
        decl.append("CONSTRAINT ");
    }
    if (!legacySyntax) {
        //$NON-NLS-1$
        decl.append(constraintName).append(" ");
    }
    //$NON-NLS-1$
    decl.append(foreignKey.getConstraintType().getName().toUpperCase(Locale.ENGLISH)).append(//$NON-NLS-1$
    " (");
    try {
        // Get columns using void monitor
        final Collection<? extends DBSEntityAttributeRef> columns = command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE);
        boolean firstColumn = true;
        for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
            final DBSEntityAttribute attribute = constraintColumn.getAttribute();
            //$NON-NLS-1$
            if (!firstColumn)
                decl.append(",");
            firstColumn = false;
            if (attribute != null) {
                decl.append(DBUtils.getQuotedIdentifier(attribute));
            }
        }
    } catch (DBException e) {
        log.error("Can't obtain reference attributes", e);
    }
    final DBSEntityConstraint refConstraint = foreignKey.getReferencedConstraint();
    //$NON-NLS-1$ //$NON-NLS-2$
    decl.append(") REFERENCES ").append(refConstraint == null ? "<?>" : DBUtils.getObjectFullName(refConstraint.getParentObject(), DBPEvaluationContext.DDL)).append("(");
    if (refConstraint instanceof DBSEntityReferrer) {
        try {
            boolean firstColumn = true;
            List<? extends DBSEntityAttributeRef> columns = ((DBSEntityReferrer) refConstraint).getAttributeReferences(VoidProgressMonitor.INSTANCE);
            for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
                //$NON-NLS-1$
                if (!firstColumn)
                    decl.append(",");
                firstColumn = false;
                final DBSEntityAttribute attribute = constraintColumn.getAttribute();
                if (attribute != null) {
                    decl.append(DBUtils.getQuotedIdentifier(attribute));
                }
            }
        } catch (DBException e) {
            log.error("Can't obtain ref constraint reference attributes", e);
        }
    }
    //$NON-NLS-1$
    decl.append(")");
    if (foreignKey.getDeleteRule() != null && !CommonUtils.isEmpty(foreignKey.getDeleteRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON DELETE ").append(foreignKey.getDeleteRule().getClause());
    }
    if (foreignKey.getUpdateRule() != null && !CommonUtils.isEmpty(foreignKey.getUpdateRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON UPDATE ").append(foreignKey.getUpdateRule().getClause());
    }
    if (legacySyntax) {
        //$NON-NLS-1$
        decl.append(" CONSTRAINT ").append(constraintName);
    }
    return decl;
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) DBSEntityConstraint(org.jkiss.dbeaver.model.struct.DBSEntityConstraint) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Aggregations

DBException (org.jkiss.dbeaver.DBException)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)1 DBSEntityConstraint (org.jkiss.dbeaver.model.struct.DBSEntityConstraint)1 DBSEntityReferrer (org.jkiss.dbeaver.model.struct.DBSEntityReferrer)1