Search in sources :

Example 6 with DatabaseProducerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings in project dbeaver by dbeaver.

the class DatabaseProducerPageExtractSettings method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    Composite composite = UIUtils.createComposite(parent, 1);
    final DatabaseProducerSettings settings = getWizard().getPageSettings(this, DatabaseProducerSettings.class);
    {
        Group generalSettings = UIUtils.createControlGroup(composite, DTMessages.data_transfer_wizard_output_group_progress, 4, GridData.FILL_HORIZONTAL, 0);
        Label threadsNumLabel = UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_max_threads);
        threadsNumText = new Text(generalSettings, SWT.BORDER);
        threadsNumText.setToolTipText(DTUIMessages.database_producer_page_extract_settings_threads_num_text_tooltip);
        threadsNumText.addVerifyListener(UIUtils.getIntegerVerifyListener(Locale.ENGLISH));
        threadsNumText.addModifyListener(e -> {
            try {
                getWizard().getSettings().setMaxJobCount(Integer.parseInt(threadsNumText.getText()));
            } catch (NumberFormatException e1) {
            // do nothing
            }
        });
        if (getWizard().getSettings().getDataPipes().size() < 2) {
            threadsNumLabel.setEnabled(false);
            threadsNumText.setEnabled(false);
        }
        threadsNumText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
        {
            UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_extract_type);
            rowsExtractType = new Combo(generalSettings, SWT.DROP_DOWN | SWT.READ_ONLY);
            rowsExtractType.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
            rowsExtractType.setItems(DTMessages.data_transfer_wizard_output_combo_extract_type_item_single_query, DTMessages.data_transfer_wizard_output_combo_extract_type_item_by_segments);
            rowsExtractType.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    switch(rowsExtractType.getSelectionIndex()) {
                        case EXTRACT_TYPE_SEGMENTS:
                            settings.setExtractType(DatabaseProducerSettings.ExtractType.SEGMENTS);
                            break;
                        case EXTRACT_TYPE_SINGLE_QUERY:
                            settings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                            break;
                    }
                    updatePageCompletion();
                }
            });
            segmentSizeLabel = UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_segment_size);
            segmentSizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 1, 1));
            segmentSizeText = new Text(generalSettings, SWT.BORDER);
            segmentSizeText.addModifyListener(e -> {
                try {
                    settings.setSegmentSize(Integer.parseInt(segmentSizeText.getText()));
                } catch (NumberFormatException e1) {
                // just skip it
                }
            });
            segmentSizeText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 1, 1));
        }
        newConnectionCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_new_connection, DTUIMessages.database_producer_page_extract_settings_new_connection_checkbox_tooltip, true, 4);
        newConnectionCheckbox.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                settings.setOpenNewConnections(newConnectionCheckbox.getSelection());
            }
        });
        rowCountCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_select_row_count, DTUIMessages.database_producer_page_extract_settings_row_count_checkbox_tooltip, true, 4);
        rowCountCheckbox.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                settings.setQueryRowCount(rowCountCheckbox.getSelection());
            }
        });
        fetchSizeText = UIUtils.createLabelText(generalSettings, DTUIMessages.database_producer_page_extract_settings_text_fetch_size_label, "", SWT.BORDER);
        fetchSizeText.setToolTipText(DTUIMessages.database_producer_page_extract_settings_text_fetch_size_tooltip);
        fetchSizeText.addVerifyListener(UIUtils.getIntegerVerifyListener(Locale.ENGLISH));
        fetchSizeText.addModifyListener(e -> {
            settings.setFetchSize(Integer.parseInt(fetchSizeText.getText()));
        });
        IStructuredSelection curSelection = getWizard().getCurrentSelection();
        boolean hasSelection = curSelection != null && !curSelection.isEmpty() && curSelection.getFirstElement() instanceof DBDCellValue;
        if (hasSelection) {
            selectedColumnsOnlyCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_selected_columns_only, null, false, 4);
            selectedColumnsOnlyCheckbox.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    settings.setSelectedColumnsOnly(selectedColumnsOnlyCheckbox.getSelection());
                }
            });
            selectedRowsOnlyCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_selected_rows_only, null, false, 4);
            selectedRowsOnlyCheckbox.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    settings.setSelectedRowsOnly(selectedRowsOnlyCheckbox.getSelection());
                }
            });
            SelectionAdapter listener = new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    boolean selection = selectedColumnsOnlyCheckbox.getSelection() || selectedRowsOnlyCheckbox.getSelection();
                    newConnectionCheckbox.setEnabled(!selection);
                }
            };
            selectedColumnsOnlyCheckbox.addSelectionListener(listener);
            selectedRowsOnlyCheckbox.addSelectionListener(listener);
        }
    }
    setControl(composite);
}
Also used : DTUIMessages(org.jkiss.dbeaver.tools.transfer.ui.internal.DTUIMessages) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBDCellValue(org.jkiss.dbeaver.model.data.DBDCellValue) DataTransferWizard(org.jkiss.dbeaver.tools.transfer.ui.wizard.DataTransferWizard) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Locale(java.util.Locale) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DTMessages(org.jkiss.dbeaver.tools.transfer.internal.DTMessages) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBDCellValue(org.jkiss.dbeaver.model.data.DBDCellValue) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 7 with DatabaseProducerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings in project dbeaver by dbeaver.

