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()));
}
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);
}
}
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);
}
}
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();
}
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();
}
Aggregations