Search in sources :

Example 16 with DBDAttributeConstraint

use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.

the class FilterResetAttributeAction method run.

@Override
public void run() {
    DBDDataFilter dataFilter = new DBDDataFilter(resultSetViewer.getModel().getDataFilter());
    DBDAttributeConstraint constraint = dataFilter.getConstraint(attribute);
    if (constraint != null) {
        constraint.setCriteria(null);
        resultSetViewer.setDataFilter(dataFilter, true);
    }
}
Also used : DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint)

Example 17 with DBDAttributeConstraint

use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.

the class SchedulerJobLogEditor method getEditorDataFilter.

@Override
protected DBDDataFilter getEditorDataFilter() {
    OracleSchedulerJob job = getDatabaseObject();
    OracleTableBase logView = getJobLogView();
    if (logView == null) {
        return null;
    }
    List<DBDAttributeConstraint> constraints = new ArrayList<>();
    try {
        DBRProgressMonitor monitor = new VoidProgressMonitor();
        OracleTableColumn ownerAttr = logView.getAttribute(monitor, "OWNER");
        if (ownerAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(ownerAttr, ownerAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getOwner());
            constraints.add(ac);
        }
        OracleTableColumn jobNameAttr = logView.getAttribute(monitor, "JOB_NAME");
        if (jobNameAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(jobNameAttr, jobNameAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getName());
            constraints.add(ac);
        }
        OracleTableColumn logDateAttr = logView.getAttribute(monitor, "LOG_DATE");
        if (logDateAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(logDateAttr, logDateAttr.getOrdinalPosition());
            ac.setOrderPosition(1);
            ac.setOrderDescending(true);
            ac.setVisible(true);
            constraints.add(ac);
        }
    } catch (DBException e) {
        log.error(e);
    }
    return new DBDDataFilter(constraints);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ArrayList(java.util.ArrayList) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 18 with DBDAttributeConstraint

use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.

the class GenericFilterValueEdit method loadMultiValueList.

private void loadMultiValueList(@NotNull Collection<DBDLabelValuePair> values, boolean mergeResultsWithData) {
    if (tableViewer == null || tableViewer.getControl() == null || tableViewer.getControl().isDisposed()) {
        return;
    }
    Pattern pattern = null;
    if (!CommonUtils.isEmpty(filterPattern) && attribute.getDataKind() == DBPDataKind.STRING) {
        pattern = Pattern.compile(SQLUtils.makeLikePattern("%" + filterPattern + "%"), Pattern.CASE_INSENSITIVE);
    }
    // Get all values from actual RSV data
    boolean hasNulls = false;
    Map<Object, DBDLabelValuePair> rowData = new HashMap<>();
    for (DBDLabelValuePair pair : values) {
        final DBDLabelValuePair oldLabel = rowData.get(pair.getValue());
        if (oldLabel != null) {
            // Duplicate label for single key - may happen in case of composite foreign keys
            String multiLabel = oldLabel.getLabel() + "," + pair.getLabel();
            if (multiLabel.length() > 200) {
                multiLabel = multiLabel.substring(0, 200) + MULTI_KEY_LABEL;
            }
            rowData.put(pair.getValue(), new DBDLabelValuePair(multiLabel, pair.getValue()));
        } else {
            rowData.put(pair.getValue(), pair);
        }
    }
    if (mergeResultsWithData) {
        // Add values from fetched rows
        for (ResultSetRow row : viewer.getModel().getAllRows()) {
            Object cellValue = viewer.getModel().getCellValue(attribute, row);
            if (DBUtils.isNullValue(cellValue)) {
                hasNulls = true;
                continue;
            }
            if (!keyPresents(rowData, cellValue)) {
                String itemString = attribute.getValueHandler().getValueDisplayString(attribute, cellValue, DBDDisplayFormat.UI);
                rowData.put(cellValue, new DBDLabelValuePair(itemString, cellValue));
            }
        }
    }
    List<DBDLabelValuePair> sortedList = new ArrayList<>(rowData.values());
    if (pattern != null) {
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.UI);
            if (!pattern.matcher(itemString).matches() && (valuePair.getLabel() == null || !pattern.matcher(valuePair.getLabel()).matches())) {
                iter.remove();
            }
        }
    } else if (filterPattern != null && attribute.getDataKind() == DBPDataKind.NUMERIC) {
        // Filter numeric values
        double minValue = CommonUtils.toDouble(filterPattern);
        for (Iterator<DBDLabelValuePair> iter = sortedList.iterator(); iter.hasNext(); ) {
            final DBDLabelValuePair valuePair = iter.next();
            String itemString = attribute.getValueHandler().getValueDisplayString(attribute, valuePair.getValue(), DBDDisplayFormat.EDIT);
            double itemValue = CommonUtils.toDouble(itemString);
            if (itemValue < minValue) {
                iter.remove();
            }
        }
    }
    try {
        Collections.sort(sortedList);
    } catch (Exception e) {
        // FIXME: This may happen in some crazy cases -
        // FIXME: error "Comparison method violates its general contract!" happens in case of long strings sorting
        // FIXME: Test on sakila.film.description
        log.error("Error sorting value collection", e);
    }
    if (hasNulls) {
        boolean nullPresents = false;
        for (DBDLabelValuePair val : rowData.values()) {
            if (DBUtils.isNullValue(val.getValue())) {
                nullPresents = true;
                break;
            }
        }
        if (!nullPresents) {
            sortedList.add(0, new DBDLabelValuePair(DBValueFormatting.getDefaultValueDisplayString(null, DBDDisplayFormat.UI), null));
        }
    }
    Set<Object> checkedValues = new HashSet<>();
    for (ResultSetRow row : rows) {
        Object value = viewer.getModel().getCellValue(attribute, row);
        checkedValues.add(value);
    }
    DBDAttributeConstraint constraint = viewer.getModel().getDataFilter().getConstraint(attribute);
    if (constraint != null && constraint.getOperator() == DBCLogicalOperator.IN) {
        // checkedValues.add(constraint.getValue());
        if (constraint.getValue() instanceof Object[]) {
            Collections.addAll(checkedValues, (Object[]) constraint.getValue());
        }
    }
    checkedValues.addAll(savedValues);
    tableViewer.setInput(sortedList);
    DBDLabelValuePair firstVisibleItem = null;
    if (isCheckedTable)
        for (DBDLabelValuePair row : sortedList) {
            Object cellValue = row.getValue();
            if (checkedValues.contains(cellValue)) {
                TableItem t = (TableItem) tableViewer.testFindItem(row);
                t.setChecked(true);
                // ((CheckboxTableViewer) tableViewer).setChecked(row, true);
                if (firstVisibleItem == null) {
                    firstVisibleItem = row;
                }
            }
        }
    ViewerColumnController vcc = ViewerColumnController.getFromControl(tableViewer.getTable());
    if (vcc != null) {
        vcc.repackColumns();
    } else {
        UIUtils.packColumns(tableViewer.getTable(), true);
    }
    if (firstVisibleItem != null) {
        final Widget item = tableViewer.testFindItem(firstVisibleItem);
        if (item != null) {
            tableViewer.getTable().setSelection((TableItem) item);
            tableViewer.getTable().showItem((TableItem) item);
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ViewerColumnController(org.jkiss.dbeaver.ui.controls.ViewerColumnController) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBException(org.jkiss.dbeaver.DBException) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair)

Example 19 with DBDAttributeConstraint

use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.

the class VirtualAttributeAddAction method run.

@Override
public void run() {
    DBVEntity vEntity = resultSetViewer.getModel().getVirtualEntity(false);
    DBVEntityAttribute vAttr = new DBVEntityAttribute(vEntity, null, "vcolumn");
    if (new EditVirtualAttributePage(resultSetViewer, vAttr).edit(resultSetViewer.getControl().getShell())) {
        vAttr.setCustom(true);
        vEntity.addVirtualAttribute(vAttr);
        vEntity.persistConfiguration();
        resultSetViewer.refreshMetaData();
        DBDAttributeConstraint vAttrConstr = resultSetViewer.getModel().getDataFilter().getConstraint(vAttr, false);
        if (vAttrConstr != null) {
        }
    }
}
Also used : DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity)