the class DatabaseProducerPageExtractSettings method activatePage.

@Override
public void activatePage() {
    final DatabaseProducerSettings settings = getWizard().getPageSettings(this, DatabaseProducerSettings.class);
    threadsNumText.setText(String.valueOf(getWizard().getSettings().getMaxJobCount()));
    newConnectionCheckbox.setSelection(settings.isOpenNewConnections());
    rowCountCheckbox.setSelection(settings.isQueryRowCount());
    if (segmentSizeText != null) {
        segmentSizeText.setText(String.valueOf(settings.getSegmentSize()));
        switch(settings.getExtractType()) {
            case SINGLE_QUERY:
                rowsExtractType.select(EXTRACT_TYPE_SINGLE_QUERY);
                break;
            case SEGMENTS:
                rowsExtractType.select(EXTRACT_TYPE_SEGMENTS);
                break;
        }
    }
    fetchSizeText.setText(String.valueOf(settings.getFetchSize()));
    if (selectedColumnsOnlyCheckbox != null) {
        selectedColumnsOnlyCheckbox.setSelection(settings.isSelectedColumnsOnly());
    }
    if (selectedRowsOnlyCheckbox != null) {
        selectedRowsOnlyCheckbox.setSelection(settings.isSelectedRowsOnly());
    }
    updatePageCompletion();
}
Also used : DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)

Example 8 with DatabaseProducerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings 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 9 with DatabaseProducerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings in project dbeaver by serge-rider.

the class DatabaseProducerPageExtractSettings method createControl.

