Search in sources :

Example 11 with DBDDataFilter

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

the class ResultSetHandlerOpenWith method openResultsWith.

private static void openResultsWith(IResultSetController resultSet, DataTransferProcessorDescriptor processor) {
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    List<DBDAttributeBinding> rsSelectedAttributes = rsSelection.getSelectedAttributes();
    if (rsSelectedRows.size() > 1 || rsSelectedAttributes.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelectedAttributes) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Open " + processor.getAppName(), ModelMessages.error_not_connected_to_database);
        return;
    }
    DBPPreferenceStore preferenceStore = DBWorkbench.getPlatform().getPreferenceStore();
    String prevActiveApp = preferenceStore.getString(PARAM_ACTIVE_APP);
    if (!CommonUtils.equalObjects(prevActiveApp, processor.getFullId())) {
    // preferenceStore.setValue(PARAM_ACTIVE_APP, processor.getFullId());
    // resultSet.updateEditControls();
    // resultSet.getControl().layout(true);
    }
    AbstractJob exportJob = new AbstractJob("Open " + processor.getAppName()) {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "data-files");
                File tempFile = new File(tempDir, new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + "." + processor.getAppFileExtension());
                tempFile.deleteOnExit();
                IDataTransferProcessor processorInstance = processor.getInstance();
                if (!(processorInstance instanceof IStreamDataExporter)) {
                    return Status.CANCEL_STATUS;
                }
                IStreamDataExporter exporter = (IStreamDataExporter) processorInstance;
                StreamTransferConsumer consumer = new StreamTransferConsumer();
                StreamConsumerSettings settings = new StreamConsumerSettings();
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<String, Object> properties = new HashMap<>();
                // Default values from wizard
                IDialogSettings dtSettings = DataTransferWizard.getWizardDialogSettings();
                IDialogSettings procListSection = dtSettings.getSection("processors");
                IDialogSettings procSettings = null;
                if (procListSection != null) {
                    procSettings = procListSection.getSection("stream_consumer:" + processor.getId());
                }
                for (DBPPropertyDescriptor prop : processor.getProperties()) {
                    Object defValue = procSettings == null ? null : procSettings.get(CommonUtils.toString(prop.getId()));
                    properties.put(prop.getId(), defValue != null ? defValue : prop.getDefaultValue());
                }
                // Remove extension property (we specify file name directly)
                properties.remove(StreamConsumerSettings.PROP_FILE_EXTENSION);
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(processor.isBinaryFormat(), processor.isHTMLFormat()), exporter, properties);
                DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                // disable OpenNewconnection by default (#6432)
                producerSettings.setOpenNewConnections(false);
                producerSettings.setSelectedRowsOnly(!CommonUtils.isEmpty(options.getSelectedRows()));
                producerSettings.setSelectedColumnsOnly(!CommonUtils.isEmpty(options.getSelectedColumns()));
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open " + processor.getAppName(), "Can't open " + processor.getAppFileExtension() + " file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in " + processor.getAppName(), null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore) IDataTransferProcessor(org.jkiss.dbeaver.tools.transfer.IDataTransferProcessor) ExecutionException(org.eclipse.core.commands.ExecutionException) DBPPropertyDescriptor(org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) IStreamDataExporter(org.jkiss.dbeaver.tools.transfer.stream.IStreamDataExporter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 12 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter in project dbeaver by serge-rider.

the class DatabaseDataEditor method activatePart.

@Override
public void activatePart() {
    createResultSetView();
    IDatabaseEditorInput editorInput = getEditorInput();
    boolean suspendQuery = CommonUtils.toBoolean(editorInput.getAttribute(ATTR_SUSPEND_QUERY));
    DBDDataFilter dataFilter = (DBDDataFilter) editorInput.getAttribute(ATTR_DATA_FILTER);
    if (!loaded && !suspendQuery) {
        if (getDatabaseObject() != null && getDatabaseObject().isPersisted()) {
            resultSetView.setStatus("Query data from '" + editorInput.getDatabaseObject().getName() + "'...");
            if (dataFilter == null) {
                resultSetView.refresh();
            } else {
                resultSetView.refreshWithFilter(dataFilter);
            }
            loaded = true;
        }
    }
//resultSetView.setSelection(resultSetView.getSelection());
}
Also used : IDatabaseEditorInput(org.jkiss.dbeaver.ui.editors.IDatabaseEditorInput) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter)

