Search in sources :

Example 1 with DBSTableForeignKeyColumn

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn in project dbeaver by dbeaver.

the class FKGenerator method generateOneValue.

@Override
public Object generateOneValue(DBRProgressMonitor monitor) throws DBException, IOException {
    if (refValues == null) {
        refValues = new ArrayList<>();
        List<DBSEntityReferrer> attributeReferrers = DBUtils.getAttributeReferrers(monitor, (DBSEntityAttribute) attribute);
        // TODO only the first
        DBSEntityReferrer fk = attributeReferrers.get(0);
        List<? extends DBSEntityAttributeRef> references = ((DBSEntityReferrer) fk).getAttributeReferences(monitor);
        DBSTableForeignKeyColumn column = null;
        for (DBSEntityAttributeRef ref : references) {
            if (((DBPNamedObject) ref).getName().equals(attribute.getName())) {
                column = (DBSTableForeignKeyColumn) ref;
            }
        }
        if (column == null) {
            throw new DBException("Can't find reference column for '" + attribute.getName() + "'");
        }
        int numberRefRecords = DBUtils.checkUnique(monitor, dbsEntity, attribute) ? UNIQ_REF_RECORDS_LIMIT : REF_RECORDS_LIMIT;
        Collection<DBDLabelValuePair> values = readColumnValues(monitor, fk.getDataSource(), (DBSAttributeEnumerable) column.getReferencedColumn(), numberRefRecords);
        for (DBDLabelValuePair value : values) {
            refValues.add(value.getValue());
        }
    }
    return refValues.get(random.nextInt(refValues.size()));
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBSTableForeignKeyColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 2 with DBSTableForeignKeyColumn

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

the class ERDAssociation method resolveAttributes.

/**
 */
protected void resolveAttributes(DBSEntityReferrer association, ERDEntity sourceEntity, ERDEntity targetEntity) {
    try {
        List<? extends DBSEntityAttributeRef> attrRefs = association.getAttributeReferences(new VoidProgressMonitor());
        if (!CommonUtils.isEmpty(attrRefs)) {
            for (DBSEntityAttributeRef attrRef : attrRefs) {
                if (attrRef instanceof DBSTableForeignKeyColumn) {
                    DBSEntityAttribute targetAttr = ((DBSTableForeignKeyColumn) attrRef).getReferencedColumn();
                    DBSEntityAttribute sourceAttr = attrRef.getAttribute();
                    if (sourceAttr != null && targetAttr != null) {
                        ERDEntityAttribute erdSourceAttr = ERDUtils.getAttributeByModel(sourceEntity, sourceAttr);
                        ERDEntityAttribute erdTargetAttr = ERDUtils.getAttributeByModel(targetEntity, targetAttr);
                        if (erdSourceAttr != null || erdTargetAttr != null) {
                            addCondition(erdSourceAttr, erdTargetAttr);
                        }
                    }
                }
            }
        }
    } catch (DBException e) {
        log.error("Error resolving ERD association attributes", e);
    }
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSTableForeignKeyColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 3 with DBSTableForeignKeyColumn

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn in project dbeaver by dbeaver.

the class ERDAssociation method resolveAttributes.

/**
 */
protected void resolveAttributes(DBSEntityReferrer association, ERDEntity sourceEntity, ERDEntity targetEntity) {
    try {
        List<? extends DBSEntityAttributeRef> attrRefs = association.getAttributeReferences(new VoidProgressMonitor());
        if (!CommonUtils.isEmpty(attrRefs)) {
            for (DBSEntityAttributeRef attrRef : attrRefs) {
                if (attrRef instanceof DBSTableForeignKeyColumn) {
                    DBSEntityAttribute targetAttr = ((DBSTableForeignKeyColumn) attrRef).getReferencedColumn();
                    DBSEntityAttribute sourceAttr = attrRef.getAttribute();
                    if (sourceAttr != null && targetAttr != null) {
                        ERDEntityAttribute erdSourceAttr = ERDUtils.getAttributeByModel(sourceEntity, sourceAttr);
                        ERDEntityAttribute erdTargetAttr = ERDUtils.getAttributeByModel(targetEntity, targetAttr);
                        if (erdSourceAttr != null || erdTargetAttr != null) {
                            addCondition(erdSourceAttr, erdTargetAttr);
                        }
                    }
                }
            }
        }
    } catch (DBException e) {
        log.error("Error resolving ERD association attributes", e);
    }
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSTableForeignKeyColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 4 with DBSTableForeignKeyColumn

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

the class SQLUtils method generateTablesJoin.

private static String generateTablesJoin(DBRProgressMonitor monitor, DBSTableForeignKey fk, String leftAlias, String rightAlias) throws DBException {
    boolean hasCriteria = false;
    StringBuilder joinSQL = new StringBuilder();
    for (DBSEntityAttributeRef ar : fk.getAttributeReferences(monitor)) {
        if (ar instanceof DBSTableForeignKeyColumn) {
            if (hasCriteria) {
                joinSQL.append(" AND ");
            }
            DBSTableForeignKeyColumn fkc = (DBSTableForeignKeyColumn) ar;
            joinSQL.append(leftAlias).append(".").append(DBUtils.getQuotedIdentifier(fkc)).append(" = ").append(rightAlias).append(".").append(DBUtils.getQuotedIdentifier(fkc.getReferencedColumn()));
            hasCriteria = true;
        }
    }
    return joinSQL.toString();
}
Also used : DBSTableForeignKeyColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn)

Example 5 with DBSTableForeignKeyColumn

use of org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn in project dbeaver by dbeaver.

the class SQLUtils method generateTablesJoin.

private static String generateTablesJoin(DBRProgressMonitor monitor, DBSTableForeignKey fk, String leftAlias, String rightAlias) throws DBException {
    boolean hasCriteria = false;
    StringBuilder joinSQL = new StringBuilder();
    for (DBSEntityAttributeRef ar : fk.getAttributeReferences(monitor)) {
        if (ar instanceof DBSTableForeignKeyColumn) {
            if (hasCriteria) {
                joinSQL.append(" AND ");
            }
            DBSTableForeignKeyColumn fkc = (DBSTableForeignKeyColumn) ar;
            joinSQL.append(leftAlias).append(".").append(DBUtils.getQuotedIdentifier(fkc)).append(" = ").append(rightAlias).append(".").append(DBUtils.getQuotedIdentifier(fkc.getReferencedColumn()));
            hasCriteria = true;
        }
    }
    return joinSQL.toString();
}
Also used : DBSTableForeignKeyColumn(org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn)

Aggregations

DBSTableForeignKeyColumn (org.jkiss.dbeaver.model.struct.rdb.DBSTableForeignKeyColumn)5 DBException (org.jkiss.dbeaver.DBException)3 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)2 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)2 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)2 DBDLabelValuePair (org.jkiss.dbeaver.model.data.DBDLabelValuePair)1