Search in sources :

Example 1 with StatResultSet

use of org.jkiss.dbeaver.model.impl.local.StatResultSet in project dbeaver by serge-rider.

the class ResultSetViewer method updatePresentation.

void updatePresentation(final DBCResultSet resultSet) {
    if (getControl().isDisposed()) {
        return;
    }
    boolean changed = false;
    try {
        if (resultSet instanceof StatResultSet) {
            // Statistics - let's use special presentation for it
            availablePresentations = Collections.emptyList();
            setActivePresentation(new StatisticsPresentation());
            activePresentationDescriptor = null;
            changed = true;
        } else {
            // Regular results
            IResultSetContext context = new IResultSetContext() {

                @Override
                public boolean supportsAttributes() {
                    DBDAttributeBinding[] attrs = model.getAttributes();
                    return attrs.length > 0 && (attrs[0].getDataKind() != DBPDataKind.DOCUMENT || !CommonUtils.isEmpty(attrs[0].getNestedBindings()));
                }

                @Override
                public boolean supportsDocument() {
                    return model.getDocumentAttribute() != null;
                }

                @Override
                public String getDocumentContentType() {
                    DBDAttributeBinding docAttr = model.getDocumentAttribute();
                    return docAttr == null ? null : docAttr.getValueHandler().getValueContentType(docAttr);
                }
            };
            final List<ResultSetPresentationDescriptor> newPresentations = ResultSetPresentationRegistry.getInstance().getAvailablePresentations(resultSet, context);
            changed = CommonUtils.isEmpty(this.availablePresentations) || !newPresentations.equals(this.availablePresentations);
            this.availablePresentations = newPresentations;
            if (!this.availablePresentations.isEmpty()) {
                for (ResultSetPresentationDescriptor pd : this.availablePresentations) {
                    if (pd == activePresentationDescriptor) {
                        // Keep the same presentation
                        return;
                    }
                }
                String defaultPresentationId = getPreferenceStore().getString(DBeaverPreferences.RESULT_SET_PRESENTATION);
                ResultSetPresentationDescriptor newPresentation = null;
                if (!CommonUtils.isEmpty(defaultPresentationId)) {
                    for (ResultSetPresentationDescriptor pd : this.availablePresentations) {
                        if (pd.getId().equals(defaultPresentationId)) {
                            newPresentation = pd;
                            break;
                        }
                    }
                }
                changed = true;
                if (newPresentation == null) {
                    newPresentation = this.availablePresentations.get(0);
                }
                try {
                    IResultSetPresentation instance = newPresentation.createInstance();
                    activePresentationDescriptor = newPresentation;
                    setActivePresentation(instance);
                } catch (Throwable e) {
                    log.error(e);
                }
            }
        }
    } finally {
        if (changed) {
            // Update combo
            statusBar.setRedraw(false);
            try {
                boolean pVisible = activePresentationDescriptor != null;
                ((RowData) presentationSwitchToolbar.getLayoutData()).exclude = !pVisible;
                presentationSwitchToolbar.setVisible(pVisible);
                if (!pVisible) {
                    presentationSwitchToolbar.setEnabled(false);
                } else {
                    presentationSwitchToolbar.setEnabled(true);
                    for (ToolItem item : presentationSwitchToolbar.getItems()) item.dispose();
                    for (ResultSetPresentationDescriptor pd : availablePresentations) {
                        ToolItem item = new ToolItem(presentationSwitchToolbar, SWT.CHECK);
                        item.setImage(DBeaverIcons.getImage(pd.getIcon()));
                        item.setText(pd.getLabel());
                        item.setToolTipText(pd.getDescription());
                        item.setData(pd);
                        if (pd == activePresentationDescriptor) {
                            item.setSelection(true);
                        }
                        item.addSelectionListener(new SelectionAdapter() {

                            @Override
                            public void widgetSelected(SelectionEvent e) {
                                if (e.widget != null && e.widget.getData() != null) {
                                    switchPresentation((ResultSetPresentationDescriptor) e.widget.getData());
                                }
                            }
                        });
                    }
                }
                statusBar.layout();
            } finally {
                // Enable redraw
                statusBar.setRedraw(true);
            }
        }
    }
}
Also used : StatResultSet(org.jkiss.dbeaver.model.impl.local.StatResultSet) StatisticsPresentation(org.jkiss.dbeaver.ui.controls.resultset.view.StatisticsPresentation)

Example 2 with StatResultSet

use of org.jkiss.dbeaver.model.impl.local.StatResultSet in project dbeaver by serge-rider.

the class SQLQueryJob method fetchExecutionResult.

private void fetchExecutionResult(@NotNull DBCSession session, @NotNull DBDDataReceiver dataReceiver, @NotNull SQLQuery query) throws DBCException {
    // Fetch fake result set
    StatResultSet fakeResultSet = new StatResultSet(session, curStatement);
    SQLQueryResult resultInfo = new SQLQueryResult(query);
    if (statistics.getStatementsCount() > 1) {
        // Multiple statements - show script statistics
        fakeResultSet.addColumn("Queries", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Updated Rows", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Execute time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Fetch time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Total time", DBPDataKind.NUMERIC);
        fakeResultSet.addColumn("Finish time", DBPDataKind.DATETIME);
        fakeResultSet.addRow(statistics.getStatementsCount(), statistics.getRowsUpdated(), statistics.getExecuteTime(), statistics.getFetchTime(), statistics.getTotalTime(), new Date());
        resultInfo.setResultSetName("Statistics");
    } else {
        // Single statement
        long updateCount = statistics.getRowsUpdated();
        if (updateCount >= 0) {
            fakeResultSet.addColumn("Query", DBPDataKind.STRING);
            fakeResultSet.addColumn("Updated Rows", DBPDataKind.NUMERIC);
            fakeResultSet.addColumn("Finish time", DBPDataKind.DATETIME);
            fakeResultSet.addRow(query.getQuery(), updateCount, new Date());
        } else {
            fakeResultSet.addColumn("Result", DBPDataKind.NUMERIC);
        }
        resultInfo.setResultSetName("Result");
    }
    fetchQueryData(session, fakeResultSet, resultInfo, dataReceiver, false);
}
Also used : SQLQueryResult(org.jkiss.dbeaver.model.sql.SQLQueryResult) StatResultSet(org.jkiss.dbeaver.model.impl.local.StatResultSet) Date(java.util.Date)

Aggregations

StatResultSet (org.jkiss.dbeaver.model.impl.local.StatResultSet)2 Date (java.util.Date)1 SQLQueryResult (org.jkiss.dbeaver.model.sql.SQLQueryResult)1 StatisticsPresentation (org.jkiss.dbeaver.ui.controls.resultset.view.StatisticsPresentation)1