use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
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, null, getExecutionContext()) {
public Exception objectProcessingError;
@Override
protected IStatus run(final DBRProgressMonitor monitor) {
final DataSourceJob curJob = this;
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
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 T object = objects.get(i);
monitor.subTask("Process " + DBUtils.getObjectFullName(object, DBPEvaluationContext.UI));
objectProcessingError = null;
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
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
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
try {
scriptListener.processObjectResults(object, statement, resultSet);
} catch (DBCException e) {
objectProcessingError = e;
}
}
});
}
if (objectProcessingError != null) {
break;
}
} else {
DBeaverUI.syncExec(new Runnable() {
@Override
public void run() {
try {
scriptListener.processObjectResults(object, statement, null);
} catch (DBCException e) {
objectProcessingError = e;
}
}
});
}
}
}
} catch (Exception e) {
objectProcessingError = e;
} finally {
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
scriptListener.endObjectProcessing(object, objectProcessingError);
}
});
}
monitor.worked(1);
}
} finally {
monitor.done();
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
scriptListener.endScriptProcessing();
}
});
}
return Status.OK_STATUS;
}
};
job.setUser(false);
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
}
});
job.schedule();
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class GenerateSQLDialog method executeSQL.
protected void executeSQL() {
final String jobName = getShell().getText();
final String[] scriptLines = generateSQLScript();
DataSourceJob job = new DataSourceJob(jobName, null, executionContext) {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try (DBCSession session = getExecutionContext().openSession(monitor, DBCExecutionPurpose.UTIL, jobName)) {
for (String line : scriptLines) {
DBCStatement statement = DBUtils.makeStatement(session, line, false);
try {
statement.executeStatement();
} finally {
statement.close();
}
}
} catch (DBCException e) {
return GeneralUtils.makeExceptionStatus(e);
}
return Status.OK_STATUS;
}
};
job.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
if (event.getResult().isOK()) {
if (onSuccess != null) {
onSuccess.run();
}
}
}
});
job.schedule();
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class DriverDependenciesTree method showVersionEditor.
private void showVersionEditor(final TreeItem item) {
disposeOldEditor();
final DBPDriverDependencies.DependencyNode dependencyNode = (DBPDriverDependencies.DependencyNode) item.getData();
if (dependencyNode == null || dependencyNode.library == null || !dependencyNode.library.isDownloadable()) {
return;
}
final List<String> allVersions = new ArrayList<>();
try {
runnableContext.run(true, true, new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
allVersions.addAll(dependencyNode.library.getAvailableVersions(monitor));
} catch (IOException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(filesTree.getShell(), "Versions", "Error reading versions", e.getTargetException());
return;
} catch (InterruptedException e) {
return;
}
final String currentVersion = dependencyNode.library.getVersion();
if (currentVersion != null && !allVersions.contains(currentVersion)) {
allVersions.add(currentVersion);
}
final Combo editor = new Combo(filesTree, SWT.DROP_DOWN | SWT.READ_ONLY);
int versionIndex = -1;
for (int i = 0; i < allVersions.size(); i++) {
String version = allVersions.get(i);
editor.add(version);
if (version.equals(currentVersion)) {
versionIndex = i;
}
}
if (versionIndex >= 0) {
editor.select(versionIndex);
}
editor.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String newVersion = editor.getItem(editor.getSelectionIndex());
disposeOldEditor();
setLibraryVersion(dependencyNode.library, newVersion);
}
});
treeEditor.setEditor(editor, item, 1);
editor.setListVisible(true);
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class DialogUtils method saveToFile.
public static void saveToFile(IValueController controller) {
if (!(controller.getValue() instanceof DBDContent)) {
log.error(CoreMessages.model_jdbc_bad_content_value_ + controller.getValue());
return;
}
Shell shell = UIUtils.getShell(controller.getValueSite());
final File saveFile = selectFileForSave(shell, controller.getValueName());
if (saveFile == null) {
return;
}
final DBDContent value = (DBDContent) controller.getValue();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
DBDContentStorage storage = value.getContents(monitor);
if (ContentUtils.isTextContent(value)) {
try (Reader cr = storage.getContentReader()) {
ContentUtils.saveContentToFile(cr, saveFile, GeneralUtils.UTF8_ENCODING, monitor);
}
} else {
try (InputStream cs = storage.getContentStream()) {
ContentUtils.saveContentToFile(cs, saveFile, monitor);
}
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
UIUtils.showErrorDialog(shell, CoreMessages.model_jdbc_could_not_save_content, //$NON-NLS-2$
CoreMessages.model_jdbc_could_not_save_content_to_file_ + saveFile.getAbsolutePath() + "'", e.getTargetException());
} catch (InterruptedException e) {
// do nothing
}
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class ERDResourceHandler method createDiagram.
public static IFile createDiagram(final EntityDiagram copyFrom, final String title, IFolder folder, DBRProgressMonitor monitor) throws DBException {
if (folder == null) {
try {
folder = getDiagramsFolder(DBeaverCore.getInstance().getProjectRegistry().getActiveProject(), true);
} catch (CoreException e) {
throw new DBException("Can't obtain folder for diagram", e);
}
}
if (folder == null) {
throw new DBException("Can't detect folder for diagram");
}
final IFile file = ContentUtils.getUniqueFile(folder, CommonUtils.escapeFileName(title), ERD_EXT);
try {
DBRRunnableWithProgress runnable = new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
EntityDiagram newDiagram = copyFrom == null ? new EntityDiagram(null, "<Diagram>") : copyFrom.copy();
newDiagram.setName(title);
newDiagram.setLayoutManualAllowed(true);
newDiagram.setLayoutManualDesired(true);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DiagramLoader.save(monitor, null, newDiagram, false, buffer);
InputStream data = new ByteArrayInputStream(buffer.toByteArray());
file.create(data, true, RuntimeUtils.getNestedMonitor(monitor));
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
};
if (monitor == null) {
DBeaverUI.runInProgressService(runnable);
} else {
runnable.run(monitor);
}
} catch (InvocationTargetException e) {
throw new DBException("Error creating diagram", e.getTargetException());
} catch (InterruptedException e) {
// interrupted
}
return file;
}
Aggregations