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