Search in sources :

Example 26 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class StreamImporterAbstract method applyTransformHints.

protected void applyTransformHints(StreamTransferResultSet resultSet, IDataTransferConsumer consumer, Map<String, Object> properties, String formatPropName, String zoneIdPropName) throws DBException {
    DateTimeFormatter tsFormat = formatPropName == null ? null : getTimeStampFormat(properties, formatPropName);
    ZoneId tsZoneId = null;
    if (zoneIdPropName != null) {
        String zoneId = CommonUtils.toString(properties.get(zoneIdPropName));
        if (!CommonUtils.isEmpty(zoneId)) {
            tsZoneId = ZoneId.of(zoneId);
        }
    }
    if (tsFormat != null) {
        resultSet.setDateTimeFormat(tsFormat, tsZoneId);
    }
    // Do it only for valid String mappings
    if (consumer instanceof DatabaseTransferConsumer) {
        for (DatabaseTransferConsumer.ColumnMapping cm : ((DatabaseTransferConsumer) consumer).getColumnMappings()) {
            if (cm == null) {
                continue;
            }
            for (StreamDataImporterColumnInfo attributeMapping : resultSet.getAttributeMappings()) {
                if (cm.targetAttr.getMappingType().isValid()) {
                    if (cm.sourceAttr.getDataKind() == DBPDataKind.STRING && cm.sourceAttr.getName().equals(attributeMapping.getName())) {
                        // Gotcha
                        DBSEntityAttribute targetAttr = cm.targetAttr.getTarget();
                        if (targetAttr != null) {
                            switch(targetAttr.getDataKind()) {
                                case DATETIME:
                                case NUMERIC:
                                case BOOLEAN:
                                    attributeMapping.setDataKind(targetAttr.getDataKind());
                                    break;
                            }
                        }
                    }
                }
            }
        }
        Object targetObject = consumer.getTargetObject();
        if (targetObject instanceof DBSEntity) {
        }
    }
}
Also used : DatabaseTransferConsumer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferConsumer) ZoneId(java.time.ZoneId) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) StreamDataImporterColumnInfo(org.jkiss.dbeaver.tools.transfer.stream.StreamDataImporterColumnInfo) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 27 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute 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 28 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class ERDEntity method addModelRelations.

/**
 * Resolve and create entity associations.
 * Also caches all unresolved associations (associations with entities which are not present in diagram yet)
 * @param diagram all diagram entities map
 * @param create    if true then creates all found model association. Otherwise only saves unresolved ones.
 * @param reflect   reflect UI
 */
public void addModelRelations(DBRProgressMonitor monitor, ERDContainer diagram, boolean create, boolean reflect) {
    try {
        Set<DBSEntityAttribute> fkAttrs = new HashSet<>();
        // Make associations
        Collection<? extends DBSEntityAssociation> fks = DBVUtils.getAllAssociations(monitor, getObject());
        if (fks != null) {
            for (DBSEntityAssociation fk : fks) {
                if (fk instanceof DBSEntityReferrer) {
                    fkAttrs.addAll(DBUtils.getEntityAttributes(monitor, (DBSEntityReferrer) fk));
                }
                ERDEntity entity2 = diagram.getEntityMap().get(DBVUtils.getRealEntity(monitor, fk.getAssociatedEntity()));
                if (entity2 == null) {
                    // log.debug("Table '" + fk.getReferencedKey().getTable().getFullyQualifiedName() + "' not found in ERD");
                    if (unresolvedKeys == null) {
                        unresolvedKeys = new ArrayList<>();
                    }
                    unresolvedKeys.add(fk);
                } else {
                    if (create) {
                        if (DBUtils.isInheritedObject(fk)) {
                            continue;
                        }
                        diagram.getContentProvider().createAutoAssociation(diagram, fk, this, entity2, reflect);
                    }
                }
            }
        }
        // Mark attribute's fk flag
        if (!fkAttrs.isEmpty()) {
            for (ERDEntityAttribute attribute : this.getAttributes()) {
                if (fkAttrs.contains(attribute.getObject())) {
                    attribute.setInForeignKey(true);
                }
            }
        }
    } catch (Throwable e) {
        log.error("Can't load table '" + getObject().getName() + "' foreign keys", e);
    }
}
Also used : DBSEntityAssociation(org.jkiss.dbeaver.model.struct.DBSEntityAssociation) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Example 29 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class PostgreInsertReplaceMethod method getTrailingClause.

