Search in sources :

Example 11 with DBSEntityAttribute

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

the class EditForeignKeyPage method handleUniqueKeySelect.

private void handleUniqueKeySelect() {
    curConstraint = null;
    fkColumns.clear();
    ownColumns = null;
    columnsTable.removeAll();
    if (curConstraints.isEmpty() || uniqueKeyCombo.getSelectionIndex() < 0) {
        return;
    }
    curConstraint = curConstraints.get(uniqueKeyCombo.getSelectionIndex());
    try {
        // Read column nodes with void monitor because we already cached them above
        for (DBSEntityAttributeRef pkColumn : curConstraint.getAttributeReferences(VoidProgressMonitor.INSTANCE)) {
            FKColumnInfo fkColumnInfo = new FKColumnInfo(pkColumn.getAttribute());
            // Try to find matched column in own table
            Collection<? extends DBSEntityAttribute> tmpColumns = ownTable.getAttributes(VoidProgressMonitor.INSTANCE);
            ownColumns = tmpColumns == null ? Collections.<DBSTableColumn>emptyList() : new ArrayList<>(ownTable.getAttributes(VoidProgressMonitor.INSTANCE));
            if (!CommonUtils.isEmpty(ownColumns)) {
                for (DBSEntityAttribute ownColumn : ownColumns) {
                    if (ownColumn.getName().equals(pkColumn.getAttribute().getName()) && ownTable != pkColumn.getAttribute().getParentObject()) {
                        fkColumnInfo.ownColumn = ownColumn;
                        break;
                    }
                }
            }
            fkColumns.add(fkColumnInfo);
            TableItem item = new TableItem(columnsTable, SWT.NONE);
            if (fkColumnInfo.ownColumn != null) {
                item.setText(0, fkColumnInfo.ownColumn.getName());
                item.setImage(0, getColumnIcon(fkColumnInfo.ownColumn));
                item.setText(1, fkColumnInfo.ownColumn.getFullTypeName());
            }
            item.setText(2, pkColumn.getAttribute().getName());
            item.setImage(2, getColumnIcon(pkColumn.getAttribute()));
            item.setText(3, pkColumn.getAttribute().getFullTypeName());
            item.setData(fkColumnInfo);
        }
    } catch (DBException e) {
        UIUtils.showErrorDialog(getShell(), CoreMessages.dialog_struct_edit_fk_error_load_constraint_columns_title, CoreMessages.dialog_struct_edit_fk_error_load_constraint_columns_message, e);
    }
    UIUtils.packColumns(columnsTable, true);
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) ArrayList(java.util.ArrayList)

Example 12 with DBSEntityAttribute

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

the class DBVUtils method findAttributeTransformers.

@Nullable
public static DBDAttributeTransformer[] findAttributeTransformers(@NotNull DBDAttributeBinding binding, @Nullable Boolean custom) {
    DBPDataSource dataSource = binding.getDataSource();
    DBPDataSourceContainer container = dataSource.getContainer();
    List<? extends DBDAttributeTransformerDescriptor> tdList = container.getPlatform().getValueHandlerRegistry().findTransformers(dataSource, binding.getAttribute(), custom);
    if (tdList == null || tdList.isEmpty()) {
        return null;
    }
    boolean filtered = false;
    DBSEntityAttribute entityAttribute = binding.getEntityAttribute();
    if (entityAttribute != null) {
        DBVEntity vEntity = findVirtualEntity(entityAttribute.getParentObject(), false);
        if (vEntity != null) {
            DBVEntityAttribute vAttr = vEntity.getVirtualAttribute(binding, false);
            if (vAttr != null) {
                final DBVTransformSettings transformSettings = getTransformSettings(vAttr, false);
                if (transformSettings != null) {
                    filtered = transformSettings.filterTransformers(tdList);
                }
            }
        }
    }
    if (!filtered) {
        // Leave only default transformers
        for (int i = 0; i < tdList.size(); ) {
            if (tdList.get(i).isCustom() || !tdList.get(i).isApplicableByDefault()) {
                tdList.remove(i);
            } else {
                i++;
            }
        }
    }
    if (tdList.isEmpty()) {
        return null;
    }
    DBDAttributeTransformer[] result = new DBDAttributeTransformer[tdList.size()];
    for (int i = 0; i < tdList.size(); i++) {
        result[i] = tdList.get(i).getInstance();
    }
    return result;
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBPDataSourceContainer(org.jkiss.dbeaver.model.DBPDataSourceContainer) Nullable(org.jkiss.code.Nullable)

Example 13 with DBSEntityAttribute

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

the class ComplexTypeAttributeTransformer method createNestedTypeBindings.

static void createNestedTypeBindings(DBCSession session, DBDAttributeBinding attribute, List<Object[]> rows, DBSEntity dataType) throws DBException {
    List<DBDAttributeBinding> nestedBindings = new ArrayList<>();
    for (DBSEntityAttribute nestedAttr : CommonUtils.safeCollection(dataType.getAttributes(session.getProgressMonitor()))) {
        DBDAttributeBindingType nestedBinding = new DBDAttributeBindingType(attribute, nestedAttr);
        nestedBinding.lateBinding(session, rows);
        nestedBindings.add(nestedBinding);
    }
    if (!nestedBindings.isEmpty()) {
        attribute.setNestedBindings(nestedBindings);
    }
}
Also used : DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) ArrayList(java.util.ArrayList) DBDAttributeBindingType(org.jkiss.dbeaver.model.data.DBDAttributeBindingType) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 14 with DBSEntityAttribute

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

