use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by dbeaver.
the class SelectDataSourceDialog method createDialogArea.
@Override
protected Control createDialogArea(Composite parent) {
showConnected = getDialogBoundsSettings().getBoolean(PARAM_SHOW_CONNECTED);
showAllProjects = getDialogBoundsSettings().getBoolean(PARAM_SHOW_ALL_PROJECTS);
Composite group = (Composite) super.createDialogArea(parent);
GridData gd = new GridData(GridData.FILL_BOTH);
group.setLayoutData(gd);
rootNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot();
projectNode = null;
if (project != null) {
DBNProject projectBaseNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(project);
if (projectBaseNode != null) {
projectNode = projectBaseNode.getDatabases();
}
}
INavigatorFilter dsFilter = new INavigatorFilter() {
@Override
public boolean filterFolders() {
return true;
}
@Override
public boolean isLeafObject(Object object) {
return object instanceof DBNDataSource;
}
@Override
public boolean select(Object element) {
return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder;
}
};
DatabaseNavigatorTree dataSourceTree = new DatabaseNavigatorTree(group, getTreeRootNode(), SWT.SINGLE | SWT.BORDER, false, dsFilter) {
@Override
protected void onTreeRefresh() {
DBNNode treeRootNode = getTreeRootNode();
if (dataSource != null && projectNode != null) {
DBPDataSourceFolder dsFolder;
for (dsFolder = dataSource.getFolder(); dsFolder != null; dsFolder = dsFolder.getParent()) {
if (dsFolder.getParent() == null) {
break;
}
}
if (dsFolder != null) {
// Expand only current DS folder
DBNLocalFolder folderNode = projectNode.getFolderNode(dsFolder);
if (folderNode != null) {
expandFolders(this, folderNode);
}
} else {
// Do not expand anything
}
return;
}
expandFolders(this, treeRootNode);
}
};
gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 500;
gd.minimumHeight = 100;
gd.minimumWidth = 100;
dataSourceTree.setLayoutData(gd);
final TreeViewer treeViewer = dataSourceTree.getViewer();
final Text descriptionText = new Text(group, SWT.READ_ONLY);
descriptionText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
final Button showConnectedCheck = new Button(group, SWT.CHECK);
showConnectedCheck.setText(UINavigatorMessages.label_show_connected);
showConnectedCheck.setSelection(showConnected);
showConnectedCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showConnected = showConnectedCheck.getSelection();
treeViewer.getControl().setRedraw(false);
try {
treeViewer.refresh();
if (showConnected) {
treeViewer.expandAll();
}
} finally {
treeViewer.getControl().setRedraw(true);
}
getDialogBoundsSettings().put(PARAM_SHOW_CONNECTED, showConnected);
}
});
final Button showAllProjectsCheck = new Button(group, SWT.CHECK);
showAllProjectsCheck.setText(UINavigatorMessages.label_show_all_projects);
showAllProjectsCheck.setSelection(showAllProjects);
showAllProjectsCheck.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
showAllProjects = showAllProjectsCheck.getSelection();
treeViewer.getControl().setRedraw(false);
try {
dataSourceTree.reloadTree(getTreeRootNode());
if (showAllProjects) {
treeViewer.expandToLevel(3);
}
} finally {
treeViewer.getControl().setRedraw(true);
}
getDialogBoundsSettings().put(PARAM_SHOW_ALL_PROJECTS, showAllProjects);
}
});
if (this.dataSource != null) {
DBNDatabaseNode dsNode = DBWorkbench.getPlatform().getNavigatorModel().getNodeByObject(this.dataSource);
if (dsNode != null) {
treeViewer.setSelection(new StructuredSelection(dsNode), true);
}
}
group.setTabList(new Control[] { dataSourceTree, showConnectedCheck, showAllProjectsCheck });
treeViewer.addFilter(new ViewerFilter() {
@Override
public boolean select(Viewer viewer, Object parentElement, Object element) {
if (showConnected) {
if (element instanceof DBNDataSource) {
return ((DBNDataSource) element).getDataSource() != null;
}
if (element instanceof DBNLocalFolder) {
return ((DBNLocalFolder) element).hasConnected();
}
}
return element instanceof DBNProject || element instanceof DBNProjectDatabases || element instanceof DBNLocalFolder || element instanceof DBNDataSource;
}
});
treeViewer.addSelectionChangedListener(event -> {
IStructuredSelection structSel = (IStructuredSelection) event.getSelection();
Object selNode = structSel.isEmpty() ? null : structSel.getFirstElement();
if (selNode instanceof DBNDataSource) {
dataSource = ((DBNDataSource) selNode).getObject();
getButton(IDialogConstants.OK_ID).setEnabled(true);
String description = dataSource.getDescription();
if (description == null) {
description = dataSource.getName();
}
descriptionText.setText(description);
} else {
dataSource = null;
getButton(IDialogConstants.OK_ID).setEnabled(false);
}
});
treeViewer.addDoubleClickListener(event -> {
if (getButton(IDialogConstants.OK_ID).isEnabled()) {
okPressed();
}
});
UIUtils.asyncExec(() -> {
Point treeSize = dataSourceTree.getViewer().getTree().computeSize(SWT.DEFAULT, SWT.DEFAULT);
Point shellSize = getShell().getSize();
if (treeSize.x >= shellSize.x) {
Point shellCompSize = getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT);
getShell().setSize(shellCompSize.x, shellSize.y);
getShell().layout(true);
}
dataSourceTree.getFilterControl().setFocus();
if (showConnected) {
treeViewer.expandAll();
}
});
closeOnFocusLost(treeViewer.getControl(), dataSourceTree.getFilterControl(), descriptionText, showConnectedCheck, showAllProjectsCheck);
return group;
}
use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by dbeaver.
the class NavigatorHandlerLocalFolderCreate method createFolder.
public static boolean createFolder(IWorkbenchWindow workbenchWindow, IWorkbenchPart activePart, DBNProjectDatabases databases, final DBNLocalFolder parentFolder, final Collection<DBNDataSource> nodes, String newName) {
if (newName == null) {
newName = EnterNameDialog.chooseName(workbenchWindow.getShell(), "Folder name");
}
if (CommonUtils.isEmpty(newName)) {
return false;
}
// Create folder and refresh databases root
// DS container will reload folders on refresh
final DBPDataSourceRegistry dsRegistry = databases.getDataSourceRegistry();
DBPDataSourceFolder folder = dsRegistry.addFolder(parentFolder == null ? null : parentFolder.getFolder(), newName);
for (DBNDataSource node : nodes) {
node.setFolder(folder);
}
if (parentFolder != null && activePart instanceof NavigatorViewBase) {
final TreeViewer viewer = ((NavigatorViewBase) activePart).getNavigatorViewer();
if (viewer != null) {
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
viewer.expandToLevel(parentFolder, 1);
}
});
}
}
DBNModel.updateConfigAndRefreshDatabases(databases);
return true;
}
use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by dbeaver.
the class DataSourceDescriptorManager method createNewObject.
@Override
public DataSourceDescriptor createNewObject(DBRProgressMonitor monitor, DBECommandContext commandContext, DBPObject parent, Object copyFrom) {
if (copyFrom != null) {
DataSourceDescriptor dsTpl = (DataSourceDescriptor) copyFrom;
DBPDataSourceRegistry registry;
DBPDataSourceFolder folder = null;
if (parent instanceof DataSourceRegistry) {
registry = (DBPDataSourceRegistry) parent;
} else if (parent instanceof DBPDataSourceFolder) {
folder = (DBPDataSourceFolder) parent;
registry = folder.getDataSourceRegistry();
} else {
registry = dsTpl.getRegistry();
}
DataSourceDescriptor dataSource = new DataSourceDescriptor(registry, DataSourceDescriptor.generateNewId(dsTpl.getDriver()), dsTpl.getDriver(), new DBPConnectionConfiguration(dsTpl.getConnectionConfiguration()));
dataSource.copyFrom(dsTpl);
if (folder != null) {
dataSource.setFolder(folder);
}
// Generate new name
String origName = dsTpl.getName();
String newName = origName;
for (int i = 0; ; i++) {
if (registry.findDataSourceByName(newName) == null) {
break;
}
newName = origName + " " + (i + 1);
}
dataSource.setName(newName);
registry.addDataSource(dataSource);
} else {
DBeaverUI.asyncExec(new Runnable() {
@Override
public void run() {
CreateConnectionDialog dialog = new CreateConnectionDialog(DBeaverUI.getActiveWorkbenchWindow(), new NewConnectionWizard());
dialog.open();
}
});
}
return null;
}
use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by dbeaver.
the class ResourceUtils method createNewScript.
public static IFile createNewScript(IProject project, @Nullable IFolder folder, @Nullable DBPDataSourceContainer dataSourceContainer) throws CoreException {
final IProgressMonitor progressMonitor = new NullProgressMonitor();
// Get folder
final IFolder scriptsRootFolder = ResourceUtils.getScriptsFolder(project, true);
IFolder scriptsFolder = folder;
if (scriptsFolder == null) {
scriptsFolder = scriptsRootFolder;
}
if (!scriptsFolder.exists()) {
scriptsFolder.create(true, true, new NullProgressMonitor());
}
if (CommonUtils.equalObjects(scriptsRootFolder, scriptsFolder)) {
// We are in the root folder
if (dataSourceContainer != null) {
if (dataSourceContainer.getPreferenceStore().getBoolean(SQLPreferenceConstants.SCRIPT_CREATE_CONNECTION_FOLDERS)) {
// Create script folders according to connection folders
DBPDataSourceFolder conFolder = dataSourceContainer.getFolder();
if (conFolder != null) {
List<DBPDataSourceFolder> conFolders = new ArrayList<>();
for (DBPDataSourceFolder f = conFolder; f != null; f = f.getParent()) {
conFolders.add(0, f);
}
for (DBPDataSourceFolder f : conFolders) {
IFolder dbFolder = scriptsFolder.getFolder(CommonUtils.escapeFileName(f.getName()));
if (dbFolder != null) {
if (!dbFolder.exists()) {
dbFolder.create(true, true, progressMonitor);
}
scriptsFolder = dbFolder;
}
}
}
}
if (dataSourceContainer.getPreferenceStore().getBoolean(SQLPreferenceConstants.SCRIPT_AUTO_FOLDERS)) {
// Create special folder for connection
IFolder dbFolder = scriptsFolder.getFolder(CommonUtils.escapeFileName(dataSourceContainer.getName()));
if (dbFolder != null) {
if (!dbFolder.exists()) {
dbFolder.create(true, true, progressMonitor);
}
scriptsFolder = dbFolder;
}
}
}
}
// Make new script file
IFile tempFile = ContentUtils.getUniqueFile(scriptsFolder, "Script", SCRIPT_FILE_EXTENSION);
tempFile.create(new ByteArrayInputStream(new byte[] {}), true, progressMonitor);
// Save ds container reference
if (dataSourceContainer != null) {
EditorUtils.setFileDataSource(tempFile, dataSourceContainer);
}
return tempFile;
}
use of org.jkiss.dbeaver.model.DBPDataSourceFolder in project dbeaver by serge-rider.
the class DataSourceUtils method getDataSourceBySpec.
public static DBPDataSourceContainer getDataSourceBySpec(@NotNull DBPProject project, @NotNull String connectionSpec, @Nullable GeneralUtils.IParameterHandler parameterHandler, boolean searchByParameters, boolean createNewDataSource) {
String driverName = null, url = null, host = null, port = null, server = null, database = null, user = null, password = null, authModelId = null;
boolean showSystemObjects = false, showUtilityObjects = false, showOnlyEntities = false, hideFolders = false, hideSchemas = false, mergeEntities = false, savePassword = true;
Boolean autoCommit = null;
Map<String, String> conProperties = new HashMap<>();
Map<String, Map<String, String>> handlerProps = new HashMap<>();
Map<String, String> authProperties = new HashMap<>();
DBPDataSourceFolder folder = null;
String dsId = null, dsName = null;
DBPDataSourceRegistry dsRegistry = project == null ? null : project.getDataSourceRegistry();
if (dsRegistry == null) {
log.debug("No datasource registry for project '" + project.getName() + "'");
return null;
}
String[] conParams = connectionSpec.split("\\|");
for (String cp : conParams) {
int divPos = cp.indexOf('=');
if (divPos == -1) {
continue;
}
String paramName = cp.substring(0, divPos);
String paramValue = cp.substring(divPos + 1);
switch(paramName) {
case PARAM_ID:
dsId = paramValue;
break;
case PARAM_DRIVER:
driverName = paramValue;
break;
case PARAM_NAME:
dsName = paramValue;
break;
case PARAM_URL:
url = paramValue;
break;
case PARAM_HOST:
host = paramValue;
break;
case PARAM_PORT:
port = paramValue;
break;
case PARAM_SERVER:
server = paramValue;
break;
case PARAM_DATABASE:
database = paramValue;
break;
case PARAM_USER:
user = paramValue;
break;
case PARAM_PASSWORD:
password = paramValue;
break;
case PARAM_AUTH_MODEL:
authModelId = paramValue;
break;
case PARAM_SAVE_PASSWORD:
savePassword = CommonUtils.toBoolean(paramValue);
break;
case PARAM_SHOW_SYSTEM_OBJECTS:
showSystemObjects = CommonUtils.toBoolean(paramValue);
break;
case PARAM_SHOW_UTILITY_OBJECTS:
showUtilityObjects = CommonUtils.toBoolean(paramValue);
break;
case PARAM_SHOW_ONLY_ENTITIES:
showOnlyEntities = CommonUtils.toBoolean(paramValue);
break;
case PARAM_HIDE_FOLDERS:
hideFolders = CommonUtils.toBoolean(paramValue);
break;
case PARAM_HIDE_SCHEMAS:
hideSchemas = CommonUtils.toBoolean(paramValue);
break;
case PARAM_MERGE_ENTITIES:
mergeEntities = CommonUtils.toBoolean(paramValue);
break;
case PARAM_FOLDER:
folder = dsRegistry.getFolder(paramValue);
break;
case PARAM_AUTO_COMMIT:
autoCommit = CommonUtils.toBoolean(paramValue);
break;
default:
boolean handled = false;
if (paramName.length() > PREFIX_PROP.length() && paramName.startsWith(PREFIX_PROP)) {
paramName = paramName.substring(PREFIX_PROP.length());
conProperties.put(paramName, paramValue);
handled = true;
} else if (paramName.length() > PREFIX_AUTH_PROP.length() && paramName.startsWith(PREFIX_AUTH_PROP)) {
paramName = paramName.substring(PREFIX_AUTH_PROP.length());
authProperties.put(paramName, paramValue);
handled = true;
} else if (paramName.length() > PREFIX_HANDLER.length() && paramName.startsWith(PREFIX_HANDLER)) {
// network handler prop
paramName = paramName.substring(PREFIX_HANDLER.length());
divPos = paramName.indexOf('.');
if (divPos == -1) {
log.debug("Wrong handler parameter: '" + paramName + "'");
continue;
}
String handlerId = paramName.substring(0, divPos);
paramName = paramName.substring(divPos + 1);
Map<String, String> handlerPopMap = handlerProps.computeIfAbsent(handlerId, k -> new HashMap<>());
handlerPopMap.put(paramName, paramValue);
handled = true;
} else if (parameterHandler != null) {
handled = parameterHandler.setParameter(paramName, paramValue);
}
if (!handled) {
log.debug("Unknown connection parameter '" + paramName + "'");
}
}
}
DBPDataSourceContainer dataSource = null;
if (dsId != null) {
dataSource = dsRegistry.getDataSource(dsId);
}
if (dsName != null) {
dataSource = dsRegistry.findDataSourceByName(dsName);
}
if (dataSource != null) {
DBPConnectionConfiguration connConfig = dataSource.getConnectionConfiguration();
if (!CommonUtils.isEmpty(database))
connConfig.setDatabaseName(database);
if (!CommonUtils.isEmpty(user))
connConfig.setUserName(user);
if (!CommonUtils.isEmpty(password))
connConfig.setUserPassword(password);
if (!CommonUtils.isEmpty(conProperties))
connConfig.setProperties(conProperties);
if (!CommonUtils.isEmpty(authProperties))
connConfig.setAuthProperties(authProperties);
if (!CommonUtils.isEmpty(authModelId))
connConfig.setAuthModelId(authModelId);
return dataSource;
}
if (searchByParameters) {
// Try to find by parameters / handler props
if (url != null) {
for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
if (url.equals(ds.getConnectionConfiguration().getUrl())) {
if (user == null || user.equals(ds.getConnectionConfiguration().getUserName())) {
return ds;
}
}
}
} else {
for (DBPDataSourceContainer ds : dsRegistry.getDataSources()) {
DBPConnectionConfiguration cfg = ds.getConnectionConfiguration();
if (server != null && !server.equals(cfg.getServerName()) || host != null && !host.equals(cfg.getHostName()) || port != null && !port.equals(cfg.getHostPort()) || database != null && !database.equals(cfg.getDatabaseName()) || user != null && !user.equals(cfg.getUserName())) {
continue;
}
boolean matched = true;
if (!conProperties.isEmpty()) {
for (Map.Entry<String, String> prop : conProperties.entrySet()) {
if (!CommonUtils.equalObjects(cfg.getProperty(prop.getKey()), prop.getValue())) {
matched = false;
break;
}
}
if (!matched) {
continue;
}
}
if (!handlerProps.isEmpty()) {
for (Map.Entry<String, Map<String, String>> handlerProp : handlerProps.entrySet()) {
DBWHandlerConfiguration handler = cfg.getHandler(handlerProp.getKey());
if (handler == null) {
matched = false;
break;
}
for (Map.Entry<String, String> prop : handlerProp.getValue().entrySet()) {
if (!CommonUtils.equalObjects(handler.getProperty(prop.getKey()), prop.getValue())) {
matched = false;
break;
}
}
if (!matched) {
break;
}
}
if (!matched) {
continue;
}
}
return ds;
}
}
}
if (!createNewDataSource) {
return null;
}
if (driverName == null) {
log.error("Driver name not specified - can't create new datasource");
return null;
}
DBPDriver driver = DBWorkbench.getPlatform().getDataSourceProviderRegistry().findDriver(driverName);
if (driver == null) {
log.error("Driver '" + driverName + "' not found");
return null;
}
// Create new datasource with specified parameters
if (dsName == null) {
dsName = "Ext: " + driver.getName();
if (database != null) {
dsName += " - " + database;
} else if (server != null) {
dsName += " - " + server;
}
}
DBPConnectionConfiguration connConfig = new DBPConnectionConfiguration();
connConfig.setUrl(url);
connConfig.setHostName(host);
connConfig.setHostPort(port);
connConfig.setServerName(server);
connConfig.setDatabaseName(database);
connConfig.setUserName(user);
connConfig.setUserPassword(password);
connConfig.setProperties(conProperties);
if (!CommonUtils.isEmpty(authProperties)) {
connConfig.setAuthProperties(authProperties);
}
if (!CommonUtils.isEmpty(authModelId)) {
connConfig.setAuthModelId(authModelId);
}
if (autoCommit != null) {
connConfig.getBootstrap().setDefaultAutoCommit(autoCommit);
}
DBPDataSourceContainer newDS = dsRegistry.createDataSource(driver, connConfig);
newDS.setName(dsName);
((DataSourceDescriptor) newDS).setTemporary(true);
if (savePassword) {
newDS.setSavePassword(true);
}
if (folder != null) {
newDS.setFolder(folder);
}
DataSourceNavigatorSettings navSettings = ((DataSourceDescriptor) newDS).getNavigatorSettings();
navSettings.setShowSystemObjects(showSystemObjects);
navSettings.setShowUtilityObjects(showUtilityObjects);
navSettings.setShowOnlyEntities(showOnlyEntities);
navSettings.setHideSchemas(hideSchemas);
navSettings.setHideFolders(hideFolders);
navSettings.setMergeEntities(mergeEntities);
// ds.set
dsRegistry.addDataSource(newDS);
return newDS;
}
Aggregations