Search in sources :

Example 11 with SQLQueryParameter

use of org.jkiss.dbeaver.model.sql.SQLQueryParameter in project dbeaver by dbeaver.

the class SQLEditorParametersProvider method prepareStatementParameters.

@Override
public Boolean prepareStatementParameters(@NotNull SQLScriptContext scriptContext, @NotNull SQLQuery sqlStatement, @NotNull List<SQLQueryParameter> parameters, boolean useDefaults) {
    for (SQLQueryParameter param : parameters) {
        String paramName = param.getVarName();
        Object defValue = useDefaults ? scriptContext.getParameterDefaultValue(paramName) : null;
        if (defValue != null || scriptContext.hasVariable(paramName)) {
            Object varValue = defValue != null ? defValue : scriptContext.getVariable(paramName);
            String strValue = varValue == null ? null : varValue.toString();
            param.setValue(strValue);
            param.setVariableSet(true);
        } else {
            if (!useDefaults) {
                param.setVariableSet(false);
            }
        }
    }
    boolean allSet = true;
    for (SQLQueryParameter param : parameters) {
        if (!param.isVariableSet()) {
            allSet = false;
        }
    }
    if (allSet) {
        return true;
    }
    int paramsResult = UITask.run(() -> {
        SQLQueryParameterBindDialog dialog = new SQLQueryParameterBindDialog(site, sqlStatement, parameters);
        return dialog.open();
    });
    if (paramsResult == IDialogConstants.OK_ID) {
        // Save values back to script context
        for (SQLQueryParameter param : parameters) {
            if (param.isNamed()) {
                String strValue = param.getValue();
                if (scriptContext.hasVariable(param.getVarName())) {
                    scriptContext.setVariable(param.getVarName(), strValue);
                } else {
                    scriptContext.setParameterDefaultValue(param.getVarName(), strValue);
                }
            }
        }
        return true;
    } else if (paramsResult == IDialogConstants.IGNORE_ID) {
        scriptContext.setIgnoreParameters(true);
        return null;
    }
    return false;
}
Also used : SQLQueryParameterBindDialog(org.jkiss.dbeaver.ui.editors.sql.dialogs.SQLQueryParameterBindDialog) SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter)

Aggregations

SQLQueryParameter (org.jkiss.dbeaver.model.sql.SQLQueryParameter)11 GridData (org.eclipse.swt.layout.GridData)4 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)4 CustomTableEditor (org.jkiss.dbeaver.ui.controls.CustomTableEditor)4 StringWriter (java.io.StringWriter)2 java.util (java.util)2 List (java.util.List)2 IDialogConstants (org.eclipse.jface.dialogs.IDialogConstants)2 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)2 StatusDialog (org.eclipse.jface.dialogs.StatusDialog)2 SWT (org.eclipse.swt.SWT)2 SashForm (org.eclipse.swt.custom.SashForm)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2 FillLayout (org.eclipse.swt.layout.FillLayout)2 GridLayout (org.eclipse.swt.layout.GridLayout)2 org.eclipse.swt.widgets (org.eclipse.swt.widgets)2 IWorkbenchPartSite (org.eclipse.ui.IWorkbenchPartSite)2 Log (org.jkiss.dbeaver.Log)2 DBIcon (org.jkiss.dbeaver.model.DBIcon)2