Search in sources :

Example 1 with DBSAttributeBase

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

the class SQLSemanticProcessor method getConstraintTable.

/**
     * Extract alias (or source table name) for specified constraint from SQL select.
     * Searches in FROM and JOIN
     */
@Nullable
public static Table getConstraintTable(PlainSelect select, DBDAttributeConstraint constraint) {
    String constrTable;
    DBSAttributeBase ca = constraint.getAttribute();
    if (ca instanceof DBDAttributeBinding) {
        constrTable = ((DBDAttributeBinding) ca).getMetaAttribute().getEntityName();
    } else if (ca instanceof DBSEntityAttribute) {
        constrTable = ((DBSEntityAttribute) ca).getParentObject().getName();
    } else {
        return null;
    }
    if (constrTable == null) {
        return null;
    }
    FromItem fromItem = select.getFromItem();
    Table table = findTableInFrom(fromItem, constrTable);
    if (table == null) {
        // Maybe it is a join
        if (!CommonUtils.isEmpty(select.getJoins())) {
            for (Join join : select.getJoins()) {
                table = findTableInFrom(join.getRightItem(), constrTable);
                if (table != null) {
                    break;
                }
            }
        }
    }
    return table;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) Nullable(org.jkiss.code.Nullable)

Example 2 with DBSAttributeBase

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

the class MapAttributeTransformer method createNestedMapBindings.

private static void createNestedMapBindings(DBCSession session, DBDAttributeBinding topAttribute, List<Pair<DBSAttributeBase, Object[]>> nestedAttributes) throws DBException {
    int maxPosition = 0;
    for (Pair<DBSAttributeBase, Object[]> attr : nestedAttributes) {
        maxPosition = Math.max(maxPosition, attr.getFirst().getOrdinalPosition());
    }
    List<DBDAttributeBinding> nestedBindings = topAttribute.getNestedBindings();
    if (nestedBindings == null) {
        nestedBindings = new ArrayList<>();
    } else {
        for (DBDAttributeBinding binding : nestedBindings) {
            maxPosition = Math.max(maxPosition, binding.getOrdinalPosition());
        }
    }
    Object[] fakeRow = new Object[maxPosition + 1];
    List<Object[]> fakeRows = Collections.singletonList(fakeRow);
    for (Pair<DBSAttributeBase, Object[]> nestedAttr : nestedAttributes) {
        DBSAttributeBase attribute = nestedAttr.getFirst();
        Object[] values = nestedAttr.getSecond();
        DBDAttributeBinding nestedBinding = null;
        for (DBDAttributeBinding binding : nestedBindings) {
            if (binding.getName().equals(attribute.getName())) {
                nestedBinding = binding;
                break;
            }
        }
        if (nestedBinding == null) {
            nestedBinding = new DBDAttributeBindingType(topAttribute, attribute);
            nestedBindings.add(nestedBinding);
        }
        if (attribute.getDataKind().isComplex()) {
            // Make late binding for each row value
            for (int i = 0; i < values.length; i++) {
                if (DBUtils.isNullValue(values[i])) {
                    continue;
                }
                fakeRow[nestedBinding.getOrdinalPosition()] = values[i];
                nestedBinding.lateBinding(session, fakeRows);
            }
        }
    }
    if (!nestedBindings.isEmpty()) {
        topAttribute.setNestedBindings(nestedBindings);
    }
}
Also used : DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase)

Example 3 with DBSAttributeBase

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

the class ResultSetValueController method getValueManager.

@Override
public IValueManager getValueManager() {
    DBSAttributeBase valueType = binding.getPresentationAttribute();
    final DBCExecutionContext executionContext = getExecutionContext();
    Class<?> valueObjectType = getValueHandler().getValueObjectType(valueType);
    if (valueObjectType == Object.class) {
        // Try to get type from value itself
        Object value = getValue();
        if (value != null) {
            valueObjectType = value.getClass();
        }
    }
    return ValueManagerRegistry.findValueManager(executionContext == null ? null : executionContext.getDataSource(), valueType, valueObjectType);
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) DBSTypedObject(org.jkiss.dbeaver.model.struct.DBSTypedObject)

Example 4 with DBSAttributeBase

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

the class ResultSetModel method setDataFilter.

/**
     * Sets new data filter
     *
     * @param dataFilter data filter
     * @return true if visible attributes were changed. Spreadsheet has to be refreshed
     */
boolean setDataFilter(DBDDataFilter dataFilter) {
    this.dataFilter = dataFilter;
    // Check if filter misses some attributes
    List<DBDAttributeConstraint> newConstraints = new ArrayList<>();
    for (DBDAttributeBinding binding : attributes) {
        if (dataFilter.getConstraint(binding) == null) {
            addConstraints(newConstraints, binding);
        }
    }
    if (!newConstraints.isEmpty()) {
        dataFilter.addConstraints(newConstraints);
    }
    List<DBDAttributeBinding> newBindings = new ArrayList<>();
    for (DBSAttributeBase attr : this.dataFilter.getOrderedVisibleAttributes()) {
        DBDAttributeBinding binding = getAttributeBinding(attr);
        if (binding != null) {
            newBindings.add(binding);
        }
    }
    if (!newBindings.equals(visibleAttributes)) {
        visibleAttributes = newBindings;
        return true;
    }
    return false;
}
Also used : DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase)

Example 5 with DBSAttributeBase

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

the class MapAttributeTransformer method resolveMapsFromData.

static void resolveMapsFromData(DBCSession session, DBDAttributeBinding attribute, List<Object[]> rows) throws DBException {
    // Analyse rows and extract meta information from values
    List<Pair<DBSAttributeBase, Object[]>> valueAttributes = null;
    for (int i = 0; i < rows.size(); i++) {
        Object value = rows.get(i)[attribute.getOrdinalPosition()];
        if (value instanceof DBDCollection) {
            // Use first element to discover structure
            DBDCollection collection = (DBDCollection) value;
            if (collection.getItemCount() > 0) {
                value = collection.getItem(0);
            }
        }
        if (value instanceof DBDComposite) {
            DBSAttributeBase[] attributes = ((DBDComposite) value).getAttributes();
            for (DBSAttributeBase attr : attributes) {
                Pair<DBSAttributeBase, Object[]> attrValue = null;
                if (valueAttributes != null) {
                    for (Pair<DBSAttributeBase, Object[]> pair : valueAttributes) {
                        if (pair.getFirst().getName().equals(attr.getName())) {
                            attrValue = pair;
                            break;
                        }
                    }
                }
                if (attrValue != null) {
                    // Update attr value
                    attrValue.getSecond()[i] = ((DBDComposite) value).getAttributeValue(attr);
                } else {
                    Object[] valueList = new Object[rows.size()];
                    valueList[i] = ((DBDComposite) value).getAttributeValue(attr);
                    if (valueAttributes == null) {
                        valueAttributes = new ArrayList<>();
                    }
                    valueAttributes.add(new Pair<>(attr, valueList));
                }
            }
        }
    }
    if (valueAttributes != null && !valueAttributes.isEmpty()) {
        createNestedMapBindings(session, attribute, valueAttributes);
    }
}
Also used : DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) Pair(org.jkiss.utils.Pair)

Aggregations

DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)6 Table (net.sf.jsqlparser.schema.Table)1 Nullable (org.jkiss.code.Nullable)1 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)1 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)1 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)1 DBSTypedObject (org.jkiss.dbeaver.model.struct.DBSTypedObject)1 Pair (org.jkiss.utils.Pair)1