the class SQLForeignKeyManager method getNestedDeclaration.

@Override
protected StringBuilder getNestedDeclaration(TABLE_TYPE owner, DBECommandAbstract<OBJECT_TYPE> command) {
    OBJECT_TYPE foreignKey = command.getObject();
    boolean legacySyntax = isLegacyForeignKeySyntax(owner);
    // Create column
    String constraintName = DBUtils.getQuotedIdentifier(foreignKey.getDataSource(), foreignKey.getName());
    StringBuilder decl = new StringBuilder(40);
    if (!legacySyntax || !foreignKey.isPersisted()) {
        decl.append("CONSTRAINT ");
    }
    if (!legacySyntax) {
        //$NON-NLS-1$
        decl.append(constraintName).append(" ");
    }
    //$NON-NLS-1$
    decl.append(foreignKey.getConstraintType().getName().toUpperCase(Locale.ENGLISH)).append(//$NON-NLS-1$
    " (");
    try {
        // Get columns using void monitor
        final Collection<? extends DBSEntityAttributeRef> columns = command.getObject().getAttributeReferences(VoidProgressMonitor.INSTANCE);
        boolean firstColumn = true;
        for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
            final DBSEntityAttribute attribute = constraintColumn.getAttribute();
            //$NON-NLS-1$
            if (!firstColumn)
                decl.append(",");
            firstColumn = false;
            if (attribute != null) {
                decl.append(DBUtils.getQuotedIdentifier(attribute));
            }
        }
    } catch (DBException e) {
        log.error("Can't obtain reference attributes", e);
    }
    final DBSEntityConstraint refConstraint = foreignKey.getReferencedConstraint();
    //$NON-NLS-1$ //$NON-NLS-2$
    decl.append(") REFERENCES ").append(refConstraint == null ? "<?>" : DBUtils.getObjectFullName(refConstraint.getParentObject(), DBPEvaluationContext.DDL)).append("(");
    if (refConstraint instanceof DBSEntityReferrer) {
        try {
            boolean firstColumn = true;
            List<? extends DBSEntityAttributeRef> columns = ((DBSEntityReferrer) refConstraint).getAttributeReferences(VoidProgressMonitor.INSTANCE);
            for (DBSEntityAttributeRef constraintColumn : CommonUtils.safeCollection(columns)) {
                //$NON-NLS-1$
                if (!firstColumn)
                    decl.append(",");
                firstColumn = false;
                final DBSEntityAttribute attribute = constraintColumn.getAttribute();
                if (attribute != null) {
                    decl.append(DBUtils.getQuotedIdentifier(attribute));
                }
            }
        } catch (DBException e) {
            log.error("Can't obtain ref constraint reference attributes", e);
        }
    }
    //$NON-NLS-1$
    decl.append(")");
    if (foreignKey.getDeleteRule() != null && !CommonUtils.isEmpty(foreignKey.getDeleteRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON DELETE ").append(foreignKey.getDeleteRule().getClause());
    }
    if (foreignKey.getUpdateRule() != null && !CommonUtils.isEmpty(foreignKey.getUpdateRule().getClause())) {
        //$NON-NLS-1$
        decl.append(" ON UPDATE ").append(foreignKey.getUpdateRule().getClause());
    }
    if (legacySyntax) {
        //$NON-NLS-1$
        decl.append(" CONSTRAINT ").append(constraintName);
    }
    return decl;
}
Also used : DBSEntityAttributeRef(org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef) DBException(org.jkiss.dbeaver.DBException) DBSEntityReferrer(org.jkiss.dbeaver.model.struct.DBSEntityReferrer) DBSEntityConstraint(org.jkiss.dbeaver.model.struct.DBSEntityConstraint) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute)

Aggregations

DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)14 DBException (org.jkiss.dbeaver.DBException)8 ArrayList (java.util.ArrayList)5 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)3 DBSEntity (org.jkiss.dbeaver.model.struct.DBSEntity)3 DBSEntityAttributeRef (org.jkiss.dbeaver.model.struct.DBSEntityAttributeRef)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BigDecimal (java.math.BigDecimal)2 Nullable (org.jkiss.code.Nullable)2 DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)2 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)2 DBRRunnableWithProgress (org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress)2 DBSEntityConstraint (org.jkiss.dbeaver.model.struct.DBSEntityConstraint)2 BigInteger (java.math.BigInteger)1 LinkedHashSet (java.util.LinkedHashSet)1 Table (net.sf.jsqlparser.schema.Table)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 TemplateVariable (org.eclipse.jface.text.templates.TemplateVariable)1 KeyAdapter (org.eclipse.swt.events.KeyAdapter)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1