Search in sources :

Example 6 with SQLQueryParameter

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

the class SQLQueryParameterBindDialog method fillParameterList.

private void fillParameterList(boolean hideVariables) {
    paramTable.removeAll();
    for (SQLQueryParameter param : parameters) {
        if (hideVariables && param.isVariableSet()) {
            continue;
        }
        if (param.getPrevious() != null) {
            // Skip duplicates
            List<SQLQueryParameter> dups = dupParameters.computeIfAbsent(param.getName(), k -> new ArrayList<>());
            dups.add(param);
            continue;
        }
        TableItem item = new TableItem(paramTable, SWT.NONE);
        item.setData(param);
        item.setImage(DBeaverIcons.getImage(DBIcon.TREE_ATTRIBUTE));
        item.setText(0, String.valueOf(param.getOrdinalPosition() + 1));
        item.setText(1, param.getVarName());
        item.setText(2, CommonUtils.notEmpty(param.getValue()));
    }
}
Also used : SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter)

Example 7 with SQLQueryParameter

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

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)

Example 8 with SQLQueryParameter

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

the class SQLQueryParameterBindDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    getShell().setText("Bind parameter(s)");
    final Composite composite = (Composite) super.createDialogArea(parent);
    final Table paramTable = new Table(composite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    final GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 400;
    gd.heightHint = 200;
    paramTable.setLayoutData(gd);
    paramTable.setHeaderVisible(true);
    paramTable.setLinesVisible(true);
    final TableColumn indexColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, "#");
    indexColumn.setWidth(40);
    final TableColumn nameColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, "Name");
    nameColumn.setWidth(100);
    final TableColumn valueColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, "Value");
    valueColumn.setWidth(200);
    for (SQLQueryParameter param : parameters) {
        if (param.getPrevious() != null) {
            // Skip duplicates
            List<SQLQueryParameter> dups = dupParameters.get(param.getName());
            if (dups == null) {
                dups = new ArrayList<>();
                dupParameters.put(param.getName(), dups);
            }
            dups.add(param);
            continue;
        }
        TableItem item = new TableItem(paramTable, SWT.NONE);
        item.setData(param);
        item.setImage(DBeaverIcons.getImage(DBIcon.TREE_ATTRIBUTE));
        item.setText(0, String.valueOf(param.getOrdinalPosition() + 1));
        item.setText(1, param.getTitle());
        item.setText(2, CommonUtils.notEmpty(param.getValue()));
    }
    final CustomTableEditor tableEditor = new CustomTableEditor(paramTable) {

        {
            firstTraverseIndex = 2;
            lastTraverseIndex = 2;
            editOnEnter = false;
        }

        @Override
        protected Control createEditor(Table table, int index, TableItem item) {
            if (index != 2) {
                return null;
            }
            SQLQueryParameter param = (SQLQueryParameter) item.getData();
            Text editor = new Text(table, SWT.BORDER);
            editor.setText(CommonUtils.notEmpty(param.getValue()));
            editor.selectAll();
            return editor;
        }

        @Override
        protected void saveEditorValue(Control control, int index, TableItem item) {
            SQLQueryParameter param = (SQLQueryParameter) item.getData();
            String newValue = ((Text) control).getText();
            item.setText(2, newValue);
            param.setValue(newValue);
            if (param.isNamed()) {
                final List<SQLQueryParameter> dups = dupParameters.get(param.getName());
                if (dups != null) {
                    for (SQLQueryParameter dup : dups) {
                        dup.setValue(newValue);
                    }
                }
            }
            savedParamValues.put(param.getName().toUpperCase(Locale.ENGLISH), new SQLQueryParameterRegistry.ParameterInfo(param.getName(), newValue));
        }
    };
    if (!parameters.isEmpty()) {
        DBeaverUI.asyncExec(new Runnable() {

            @Override
            public void run() {
                paramTable.select(0);
                tableEditor.showEditor(paramTable.getItem(0), 2);
            }
        });
    }
    updateStatus(GeneralUtils.makeInfoStatus("Use Tab to switch. String values must be quoted. You can use expressions in values"));
    return composite;
}
Also used : SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter) CustomTableEditor(org.jkiss.dbeaver.ui.controls.CustomTableEditor) GridData(org.eclipse.swt.layout.GridData)

