Search in sources :

Example 1 with ResultSetDataContainer

use of org.jkiss.dbeaver.ui.controls.resultset.ResultSetDataContainer in project dbeaver by dbeaver.

the class DatabaseTransferProducer method transferData.

@Override
public void transferData(DBRProgressMonitor monitor, IDataTransferConsumer consumer, DatabaseProducerSettings settings) throws DBException {
    String contextTask = CoreMessages.data_transfer_wizard_job_task_export;
    DBPDataSource dataSource = getSourceObject().getDataSource();
    assert (dataSource != null);
    boolean selectiveExportFromUI = settings.isSelectedColumnsOnly() || settings.isSelectedRowsOnly();
    if (dataContainer instanceof ResultSetDataContainer) {
        ((ResultSetDataContainer) dataContainer).getOptions().setExportSelectedRows(settings.isSelectedRowsOnly());
        ((ResultSetDataContainer) dataContainer).getOptions().setExportSelectedColumns(settings.isSelectedColumnsOnly());
    }
    boolean newConnection = settings.isOpenNewConnections();
    boolean forceDataReadTransactions = Boolean.TRUE.equals(dataSource.getDataSourceFeature(FEATURE_FORCE_TRANSACTIONS));
    DBCExecutionContext context = !selectiveExportFromUI && newConnection ? dataSource.openIsolatedContext(monitor, "Data transfer producer") : dataSource.getDefaultContext(false);
    try (DBCSession session = context.openSession(monitor, DBCExecutionPurpose.UTIL, contextTask)) {
        try {
            AbstractExecutionSource transferSource = new AbstractExecutionSource(dataContainer, context, consumer);
            session.enableLogging(false);
            if (!selectiveExportFromUI && (newConnection || forceDataReadTransactions)) {
                // other complex structures only in transactional mode
                try {
                    DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
                    if (txnManager != null) {
                        txnManager.setAutoCommit(monitor, false);
                    }
                } catch (DBCException e) {
                    log.warn("Can't change auto-commit", e);
                }
            }
            long totalRows = 0;
            if (settings.isQueryRowCount() && (dataContainer.getSupportedFeatures() & DBSDataContainer.DATA_COUNT) != 0) {
                monitor.beginTask(CoreMessages.data_transfer_wizard_job_task_retrieve, 1);
                try {
                    totalRows = dataContainer.countData(transferSource, session, dataFilter);
                } catch (Throwable e) {
                    log.warn("Can't retrieve row count from '" + dataContainer.getName() + "'", e);
                    try {
                        DBCTransactionManager txnManager = DBUtils.getTransactionManager(session.getExecutionContext());
                        if (txnManager != null && !txnManager.isAutoCommit()) {
                            txnManager.rollback(session, null);
                        }
                    } catch (Throwable e1) {
                        log.warn("Error rolling back transaction", e1);
                    }
                } finally {
                    monitor.done();
                }
            }
            monitor.beginTask(CoreMessages.data_transfer_wizard_job_task_export_table_data, (int) totalRows);
            try {
                // Perform export
                if (settings.getExtractType() == DatabaseProducerSettings.ExtractType.SINGLE_QUERY) {
                    // Just do it in single query
                    dataContainer.readData(transferSource, session, consumer, dataFilter, -1, -1, DBSDataContainer.FLAG_NONE);
                } else {
                    // Read all data by segments
                    long offset = 0;
                    int segmentSize = settings.getSegmentSize();
                    for (; ; ) {
                        DBCStatistics statistics = dataContainer.readData(transferSource, session, consumer, dataFilter, offset, segmentSize, DBSDataContainer.FLAG_NONE);
                        if (statistics == null || statistics.getRowsFetched() < segmentSize) {
                            // Done
                            break;
                        }
                        offset += statistics.getRowsFetched();
                    }
                }
            } finally {
                monitor.done();
            }
        } finally {
            if (!selectiveExportFromUI && (newConnection || forceDataReadTransactions)) {
                DBCTransactionManager txnManager = DBUtils.getTransactionManager(context);
                if (txnManager != null) {
                    try {
                        txnManager.commit(session);
                    } catch (DBCException e) {
                        log.error("Can't finish transaction in data producer connection", e);
                    }
                }
            }
            if (newConnection) {
                context.close();
            }
        }
    }
}
Also used : DBPDataSource(org.jkiss.dbeaver.model.DBPDataSource) ResultSetDataContainer(org.jkiss.dbeaver.ui.controls.resultset.ResultSetDataContainer) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource)

Aggregations

DBPDataSource (org.jkiss.dbeaver.model.DBPDataSource)1 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)1 ResultSetDataContainer (org.jkiss.dbeaver.ui.controls.resultset.ResultSetDataContainer)1