Search in sources :

Example 1 with ConsoleQueryParameter

use of org.hibernate.console.ConsoleQueryParameter in project jbosstools-hibernate by jbosstools.

the class QueryParametersTest method testQueryParameter.

@Test
public void testQueryParameter() {
    QueryInputModel model = new QueryInputModel(service);
    ConsoleQueryParameter[] cqps = model.getQueryParameters();
    Assert.assertNotNull(cqps);
    QueryInputModel qpmodel = model;
    Assert.assertNotNull(qpmodel);
    class TestObserver implements Observer {

        int cnt = 0;

        public void update(Observable o, Object arg) {
            cnt++;
        }
    }
    ;
    TestObserver testObserver = new TestObserver();
    qpmodel.addObserver(testObserver);
    ConsoleQueryParameter consoleQueryParameter = new ConsoleQueryParameter(service);
    qpmodel.addParameter(consoleQueryParameter);
    Assert.assertEquals(1, testObserver.cnt);
    qpmodel.removeParameter(consoleQueryParameter);
    Assert.assertEquals(2, testObserver.cnt);
}
Also used : ConsoleQueryParameter(org.hibernate.console.ConsoleQueryParameter) Observer(java.util.Observer) Observable(java.util.Observable) QueryInputModel(org.hibernate.console.QueryInputModel) Test(org.junit.Test)

Example 2 with ConsoleQueryParameter

use of org.hibernate.console.ConsoleQueryParameter in project jbosstools-hibernate by jbosstools.

the class QueryParametersPage method createQueryParametersTable.

/**
 * This method initializes queryParametersTable
 */
