use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.
the class SQLSemanticProcessor method patchSelectQuery.
private static boolean patchSelectQuery(DBRProgressMonitor monitor, DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException, DBException {
// WHERE
if (filter.hasConditions()) {
for (DBDAttributeConstraint co : filter.getConstraints()) {
if (co.hasCondition()) {
Table table = getConstraintTable(select, co);
if (!isValidTableColumn(monitor, dataSource, table, co)) {
table = null;
}
if (table != null) {
if (table.getAlias() != null) {
co.setEntityAlias(table.getAlias().getName());
} else {
co.setEntityAlias(table.getName());
}
} else {
co.setEntityAlias(null);
}
}
}
StringBuilder whereString = new StringBuilder();
SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
String condString = whereString.toString();
addWhereToSelect(select, condString);
}
// ORDER
if (filter.hasOrdering()) {
List<OrderByElement> orderByElements = select.getOrderByElements();
if (orderByElements == null) {
orderByElements = new ArrayList<>();
select.setOrderByElements(orderByElements);
}
for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
String columnName = co.getAttributeName();
boolean forceNumeric = filter.hasNameDuplicates(columnName) || !SQLUtils.PATTERN_SIMPLE_NAME.matcher(columnName).matches();
Expression orderExpr = getOrderConstraintExpression(monitor, dataSource, select, co, forceNumeric);
OrderByElement element = new OrderByElement();
element.setExpression(orderExpr);
if (co.isOrderDescending()) {
element.setAsc(false);
element.setAscDescPresent(true);
}
orderByElements.add(element);
}
}
return true;
}
use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.
the class FilterSettingsDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
getShell().setText(ResultSetMessages.controls_resultset_filter_title);
getShell().setImage(DBeaverIcons.getImage(UIIcon.FILTER));
Composite composite = (Composite) super.createDialogArea(parent);
TabFolder tabFolder = new TabFolder(composite, SWT.NONE);
tabFolder.setLayoutData(new GridData(GridData.FILL_BOTH));
TreeColumn criteriaColumn;
{
Composite columnsGroup = UIUtils.createPlaceholder(tabFolder, 1);
FilteredTree filteredTree = new FilteredTree(columnsGroup, SWT.SINGLE | SWT.FULL_SELECTION | SWT.CHECK, new NamedObjectPatternFilter(), true) {
@Override
protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
columnsViewer = new CheckboxTreeViewer(parent, style);
return columnsViewer;
}
};
columnsViewer.setContentProvider(new TreeContentProvider() {
@Override
public Object[] getChildren(Object parentElement) {
final java.util.List<DBDAttributeBinding> nestedBindings = ((DBDAttributeBinding) parentElement).getNestedBindings();
if (nestedBindings == null || nestedBindings.isEmpty()) {
return null;
}
final DBDAttributeBinding[] res = nestedBindings.toArray(new DBDAttributeBinding[0]);
Arrays.sort(res, activeSorter);
return res;
}
@Override
public boolean hasChildren(Object element) {
final java.util.List<DBDAttributeBinding> nestedBindings = ((DBDAttributeBinding) element).getNestedBindings();
return nestedBindings != null && !nestedBindings.isEmpty();
}
});
columnsViewer.setLabelProvider(new ColumnLabelProvider());
columnsViewer.setCheckStateProvider(new CheckStateProvider());
final Tree columnsTree = columnsViewer.getTree();
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 300;
columnsTree.setLayoutData(gd);
columnsTree.setHeaderVisible(true);
columnsTree.setLinesVisible(true);
UIUtils.createTreeColumn(columnsTree, SWT.LEFT, ResultSetMessages.controls_resultset_filter_column_name);
UIUtils.createTreeColumn(columnsTree, SWT.LEFT, "#");
UIUtils.createTreeColumn(columnsTree, SWT.LEFT, ResultSetMessages.controls_resultset_filter_column_order);
criteriaColumn = UIUtils.createTreeColumn(columnsTree, SWT.LEFT, ResultSetMessages.controls_resultset_filter_column_criteria);
treeEditor = new FilterSettingsTreeEditor(columnsTree);
columnsViewer.addCheckStateListener(event -> {
DBDAttributeConstraint constraint = getBindingConstraint((DBDAttributeBinding) event.getElement());
constraint.setVisible(event.getChecked());
});
{
ToolBar toolbar = new ToolBar(columnsGroup, SWT.HORIZONTAL | SWT.RIGHT);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.verticalIndent = 3;
toolbar.setLayoutData(gd);
toolbar.setLayout(new FillLayout());
moveTopButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_move_to_top, UIIcon.ARROW_TOP, () -> {
int selectionIndex = getSelectionIndex(columnsViewer.getTree());
moveColumns(selectionIndex, 0);
});
moveTopButton.setEnabled(false);
moveUpButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_move_up, UIIcon.ARROW_UP, () -> {
int selectionIndex = getSelectionIndex(columnsViewer.getTree());
swapColumns(selectionIndex, selectionIndex - 1);
});
moveUpButton.setEnabled(false);
moveDownButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_move_down, UIIcon.ARROW_DOWN, () -> {
int selectionIndex = getSelectionIndex(columnsViewer.getTree());
swapColumns(selectionIndex, selectionIndex + 1);
});
moveDownButton.setEnabled(false);
moveBottomButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_move_to_bottom, UIIcon.ARROW_BOTTOM, () -> {
int selectionIndex = getSelectionIndex(columnsViewer.getTree());
moveColumns(selectionIndex, getItemsCount() - 1);
});
moveBottomButton.setEnabled(false);
UIUtils.createToolBarSeparator(toolbar, SWT.VERTICAL);
createToolItem(toolbar, ResultSetMessages.dialog_toolbar_sort, UIIcon.SORT, () -> {
attributes.sort(ALPHA_SORTER);
for (int i = 0; i < attributes.size(); i++) {
final DBDAttributeConstraint constraint = getBindingConstraint(attributes.get(i));
constraint.setVisualPosition(i);
}
columnsViewer.refresh();
});
UIUtils.createToolBarSeparator(toolbar, SWT.VERTICAL);
ToolItem showAllButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_show_all, null, () -> {
for (DBDAttributeConstraint constraint : constraints) {
constraint.setVisible(true);
}
columnsViewer.refresh();
});
showAllButton.setImage(UIUtils.getShardImage(ISharedImages.IMG_ETOOL_DEF_PERSPECTIVE));
ToolItem showNoneButton = createToolItem(toolbar, ResultSetMessages.dialog_toolbar_show_none, null, () -> {
for (DBDAttributeConstraint constraint : constraints) {
constraint.setVisible(false);
}
columnsViewer.refresh();
});
showNoneButton.setImage(UIUtils.getShardImage(ISharedImages.IMG_ELCL_REMOVEALL));
createToolItem(toolbar, ResultSetMessages.dialog_toolbar_reset, UIIcon.REFRESH, () -> {
dataFilter.reset();
constraints = new ArrayList<>(dataFilter.getConstraints());
refreshData();
// columnsViewer.refresh();
// $NON-NLS-1$
orderText.setText("");
// $NON-NLS-1$
whereText.setText("");
});
columnsViewer.addSelectionChangedListener(event -> {
int selectionIndex = getSelectionIndex(columnsViewer.getTree());
moveTopButton.setEnabled(selectionIndex > 0);
moveUpButton.setEnabled(selectionIndex > 0);
moveDownButton.setEnabled(selectionIndex >= 0 && selectionIndex < getItemsCount() - 1);
moveBottomButton.setEnabled(selectionIndex >= 0 && selectionIndex < getItemsCount() - 1);
});
}
TabItem libsTab = new TabItem(tabFolder, SWT.NONE);
libsTab.setText(ResultSetMessages.controls_resultset_filter_group_columns);
libsTab.setToolTipText("Set criteria and order for individual column(s)");
libsTab.setControl(columnsGroup);
}
createCustomFilters(tabFolder);
// Fill columns
columnsViewer.setInput(attributes);
refreshData();
// Pack UI
UIUtils.asyncExec(() -> UIUtils.packColumns(columnsViewer.getTree(), true, new float[] { .45f, .05f, .05f, .45f }));
if (criteriaColumn.getWidth() < 200) {
criteriaColumn.setWidth(200);
}
if (!resultSetViewer.supportsDataFilter()) {
Label warnLabel = new Label(composite, SWT.NONE);
warnLabel.setText(ResultSetMessages.controls_resultset_filter_warning_custom_order_disabled);
warnLabel.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_RED));
}
return parent;
}
use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.
the class FilterSettingsDialog method moveColumns.
private void moveColumns(int curIndex, int newIndex) {
if (curIndex == newIndex) {
return;
}
final DBDAttributeConstraint curAttr = getBindingConstraint((DBDAttributeBinding) columnsViewer.getTree().getItem(curIndex).getData());
// Update other constraints indexes
for (DBDAttributeConstraint c : constraints) {
if (newIndex < curIndex) {
if (c.getVisualPosition() >= newIndex && c.getVisualPosition() < curIndex) {
c.setVisualPosition(c.getVisualPosition() + 1);
}
} else {
if (c.getVisualPosition() > curIndex && c.getVisualPosition() <= newIndex) {
c.setVisualPosition(c.getVisualPosition() - 1);
}
}
}
curAttr.setVisualPosition(newIndex);
refreshData();
moveTopButton.setEnabled(newIndex > 0);
moveUpButton.setEnabled(newIndex > 0);
moveDownButton.setEnabled(newIndex < getItemsCount() - 1);
moveBottomButton.setEnabled(newIndex < getItemsCount() - 1);
}
use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.
the class ResultSetFilterPanel method setCustomDataFilter.
private void setCustomDataFilter() {
DBCExecutionContext context = viewer.getExecutionContext();
if (context == null) {
return;
}
String condition = filtersText.getText();
StringBuilder currentCondition = new StringBuilder();
SQLUtils.appendConditionString(viewer.getModel().getDataFilter(), context.getDataSource(), null, currentCondition, true);
if (currentCondition.toString().trim().equals(condition.trim())) {
// The same
return;
}
DBDDataFilter newFilter = new DBDDataFilter(viewer.getModel().getDataFilter());
for (DBDAttributeConstraint ac : newFilter.getConstraints()) {
ac.setCriteria(null);
}
newFilter.setWhere(condition);
viewer.setDataFilter(newFilter, true);
// viewer.getControl().setFocus();
}
use of org.jkiss.dbeaver.model.data.DBDAttributeConstraint in project dbeaver by dbeaver.
the class FilterByAttributeAction method run.
@Override
public void run() {
Object value = type.getValue(resultSetViewer, attribute, operator, false);
if (operator.getArgumentCount() != 0 && value == null) {
return;
}
DBDDataFilter filter = new DBDDataFilter(resultSetViewer.getModel().getDataFilter());
DBDAttributeConstraint constraint = filter.getConstraint(attribute);
if (constraint != null) {
constraint.setOperator(operator);
constraint.setValue(value);
resultSetViewer.setDataFilter(filter, true);
}
}
Aggregations