use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.
the class DBGJDBCSession method runAsync.
/**
* Start thread for SQL command
*/
protected void runAsync(String commandSQL, String name, DBGEvent begin, DBGEvent end) throws DBGException {
workerJob = new DBGJDBCWorker(this, name, commandSQL, begin, end);
workerJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
workerJob = null;
}
});
workerJob.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.
the class ClientHomesSelector method populateHomes.
public void populateHomes(DBPDriver driver, String currentHome, boolean selectDefault) {
this.driver = driver;
this.currentHomeId = currentHome;
this.homesCombo.removeAll();
this.homeIds.clear();
Map<String, DBPNativeClientLocation> homes = new LinkedHashMap<>();
AbstractJob hlJob = new AbstractJob("Find native client homes") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
for (DBPNativeClientLocation ncl : driver.getNativeClientLocations()) {
homes.put(ncl.getName(), ncl);
}
DBPNativeClientLocationManager clientManager = driver.getNativeClientManager();
if (clientManager != null) {
for (DBPNativeClientLocation location : clientManager.findLocalClientLocations()) {
homes.putIfAbsent(location.getName(), location);
}
}
return Status.OK_STATUS;
}
};
hlJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
UIUtils.syncExec(() -> {
for (DBPNativeClientLocation location : homes.values()) {
homesCombo.add(location.getDisplayName());
homeIds.add(location.getName());
if (currentHomeId != null && location.getName().equals(currentHomeId)) {
homesCombo.select(homesCombo.getItemCount() - 1);
}
}
if (homesCombo.getItemCount() == 0) {
homesCombo.add(UIConnectionMessages.controls_client_home_selector_missing);
homeIds.add(null);
}
if (selectDefault && homesCombo.getSelectionIndex() == -1) {
homesCombo.select(0);
currentHomeId = homeIds.get(0);
}
homesCombo.add(UIConnectionMessages.controls_client_home_selector_browse);
displayClientVersion();
homesCombo.setEnabled(true);
});
super.done(event);
}
});
hlJob.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.
the class GenerateMultiSQLDialog method executeSQL.
@Override
protected void executeSQL() {
final String jobName = getShell().getText();
final SQLScriptProgressListener<T> scriptListener = getScriptListener();
final List<T> objects = getCheckedObjects();
final Map<T, List<String>> objectsSQL = new LinkedHashMap<>();
for (T object : objects) {
final List<String> lines = new ArrayList<>();
generateObjectCommand(lines, object);
objectsSQL.put(object, lines);
}
final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {
Exception objectProcessingError;
@Override
protected IStatus run(final DBRProgressMonitor monitor) {
final DataSourceJob curJob = this;
UIUtils.asyncExec(() -> scriptListener.beginScriptProcessing(curJob, objects));
monitor.beginTask(jobName, objects.size());
try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
if (isRunInSeparateTransaction()) {
commitChanges(session);
}
for (int i = 0; i < objects.size(); i++) {
if (monitor.isCanceled()) {
break;
}
final int objectNumber = i;
final T object = objects.get(i);
monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
objectProcessingError = null;
UIUtils.asyncExec(() -> scriptListener.beginObjectProcessing(object, objectNumber));
try {
final List<String> lines = objectsSQL.get(object);
for (String line : lines) {
try (final DBCStatement statement = DBUtils.makeStatement(session, line, false)) {
if (statement.executeStatement()) {
try (DBCResultSet resultSet = statement.openResultSet()) {
// Run in sync because we need result set
UIUtils.syncExec(() -> {
try {
scriptListener.processObjectResults(object, statement, resultSet);
} catch (DBCException e) {
objectProcessingError = e;
}
});
}
if (objectProcessingError != null) {
break;
}
} else {
UIUtils.syncExec(() -> {
try {
scriptListener.processObjectResults(object, statement, null);
} catch (DBCException e) {
objectProcessingError = e;
}
});
}
}
}
} catch (Exception e) {
objectProcessingError = e;
} finally {
UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
}
monitor.worked(1);
}
if (isRunInSeparateTransaction()) {
commitChanges(session);
}
} finally {
monitor.done();
UIUtils.asyncExec(scriptListener::endScriptProcessing);
}
return Status.OK_STATUS;
}
};
job.setUser(false);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (needsRefreshOnFinish()) {
List<T> objectToRefresh = new ArrayList<>(selectedObjects);
UIUtils.asyncExec(() -> {
try {
UIUtils.runInProgressDialog(monitor -> {
monitor.beginTask("Refresh objects", objectToRefresh.size());
for (T object : objectToRefresh) {
try {
DBNDatabaseNode objectNode = DBNUtils.getNodeByObject(object);
if (objectNode != null) {
objectNode.refreshNode(monitor, DBNEvent.FORCE_REFRESH);
}
} catch (Exception e) {
log.error("Error refreshing object '" + object.getName() + "'", e);
}
}
monitor.done();
});
} catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Objects refresh", "Error refreshing navigator objects", e);
}
});
}
}
});
job.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.
the class ExasolBaseTableToolDialog method executeSQL.
@Override
protected void executeSQL() {
final String jobName = getShell().getText();
final SQLScriptProgressListener<ExasolTableBase> scriptListener = getScriptListener();
final List<ExasolTableBase> objects = getCheckedObjects();
final Map<ExasolTableBase, List<String>> objectsSQL = new LinkedHashMap<>();
for (ExasolTableBase object : objects) {
final List<String> lines = new ArrayList<>();
generateObjectCommand(lines, object);
objectsSQL.put(object, lines);
}
final DataSourceJob job = new DataSourceJob(jobName, getExecutionContext()) {
Exception objectProcessingError;
@SuppressWarnings("rawtypes")
@Override
protected IStatus run(final DBRProgressMonitor monitor) {
final DataSourceJob curJob = this;
UIUtils.asyncExec(() -> scriptListener.beginScriptProcessing(curJob, objects));
monitor.beginTask(jobName, objects.size());
try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
for (int i = 0; i < objects.size(); i++) {
if (monitor.isCanceled()) {
break;
}
final int objectNumber = i;
final ExasolTableBase object = objects.get(i);
monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
objectProcessingError = null;
UIUtils.asyncExec(() -> scriptListener.beginObjectProcessing(object, objectNumber));
try {
final List<String> lines = objectsSQL.get(object);
for (String line : lines) {
try (final Statement statement = ((JDBCSession) session).getOriginal().createStatement()) {
int affectedRows = statement.executeUpdate(line);
Integer[] resultSetData = new Integer[] { affectedRows };
final LocalResultSet resultSet = new LocalResultSet<>(session, new JDBCStatementImpl<>((JDBCSession) session, statement, true));
resultSet.addColumn("ROWS_AFFECTED", DBPDataKind.NUMERIC);
resultSet.addRow((Object[]) resultSetData);
// Run in sync because we need result set
UIUtils.syncExec(() -> {
try {
scriptListener.processObjectResults(object, null, resultSet);
} catch (DBCException e) {
objectProcessingError = e;
}
});
if (objectProcessingError != null) {
break;
}
}
}
} catch (Exception e) {
objectProcessingError = e;
} finally {
UIUtils.asyncExec(() -> scriptListener.endObjectProcessing(object, objectProcessingError));
}
monitor.worked(1);
}
} finally {
monitor.done();
UIUtils.asyncExec(scriptListener::endScriptProcessing);
}
return Status.OK_STATUS;
}
};
job.setUser(false);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
}
});
job.schedule();
}
use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by dbeaver.
the class SQLEditorHandlerOpenObjectConsole method openConsole.
void openConsole(IWorkbenchWindow workbenchWindow, DBRRunnableWithResult<String> generator, SQLNavigatorContext navigatorContext, String title, boolean doRun, ISelection currentSelection) throws Exception {
UIUtils.runInUI(workbenchWindow, generator);
String sql = CommonUtils.notEmpty(generator.getResult());
DBPProject project = navigatorContext.getProject();
SQLEditorHandlerOpenEditor.checkProjectIsOpen(project);
IFolder folder = SQLEditorHandlerOpenEditor.getCurrentScriptFolder(currentSelection);
IFile scriptFile = SQLEditorUtils.createNewScript(project, folder, navigatorContext);
FileEditorInput sqlInput = new FileEditorInput(scriptFile);
SQLEditor editor = (SQLEditor) workbenchWindow.getActivePage().openEditor(sqlInput, SQLEditor.class.getName());
if (editor != null) {
editor.getDocument().set(sql);
AbstractJob execJob = new AbstractJob("Execute SQL in console") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
// So let's give it a chance and wait for 10 seconds
for (int i = 0; i < 100; i++) {
if (editor.getExecutionContext() != null) {
break;
}
RuntimeUtils.pause(100);
}
return Status.OK_STATUS;
}
};
if (doRun) {
execJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
UIUtils.syncExec(() -> editor.processSQL(false, false));
}
});
}
execJob.schedule();
}
}
Aggregations