private void createQueryParametersTable() {
    GridData gridData = new org.eclipse.swt.layout.GridData();
    gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
    gridData.grabExcessVerticalSpace = true;
    gridData.grabExcessHorizontalSpace = true;
    gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
    queryParametersTable = new Table(top, SWT.FULL_SELECTION);
    queryParametersTable.setHeaderVisible(true);
    queryParametersTable.setLayoutData(gridData);
    queryParametersTable.setLinesVisible(true);
    TableColumn nameColumn = new TableColumn(queryParametersTable, SWT.NONE);
    nameColumn.setWidth(100);
    nameColumn.setText(HibernateConsoleMessages.QueryParametersPage_name);
    TableColumn typeColumn = new TableColumn(queryParametersTable, SWT.NONE);
    typeColumn.setWidth(75);
    typeColumn.setText(HibernateConsoleMessages.QueryParametersPage_type);
    TableColumn valueColumn = new TableColumn(queryParametersTable, SWT.NONE);
    valueColumn.setWidth(100);
    valueColumn.setText(HibernateConsoleMessages.QueryParametersPage_value);
    TableColumn nullColumn = new TableColumn(queryParametersTable, SWT.NONE);
    nullColumn.setWidth(32);
    nullColumn.setText(HibernateConsoleMessages.QueryParametersPage_null);
    tableViewer = new TableViewer(queryParametersTable);
    tableViewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(SelectionChangedEvent event) {
            if (statusLabel != null) {
                Object firstElement = ((IStructuredSelection) tableViewer.getSelection()).getFirstElement();
                if (firstElement instanceof ConsoleQueryParameter) {
                    statusLabel.setText(HibernateConsoleMessages.QueryParametersPage_format + ((ConsoleQueryParameter) firstElement).getDefaultFormat());
                } else {
                    // $NON-NLS-1$
                    statusLabel.setText("");
                }
            }
        }
    });
    final List<IType> possibleTypes = new ArrayList<IType>(model.getService().newTypeFactory().getTypeFormats().keySet());
    Collections.sort(possibleTypes, new Comparator<IType>() {

        public int compare(IType t1, IType t2) {
            return t1.getName().compareTo(t2.getName());
        }
    });
    tableViewer.setCellModifier(new ICellModifier() {

        public void modify(Object element, String property, Object value) {
            TableItem item = (TableItem) element;
            ConsoleQueryParameter cqp = (ConsoleQueryParameter) item.getData();
            if (NAME_PROPERTY.equals(property)) {
                cqp.setName((String) value);
            }
            if (TYPE_PROPERTY.equals(property)) {
                Iterator<IType> iterator = possibleTypes.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    IType type = iterator.next();
                    if (i == ((Integer) value).intValue()) {
                        if (!cqp.getTypeName().equals(type.getName())) {
                            cqp.setType(type);
                            // have to reset to ensure it's working
                            cqp.setNull();
                        }
                        break;
                    }
                    i++;
                }
            }
            if (VALUE_PROPERTY.equals(property)) {
                String[] inputStrings;
                if (value instanceof String[]) {
                    inputStrings = (String[]) value;
                } else {
                    inputStrings = new String[] { (String) value };
                }
                Object[] values = new Object[inputStrings.length];
                for (int i = 0; i < inputStrings.length; i++) {
                    values[i] = cqp.convertStringToValue(inputStrings[i]);
                }
                if (values.length > 1) {
                    cqp.setValue(values);
                } else if (values.length == 1) {
                    cqp.setValue(values[0]);
                } else {
                    cqp.setValue(null);
                }
            }
            if (NULL_PROPERTY.equals(property)) {
                if (cqp.isNull()) {
                    // best attempt to "unnull" //$NON-NLS-1$
                    cqp.setValue(cqp.convertStringToValue(""));
                } else {
                    cqp.setNull();
                }
            }
            tableViewer.refresh(cqp);
        }

        public Object getValue(Object element, String property) {
            ConsoleQueryParameter cqp = (ConsoleQueryParameter) element;
            if (NAME_PROPERTY.equals(property)) {
                return cqp.getName();
            }
            if (TYPE_PROPERTY.equals(property)) {
                Iterator<IType> iterator = possibleTypes.iterator();
                String type = cqp.getTypeName();
                int i = 0;
                while (iterator.hasNext()) {
                    if (type.equals(iterator.next().getName())) {
                        return Integer.valueOf(i);
                    }
                    i++;
                }
            }
            if (VALUE_PROPERTY.equals(property)) {
                return cqp.getStringValues();
            }
            if (NULL_PROPERTY.equals(property)) {
                return Boolean.valueOf(cqp.isNull());
            }
            return null;
        }

        public boolean canModify(Object element, String property) {
            return true;
        }
    });
    tableViewer.setContentProvider(new IStructuredContentProvider() {

        public Object[] getElements(Object inputElement) {
            return ((QueryInputModel) inputElement).getQueryParameters();
        }

        public void dispose() {
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    });
    String[] columnProperties = new String[] { NAME_PROPERTY, TYPE_PROPERTY, VALUE_PROPERTY, NULL_PROPERTY };
    tableViewer.setColumnProperties(columnProperties);
    String[] valueTypes = new String[possibleTypes.size()];
    Iterator<IType> iterator = possibleTypes.iterator();
    int i = 0;
    while (iterator.hasNext()) {
        IType element = iterator.next();
        valueTypes[i++] = element.getName();
    }
    CellEditor[] editors = new CellEditor[columnProperties.length];
    editors[0] = new TextCellEditor(queryParametersTable);
    editors[1] = new ComboBoxCellEditor(queryParametersTable, valueTypes, SWT.READ_ONLY);
    editors[2] = new StringArrayDialogCellEditor(queryParametersTable) {

        private Button b;

        public void activate() {
            Object param = ((IStructuredSelection) tableViewer.getSelection()).getFirstElement();
            if (param instanceof ConsoleQueryParameter) {
                try {
                    Integer.parseInt(((ConsoleQueryParameter) param).getName());
                    // "ordered" parameter doesn't allow list value
                    // see also HQLQueryPage#setupParameters()
                    b.setVisible(false);
                } catch (NumberFormatException nfe) {
                    // "named" parameter allows parameter list value
                    b.setVisible(true);
                }
            }
        }

        protected org.eclipse.swt.widgets.Button createButton(Composite parent) {
            return b = super.createButton(parent);
        }
    };
    editors[3] = new CheckboxCellEditor(queryParametersTable);
    tableViewer.setCellEditors(editors);
    tableViewer.setLabelProvider(new ITableLabelProvider() {

        public void removeListener(ILabelProviderListener listener) {
        }

        public boolean isLabelProperty(Object element, String property) {
            return true;
        }

        public void dispose() {
        }

        public void addListener(ILabelProviderListener listener) {
        }

        public String getColumnText(Object element, int columnIndex) {
            ConsoleQueryParameter cqp = (ConsoleQueryParameter) element;
            switch(columnIndex) {
                case 0:
                    return cqp.getName();
                case 1:
                    return cqp.getTypeName();
                case 2:
                    {
                        String label = cqp.getStringValues()[0];
                        for (int j = 1; j < cqp.getStringValues().length; j++) {
                            // $NON-NLS-1$
                            label += "|" + cqp.getStringValues()[j];
                        }
                        return label;
                    }
                case 3:
                    // cqp.isNull()?"X":"";
                    return null;
                default:
                    return null;
            }
        }

        public Image getColumnImage(Object element, int columnIndex) {
            if (columnIndex == 3) {
                ConsoleQueryParameter cqp = (ConsoleQueryParameter) element;
                return cqp.isNull() ? EclipseImages.getImage(ImageConstants.CHECKBOX_FULL) : EclipseImages.getImage(ImageConstants.CHECKBOX_EMPTY);
            } else {
                return null;
            }
        }
    });
}
Also used : ConsoleQueryParameter(org.hibernate.console.ConsoleQueryParameter) CheckboxCellEditor(org.eclipse.jface.viewers.CheckboxCellEditor) CellEditor(org.eclipse.jface.viewers.CellEditor) CheckboxCellEditor(org.eclipse.jface.viewers.CheckboxCellEditor) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) TableItem(org.eclipse.swt.widgets.TableItem) ArrayList(java.util.ArrayList) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) TableViewer(org.eclipse.jface.viewers.TableViewer) Viewer(org.eclipse.jface.viewers.Viewer) Image(org.eclipse.swt.graphics.Image) IType(org.jboss.tools.hibernate.runtime.spi.IType) ITableLabelProvider(org.eclipse.jface.viewers.ITableLabelProvider) Button(org.eclipse.swt.widgets.Button) ICellModifier(org.eclipse.jface.viewers.ICellModifier) Iterator(java.util.Iterator) Table(org.eclipse.swt.widgets.Table) Composite(org.eclipse.swt.widgets.Composite) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) ComboBoxCellEditor(org.eclipse.jface.viewers.ComboBoxCellEditor) TableColumn(org.eclipse.swt.widgets.TableColumn) ILabelProviderListener(org.eclipse.jface.viewers.ILabelProviderListener) GridData(org.eclipse.swt.layout.GridData) IStructuredContentProvider(org.eclipse.jface.viewers.IStructuredContentProvider) TextCellEditor(org.eclipse.jface.viewers.TextCellEditor) TableViewer(org.eclipse.jface.viewers.TableViewer)

