use of org.jkiss.dbeaver.model.DBPDataSource 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();
}
}
}
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class AbstractDataSourceHandler method getExecutionContext.
protected DBCExecutionContext getExecutionContext(ExecutionEvent event, boolean useEditor) {
if (useEditor) {
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (editor instanceof DBPContextProvider) {
return ((DBPContextProvider) editor).getExecutionContext();
}
return null;
}
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof DBPContextProvider) {
return ((DBPContextProvider) activePart).getExecutionContext();
}
ISelection selection = HandlerUtil.getCurrentSelection(event);
DBSObject selectedObject = NavigatorUtils.getSelectedObject(selection);
if (selectedObject != null) {
DBPDataSource dataSource = selectedObject.getDataSource();
if (dataSource != null) {
return dataSource.getDefaultContext(false);
}
}
return null;
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class SQLQueryJob method executeSingleQuery.
private boolean executeSingleQuery(@NotNull DBCSession session, @NotNull SQLScriptElement element, final boolean fireEvents) {
if (element instanceof SQLControlCommand) {
try {
return executeControlCommand((SQLControlCommand) element);
} catch (Throwable e) {
if (!(e instanceof DBException)) {
log.error("Unexpected error while processing SQL command", e);
}
lastError = e;
return false;
}
}
SQLQuery sqlQuery = (SQLQuery) element;
lastError = null;
if (!skipConfirmation && getDataSourceContainer().getConnectionConfiguration().getConnectionType().isConfirmExecute()) {
// Validate all transactional queries
if (!SQLSemanticProcessor.isSelectQuery(element.getText())) {
int confirmResult = confirmQueryExecution((SQLQuery) element, queries.size() > 1);
switch(confirmResult) {
case IDialogConstants.NO_ID:
return true;
case IDialogConstants.YES_ID:
break;
case IDialogConstants.YES_TO_ALL_ID:
skipConfirmation = true;
break;
default:
return false;
}
}
}
final DBCExecutionContext executionContext = getExecutionContext();
final DBPDataSource dataSource = executionContext.getDataSource();
final SQLQuery originalQuery = sqlQuery;
long startTime = System.currentTimeMillis();
boolean startQueryAlerted = false;
if (!prepareStatementParameters(sqlQuery)) {
return false;
}
// Modify query (filters + parameters)
// .trim();
String queryText = originalQuery.getText();
if (dataFilter != null && dataFilter.hasFilters() && dataSource instanceof SQLDataSource) {
String filteredQueryText = ((SQLDataSource) dataSource).getSQLDialect().addFiltersToQuery(dataSource, queryText, dataFilter);
sqlQuery = new SQLQuery(executionContext.getDataSource(), filteredQueryText, sqlQuery);
} else {
sqlQuery = new SQLQuery(executionContext.getDataSource(), queryText, sqlQuery);
}
final SQLQueryResult curResult = new SQLQueryResult(sqlQuery);
if (rsOffset > 0) {
curResult.setRowOffset(rsOffset);
}
try {
// Prepare statement
closeStatement();
// Check and invalidate connection
if (!connectionInvalidated && dataSource.getContainer().getPreferenceStore().getBoolean(DBeaverPreferences.STATEMENT_INVALIDATE_BEFORE_EXECUTE)) {
executionContext.invalidateContext(session.getProgressMonitor(), true);
connectionInvalidated = true;
}
statistics.setQueryText(sqlQuery.getText());
// Notify query start
if (fireEvents && listener != null) {
// Notify query start
try {
listener.onStartQuery(session, sqlQuery);
} catch (Exception e) {
log.error(e);
}
startQueryAlerted = true;
}
startTime = System.currentTimeMillis();
SQLQuery execStatement = sqlQuery;
long execStartTime = startTime;
DBUtils.tryExecuteRecover(session, session.getDataSource(), param -> {
try {
executeStatement(session, execStatement, execStartTime, curResult);
} catch (Throwable e) {
throw new InvocationTargetException(e);
}
});
} catch (Throwable ex) {
if (!(ex instanceof DBException)) {
log.error("Unexpected error while processing SQL", ex);
}
curResult.setError(ex);
lastError = ex;
} finally {
curResult.setQueryTime(System.currentTimeMillis() - startTime);
if (fireEvents && listener != null && startQueryAlerted) {
// Notify query end
try {
listener.onEndQuery(session, curResult);
} catch (Exception e) {
log.error(e);
}
}
}
if (curResult.getError() != null && errorHandling != SQLScriptErrorHandling.IGNORE) {
return false;
}
// Success
lastGoodQuery = originalQuery;
return true;
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class InvalidateJob method run.
@Override
protected IStatus run(DBRProgressMonitor monitor) {
DBPDataSource dataSource = getExecutionContext().getDataSource();
this.invalidateResults = invalidateDataSource(monitor, dataSource, true);
return Status.OK_STATUS;
}
use of org.jkiss.dbeaver.model.DBPDataSource in project dbeaver by dbeaver.
the class GenerateMultiSQLDialog method getContextFromObjects.
protected static <T extends DBSObject> DBCExecutionContext getContextFromObjects(@NotNull Collection<T> objects, boolean meta) {
Iterator<T> iterator = objects.iterator();
if (iterator.hasNext()) {
T object = iterator.next();
DBPDataSource dataSource = object.getDataSource();
return dataSource == null ? null : dataSource.getDefaultContext(meta);
}
return null;
}
Aggregations