use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by serge-rider.
the class PostgreFDWConfigWizardPageConfig method loadSettings.
private void loadSettings() {
DBPDataSourceContainer targetDataSource = getWizard().getSelectedDataSource();
if (targetDataSource == null) {
setErrorMessage("No target data source");
return;
}
// Fill FDW list
try {
getWizard().getRunnableContext().run(false, true, monitor -> {
try {
schemaList = new ArrayList<>();
schemaList.addAll(getWizard().getDatabase().getSchemas(monitor));
// Fill from both installed FDW and pre-configured FDW
fdwList = new ArrayList<>();
for (PostgreForeignDataWrapper fdw : CommonUtils.safeCollection(getWizard().getDatabase().getForeignDataWrappers(monitor))) {
PostgreFDWConfigWizard.FDWInfo fdwInfo = new PostgreFDWConfigWizard.FDWInfo();
fdwInfo.installedFDW = fdw;
fdwList.add(fdwInfo);
}
for (FDWConfigDescriptor fdw : FDWConfigRegistry.getInstance().getConfigDescriptors()) {
boolean found = false;
for (PostgreFDWConfigWizard.FDWInfo fdwInfo : fdwList) {
if (fdwInfo.getId().equals(fdw.getFdwId())) {
fdwInfo.fdwDescriptor = fdw;
found = true;
break;
}
}
if (!found) {
PostgreFDWConfigWizard.FDWInfo fdwInfo = new PostgreFDWConfigWizard.FDWInfo();
fdwInfo.fdwDescriptor = fdw;
fdwList.add(fdwInfo);
}
}
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
log.debug(e.getTargetException());
setErrorMessage(e.getTargetException().getMessage());
return;
} catch (InterruptedException e) {
return;
}
setErrorMessage(null);
// Detect FDW from target container
PostgreFDWConfigWizard.FDWInfo fdwInfo = getWizard().getSelectedFDW();
if (fdwInfo == null) {
FDWConfigDescriptor fdwConfig = FDWConfigRegistry.getInstance().findFirstMatch(targetDataSource);
if (fdwConfig != null) {
for (PostgreFDWConfigWizard.FDWInfo fdw : fdwList) {
if (fdw.fdwDescriptor == fdwConfig) {
fdwInfo = fdw;
break;
}
}
}
}
fdwCombo.removeAll();
for (PostgreFDWConfigWizard.FDWInfo fdw : fdwList) {
String fdwName = fdw.getId();
if (!CommonUtils.isEmpty(fdw.getDescription())) {
fdwName += " (" + fdw.getDescription() + ")";
}
fdwCombo.add(fdwName);
}
if (fdwInfo != null) {
getWizard().setSelectedFDW(fdwInfo);
fdwCombo.setText(fdwInfo.getId());
}
schemaCombo.removeAll();
for (PostgreSchema schema : schemaList) {
schemaCombo.add(schema.getName());
}
PostgreSchema selectedSchema = getWizard().getSelectedSchema();
if (selectedSchema != null) {
schemaCombo.setText(selectedSchema.getName());
} else {
PostgreSchema publicSchema = DBUtils.findObject(schemaList, PostgreConstants.PUBLIC_SCHEMA_NAME);
if (publicSchema != null) {
schemaCombo.setText(publicSchema.getName());
getWizard().setSelectedSchema(publicSchema);
}
}
refreshFDWProperties();
if (CommonUtils.isEmpty(fdwServerText.getText())) {
String fdwServerId = (fdwInfo == null ? targetDataSource.getDriver().getId() : fdwInfo.getId()) + "_srv";
getWizard().setFdwServerId(fdwServerId);
fdwServerText.setText(fdwServerId);
}
// Fill entities
targetDataSourceText.setText(targetDataSource.getName());
targetDriverText.setText(targetDataSource.getDriver().getName());
entityTable.removeAll();
for (DBNDatabaseNode entityNode : getWizard().getSelectedEntities()) {
TableItem item = new TableItem(entityTable, SWT.NONE);
item.setImage(0, DBeaverIcons.getImage(entityNode.getNodeIconDefault()));
item.setText(0, entityNode.getNodeFullName());
}
UIUtils.packColumns(entityTable, false);
propsEditor.repackColumns();
updatePageCompletion();
}
use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by serge-rider.
the class DBVModel method handleEntityRename.
private static void handleEntityRename(DBSEntity object, String oldName, String newName) {
DBNDatabaseNode objectNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(object);
if (objectNode != null) {
String objectNodePath = objectNode.getNodeItemPath();
renameEntityInGlobalCache(objectNodePath, oldName, newName);
}
if (object.getDataSource() != null) {
DBVModel vModel = object.getDataSource().getContainer().getVirtualModel();
DBVEntity vEntity = vModel.findEntity(object, oldName, false);
if (vEntity != null) {
vEntity.handleRename(oldName, newName);
vEntity.persistConfiguration();
}
}
}
use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode 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, 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.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by serge-rider.
the class EditForeignKeyPage method loadTableList.
private void loadTableList(DBNDatabaseNode newContainerNode) {
tableList.removeAll();
final List<DBNDatabaseNode> entities = new ArrayList<>();
try {
UIUtils.runInProgressService(monitor -> {
try {
loadEntities(monitor, entities, newContainerNode);
} catch (DBException e) {
throw new InvocationTargetException(e);
}
});
} catch (InvocationTargetException e) {
DBWorkbench.getPlatformUI().showError("Error loading tables", "Error during table load", e);
} catch (InterruptedException e) {
// Ignore
}
for (DBNDatabaseNode entityNode : entities) {
TableItem tableItem = new TableItem(tableList, SWT.LEFT);
tableItem.setText(entityNode.getNodeName());
tableItem.setImage(DBeaverIcons.getImage(entityNode.getNodeIconDefault()));
tableItem.setData(entityNode);
}
}
use of org.jkiss.dbeaver.model.navigator.DBNDatabaseNode in project dbeaver by serge-rider.
the class DatabaseLazyEditorInput method initializeRealInput.
public IDatabaseEditorInput initializeRealInput(final DBRProgressMonitor monitor) throws DBException {
// Get the node path.
if (project != null) {
dataSourceContainer = project.getDataSourceRegistry().getDataSource(dataSourceId);
}
if (dataSourceContainer == null) {
// $NON-NLS-2$
log.error("Can't find data source '" + dataSourceId + "'");
return null;
}
if (project == null) {
project = dataSourceContainer.getRegistry().getProject();
}
final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
navigatorModel.ensureProjectLoaded(project);
// dataSourceContainer, project, nodePath, nodeName, activePageId, activeFolderId
DBPDataSource dataSource;
while (!dataSourceContainer.isConnected()) {
try {
dataSourceContainer.connect(monitor, true, true);
} catch (final DBException e) {
// Connection error
final Integer result = new UITask<Integer>() {
@Override
protected Integer runTask() {
ConnectionLostDialog clDialog = new ConnectionLostDialog(UIUtils.getActiveWorkbenchShell(), dataSourceContainer, e, "Close");
return clDialog.open();
}
}.execute();
if (result == IDialogConstants.STOP_ID) {
// Close editor
return null;
} else if (result == IDialogConstants.RETRY_ID) {
continue;
} else {
return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
}
}
break;
}
try {
dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
throw new DBException("Connection to '" + dataSourceContainer.getName() + "' canceled");
}
final DBNNode[] editorNodeResult = new DBNNode[1];
DBExecUtils.tryExecuteRecover(monitor, dataSource, param -> {
try {
DBNDataSource dsNode = (DBNDataSource) navigatorModel.getNodeByObject(monitor, this.dataSourceContainer, true);
if (dsNode == null) {
throw new DBException("Datasource '" + this.dataSourceContainer.getName() + "' navigator node not found");
}
dsNode.initializeNode(monitor, null);
editorNodeResult[0] = navigatorModel.getNodeByPath(monitor, project, nodePath);
} catch (Exception e) {
throw new InvocationTargetException(e);
}
});
DBNNode node = editorNodeResult[0];
if (node == null) {
throw new DBException("Navigator node '" + nodePath + "' not found");
}
if (node instanceof DBNDatabaseNode) {
DatabaseNodeEditorInput realInput = new DatabaseNodeEditorInput((DBNDatabaseNode) node);
realInput.setDefaultFolderId(activeFolderId);
realInput.setDefaultPageId(activePageId);
return realInput;
} else {
throw new DBException("Database node has bad type: " + node.getClass().getName());
}
} catch (DBException e) {
return new ErrorEditorInput(GeneralUtils.makeExceptionStatus(e), navigatorModel.getNodeByObject(dataSourceContainer));
}
}
Aggregations