Example 3 with ConsoleQueryParameter

use of org.hibernate.console.ConsoleQueryParameter in project jbosstools-hibernate by jbosstools.

the class HQLQueryPage method setupParameters.

private void setupParameters(IQuery query2, QueryInputModel model) {
    if (model.getMaxResults() != null) {
        query2.setMaxResults(model.getMaxResults().intValue());
    }
    ConsoleQueryParameter[] qp = model.getQueryParameters();
    for (int i = 0; i < qp.length; i++) {
        ConsoleQueryParameter parameter = qp[i];
        try {
            int pos = Integer.parseInt(parameter.getName());
            // FIXME no method to set positioned list value
            query2.setParameter(pos, calcValue(parameter), getTypeFactory().getNamedType(parameter.getTypeName()));
        } catch (NumberFormatException nfe) {
            Object value = parameter.getValue();
            if (value != null && value.getClass().isArray()) {
                Object[] values = (Object[]) value;
                query2.setParameterList(parameter.getName(), Arrays.asList(values), getTypeFactory().getNamedType(parameter.getTypeName()));
            } else {
                query2.setParameter(parameter.getName(), calcValue(parameter), getTypeFactory().getNamedType(parameter.getTypeName()));
            }
        }
    }
}
Also used : ConsoleQueryParameter(org.hibernate.console.ConsoleQueryParameter)

Example 4 with ConsoleQueryParameter

use of org.hibernate.console.ConsoleQueryParameter in project jbosstools-hibernate by jbosstools.

the class ConsoleConfigurationTest method testHQLListParameters.

