Search in sources :

Example 56 with DBDAttributeBinding

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

the class DataExporterMarkdownTable method printHeader.

private void printHeader(boolean separator) {
    if (confluenceFormat)
        writeDelimiter();
    writeDelimiter();
    for (int i = 0, columnsSize = columns.length; i < columnsSize; i++) {
        DBDAttributeBinding column = columns[i];
        String colName = column.getLabel();
        if (CommonUtils.isEmpty(colName)) {
            colName = column.getName();
        }
        if (!separator) {
            writeCellValue(colName);
        } else {
            for (int k = 0; k < colName.length(); k++) {
                getWriter().write('-');
            }
        }
        writeDelimiter();
        if (confluenceFormat)
            writeDelimiter();
    }
    writeRowLimit();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 57 with DBDAttributeBinding

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

the class DataExporterXML method exportRow.

@Override
public void exportRow(DBCSession session, DBCResultSet resultSet, Object[] row) throws DBException, IOException {
    PrintWriter out = getWriter();
    out.write("  <DATA_RECORD>\n");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns[i];
        String columnName = escapeXmlElementName(column.getName());
        out.write("    <" + columnName + ">");
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null);
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader reader = cs.getContentReader()) {
                            writeCellValue(reader);
                        }
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
            } finally {
                content.release();
            }
        } else {
            writeTextCell(super.getValueDisplayString(column, row[i]));
        }
        out.write("</" + columnName + ">\n");
    }
    out.write("  </DATA_RECORD>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) Reader(java.io.Reader) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage) PrintWriter(java.io.PrintWriter)

Example 58 with DBDAttributeBinding

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

the class SQLGeneratorContributor method makeResultSetContributions.

private void makeResultSetContributions(List<IContributionItem> menu, IResultSetSelection rss) {
    final IResultSetController rsv = rss.getController();
    DBSDataContainer dataContainer = rsv.getDataContainer();
    final List<DBDAttributeBinding> visibleAttributes = rsv.getModel().getVisibleAttributes();
    final DBSEntity entity = rsv.getModel().getSingleSource();
    if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null) {
        final List<ResultSetRow> selectedRows = new ArrayList<>(rss.getSelectedRows());
        if (!CommonUtils.isEmpty(selectedRows)) {
            List<IResultSetController> objects = new ArrayList<>();
            objects.add(rsv);
            List<SQLGeneratorDescriptor> generators = SQLGeneratorConfigurationRegistry.getInstance().getApplicableGenerators(objects, rsv);
            for (SQLGeneratorDescriptor gen : generators) {
                if (gen.isMultiObject() && selectedRows.size() < 2) {
                    continue;
                }
                menu.add(makeAction(gen.getLabel(), gen, objects));
            }
        }
    } else {
        // if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null)
        String message = dataContainer == null ? "no data container" : (visibleAttributes.isEmpty() ? "empty attribute list" : "can't resolve table");
        Action disabledAction = new Action("Not available - " + message) {
        };
        disabledAction.setEnabled(false);
        menu.add(new ActionContributionItem(disabledAction));
    }
}
Also used : ArrayList(java.util.ArrayList) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) IResultSetController(org.jkiss.dbeaver.ui.controls.resultset.IResultSetController) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) SQLGeneratorDescriptor(org.jkiss.dbeaver.model.sql.registry.SQLGeneratorDescriptor) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer)

Example 59 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding 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;
}
Also used : java.util(java.util) Image(org.eclipse.swt.graphics.Image) CustomTreeEditor(org.jkiss.dbeaver.ui.controls.CustomTreeEditor) ControlEnableState(org.eclipse.jface.dialogs.ControlEnableState) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) Nullable(org.jkiss.code.Nullable) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) NotNull(org.jkiss.code.NotNull) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) UIIcon(org.jkiss.dbeaver.ui.UIIcon) ResultSetMessages(org.jkiss.dbeaver.ui.controls.resultset.internal.ResultSetMessages) GridData(org.eclipse.swt.layout.GridData) FillLayout(org.eclipse.swt.layout.FillLayout) HelpEnabledDialog(org.jkiss.dbeaver.ui.dialogs.HelpEnabledDialog) TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) DBValueFormatting(org.jkiss.dbeaver.model.DBValueFormatting) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) org.eclipse.jface.viewers(org.eclipse.jface.viewers) ISharedImages(org.eclipse.ui.ISharedImages) List(java.util.List) DBIcon(org.jkiss.dbeaver.model.DBIcon) SWT(org.eclipse.swt.SWT) IHelpContextIds(org.jkiss.dbeaver.ui.IHelpContextIds) SQLUtils(org.jkiss.dbeaver.model.sql.SQLUtils) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridLayout(org.eclipse.swt.layout.GridLayout) NamedObjectPatternFilter(org.jkiss.dbeaver.ui.controls.NamedObjectPatternFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) FilteredTree(org.eclipse.ui.dialogs.FilteredTree) NamedObjectPatternFilter(org.jkiss.dbeaver.ui.controls.NamedObjectPatternFilter) FillLayout(org.eclipse.swt.layout.FillLayout) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) TreeContentProvider(org.jkiss.dbeaver.ui.controls.TreeContentProvider) java.util(java.util) GridData(org.eclipse.swt.layout.GridData)

Example 60 with DBDAttributeBinding

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

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)

Aggregations

DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)147 ArrayList (java.util.ArrayList)32 DBException (org.jkiss.dbeaver.DBException)29 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)24 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)23 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)23 List (java.util.List)18 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)16 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)14 Reader (java.io.Reader)13 PrintWriter (java.io.PrintWriter)12 GridData (org.eclipse.swt.layout.GridData)12 Nullable (org.jkiss.code.Nullable)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 Date (java.util.Date)10 SWT (org.eclipse.swt.SWT)10 NotNull (org.jkiss.code.NotNull)10 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)10 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)10 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)10