use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class ERDEditorEmbedded method loadDiagram.
@Override
protected synchronized void loadDiagram(final boolean refreshMetadata) {
final DBSObject object = getRootObject();
if (object == null) {
return;
}
if (diagramLoadingJob != null) {
// Do not start new one while old is running
return;
}
diagramLoadingJob = LoadingJob.createService(new DatabaseLoadService<EntityDiagram>("Load diagram '" + object.getName() + "'", object.getDataSource()) {
@Override
public EntityDiagram evaluate(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
if (refreshMetadata && object instanceof DBPRefreshableObject) {
try {
getEditorInput().getNavigatorNode().refreshNode(monitor, ERDEditorEmbedded.this);
} catch (DBException e) {
log.warn("Error refreshing database metadata", e);
}
}
try {
return loadFromDatabase(monitor);
} catch (DBException e) {
log.error("Error loading ER diagram", e);
}
return null;
}
}, progressControl.createLoadVisualizer());
diagramLoadingJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
diagramLoadingJob = null;
}
});
diagramLoadingJob.schedule();
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class RuntimeUtils method runTask.
public static boolean runTask(final DBRRunnableWithProgress task, String taskName, final long waitTime) {
final MonitoringTask monitoringTask = new MonitoringTask(task);
Job monitorJob = new AbstractJob(taskName) {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try {
monitoringTask.run(monitor);
} catch (InvocationTargetException e) {
log.error(getName() + " - error", e.getTargetException());
return Status.OK_STATUS;
} catch (InterruptedException e) {
// do nothing
}
return Status.OK_STATUS;
}
};
monitorJob.schedule();
// Wait for job to finish
long startTime = System.currentTimeMillis();
if (waitTime > 0) {
while (!monitoringTask.finished && System.currentTimeMillis() - startTime < waitTime) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
break;
}
}
}
return monitoringTask.finished;
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class DriverDescriptor method validateFilesPresence.
@NotNull
private List<File> validateFilesPresence(boolean resetVersions) {
boolean localLibsExists = false;
final List<DBPDriverLibrary> downloadCandidates = new ArrayList<>();
for (DBPDriverLibrary library : libraries) {
if (library.isDisabled()) {
// Nothing we can do about it
continue;
}
if (!library.matchesCurrentPlatform()) {
// Wrong OS or architecture
continue;
}
if (library.isDownloadable()) {
boolean allExists = true;
if (resetVersions) {
allExists = false;
} else {
List<DriverFileInfo> files = resolvedFiles.get(library);
if (files == null) {
allExists = false;
} else {
for (DriverFileInfo file : files) {
if (file.file == null || !file.file.exists()) {
allExists = false;
break;
}
}
}
}
if (!allExists) {
downloadCandidates.add(library);
}
} else {
localLibsExists = true;
}
}
// if (!CommonUtils.isEmpty(fileSources)) {
// for (DriverFileSource source : fileSources) {
// for (DriverFileSource.FileInfo fileInfo : source.getFiles()) {
// DriverLibraryLocal libraryLocal = new DriverLibraryLocal(this, DBPDriverLibrary.FileType.jar, fileInfo.getName());
// final File localFile = libraryLocal.getLocalFile();
// }
// }
// }
boolean downloaded = false;
if (!downloadCandidates.isEmpty() || (!localLibsExists && !fileSources.isEmpty())) {
final DriverDependencies dependencies = new DriverDependencies(downloadCandidates);
boolean downloadOk = new UITask<Boolean>() {
@Override
protected Boolean runTask() {
return DriverDownloadDialog.downloadDriverFiles(null, DriverDescriptor.this, dependencies);
}
}.execute();
if (!downloadOk) {
return Collections.emptyList();
}
if (resetVersions) {
resetDriverInstance();
/*
for (DBPDriverLibrary library : libraries) {
if (!library.isDisabled()) {
library.resetVersion();
}
}
*/
}
downloaded = true;
for (DBPDriverDependencies.DependencyNode node : dependencies.getLibraryMap()) {
List<DriverFileInfo> info = new ArrayList<>();
resolvedFiles.put(node.library, info);
collectLibraryFiles(node, info);
}
providerDescriptor.getRegistry().saveDrivers();
}
List<File> result = new ArrayList<>();
for (DBPDriverLibrary library : libraries) {
if (library.isDisabled() || !library.matchesCurrentPlatform()) {
// Wrong OS or architecture
continue;
}
if (library.isDownloadable()) {
List<DriverFileInfo> files = resolvedFiles.get(library);
if (files != null) {
for (DriverFileInfo file : files) {
result.add(file.file);
}
}
} else {
result.add(library.getLocalFile());
}
}
// Now check driver version
if (DBeaverCore.getGlobalPreferenceStore().getBoolean(DBeaverPreferences.UI_DRIVERS_VERSION_UPDATE) && !downloaded) {
// TODO: implement new version check
if (false) {
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
checkDriverVersion(monitor);
} catch (IOException e) {
throw new InvocationTargetException(e);
}
}
});
} catch (InvocationTargetException e) {
log.error(e.getTargetException());
} catch (InterruptedException e) {
// ignore
}
}
}
return result;
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class GenerateSQLContributor method makeResultSetContributions.
private void makeResultSetContributions(List<IContributionItem> menu, IResultSetSelection rss) {
final IResultSetController rsv = rss.getController();
DBSDataContainer dataContainer = rsv.getDataContainer();
final List<DBDAttributeBinding> visibleAttributes = rsv.getModel().getVisibleAttributes();
final DBSEntity entity = rsv.getModel().getSingleSource();
if (dataContainer != null && !visibleAttributes.isEmpty() && entity != null) {
final Collection<ResultSetRow> selectedRows = rss.getSelectedRows();
if (!CommonUtils.isEmpty(selectedRows)) {
menu.add(makeAction("SELECT by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("SELECT ");
boolean hasAttr = false;
for (DBSAttributeBase attr : getValueAttributes(monitor, object, keyAttributes)) {
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append("\nFROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nWHERE ");
hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
menu.add(makeAction("INSERT", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<? extends DBSAttributeBase> allAttributes = getAllAttributes(monitor, object);
sql.append("INSERT INTO ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\n(");
boolean hasAttr = false;
for (DBSAttributeBase attr : allAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
hasAttr = true;
}
sql.append(")\nVALUES(");
hasAttr = false;
for (DBSAttributeBase attr : allAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
if (binding == null) {
appendDefaultValue(sql, attr);
} else {
appendAttributeValue(rsv, sql, binding, firstRow);
}
hasAttr = true;
}
sql.append(");\n");
}
}
}));
menu.add(makeAction("UPDATE", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
Collection<? extends DBSAttributeBase> valueAttributes = getValueAttributes(monitor, object, keyAttributes);
sql.append("UPDATE ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nSET ");
boolean hasAttr = false;
for (DBSAttributeBase attr : valueAttributes) {
if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
continue;
}
if (hasAttr)
sql.append(", ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
DBDAttributeBinding binding = rsv.getModel().getAttributeBinding(attr);
if (binding == null) {
appendDefaultValue(sql, attr);
} else {
appendAttributeValue(rsv, sql, binding, firstRow);
}
hasAttr = true;
}
sql.append("\nWHERE ");
hasAttr = false;
for (DBDAttributeBinding attr : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, attr, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
menu.add(makeAction("DELETE by Unique Key", new ResultSetAnalysisRunner(dataContainer.getDataSource(), rsv.getModel()) {
@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, ResultSetModel object) throws DBException {
for (ResultSetRow firstRow : selectedRows) {
Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
sql.append("DELETE FROM ").append(DBUtils.getObjectFullName(entity, DBPEvaluationContext.DML));
sql.append("\nWHERE ");
boolean hasAttr = false;
for (DBDAttributeBinding binding : keyAttributes) {
if (hasAttr)
sql.append(" AND ");
sql.append(DBUtils.getObjectFullName(binding.getAttribute(), DBPEvaluationContext.DML)).append("=");
appendAttributeValue(rsv, sql, binding, firstRow);
hasAttr = true;
}
sql.append(";\n");
}
}
}));
}
}
}
use of org.jkiss.dbeaver.model.runtime.DBRProgressMonitor in project dbeaver by serge-rider.
the class SQLEditor method runPostExecuteActions.
private void runPostExecuteActions(@Nullable SQLQueryResult result) {
final DBCExecutionContext executionContext = getExecutionContext();
if (executionContext != null) {
final DBPDataSource dataSource = executionContext.getDataSource();
// Dump server output
DBCServerOutputReader outputReader = DBUtils.getAdapter(DBCServerOutputReader.class, dataSource);
if (outputReader == null && result != null) {
outputReader = new DefaultServerOutputReader(result);
}
if (outputReader != null && outputReader.isServerOutputEnabled()) {
dumpServerOutput(executionContext, outputReader);
}
// Refresh active object
if (result == null || !result.hasError() && getActivePreferenceStore().getBoolean(SQLPreferenceConstants.REFRESH_DEFAULTS_AFTER_EXECUTE)) {
final DBSObjectSelector objectSelector = DBUtils.getAdapter(DBSObjectSelector.class, dataSource);
if (objectSelector != null) {
new AbstractJob("Refresh default object") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Refresh default object")) {
objectSelector.refreshDefaultObject(session);
} catch (Exception e) {
log.error(e);
}
return Status.OK_STATUS;
}
}.schedule();
}
}
}
}
Aggregations