@Test
public void testHQLListParameters() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    // fix for https://issues.jboss.org/browse/JBIDE-9392
    // the view calls jdbc connection
    IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IViewPart view = activePage.findView(QueryPageTabView.ID);
    if (view != null) {
        activePage.hideView(view);
        view.dispose();
    }
    consoleCfg.build();
    IConfiguration c = consoleCfg.getConfiguration();
    IPersistentClass rc = service.newRootClass();
    rc.setEntityName("java.awt.Button");
    rc.setClassName("java.awt.Button");
    IColumn column = service.newColumn("label");
    ITable table = service.newTable("faketable");
    IPrimaryKey pk = table.getPrimaryKey();
    pk.addColumn(column);
    rc.setTable(table);
    table.addColumn(column);
    IProperty fakeProp = service.newProperty();
    fakeProp.setName("label");
    IValue sv = service.newSimpleValue();
    sv.addColumn(column);
    sv.setTypeName("string");
    sv.setTable(table);
    fakeProp.setValue(sv);
    rc.setIdentifierProperty(fakeProp);
    rc.setIdentifier(fakeProp.getValue());
    c.addClass(rc);
    consoleCfg.buildSessionFactory();
    ConsoleQueryParameter paramA = new ConsoleQueryParameter(service, "a", typeFactory.getIntegerType(), new Integer[] { new Integer(1), new Integer(2) });
    ConsoleQueryParameter paramB = new ConsoleQueryParameter(service, "b", typeFactory.getIntegerType(), new Integer(3));
    ConsoleQueryParameter paramOrdered = new ConsoleQueryParameter(service, "0", typeFactory.getIntegerType(), new Integer(4));
    QueryInputModel model = new QueryInputModel(service);
    model.addParameter(paramA);
    model.addParameter(paramB);
    model.addParameter(paramOrdered);
    // $NON-NLS-1$
    QueryPage qp = consoleCfg.executeHQLQuery("select count(*) from java.awt.Button where 1 in ( ?, :a, :b )", model);
    Assert.assertNotNull(qp);
    try {
        // execute the query
        qp.getList();
    } catch (Exception e) {
    // ignore - there is fake mapping
    }
}
Also used : ConsoleQueryParameter(org.hibernate.console.ConsoleQueryParameter) IViewPart(org.eclipse.ui.IViewPart) QueryPage(org.hibernate.console.QueryPage) IPrimaryKey(org.jboss.tools.hibernate.runtime.spi.IPrimaryKey) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) HibernateConsoleRuntimeException(org.hibernate.console.HibernateConsoleRuntimeException) QueryInputModel(org.hibernate.console.QueryInputModel) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ITable(org.jboss.tools.hibernate.runtime.spi.ITable) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) Test(org.junit.Test)

Example 5 with ConsoleQueryParameter

use of org.hibernate.console.ConsoleQueryParameter in project jbosstools-hibernate by jbosstools.

the class QueryParametersTest method testCreateUnique.

@Test
public void testCreateUnique() {
    QueryInputModel model = new QueryInputModel(service);
    // $NON-NLS-1$
    ConsoleQueryParameter parameter = model.createUniqueParameter("param");
    model.addParameter(parameter);
    // $NON-NLS-1$
    Assert.assertFalse(model.createUniqueParameter("param").getName().equals(parameter.getName()));
}
Also used : ConsoleQueryParameter(org.hibernate.console.ConsoleQueryParameter) QueryInputModel(org.hibernate.console.QueryInputModel) Test(org.junit.Test)

Aggregations

ConsoleQueryParameter (org.hibernate.console.ConsoleQueryParameter)5 QueryInputModel (org.hibernate.console.QueryInputModel)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Observable (java.util.Observable)1 Observer (java.util.Observer)1 CellEditor (org.eclipse.jface.viewers.CellEditor)1 CheckboxCellEditor (org.eclipse.jface.viewers.CheckboxCellEditor)1 ComboBoxCellEditor (org.eclipse.jface.viewers.ComboBoxCellEditor)1 ICellModifier (org.eclipse.jface.viewers.ICellModifier)1 ILabelProviderListener (org.eclipse.jface.viewers.ILabelProviderListener)1 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)1 IStructuredContentProvider (org.eclipse.jface.viewers.IStructuredContentProvider)1 ITableLabelProvider (org.eclipse.jface.viewers.ITableLabelProvider)1 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)1 TableViewer (org.eclipse.jface.viewers.TableViewer)1 TextCellEditor (org.eclipse.jface.viewers.TextCellEditor)1 Viewer (org.eclipse.jface.viewers.Viewer)1 Image (org.eclipse.swt.graphics.Image)1