@Override
public void createControl(Composite parent) {
    initializeDialogUnits(parent);
    Composite composite = UIUtils.createComposite(parent, 1);
    final DatabaseProducerSettings settings = getWizard().getPageSettings(this, DatabaseProducerSettings.class);
    {
        Group generalSettings = UIUtils.createControlGroup(composite, DTMessages.data_transfer_wizard_output_group_progress, 4, GridData.FILL_HORIZONTAL, 0);
        Label threadsNumLabel = UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_max_threads);
        threadsNumText = new Text(generalSettings, SWT.BORDER);
        threadsNumText.setToolTipText(DTUIMessages.database_producer_page_extract_settings_threads_num_text_tooltip);
        threadsNumText.addVerifyListener(UIUtils.getIntegerVerifyListener(Locale.ENGLISH));
        threadsNumText.addModifyListener(e -> {
            try {
                getWizard().getSettings().setMaxJobCount(Integer.parseInt(threadsNumText.getText()));
            } catch (NumberFormatException e1) {
            // do nothing
            }
        });
        if (getWizard().getSettings().getDataPipes().size() < 2) {
            threadsNumLabel.setEnabled(false);
            threadsNumText.setEnabled(false);
        }
        threadsNumText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
        {
            UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_extract_type);
            rowsExtractType = new Combo(generalSettings, SWT.DROP_DOWN | SWT.READ_ONLY);
            rowsExtractType.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 3, 1));
            rowsExtractType.setItems(DTMessages.data_transfer_wizard_output_combo_extract_type_item_single_query, DTMessages.data_transfer_wizard_output_combo_extract_type_item_by_segments);
            rowsExtractType.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    switch(rowsExtractType.getSelectionIndex()) {
                        case EXTRACT_TYPE_SEGMENTS:
                            settings.setExtractType(DatabaseProducerSettings.ExtractType.SEGMENTS);
                            break;
                        case EXTRACT_TYPE_SINGLE_QUERY:
                            settings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                            break;
                    }
                    updatePageCompletion();
                }
            });
            segmentSizeLabel = UIUtils.createControlLabel(generalSettings, DTMessages.data_transfer_wizard_output_label_segment_size);
            segmentSizeLabel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 1, 1));
            segmentSizeText = new Text(generalSettings, SWT.BORDER);
            segmentSizeText.addModifyListener(e -> {
                try {
                    settings.setSegmentSize(Integer.parseInt(segmentSizeText.getText()));
                } catch (NumberFormatException e1) {
                // just skip it
                }
            });
            segmentSizeText.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING, GridData.VERTICAL_ALIGN_BEGINNING, false, false, 1, 1));
        }
        newConnectionCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_new_connection, DTUIMessages.database_producer_page_extract_settings_new_connection_checkbox_tooltip, true, 4);
        newConnectionCheckbox.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                settings.setOpenNewConnections(newConnectionCheckbox.getSelection());
            }
        });
        rowCountCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_select_row_count, DTUIMessages.database_producer_page_extract_settings_row_count_checkbox_tooltip, true, 4);
        rowCountCheckbox.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                settings.setQueryRowCount(rowCountCheckbox.getSelection());
            }
        });
        fetchSizeText = UIUtils.createLabelText(generalSettings, DTUIMessages.database_producer_page_extract_settings_text_fetch_size_label, "", SWT.BORDER);
        fetchSizeText.setToolTipText(DTUIMessages.database_producer_page_extract_settings_text_fetch_size_tooltip);
        fetchSizeText.addVerifyListener(UIUtils.getIntegerVerifyListener(Locale.ENGLISH));
        fetchSizeText.addModifyListener(e -> {
            settings.setFetchSize(Integer.parseInt(fetchSizeText.getText()));
        });
        IStructuredSelection curSelection = getWizard().getCurrentSelection();
        boolean hasSelection = curSelection != null && !curSelection.isEmpty() && curSelection.getFirstElement() instanceof DBDCellValue;
        if (hasSelection) {
            selectedColumnsOnlyCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_selected_columns_only, null, false, 4);
            selectedColumnsOnlyCheckbox.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    settings.setSelectedColumnsOnly(selectedColumnsOnlyCheckbox.getSelection());
                }
            });
            selectedRowsOnlyCheckbox = UIUtils.createCheckbox(generalSettings, DTMessages.data_transfer_wizard_output_checkbox_selected_rows_only, null, false, 4);
            selectedRowsOnlyCheckbox.addSelectionListener(new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    settings.setSelectedRowsOnly(selectedRowsOnlyCheckbox.getSelection());
                }
            });
            SelectionAdapter listener = new SelectionAdapter() {

                @Override
                public void widgetSelected(SelectionEvent e) {
                    boolean selection = selectedColumnsOnlyCheckbox.getSelection() || selectedRowsOnlyCheckbox.getSelection();
                    newConnectionCheckbox.setEnabled(!selection);
                }
            };
            selectedColumnsOnlyCheckbox.addSelectionListener(listener);
            selectedRowsOnlyCheckbox.addSelectionListener(listener);
        }
    }
    setControl(composite);
}
Also used : DTUIMessages(org.jkiss.dbeaver.tools.transfer.ui.internal.DTUIMessages) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ActiveWizardPage(org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage) org.eclipse.swt.widgets(org.eclipse.swt.widgets) DBDCellValue(org.jkiss.dbeaver.model.data.DBDCellValue) DataTransferWizard(org.jkiss.dbeaver.tools.transfer.ui.wizard.DataTransferWizard) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Locale(java.util.Locale) SWT(org.eclipse.swt.SWT) SelectionEvent(org.eclipse.swt.events.SelectionEvent) GridData(org.eclipse.swt.layout.GridData) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DTMessages(org.jkiss.dbeaver.tools.transfer.internal.DTMessages) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) DBDCellValue(org.jkiss.dbeaver.model.data.DBDCellValue) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent)

Example 10 with DatabaseProducerSettings

use of org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings 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)

Aggregations

DatabaseProducerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)11 ExecutionException (org.eclipse.core.commands.ExecutionException)7 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)7 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)7 DatabaseTransferProducer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer)7 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)6 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)6 IDataTransferConsumer (org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer)6 StreamConsumerSettings (org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings)6 StreamTransferConsumer (org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer)6 File (java.io.File)5 SimpleDateFormat (java.text.SimpleDateFormat)5 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 DataExporterXLSX (org.jkiss.dbeaver.data.office.export.DataExporterXLSX)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)2