Example 20 with DBDAttributeConstraint

use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.

the class SchedulerJobLogEditor method getEditorDataFilter.

@Override
protected DBDDataFilter getEditorDataFilter() {
    OracleSchedulerJob job = getDatabaseObject();
    OracleTableBase logView = getJobLogView();
    if (logView == null) {
        return null;
    }
    List<DBDAttributeConstraint> constraints = new ArrayList<>();
    try {
        DBRProgressMonitor monitor = new VoidProgressMonitor();
        OracleTableColumn ownerAttr = logView.getAttribute(monitor, "OWNER");
        if (ownerAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(ownerAttr, ownerAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getOwner());
            constraints.add(ac);
        }
        OracleTableColumn jobNameAttr = logView.getAttribute(monitor, "JOB_NAME");
        if (jobNameAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(jobNameAttr, jobNameAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getName());
            constraints.add(ac);
        }
        OracleTableColumn logDateAttr = logView.getAttribute(monitor, "LOG_DATE");
        if (logDateAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(logDateAttr, logDateAttr.getOrdinalPosition());
            ac.setOrderPosition(1);
            ac.setOrderDescending(true);
            ac.setVisible(true);
            constraints.add(ac);
        }
    } catch (DBException e) {
        log.error(e);
    }
    return new DBDDataFilter(constraints);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ArrayList(java.util.ArrayList) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Aggregations

DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)28 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)13 DBException (org.jkiss.dbeaver.DBException)7 Expression (net.sf.jsqlparser.expression.Expression)4 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)4 Table (net.sf.jsqlparser.schema.Table)4 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)4 ArrayList (java.util.ArrayList)3 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)3 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 List (java.util.List)2 Pattern (java.util.regex.Pattern)2 ControlEnableState (org.eclipse.jface.dialogs.ControlEnableState)2 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2 org.eclipse.jface.viewers (org.eclipse.jface.viewers)2 SWT (org.eclipse.swt.SWT)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2