@Override
public String getTrailingClause(DBSTable table, DBRProgressMonitor monitor, DBSAttributeBase[] attributes) {
    StringBuilder query = new StringBuilder();
    try {
        String onConflictExpression = "ON CONFLICT (%s) DO UPDATE SET %s";
        Collection<? extends DBSTableConstraint> constraints = table.getConstraints(monitor);
        if (!CommonUtils.isEmpty(constraints)) {
            StringBuilder pkNames = new StringBuilder();
            Optional<? extends DBSTableConstraint> tableConstraint = constraints.stream().filter(key -> key.getConstraintType() == DBSEntityConstraintType.PRIMARY_KEY).findFirst();
            if (tableConstraint.isPresent()) {
                DBSTableConstraint dbsTableConstraint = tableConstraint.get();
                List<? extends DBSEntityAttributeRef> attributeReferences = dbsTableConstraint.getAttributeReferences(monitor);
                if (!CommonUtils.isEmpty(attributeReferences)) {
                    boolean hasKey = false;
                    for (DBSEntityAttributeRef column : attributeReferences) {
                        DBSEntityAttribute attribute = column.getAttribute();
                        if (attribute == null)
                            continue;
                        pkNames.append(attribute.getName());
                        if (hasKey)
                            pkNames.append(",");
                        hasKey = true;
                    }
                    StringBuilder updateExpression = new StringBuilder();
                    updateExpression.append("(");
                    addAttributesNamesList(table, attributes, false, updateExpression);
                    updateExpression.append(") = (");
                    addAttributesNamesList(table, attributes, true, updateExpression);
                    updateExpression.append(")");
                    query.append(" ").append(String.format(onConflictExpression, pkNames, updateExpression));
                }
            }
        }
    } catch (DBException e) {
        log.debug("Can't read table constraints list", e);
    }
    return query.toString();
}
Also used : CommonUtils(org.jkiss.utils.CommonUtils) DBUtils(org.jkiss.dbeaver.model.DBUtils) DBDInsertReplaceMethod(org.jkiss.dbeaver.model.data.DBDInsertReplaceMethod) Collection(java.util.Collection) DBSTable(org.jkiss.dbeaver.model.struct.rdb.DBSTable) DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBSEntityConstraintType(org.jkiss.dbeaver.model.struct.DBSEntityConstraintType) DBSTableConstraint(org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint) NotNull(org.jkiss.code.NotNull) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) List(java.util.List) DBException(org.jkiss.dbeaver.DBException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) Optional(java.util.Optional) DBPEvaluationContext(org.jkiss.dbeaver.model.DBPEvaluationContext) DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSTableConstraint(org.jkiss.dbeaver.model.struct.rdb.DBSTableConstraint)

Example 30 with DBSEntityAttribute

use of org.jkiss.dbeaver.model.struct.DBSEntityAttribute in project dbeaver by dbeaver.

the class DBDAttributeBindingMeta method lateBinding.

@Override
public void lateBinding(@NotNull DBCSession session, List<Object[]> rows) throws DBException {
    DBSEntityAttribute entityAttribute = getEntityAttribute();
    if (entityAttribute != null) {
        referrers = DBUtils.getAttributeReferrers(session.getProgressMonitor(), entityAttribute, true);
    }
    super.lateBinding(session, rows);
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Aggregations

DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)77 DBException (org.jkiss.dbeaver.DBException)25 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)16 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)14 ArrayList (java.util.ArrayList)11 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)10 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)10 EditIndexPage (org.jkiss.dbeaver.ui.editors.object.struct.EditIndexPage)10 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)9 MySQLTableColumn (org.jkiss.dbeaver.ext.mysql.model.MySQLTableColumn)6 DBDRowIdentifier (org.jkiss.dbeaver.model.data.DBDRowIdentifier)6 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)6 DBSDataType (org.jkiss.dbeaver.model.struct.DBSDataType)6 EditConstraintPage (org.jkiss.dbeaver.ui.editors.object.struct.EditConstraintPage)6 TableItem (org.eclipse.swt.widgets.TableItem)5 BigDecimal (java.math.BigDecimal)4 SQLServerTableColumn (org.jkiss.dbeaver.ext.mssql.model.SQLServerTableColumn)4 KeyAdapter (org.eclipse.swt.events.KeyAdapter)3 KeyEvent (org.eclipse.swt.events.KeyEvent)3 GridData (org.eclipse.swt.layout.GridData)3