提交 df08b266 编写于 作者: S Serge Rider

#8810 SQL tool task mode. Task registry refactoring


Former-commit-id: d9a6f9e6
上级 dfd4c7c4
......@@ -55,6 +55,7 @@
<plugin id="org.jkiss.dbeaver.ext.ui.locks" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.tasks.ui" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.tasks.native.ui" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.tasks.sql.ui" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.data.transfer.ui" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.ext.import_config" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
<plugin id="org.jkiss.dbeaver.ext.format.sqlworkbenchj" download-size="0" install-size="0" version="0.0.0" unpack="false"/>
......
......@@ -370,6 +370,7 @@
<extension point="org.jkiss.dbeaver.task">
<category id="mysql" name="MySQL" description="MySQL database task" icon="icons/mysql_icon.png"/>
<category id="mysqlTool" parent="mysql" name="Tools" description="MySQL database tools" icon="icons/mysql_icon.png"/>
<task id="mysqlDatabaseBackup" name="MySQL dump" description="MySQL database export task" icon="platform:/plugin/org.jkiss.dbeaver.ui/icons/file/export.png" type="mysql" handler="org.jkiss.dbeaver.ext.mysql.tasks.MySQLDatabaseExportHandler">
<objectType name="org.jkiss.dbeaver.ext.mysql.model.MySQLCatalog"/>
......@@ -383,11 +384,9 @@
</task>
<!-- SQL tools -->
<!--
<task id="mysqlToolCheckTable" name="Check" description="Check table(s)" type="mysql" handler="org.jkiss.dbeaver.ext.mysql.tasks.MySQLScriptExecuteHandler">
<task id="mysqlToolCheckTable" name="Check table" description="Check table(s)" icon="platform:/plugin/org.jkiss.dbeaver.model/icons/tree/admin.png" type="mysqlTool" handler="org.jkiss.dbeaver.ext.mysql.tasks.MySQLToolTableCheck">
<objectType name="org.jkiss.dbeaver.ext.mysql.model.MySQLTable"/>
</task>
-->
</extension>
......
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 DBeaver Corp and others
* Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.mysql.tasks;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.ext.mysql.model.MySQLTableBase;
import org.jkiss.dbeaver.model.DBPEvaluationContext;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.sql.task.SQLToolExecuteHandler;
import org.jkiss.utils.CommonUtils;
import java.util.List;
/**
* Table check
*/
public class MySQLToolTableCheck extends SQLToolExecuteHandler<MySQLTableBase, MySQLToolTableCheckSettings> {
@NotNull
@Override
protected MySQLToolTableCheckSettings createToolSettings() {
return new MySQLToolTableCheckSettings();
}
public void generateObjectQueries(DBCSession session, MySQLToolTableCheckSettings settings, List<String> queries, MySQLTableBase object) {
String sql = "CHECK TABLE " + object.getFullyQualifiedName(DBPEvaluationContext.DDL);
String option = settings.getOption();
if (!CommonUtils.isEmpty(option)) sql += " " + option;
queries.add(sql);
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 DBeaver Corp and others
* Copyright (C) 2011-2012 Eugene Fradkin (eugene.fradkin@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.ext.mysql.tasks;
import org.jkiss.dbeaver.ext.mysql.model.MySQLTableBase;
import org.jkiss.dbeaver.model.meta.IPropertyValueListProvider;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.sql.task.SQLToolExecuteSettings;
/**
* Table check settings
*/
public class MySQLToolTableCheckSettings extends SQLToolExecuteSettings<MySQLTableBase> {
private String option;
@Property(listProvider = CheckOptionListProvider.class)
public String getOption() {
return option;
}
public void setOption(String option) {
this.option = option;
}
private static class CheckOptionListProvider implements IPropertyValueListProvider<String> {
@Override
public boolean allowCustomValue() {
return false;
}
@Override
public Object[] getPossibleValues(String object) {
return new String[] {
"",
"FOR UPGRADE",
"QUICK",
"FAST",
"MEDIUM",
"EXTENDED",
"CHANGED"
};
}
}
}
......@@ -22,6 +22,7 @@ Export-Package: org.jkiss.dbeaver.model.sql,
org.jkiss.dbeaver.model.sql.parser.rules,
org.jkiss.dbeaver.model.sql.parser.tokens,
org.jkiss.dbeaver.model.sql.registry,
org.jkiss.dbeaver.model.sql.task,
org.jkiss.dbeaver.model.text,
org.jkiss.dbeaver.model.text.parser,
org.jkiss.dbeaver.model.text.parser.rules
......
......@@ -19,24 +19,29 @@ package org.jkiss.dbeaver.model.sql.task;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBUtils;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.exec.DBCSession;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.struct.DBSObject;
import org.jkiss.dbeaver.model.task.DBTTask;
import org.jkiss.dbeaver.model.task.DBTTaskExecutionListener;
import org.jkiss.dbeaver.model.task.DBTTaskHandler;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* SQLToolExecuteHandler
*/
public abstract class SQLToolExecuteHandler implements DBTTaskHandler {
public abstract class SQLToolExecuteHandler<OBJECT_TYPE extends DBSObject, SETTINGS extends SQLToolExecuteSettings<OBJECT_TYPE>> implements DBTTaskHandler {
@Override
public void executeTask(
public final void executeTask(
@NotNull DBRRunnableContext runnableContext,
@NotNull DBTTask task,
@NotNull Locale locale,
......@@ -44,12 +49,12 @@ public abstract class SQLToolExecuteHandler implements DBTTaskHandler {
@NotNull Writer logStream,
@NotNull DBTTaskExecutionListener listener) throws DBException
{
SQLToolExecuteSettings settings = new SQLToolExecuteSettings();
SETTINGS settings = createToolSettings();
settings.loadConfiguration(runnableContext, task.getProperties());
executeWithSettings(runnableContext, task, locale, log, logStream, listener, settings);
}
private void executeWithSettings(@NotNull DBRRunnableContext runnableContext, DBTTask task, @NotNull Locale locale, @NotNull Log log, Writer logStream, @NotNull DBTTaskExecutionListener listener, SQLToolExecuteSettings settings) throws DBException {
private void executeWithSettings(@NotNull DBRRunnableContext runnableContext, DBTTask task, @NotNull Locale locale, @NotNull Log log, Writer logStream, @NotNull DBTTaskExecutionListener listener, SETTINGS settings) throws DBException {
// Start consumers
listener.taskStarted(settings);
......@@ -73,9 +78,20 @@ public abstract class SQLToolExecuteHandler implements DBTTaskHandler {
listener.taskFinished(settings, error);
}
private void executeTool(DBRProgressMonitor monitor, DBTTask task, SQLToolExecuteSettings settings, Log log, Writer logStream) throws DBException {
List objectList = settings.getObjectList();
private void executeTool(DBRProgressMonitor monitor, DBTTask task, SETTINGS settings, Log log, Writer logStream) throws DBException {
List<String> queries = new ArrayList<>();
List<OBJECT_TYPE> objectList = settings.getObjectList();
for (OBJECT_TYPE object : objectList) {
try (DBCSession session = DBUtils.openMetaSession(monitor, object, "Generate tool queries")) {
generateObjectQueries(session, settings, queries, object);
}
}
}
@NotNull
protected abstract SETTINGS createToolSettings();
protected abstract void generateObjectQueries(DBCSession session, SETTINGS settings, List<String> queries, OBJECT_TYPE object) throws DBCException;
}
......@@ -53,4 +53,6 @@ public interface DBTTaskType {
@NotNull
DBTTaskHandler createHandler() throws DBException;
Class<? extends DBTTaskHandler> getHandlerClass();
}
......@@ -122,6 +122,11 @@ public class TaskTypeDescriptor extends AbstractContextDescriptor implements DBT
return handlerImplType.createInstance(DBTTaskHandler.class);
}
@Override
public Class<? extends DBTTaskHandler> getHandlerClass() {
return handlerImplType.getObjectClass(DBTTaskHandler.class);
}
@Override
public String toString() {
return getId();
......
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Vendor: DBeaver Corp
Bundle-Name: DBeaver Tasks - SQL Tools UI
Bundle-SymbolicName: org.jkiss.dbeaver.tasks.sql.ui;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-Release-Date: 20200601
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.swt,
org.eclipse.core.resources,
org.eclipse.jface,
org.eclipse.jface.text,
org.eclipse.ui,
org.eclipse.ui.editors,
org.eclipse.ui.views,
org.eclipse.ui.workbench,
org.eclipse.ui.workbench.texteditor,
org.jkiss.dbeaver.model,
org.jkiss.dbeaver.model.sql,
org.jkiss.dbeaver.registry,
org.jkiss.dbeaver.tasks.ui,
org.jkiss.dbeaver.ui
Bundle-ClassPath: .
Automatic-Module-Name: org.jkiss.dbeaver.tasks.sql.ui
source.. = src/
output.. = target/classes/
bin.includes = .,\
META-INF/,\
OSGI-INF/,\
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
<extension point="org.jkiss.dbeaver.task.ui">
<configurator handler="org.jkiss.dbeaver.model.sql.task.SQLToolExecuteHandler" class="org.jkiss.dbeaver.tasks.ui.sql.SQLToolTaskConfigurator"/>
</extension>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jkiss.dbeaver</groupId>
<artifactId>plugins</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>org.jkiss.dbeaver.tasks.sql.ui</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
</project>
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.tasks.ui.sql;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.sql.task.SQLToolExecuteSettings;
import org.jkiss.dbeaver.model.task.DBTTask;
import org.jkiss.dbeaver.tasks.ui.wizard.TaskConfigurationWizard;
import org.jkiss.dbeaver.ui.UIUtils;
import java.util.Map;
class SQLToolTaskConfigurationWizard extends TaskConfigurationWizard {
private SQLToolExecuteSettings settings = new SQLToolExecuteSettings();
private SQLToolTaskPageSettings pageSettings;
public SQLToolTaskConfigurationWizard() {
}
public SQLToolTaskConfigurationWizard(@NotNull DBTTask task) {
super(task);
settings.loadConfiguration(UIUtils.getDefaultRunnableContext(), task.getProperties());
}
@Override
protected String getDefaultWindowTitle() {
return "Script Execute";
}
@Override
public String getTaskTypeId() {
return getCurrentTask().getType().getId();
}
@Override
public void addPages() {
super.addPages();
pageSettings = new SQLToolTaskPageSettings(this);
addPage(pageSettings);
}
@Override
public void saveTaskState(DBRRunnableContext runnableContext, DBTTask task, Map<String, Object> state) {
//pageSettings.saveSettings();
settings.saveConfiguration(state);
}
public SQLToolExecuteSettings getSettings() {
return settings;
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.tasks.ui.sql;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.jkiss.code.NotNull;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.app.DBPProject;
import org.jkiss.dbeaver.model.runtime.DBRRunnableContext;
import org.jkiss.dbeaver.model.task.DBTTask;
import org.jkiss.dbeaver.model.task.DBTTaskType;
import org.jkiss.dbeaver.tasks.ui.DBTTaskConfigPanel;
import org.jkiss.dbeaver.tasks.ui.DBTTaskConfigurator;
import org.jkiss.dbeaver.tasks.ui.wizard.TaskConfigurationWizard;
/**
* SQL tool task configurator
*/
public class SQLToolTaskConfigurator implements DBTTaskConfigurator {
private static final Log log = Log.getLog(SQLToolTaskConfigurator.class);
@Override
public ConfigPanel createInputConfigurator(DBRRunnableContext runnableContext, @NotNull DBTTaskType taskType) {
return new ConfigPanel(runnableContext, taskType);
}
@Override
public TaskConfigurationWizard createTaskConfigWizard(@NotNull DBTTask taskConfiguration) {
return new SQLToolTaskConfigurationWizard(taskConfiguration);
}
private static class ConfigPanel implements DBTTaskConfigPanel {
private DBRRunnableContext runnableContext;
private DBTTaskType taskType;
private Table objectsTable;
private DBPProject currentProject;
private SQLToolTaskConfigurationWizard dtWizard;
ConfigPanel(DBRRunnableContext runnableContext, DBTTaskType taskType) {
this.runnableContext = runnableContext;
this.taskType = taskType;
//this.currentProject = NavigatorUtils.getSelectedProject();
}
@Override
public void createControl(Composite parent, TaskConfigurationWizard wizard, Runnable propertyChangeListener) {
dtWizard = (SQLToolTaskConfigurationWizard) wizard;
}
@Override
public void loadSettings() {
}
@Override
public void saveSettings() {
}
@Override
public boolean isComplete() {
return objectsTable.getItemCount() > 0;
}
@Override
public String getErrorMessage() {
if (objectsTable.getItemCount() == 0) {
return "No objects selected";
}
return null;
}
}
}
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2020 DBeaver Corp and others
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jkiss.dbeaver.tasks.ui.sql;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.DBPDataSourceContainer;
import org.jkiss.dbeaver.model.navigator.DBNDataSource;
import org.jkiss.dbeaver.model.navigator.DBNProject;
import org.jkiss.dbeaver.model.navigator.DBNResource;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.ui.UIUtils;
import org.jkiss.dbeaver.ui.dialogs.ActiveWizardPage;
import org.jkiss.utils.CommonUtils;
import java.util.*;
/**
* SQL task settings page
*/
class SQLToolTaskPageSettings extends ActiveWizardPage<SQLToolTaskConfigurationWizard> {
private static final Log log = Log.getLog(SQLToolTaskPageSettings.class);
private SQLToolTaskConfigurationWizard sqlWizard;
private Button ignoreErrorsCheck;
private Button dumpQueryCheck;
private Button autoCommitCheck;
private TableViewer scriptsViewer;
private TableViewer dataSourceViewer;
private List<DBNResource> selectedScripts = new ArrayList<>();
private List<DBNDataSource> selectedDataSources = new ArrayList<>();
SQLToolTaskPageSettings(SQLToolTaskConfigurationWizard wizard) {
super("Tool parameters");
setTitle("Tool parameters");
setDescription("Parameters for database tool");
this.sqlWizard = wizard;
}
@Override
public void createControl(Composite parent) {
initializeDialogUnits(parent);
Composite composite = UIUtils.createComposite(parent, 1);
composite.setLayoutData(new GridData(GridData.FILL_BOTH));
/*
SQLScriptExecuteSettings dtSettings = getWizard().getSettings();
SashForm mainGroup = new SashForm(composite, SWT.NONE);
mainGroup.setSashWidth(5);
mainGroup.setLayoutData(new GridData(GridData.FILL_BOTH));
DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(sqlWizard.getProject());
{
Composite filesGroup = UIUtils.createControlGroup(mainGroup, DTMessages.sql_script_task_page_settings_group_files, 2, GridData.FILL_BOTH, 0);
scriptsViewer = new TableViewer(filesGroup, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
scriptsViewer.setContentProvider(new ListContentProvider());
scriptsViewer.getTable().setHeaderVisible(true);
scriptsViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((DBNResource) element).getResource().getProjectRelativePath().toString();
}
@Override
public Image getImage(Object element) {
return DBeaverIcons.getImage(((DBNResource)element).getNodeIconDefault());
}
});
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 300;
gd.widthHint = 400;
scriptsViewer.getTable().setLayoutData(gd);
SQLTaskScriptSelectorDialog.createScriptColumns(scriptsViewer);
final Table scriptTable = scriptsViewer.getTable();
scriptTable.setLayoutData(new GridData(GridData.FILL_BOTH));
ToolBar buttonsToolbar = new ToolBar(filesGroup, SWT.VERTICAL);
buttonsToolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
UIUtils.createToolItem(buttonsToolbar, "Add script", UIIcon.ROW_ADD, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
SQLTaskScriptSelectorDialog dialog = new SQLTaskScriptSelectorDialog(getShell(), projectNode);
if (dialog.open() == IDialogConstants.OK_ID) {
for (DBNResource script : dialog.getSelectedScripts()) {
if (!selectedScripts.contains(script)) {
selectedScripts.add(script);
}
}
refreshScripts();
}
}
});
ToolItem deleteItem = UIUtils.createToolItem(buttonsToolbar, "Remove script", UIIcon.ROW_DELETE, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ISelection selection = scriptsViewer.getSelection();
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
for (Object element : ((IStructuredSelection) selection).toArray()) {
if (element instanceof DBNResource) {
selectedScripts.remove(element);
}
}
refreshScripts();
}
}
});
UIUtils.createToolBarSeparator(buttonsToolbar, SWT.HORIZONTAL);
ToolItem moveUpItem = UIUtils.createToolItem(buttonsToolbar, "Move script up", UIIcon.ARROW_UP, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = scriptTable.getSelectionIndex();
if (selectionIndex > 0) {
DBNResource prevScript = selectedScripts.get(selectionIndex - 1);
selectedScripts.set(selectionIndex - 1, selectedScripts.get(selectionIndex));
selectedScripts.set(selectionIndex, prevScript);
refreshScripts();
}
}
});
ToolItem moveDownItem = UIUtils.createToolItem(buttonsToolbar, "Move script down", UIIcon.ARROW_DOWN, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = scriptTable.getSelectionIndex();
if (selectionIndex < scriptTable.getItemCount() - 1) {
DBNResource nextScript = selectedScripts.get(selectionIndex + 1);
selectedScripts.set(selectionIndex + 1, selectedScripts.get(selectionIndex));
selectedScripts.set(selectionIndex, nextScript);
refreshScripts();
}
}
});
scriptsViewer.addSelectionChangedListener(event -> {
int selectionIndex = scriptTable.getSelectionIndex();
deleteItem.setEnabled(selectionIndex >= 0);
moveUpItem.setEnabled(selectionIndex > 0);
moveDownItem.setEnabled(selectionIndex < scriptTable.getItemCount() - 1);
});
deleteItem.setEnabled(false);
}
{
Composite connectionsGroup = UIUtils.createControlGroup(mainGroup, DTMessages.sql_script_task_page_settings_group_connections, 2, GridData.FILL_BOTH, 0);
dataSourceViewer = new TableViewer(connectionsGroup, SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
dataSourceViewer.setContentProvider(new ListContentProvider());
//dataSourceViewer.getTable().setHeaderVisible(true);
dataSourceViewer.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
return ((DBNDataSource) element).getNodeName();
}
@Override
public Image getImage(Object element) {
return DBeaverIcons.getImage(((DBNDataSource)element).getNodeIcon());
}
});
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 300;
gd.widthHint = 400;
dataSourceViewer.getTable().setLayoutData(gd);
final Table dsTable = dataSourceViewer.getTable();
dsTable.setLayoutData(new GridData(GridData.FILL_BOTH));
ToolBar buttonsToolbar = new ToolBar(connectionsGroup, SWT.VERTICAL);
buttonsToolbar.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
UIUtils.createToolItem(buttonsToolbar, "Add datasource", UIIcon.ROW_ADD, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
SQLTaskDataSourceSelectorDialog dialog = new SQLTaskDataSourceSelectorDialog(getShell(), projectNode);
if (dialog.open() == IDialogConstants.OK_ID) {
for (DBNDataSource ds : dialog.getSelectedDataSources()) {
if (!selectedDataSources.contains(ds)) {
selectedDataSources.add(ds);
}
}
refreshDataSources();
}
}
});
ToolItem deleteItem = UIUtils.createToolItem(buttonsToolbar, "Remove datasource", UIIcon.ROW_DELETE, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ISelection selection = dataSourceViewer.getSelection();
if (!selection.isEmpty() && selection instanceof IStructuredSelection) {
for (Object element : ((IStructuredSelection) selection).toArray()) {
if (element instanceof DBNDataSource) {
selectedDataSources.remove(element);
}
}
refreshDataSources();
}
}
});
UIUtils.createToolBarSeparator(buttonsToolbar, SWT.HORIZONTAL);
ToolItem moveUpItem = UIUtils.createToolItem(buttonsToolbar, "Move datasource up", UIIcon.ARROW_UP, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = dsTable.getSelectionIndex();
if (selectionIndex > 0) {
DBNDataSource prevScript = selectedDataSources.get(selectionIndex - 1);
selectedDataSources.set(selectionIndex - 1, selectedDataSources.get(selectionIndex));
selectedDataSources.set(selectionIndex, prevScript);
refreshDataSources();
}
}
});
ToolItem moveDownItem = UIUtils.createToolItem(buttonsToolbar, "Move datasource down", UIIcon.ARROW_DOWN, new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int selectionIndex = dsTable.getSelectionIndex();
if (selectionIndex < dsTable.getItemCount() - 1) {
DBNDataSource nextScript = selectedDataSources.get(selectionIndex + 1);
selectedDataSources.set(selectionIndex + 1, selectedDataSources.get(selectionIndex));
selectedDataSources.set(selectionIndex, nextScript);
refreshScripts();
}
}
});
dataSourceViewer.addSelectionChangedListener(event -> {
int selectionIndex = dsTable.getSelectionIndex();
deleteItem.setEnabled(selectionIndex >= 0);
moveUpItem.setEnabled(selectionIndex > 0);
moveDownItem.setEnabled(selectionIndex < dsTable.getItemCount() - 1);
});
deleteItem.setEnabled(false);
}
{
Composite settingsGroup = UIUtils.createControlGroup(composite, DTMessages.sql_script_task_page_settings_group_script, 3, GridData.HORIZONTAL_ALIGN_BEGINNING, 0);
ignoreErrorsCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_ignore_errors, "", dtSettings.isIgnoreErrors(), 1);
dumpQueryCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_dump_results, "", dtSettings.isDumpQueryResultsToLog(), 1);
dumpQueryCheck.setEnabled(false);
autoCommitCheck = UIUtils.createCheckbox(settingsGroup, DTMessages.sql_script_task_page_settings_option_auto_commit, "", dtSettings.isAutoCommit(), 1);
}
getWizard().createTaskSaveButtons(composite, true, 1);
loadSettings();
*/
setControl(composite);
}
private void refreshScripts() {
scriptsViewer.refresh(true, true);
updateSelectedScripts();
}
private void refreshDataSources() {
dataSourceViewer.refresh(true, true);
}
private void updateSelectedScripts() {
DBNProject projectNode = DBWorkbench.getPlatform().getNavigatorModel().getRoot().getProjectNode(sqlWizard.getProject());
Set<DBPDataSourceContainer> dataSources = new LinkedHashSet<>();
for (DBNResource element : selectedScripts) {
Collection<DBPDataSourceContainer> resDS = element.getAssociatedDataSources();
if (!CommonUtils.isEmpty(resDS)) {
dataSources.addAll(resDS);
}
}
if (!dataSources.isEmpty()) {
List<DBNDataSource> checkedDataSources = new ArrayList<>();
for (DBPDataSourceContainer ds : dataSources) {
DBNDataSource dsNode = projectNode.getDatabases().getDataSource(ds);
if (dsNode != null) {
checkedDataSources.add(dsNode);
}
}
if (!checkedDataSources.isEmpty()) {
refreshDataSources();
for (DBNDataSource dsNode : checkedDataSources) {
if (!selectedDataSources.contains(dsNode)) {
selectedDataSources.add(dsNode);
}
}
}
}
refreshDataSources();
updatePageCompletion();
}
@Override
public void activatePage() {
updatePageCompletion();
}
@Override
public void deactivatePage() {
}
@Override
protected boolean determinePageCompletion() {
if (selectedScripts.isEmpty()) {
setErrorMessage("You must select script(s) to execute");
return false;
}
if (selectedDataSources.isEmpty()) {
setErrorMessage("You must select connection(s)");
return false;
}
setErrorMessage(null);
return true;
}
/*
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));
// }
}
public void saveSettings() {
if (sqlWizard == null) {
return;
}
SQLScriptExecuteSettings settings = sqlWizard.getSettings();
List<String> scriptPaths = new ArrayList<>();
for (DBNResource resource : selectedScripts) {
IResource res = resource.getResource();
if (res instanceof IFile) {
scriptPaths.add(res.getFullPath().toString());
}
}
settings.setScriptFiles(scriptPaths);
List<DBPDataSourceContainer> dsList = new ArrayList<>();
for (DBNDataSource dsNode : selectedDataSources) {
dsList.add(dsNode.getDataSourceContainer());
}
settings.setDataSources(dsList);
settings.setIgnoreErrors(ignoreErrorsCheck.getSelection());
settings.setDumpQueryResultsToLog(dumpQueryCheck.getSelection());
settings.setAutoCommit(autoCommitCheck.getSelection());
}
*/
}
\ No newline at end of file
......@@ -31,14 +31,25 @@ import org.jkiss.utils.CommonUtils;
public class TaskConfiguratorDescriptor extends AbstractContextDescriptor {
private final TaskTypeDescriptor type;
private final ObjectType taskHandlerType;
private final IConfigurationElement config;
private final ObjectType implType;
private final boolean supportsPanel;
TaskConfiguratorDescriptor(IConfigurationElement config) {
super(config);
this.type = null;
this.config = config;
this.taskHandlerType = new ObjectType(config, "handler");
this.implType = new ObjectType(config, "class");
this.supportsPanel = CommonUtils.getBoolean(config.getAttribute("supportsPanel"), true);
}
TaskConfiguratorDescriptor(TaskTypeDescriptor type, IConfigurationElement config) {
super(config);
this.type = type;
this.config = config;
this.taskHandlerType = null;
this.implType = new ObjectType(config, "class");
this.supportsPanel = CommonUtils.getBoolean(config.getAttribute("supportsPanel"), true);
}
......@@ -58,6 +69,10 @@ public class TaskConfiguratorDescriptor extends AbstractContextDescriptor {
return type;
}
public ObjectType getTaskHandlerType() {
return taskHandlerType;
}
@NotNull
public DBTTaskConfigurator createConfigurator() throws DBException {
return implType.createInstance(DBTTaskConfigurator.class);
......
......@@ -22,12 +22,16 @@ import org.eclipse.core.runtime.Platform;
import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.Log;
import org.jkiss.dbeaver.model.exec.DBCException;
import org.jkiss.dbeaver.model.task.DBTTaskHandler;
import org.jkiss.dbeaver.model.task.DBTTaskType;
import org.jkiss.dbeaver.registry.task.TaskRegistry;
import org.jkiss.dbeaver.registry.task.TaskTypeDescriptor;
import org.jkiss.dbeaver.tasks.ui.DBTTaskConfigurator;
import org.jkiss.utils.CommonUtils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class TaskUIRegistry {
......@@ -45,6 +49,7 @@ public class TaskUIRegistry {
}
private final Map<DBTTaskType, TaskConfiguratorDescriptor> taskConfigurators = new LinkedHashMap<>();
private final List<TaskConfiguratorDescriptor> taskHandlerConfigurators = new ArrayList<>();
private TaskUIRegistry(IExtensionRegistry registry) {
IConfigurationElement[] extElements = registry.getConfigurationElementsFor(TASK_EXTENSION_ID);
......@@ -54,7 +59,12 @@ public class TaskUIRegistry {
String typeId = ext.getAttribute("type");
TaskTypeDescriptor taskType = TaskRegistry.getInstance().getTaskType(typeId);
if (taskType == null) {
log.debug("Task type '" + typeId + "' not found. Skip configurator.");
if (!CommonUtils.isEmpty(ext.getAttribute("handler"))) {
TaskConfiguratorDescriptor configDescriptor = new TaskConfiguratorDescriptor(ext);
taskHandlerConfigurators.add(configDescriptor);
} else {
log.debug("Task type '" + typeId + "' not found. Skip configurator.");
}
} else {
TaskConfiguratorDescriptor configDescriptor = new TaskConfiguratorDescriptor(taskType, ext);
taskConfigurators.put(taskType, configDescriptor);
......@@ -78,7 +88,18 @@ public class TaskUIRegistry {
public DBTTaskConfigurator createConfigurator(DBTTaskType taskType) throws DBCException {
TaskConfiguratorDescriptor configuratorDescriptor = taskConfigurators.get(taskType);
if (configuratorDescriptor == null) {
throw new DBCException("Task configurator not supported for " + taskType.getName());
Class<? extends DBTTaskHandler> handlerClass = taskType.getHandlerClass();
if (handlerClass != null) {
for (TaskConfiguratorDescriptor tcd : taskHandlerConfigurators) {
if (tcd.getTaskHandlerType().getObjectClass().isAssignableFrom(handlerClass)) {
configuratorDescriptor = tcd;
break;
}
}
}
if (configuratorDescriptor == null) {
throw new DBCException("Task configurator not supported for " + taskType.getName());
}
}
try {
return configuratorDescriptor.createConfigurator();
......
......@@ -117,6 +117,7 @@
<module>org.jkiss.dbeaver.ext.erd</module>
<module>org.jkiss.dbeaver.tasks.native.ui</module>
<module>org.jkiss.dbeaver.tasks.ui</module>
<module>org.jkiss.dbeaver.tasks.sql.ui</module>
<module>org.jkiss.dbeaver.data.office.ui</module>
<module>org.jkiss.dbeaver.ext.sample.database</module>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册