Example 13 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter 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 14 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter in project dbeaver by serge-rider.

the class ResultSetHandlerCopyAs method openResultsWith.

private static void openResultsWith(IResultSetController resultSet, DataTransferProcessorDescriptor processor) {
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    List<DBDAttributeBinding> rsSelectedAttributes = rsSelection.getSelectedAttributes();
    if (rsSelectedRows.size() > 1 || rsSelectedAttributes.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelectedAttributes) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Copy as " + processor.getName(), ModelMessages.error_not_connected_to_database);
        return;
    }
    AbstractJob exportJob = new AbstractJob("Copy as " + processor.getName()) {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            monitor.beginTask("Copy data as", 3);
            try {
                monitor.subTask("Init");
                IDataTransferProcessor processorInstance = processor.getInstance();
                if (!(processorInstance instanceof IStreamDataExporter)) {
                    return Status.CANCEL_STATUS;
                }
                IStreamDataExporter exporter = (IStreamDataExporter) processorInstance;
                StreamTransferConsumer consumer = new StreamTransferConsumer();
                StreamConsumerSettings settings = new StreamConsumerSettings();
                settings.setOutputClipboard(true);
                settings.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                Map<String, Object> properties = new HashMap<>();
                for (DBPPropertyDescriptor prop : processor.getProperties()) {
                    properties.put(prop.getId(), prop.getDefaultValue());
                }
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(processor.isBinaryFormat(), processor.isHTMLFormat()), exporter, properties);
                DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
                DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
                DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
                producerSettings.setOpenNewConnections(false);
                if (resultSet.isHasMoreData()) {
                    // For long resultsets we may need to open new connection
                    UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
                    if (serviceSQL != null) {
                        producerSettings.setOpenNewConnections(serviceSQL.useIsolatedConnections(resultSet));
                    }
                }
                producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                producerSettings.setSelectedRowsOnly(!CommonUtils.isEmpty(options.getSelectedRows()));
                producerSettings.setSelectedColumnsOnly(!CommonUtils.isEmpty(options.getSelectedColumns()));
                monitor.worked(1);
                monitor.subTask("Export data");
                producer.transferData(monitor, consumer, null, producerSettings, null);
                monitor.worked(1);
                monitor.subTask("Finalize export");
                consumer.finishTransfer(monitor, false);
                consumer.finishTransfer(monitor, true);
                monitor.worked(1);
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in " + processor.getAppName(), null, e);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) IDataTransferProcessor(org.jkiss.dbeaver.tools.transfer.IDataTransferProcessor) ExecutionException(org.eclipse.core.commands.ExecutionException) DBPPropertyDescriptor(org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) IStreamDataExporter(org.jkiss.dbeaver.tools.transfer.stream.IStreamDataExporter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

Example 15 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter in project dbeaver by serge-rider.

the class GroupingResultsContainer method rebuildGrouping.

public void rebuildGrouping() throws DBException {
    if (groupAttributes.isEmpty() || groupFunctions.isEmpty()) {
        groupingViewer.showEmptyPresentation();
        return;
    }
    DBCStatistics statistics = presentation.getController().getModel().getStatistics();
    if (statistics == null) {
        throw new DBException("No main query - can't perform grouping");
    }
    DBPDataSource dataSource = dataContainer.getDataSource();
    if (dataSource == null) {
        throw new DBException("No active datasource");
    }
    SQLDialect dialect = SQLUtils.getDialectFromDataSource(dataSource);
    SQLSyntaxManager syntaxManager = new SQLSyntaxManager();
    syntaxManager.init(dialect, presentation.getController().getPreferenceStore());
    String queryText = statistics.getQueryText();
    if (queryText == null || queryText.isEmpty()) {
        DBSDataContainer dataContainer = presentation.getController().getDataContainer();
        if (dataContainer != null) {
            queryText = dataContainer.getName();
        } else {
            throw new DBException("Empty data container");
        }
    }
    for (String delimiter : syntaxManager.getStatementDelimiters()) {
        while (queryText.endsWith(delimiter)) {
            queryText = queryText.substring(0, queryText.length() - delimiter.length());
        }
    }
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    for (String func : groupFunctions) {
        sql.append(", ").append(func);
    }
    sql.append(" FROM (\n");
    sql.append(queryText);
    sql.append(") src");
    sql.append("\nGROUP BY ");
    for (int i = 0; i < groupAttributes.size(); i++) {
        if (i > 0)
            sql.append(", ");
        sql.append(DBUtils.getQuotedIdentifier(dataSource, groupAttributes.get(i)));
    }
    boolean isDefaultGrouping = groupFunctions.size() == 1 && groupFunctions.get(0).equals(DEFAULT_FUNCTION);
    boolean isShowDuplicatesOnly = dataSource.getContainer().getPreferenceStore().getBoolean(ResultSetPreferences.RS_GROUPING_SHOW_DUPLICATES_ONLY);
    if (isDefaultGrouping && isShowDuplicatesOnly) {
        sql.append("\nHAVING ").append(DEFAULT_FUNCTION).append(" > 1");
    }
    dataContainer.setGroupingQuery(sql.toString());
    DBDDataFilter dataFilter;
    if (presentation.getController().getModel().isMetadataChanged()) {
        dataFilter = new DBDDataFilter();
    } else {
        dataFilter = new DBDDataFilter(groupingViewer.getModel().getDataFilter());
    }
    String defaultSorting = dataSource.getContainer().getPreferenceStore().getString(ResultSetPreferences.RS_GROUPING_DEFAULT_SORTING);
    if (!CommonUtils.isEmpty(defaultSorting) && isDefaultGrouping) {
        if (dialect.supportsOrderByIndex()) {
            // By default sort by count in desc order
            int countPosition = groupAttributes.size() + 1;
            StringBuilder orderBy = new StringBuilder();
            orderBy.append(countPosition).append(" ").append(defaultSorting);
            for (int i = 0; i < groupAttributes.size(); i++) {
                orderBy.append(",").append(i + 1);
            }
            dataFilter.setOrder(orderBy.toString());
        } else {
            dataFilter.setOrder(groupFunctions.get(groupFunctions.size() - 1) + " " + defaultSorting);
        }
    }
    groupingViewer.setDataFilter(dataFilter, true);
// groupingViewer.refresh();
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) SQLDialect(org.jkiss.dbeaver.model.sql.SQLDialect) SQLSyntaxManager(org.jkiss.dbeaver.model.sql.SQLSyntaxManager) DBCStatistics(org.jkiss.dbeaver.model.exec.DBCStatistics) DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) DBSDataContainer(org.jkiss.dbeaver.model.struct.DBSDataContainer)

Aggregations

DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)21 DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)11 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)9 DBException (org.jkiss.dbeaver.DBException)7 ExecutionException (org.eclipse.core.commands.ExecutionException)6 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)6 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)6 IDataTransferConsumer (org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer)6 DatabaseProducerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)6 DatabaseTransferProducer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer)6 StreamConsumerSettings (org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings)6 StreamTransferConsumer (org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer)6 ArrayList (java.util.ArrayList)5 File (java.io.File)4 SimpleDateFormat (java.text.SimpleDateFormat)4 DBPPropertyDescriptor (org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor)4 IDataTransferProcessor (org.jkiss.dbeaver.tools.transfer.IDataTransferProcessor)4 IStreamDataExporter (org.jkiss.dbeaver.tools.transfer.stream.IStreamDataExporter)4 VoidProgressMonitor (org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)3 BigDecimal (java.math.BigDecimal)2