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

#6959 SQL parameter values preview


Former-commit-id: 6790331d
上级 595ef43e
...@@ -1090,4 +1090,20 @@ public final class SQLUtils { ...@@ -1090,4 +1090,20 @@ public final class SQLUtils {
} }
return type; return type;
} }
public static void fillQueryParameters(SQLQuery sqlStatement, List<SQLQueryParameter> parameters) {
// Set values for all parameters
// Replace parameter tokens with parameter values
String query = sqlStatement.getText();
for (int i = parameters.size(); i > 0; i--) {
SQLQueryParameter parameter = parameters.get(i - 1);
String paramValue = parameter.getValue();
if (paramValue == null || paramValue.isEmpty()) {
paramValue = SQLConstants.NULL_VALUE;
}
query = query.substring(0, parameter.getTokenOffset()) + paramValue + query.substring(parameter.getTokenOffset() + parameter.getTokenLength());
}
sqlStatement.setText(query);
sqlStatement.setOriginalText(query);
}
} }
\ No newline at end of file
...@@ -1978,20 +1978,8 @@ public class SQLEditor extends SQLEditorBase implements ...@@ -1978,20 +1978,8 @@ public class SQLEditor extends SQLEditorBase implements
if (ignoreParameters) { if (ignoreParameters) {
return true; return true;
} }
SQLUtils.fillQueryParameters(sqlStatement, parameters);
// Set values for all parameters
// Replace parameter tokens with parameter values
String query = sqlStatement.getText();
for (int i = parameters.size(); i > 0; i--) {
SQLQueryParameter parameter = parameters.get(i - 1);
String paramValue = parameter.getValue();
if (paramValue == null || paramValue.isEmpty()) {
paramValue = SQLConstants.NULL_VALUE;
}
query = query.substring(0, parameter.getTokenOffset()) + paramValue + query.substring(parameter.getTokenOffset() + parameter.getTokenLength());
}
sqlStatement.setText(query);
sqlStatement.setOriginalText(query);
return true; return true;
} }
......
...@@ -33,6 +33,8 @@ import org.jkiss.dbeaver.model.DBIcon; ...@@ -33,6 +33,8 @@ import org.jkiss.dbeaver.model.DBIcon;
import org.jkiss.dbeaver.model.impl.DataSourceContextProvider; import org.jkiss.dbeaver.model.impl.DataSourceContextProvider;
import org.jkiss.dbeaver.model.sql.SQLQuery; import org.jkiss.dbeaver.model.sql.SQLQuery;
import org.jkiss.dbeaver.model.sql.SQLQueryParameter; import org.jkiss.dbeaver.model.sql.SQLQueryParameter;
import org.jkiss.dbeaver.model.sql.SQLScriptContext;
import org.jkiss.dbeaver.model.sql.SQLUtils;
import org.jkiss.dbeaver.runtime.DBWorkbench; import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.runtime.ui.UIServiceSQL; import org.jkiss.dbeaver.runtime.ui.UIServiceSQL;
import org.jkiss.dbeaver.ui.DBeaverIcons; import org.jkiss.dbeaver.ui.DBeaverIcons;
...@@ -44,8 +46,9 @@ import org.jkiss.dbeaver.ui.editors.sql.registry.SQLQueryParameterRegistry; ...@@ -44,8 +46,9 @@ import org.jkiss.dbeaver.ui.editors.sql.registry.SQLQueryParameterRegistry;
import org.jkiss.dbeaver.utils.GeneralUtils; import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.utils.CommonUtils; import org.jkiss.utils.CommonUtils;
import java.util.List; import java.io.StringWriter;
import java.util.*; import java.util.*;
import java.util.List;
/** /**
* Parameter binding * Parameter binding
...@@ -58,6 +61,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -58,6 +61,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
private static final Log log = Log.getLog(SQLQueryParameterBindDialog.class); private static final Log log = Log.getLog(SQLQueryParameterBindDialog.class);
private IWorkbenchPartSite site; private IWorkbenchPartSite site;
private SQLScriptContext queryContext;
private SQLQuery query; private SQLQuery query;
private List<SQLQueryParameter> parameters; private List<SQLQueryParameter> parameters;
private final Map<String, List<SQLQueryParameter>> dupParameters = new HashMap<>(); private final Map<String, List<SQLQueryParameter>> dupParameters = new HashMap<>();
...@@ -65,11 +69,14 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -65,11 +69,14 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
private static Map<String, SQLQueryParameterRegistry.ParameterInfo> savedParamValues = new HashMap<>(); private static Map<String, SQLQueryParameterRegistry.ParameterInfo> savedParamValues = new HashMap<>();
private Button hideIfSetCheck; private Button hideIfSetCheck;
private Table paramTable; private Table paramTable;
private Object queryPreviewPanel;
public SQLQueryParameterBindDialog(IWorkbenchPartSite site, SQLQuery query, List<SQLQueryParameter> parameters) public SQLQueryParameterBindDialog(IWorkbenchPartSite site, SQLQuery query, List<SQLQueryParameter> parameters)
{ {
super(site.getShell()); super(site.getShell());
this.site = site; this.site = site;
StringWriter dummyWriter = new StringWriter();
this.queryContext = new SQLScriptContext(null, new DataSourceContextProvider(query.getDataSource()), null, dummyWriter);
this.query = query; this.query = query;
this.parameters = parameters; this.parameters = parameters;
...@@ -80,6 +87,8 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -80,6 +87,8 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
SQLQueryParameterRegistry.ParameterInfo paramInfo = registry.getParameter(param.getName()); SQLQueryParameterRegistry.ParameterInfo paramInfo = registry.getParameter(param.getName());
if (paramInfo != null) { if (paramInfo != null) {
param.setValue(paramInfo.value); param.setValue(paramInfo.value);
param.setVariableSet(!CommonUtils.isEmpty(paramInfo.value));
queryContext.setVariable(paramInfo.name, paramInfo.value);
} }
} }
} }
...@@ -166,6 +175,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -166,6 +175,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
item.setText(2, newValue); item.setText(2, newValue);
param.setValue(newValue); param.setValue(newValue);
param.setVariableSet(!CommonUtils.isEmpty(newValue));
if (param.isNamed()) { if (param.isNamed()) {
final List<SQLQueryParameter> dups = dupParameters.get(param.getName()); final List<SQLQueryParameter> dups = dupParameters.get(param.getName());
if (dups != null) { if (dups != null) {
...@@ -173,11 +183,14 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -173,11 +183,14 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
dup.setValue(newValue); dup.setValue(newValue);
} }
} }
queryContext.setVariable(param.getVarName(), param.getValue());
} }
savedParamValues.put( savedParamValues.put(
param.getName().toUpperCase(Locale.ENGLISH), param.getName().toUpperCase(Locale.ENGLISH),
new SQLQueryParameterRegistry.ParameterInfo(param.getName(), newValue)); new SQLQueryParameterRegistry.ParameterInfo(param.getName(), newValue));
updateQueryPreview();
} }
}; };
...@@ -193,7 +206,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -193,7 +206,7 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
queryComposite.setLayout(new FillLayout()); queryComposite.setLayout(new FillLayout());
try { try {
DBWorkbench.getService(UIServiceSQL.class).createSQLPanel( queryPreviewPanel = DBWorkbench.getService(UIServiceSQL.class).createSQLPanel(
site, site,
queryComposite, queryComposite,
new DataSourceContextProvider(query.getDataSource()), new DataSourceContextProvider(query.getDataSource()),
...@@ -219,6 +232,9 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -219,6 +232,9 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
}); });
updateStatus(GeneralUtils.makeInfoStatus(SQLEditorMessages.dialog_sql_param_hint)); updateStatus(GeneralUtils.makeInfoStatus(SQLEditorMessages.dialog_sql_param_hint));
updateQueryPreview();
return composite; return composite;
} }
...@@ -244,6 +260,21 @@ public class SQLQueryParameterBindDialog extends StatusDialog { ...@@ -244,6 +260,21 @@ public class SQLQueryParameterBindDialog extends StatusDialog {
} }
} }
private void updateQueryPreview() {
SQLQuery queryCopy = new SQLQuery(query.getDataSource(), query.getText(), query);
List<SQLQueryParameter> setParams = new ArrayList<>(this.parameters);
setParams.removeIf(parameter -> !parameter.isVariableSet());
SQLUtils.fillQueryParameters(queryCopy, setParams);
if (!CommonUtils.equalObjects(query.getText(), queryCopy.getText())) {
UIUtils.asyncExec(() -> {
DBWorkbench.getService(UIServiceSQL.class).setSQLPanelText(
queryPreviewPanel,
queryCopy.getText());
});
}
}
@Override @Override
protected void createButtonsForButtonBar(Composite parent) { protected void createButtonsForButtonBar(Composite parent) {
Button skipButton = UIUtils.createDialogButton(parent, IDialogConstants.IGNORE_LABEL, new SelectionAdapter() { Button skipButton = UIUtils.createDialogButton(parent, IDialogConstants.IGNORE_LABEL, new SelectionAdapter() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册