use of org.jkiss.dbeaver.model.DBPDataSourceContainer in project dbeaver by serge-rider.
the class SQLScriptTaskScriptSelectorDialog method createScriptColumns.
static void createScriptColumns(ColumnViewer viewer) {
final ILabelProvider mainLabelProvider = (ILabelProvider) viewer.getLabelProvider();
ViewerColumnController columnController = new ViewerColumnController("sqlTaskScriptViewer", viewer);
columnController.setForceAutoSize(true);
columnController.addColumn(ModelMessages.model_navigator_Name, DTUIMessages.sql_script_task_selector_dialog_column_description_script, SWT.LEFT, true, true, new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return mainLabelProvider.getText(element);
}
@Override
public Image getImage(Object element) {
return mainLabelProvider.getImage(element);
}
@Override
public String getToolTipText(Object element) {
if (mainLabelProvider instanceof IToolTipProvider) {
return ((IToolTipProvider) mainLabelProvider).getToolTipText(element);
}
return null;
}
});
columnController.addColumn(ModelMessages.model_navigator_Connection, DTUIMessages.sql_script_task_selector_dialog_column_description_script_data_source, SWT.LEFT, true, true, new ColumnLabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof DBNResource) {
Collection<DBPDataSourceContainer> containers = ((DBNResource) element).getAssociatedDataSources();
if (!CommonUtils.isEmpty(containers)) {
StringBuilder text = new StringBuilder();
for (DBPDataSourceContainer container : containers) {
if (text.length() > 0) {
text.append(", ");
}
text.append(container.getName());
}
return text.toString();
}
}
return "";
}
@Override
public Image getImage(Object element) {
return null;
}
});
columnController.createColumns(true);
}
use of org.jkiss.dbeaver.model.DBPDataSourceContainer in project dbeaver by serge-rider.
the class SQLScriptTaskPageSettings method loadSettings.
public void loadSettings() {
SQLScriptExecuteSettings settings = sqlWizard.getSettings();
List<String> scriptFiles = settings.getScriptFiles();
for (String filePath : scriptFiles) {
IFile file = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
if (file == null) {
log.debug("Script file '" + filePath + "' not found");
continue;
}
DBPProject currentProject = DBWorkbench.getPlatform().getWorkspace().getProject(file.getProject());
if (currentProject == null) {
log.debug("Project '" + file.getProject().getName() + "' not found");
continue;
}
DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(currentProject);
if (projectNode != null) {
DBNResource resource = projectNode.findResource(file);
if (resource != null) {
selectedScripts.add(resource);
}
}
}
scriptsViewer.setInput(selectedScripts);
for (DBPDataSourceContainer dataSource : settings.getDataSources()) {
DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(dataSource.getProject());
DBNDataSource dsNode = projectNode.getDatabases().getDataSource(dataSource);
if (dsNode != null) {
selectedDataSources.add(dsNode);
}
}
dataSourceViewer.setInput(selectedDataSources);
// if (!selectedDataSources.isEmpty()) {
// dataSourceTree.getCheckboxViewer().setCheckedElements(selectedDataSources.toArray());
// dataSourceTree.getCheckboxViewer().reveal(selectedDataSources.get(0));
// }
}
use of org.jkiss.dbeaver.model.DBPDataSourceContainer in project dbeaver by serge-rider.
the class SQLScriptExecuteSettings method saveConfiguration.
public void saveConfiguration(Map<String, Object> config) {
config.put("scriptFiles", scriptFiles);
List<Map<String, Object>> dsConfig = new ArrayList<>();
config.put("dataSources", dsConfig);
for (DBPDataSourceContainer ds : dataSources) {
Map<String, Object> dsInfo = new LinkedHashMap<>();
dsInfo.put("project", ds.getProject().getName());
dsInfo.put("dataSource", ds.getId());
dsConfig.add(dsInfo);
}
config.put("ignoreErrors", ignoreErrors);
config.put("dumpQueryResultsToLog", dumpQueryResultsToLog);
config.put("autoCommit", autoCommit);
}
use of org.jkiss.dbeaver.model.DBPDataSourceContainer in project dbeaver by serge-rider.
the class SQLScriptExecuteSettings method loadConfiguration.
public void loadConfiguration(DBRRunnableContext runnableContext, Map<String, Object> config) {
// Legacy config support (single datasource
String projectName = JSONUtils.getString(config, "project");
DBPProject project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
if (project != null) {
String dataSourceContainerId = JSONUtils.getString(config, "dataSourceContainer");
if (!CommonUtils.isEmpty(dataSourceContainerId)) {
DBPDataSourceContainer dataSource = project.getDataSourceRegistry().getDataSource(dataSourceContainerId);
if (dataSource != null) {
dataSources.add(dataSource);
}
}
} else {
// Modern config (datasource list)
List<Map<String, Object>> dsConfig = JSONUtils.getObjectList(config, "dataSources");
for (Map<String, Object> dsInfo : dsConfig) {
projectName = JSONUtils.getString(dsInfo, "project");
project = CommonUtils.isEmpty(projectName) ? null : DBWorkbench.getPlatform().getWorkspace().getProject(projectName);
if (project != null) {
String dataSourceContainerId = JSONUtils.getString(dsInfo, "dataSource");
if (!CommonUtils.isEmpty(dataSourceContainerId)) {
DBPDataSourceContainer dataSource = project.getDataSourceRegistry().getDataSource(dataSourceContainerId);
if (dataSource != null) {
dataSources.add(dataSource);
}
}
}
}
}
scriptFiles = JSONUtils.deserializeStringList(config, "scriptFiles");
ignoreErrors = JSONUtils.getBoolean(config, "ignoreErrors");
dumpQueryResultsToLog = JSONUtils.getBoolean(config, "dumpQueryResultsToLog");
autoCommit = JSONUtils.getBoolean(config, "autoCommit");
}
use of org.jkiss.dbeaver.model.DBPDataSourceContainer in project dbeaver by serge-rider.
the class SQLScriptExecuteHandler method runScripts.
private void runScripts(DBRProgressMonitor monitor, DBTTask task, SQLScriptExecuteSettings settings, Log log, PrintStream logStream) throws DBException {
List<DBPDataSourceContainer> dataSources = settings.getDataSources();
for (String filePath : settings.getScriptFiles()) {
IFile sqlFile = SQLScriptExecuteSettings.getWorkspaceFile(filePath);
try (InputStream sqlStream = sqlFile.getContents(true)) {
try (Reader fileReader = new InputStreamReader(sqlStream, sqlFile.getCharset())) {
String sqlScriptContent = IOUtils.readToString(fileReader);
try {
for (DBPDataSourceContainer dataSourceContainer : dataSources) {
if (!dataSourceContainer.isConnected()) {
dataSourceContainer.connect(monitor, true, true);
}
DBPDataSource dataSource = dataSourceContainer.getDataSource();
if (dataSource == null) {
throw new DBException("Can't obtain data source connection");
}
DBCExecutionContext executionContext = dataSource.getDefaultInstance().getDefaultContext(monitor, false);
log.debug("> Execute script [" + filePath + "] in [" + dataSourceContainer.getName() + "]");
DBCExecutionContextDefaults contextDefaults = executionContext.getContextDefaults();
if (contextDefaults != null) {
DBSCatalog defaultCatalog = contextDefaults.getDefaultCatalog();
if (defaultCatalog != null) {
log.debug("> Default catalog: " + defaultCatalog.getName());
}
DBSSchema defaultSchema = contextDefaults.getDefaultSchema();
if (defaultSchema != null) {
log.debug("> Default schema: " + defaultSchema.getName());
}
}
processScript(monitor, task, settings, executionContext, filePath, sqlScriptContent, log, logStream);
}
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
} catch (Throwable e) {
Throwable error = e instanceof InvocationTargetException ? ((InvocationTargetException) e).getTargetException() : e;
throw new DBException("Error executing script '" + filePath + "'", error);
}
}
}
Aggregations