Search in sources :

Example 11 with DBVEntity

use of org.jkiss.dbeaver.model.virtual.DBVEntity in project dbeaver by serge-rider.

the class CustomizeColorsAction method run.

@Override
public void run() {
    final DBVEntity vEntity = getColorsVirtualEntity();
    ColorSettingsDialog dialog = new ColorSettingsDialog(resultSetViewer, vEntity, curAttribute, row);
    if (dialog.open() != IDialogConstants.OK_ID) {
        return;
    }
    updateColors(vEntity);
}
Also used : DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 12 with DBVEntity

use of org.jkiss.dbeaver.model.virtual.DBVEntity in project dbeaver by serge-rider.

the class ResultSetModel method updateColorMapping.

void updateColorMapping() {
    colorMapping.clear();
    DBSEntity entity = getSingleSource();
    if (entity == null) {
        return;
    }
    DBVEntity virtualEntity = DBVUtils.findVirtualEntity(entity, false);
    if (virtualEntity != null) {
        List<DBVColorOverride> coList = virtualEntity.getColorOverrides();
        if (!CommonUtils.isEmpty(coList)) {
            for (DBVColorOverride co : coList) {
                DBDAttributeBinding binding = getAttributeBinding(entity, co.getAttributeName());
                if (binding != null) {
                    List<AttributeColorSettings> cmList = colorMapping.get(binding);
                    if (cmList == null) {
                        cmList = new ArrayList<>();
                        colorMapping.put(binding, cmList);
                    }
                    cmList.add(new AttributeColorSettings(co));
                }
            }
        }
    }
    updateRowColors(curRows);
}
Also used : DBVColorOverride(org.jkiss.dbeaver.model.virtual.DBVColorOverride) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 13 with DBVEntity

use of org.jkiss.dbeaver.model.virtual.DBVEntity 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 14 with DBVEntity

use of org.jkiss.dbeaver.model.virtual.DBVEntity in project dbeaver by dbeaver.

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)) {
                    // First search for primary index
                    for (DBSTableIndex index : indexes) {
                        if (index.isPrimary() && DBUtils.isIdentifierIndex(monitor, index)) {
                            identifiers.add(index);
                            break;
                        }
                    }
                    // Then search for unique index
                    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 (uniqueId == null && (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 15 with DBVEntity

use of org.jkiss.dbeaver.model.virtual.DBVEntity in project dbeaver by serge-rider.

the class AssociationDeleteCommand method undo.

/**
 * Restores the relationship
 */
@Override
public void undo() {
    if (association.getSourceEntity() != null) {
        return;
    }
    DBSEntityAssociation entityAssociation = association.getObject();
    if (entityAssociation instanceof DBVEntityForeignKey) {
        DBVEntity vEntity = DBVUtils.getVirtualEntity(entityAssociation.getParentObject(), false);
        if (vEntity != null) {
            vEntity.addForeignKey((DBVEntityForeignKey) entityAssociation);
        }
    }
    association.setSourceEntity(sourceEntity);
    association.setTargetEntity(targetEntity);
    sourceEntity.addAssociation(association, true);
    targetEntity.addReferenceAssociation(association, true);
}
Also used : DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBVEntityForeignKey(org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Aggregations

DBVEntity (org.jkiss.dbeaver.model.virtual.DBVEntity)22 DBException (org.jkiss.dbeaver.DBException)5 DBVEntityAttribute (org.jkiss.dbeaver.model.virtual.DBVEntityAttribute)5 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)4 DBVEntityForeignKey (org.jkiss.dbeaver.model.virtual.DBVEntityForeignKey)4 DBSTable (org.jkiss.dbeaver.model.struct.rdb.DBSTable)3 DBSTableIndex (org.jkiss.dbeaver.model.struct.rdb.DBSTableIndex)3 DBVColorOverride (org.jkiss.dbeaver.model.virtual.DBVColorOverride)3 RGB (org.eclipse.swt.graphics.RGB)2 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)2 DBDAttributeBindingCustom (org.jkiss.dbeaver.model.data.DBDAttributeBindingCustom)2 DBSEntityAssociation (org.jkiss.dbeaver.model.struct.DBSEntityAssociation)2 DBVEntityConstraint (org.jkiss.dbeaver.model.virtual.DBVEntityConstraint)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 IStatus (org.eclipse.core.runtime.IStatus)1 Color (org.eclipse.swt.graphics.Color)1 ColorDialog (org.eclipse.swt.widgets.ColorDialog)1 Shell (org.eclipse.swt.widgets.Shell)1 NotNull (org.jkiss.code.NotNull)1