Example 9 with SQLQueryParameter

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

the class SQLQueryParameterBindDialog method createDialogArea.

@Override
protected Control createDialogArea(Composite parent) {
    getShell().setText(SQLEditorMessages.dialog_sql_param_title);
    final Composite composite = (Composite) super.createDialogArea(parent);
    SashForm sash = new SashForm(composite, SWT.VERTICAL);
    sash.setLayoutData(new GridData(GridData.FILL_BOTH));
    {
        final Composite paramsComposite = UIUtils.createComposite(sash, 1);
        paramTable = new Table(paramsComposite, SWT.SINGLE | SWT.FULL_SELECTION | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        final GridData gd = new GridData(GridData.FILL_BOTH);
        gd.widthHint = 400;
        gd.heightHint = 200;
        paramTable.setLayoutData(gd);
        paramTable.setHeaderVisible(true);
        paramTable.setLinesVisible(true);
        final TableColumn indexColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, "#");
        indexColumn.addListener(SWT.Selection, new TableColumnSortListener(paramTable, 0));
        indexColumn.setWidth(40);
        final TableColumn nameColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, SQLEditorMessages.dialog_sql_param_column_name);
        nameColumn.addListener(SWT.Selection, new TableColumnSortListener(paramTable, 1));
        nameColumn.setWidth(100);
        final TableColumn valueColumn = UIUtils.createTableColumn(paramTable, SWT.LEFT, SQLEditorMessages.dialog_sql_param_column_value);
        valueColumn.setWidth(200);
        fillParameterList(isHideIfSet());
        final CustomTableEditor tableEditor = new CustomTableEditor(paramTable) {

            {
                firstTraverseIndex = 2;
                lastTraverseIndex = 2;
                editOnEnter = false;
            }

            @Override
            protected Control createEditor(Table table, int index, TableItem item) {
                if (index != 2) {
                    return null;
                }
                SQLQueryParameter param = (SQLQueryParameter) item.getData();
                Text editor = new Text(table, SWT.NONE);
                editor.setText(CommonUtils.notEmpty(param.getValue()));
                editor.selectAll();
                editor.addTraverseListener(e -> {
                    if (e.detail == SWT.TRAVERSE_RETURN && (e.stateMask & SWT.CTRL) == SWT.CTRL) {
                        UIUtils.asyncExec(SQLQueryParameterBindDialog.this::okPressed);
                    }
                });
                editor.addModifyListener(e -> saveEditorValue(editor, index, item));
                return editor;
            }

            @Override
            protected void saveEditorValue(Control control, int index, TableItem item) {
                SQLQueryParameter param = (SQLQueryParameter) item.getData();
                String newValue = ((Text) control).getText();
                item.setText(2, newValue);
                param.setValue(newValue);
                param.setVariableSet(!CommonUtils.isEmpty(newValue));
                if (param.isNamed()) {
                    final List<SQLQueryParameter> dups = dupParameters.get(param.getName());
                    if (dups != null) {
                        for (SQLQueryParameter dup : dups) {
                            dup.setValue(newValue);
                            dup.setVariableSet(!CommonUtils.isEmpty(newValue));
                        }
                    }
                    queryContext.setVariable(param.getVarName(), param.getValue());
                }
                savedParamValues.put(param.getName().toUpperCase(Locale.ENGLISH), new SQLQueryParameterRegistry.ParameterInfo(param.getName(), newValue));
                updateQueryPreview();
            }
        };
        if (!parameters.isEmpty()) {
            UIUtils.asyncExec(() -> {
                if (paramTable.getItemCount() > 0) {
                    paramTable.select(0);
                    tableEditor.showEditor(paramTable.getItem(0), 2);
                }
            });
        }
    }
    {
        final Composite queryComposite = new Composite(sash, SWT.BORDER);
        queryComposite.setLayout(new FillLayout());
        try {
            queryPreviewPanel = DBWorkbench.getService(UIServiceSQL.class).createSQLPanel(site, queryComposite, new DataSourceContextProvider(query.getDataSource()), "Query preview", false, query.getText());
        } catch (Exception e) {
            log.error(e);
        }
    }
    sash.setWeights(new int[] { 600, 400 });
    hideIfSetCheck = UIUtils.createCheckbox(composite, SQLEditorMessages.dialog_sql_param_hide_checkbox, SQLEditorMessages.dialog_sql_param_hide_checkbox_tip, isHideIfSet(), 1);
    hideIfSetCheck.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            fillParameterList(hideIfSetCheck.getSelection());
        }
    });
    updateStatus(GeneralUtils.makeInfoStatus(SQLEditorMessages.dialog_sql_param_hint));
    updateQueryPreview();
    return composite;
}
Also used : DBWorkbench(org.jkiss.dbeaver.runtime.DBWorkbench) java.util(java.util) SashForm(org.eclipse.swt.custom.SashForm) SQLScriptContext(org.jkiss.dbeaver.model.sql.SQLScriptContext) IWorkbenchPartSite(org.eclipse.ui.IWorkbenchPartSite) IDialogConstants(org.eclipse.jface.dialogs.IDialogConstants) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) TableColumnSortListener(org.jkiss.dbeaver.ui.controls.TableColumnSortListener) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) CustomTableEditor(org.jkiss.dbeaver.ui.controls.CustomTableEditor) DBeaverIcons(org.jkiss.dbeaver.ui.DBeaverIcons) UIUtils(org.jkiss.dbeaver.ui.UIUtils) Log(org.jkiss.dbeaver.Log) GridData(org.eclipse.swt.layout.GridData) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) FillLayout(org.eclipse.swt.layout.FillLayout) StatusDialog(org.eclipse.jface.dialogs.StatusDialog) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) GeneralUtils(org.jkiss.dbeaver.utils.GeneralUtils) CommonUtils(org.jkiss.utils.CommonUtils) DataSourceContextProvider(org.jkiss.dbeaver.model.impl.DataSourceContextProvider) StringWriter(java.io.StringWriter) org.eclipse.swt.widgets(org.eclipse.swt.widgets) SQLEditorMessages(org.jkiss.dbeaver.ui.editors.sql.internal.SQLEditorMessages) List(java.util.List) DBIcon(org.jkiss.dbeaver.model.DBIcon) SWT(org.eclipse.swt.SWT) SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter) SQLUtils(org.jkiss.dbeaver.model.sql.SQLUtils) SelectionEvent(org.eclipse.swt.events.SelectionEvent) SQLQueryParameterRegistry(org.jkiss.dbeaver.model.sql.registry.SQLQueryParameterRegistry) GridLayout(org.eclipse.swt.layout.GridLayout) TableColumnSortListener(org.jkiss.dbeaver.ui.controls.TableColumnSortListener) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) DataSourceContextProvider(org.jkiss.dbeaver.model.impl.DataSourceContextProvider) FillLayout(org.eclipse.swt.layout.FillLayout) SQLQueryParameter(org.jkiss.dbeaver.model.sql.SQLQueryParameter) SashForm(org.eclipse.swt.custom.SashForm) CustomTableEditor(org.jkiss.dbeaver.ui.controls.CustomTableEditor) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) List(java.util.List) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

Example 10 with SQLQueryParameter

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

the class SQLQueryParameterBindDialog method updateQueryPreview.

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);
    {
        UIUtils.asyncExec(() -> {
            DBWorkbench.getService(UIServiceSQL.class).setSQLPanelText(queryPreviewPanel, queryCopy.getText());
        });
    }
}
Also used : SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) 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