Author: dgeraskov
Date: 2010-11-12 09:50:50 -0500 (Fri, 12 Nov 2010)
New Revision: 26512
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/TextDialogCellEditor.java
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ParametersListDialog.java
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersPage.java
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleQueryParameter.java
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java
Log:
JBIDE-4202
Entering parameter lists in Hibernate Tools
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleQueryParameter.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleQueryParameter.java 2010-11-12
14:49:31 UTC (rev 26511)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ConsoleQueryParameter.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -118,18 +118,26 @@
//if(value == null) { throw new IllegalArgumentException("Value must not be set to
null"); }
this.value = value;
}
-
- public String getValueAsString() {
- if(isNull()) return ""; //$NON-NLS-1$
- return type.toString(getValue());
+
+ public String[] getStringValues() {
+ if(value == null) return new String[]{""}; //$NON-NLS-1$
+ if (value.getClass().isArray()){
+ Object[] arr = (Object[])value;
+ String[] values = new String[arr.length];
+ for (int i = 0; i < arr.length; i++) {
+ values[i] = type.toString(arr[i]);
+ }
+ return values;
+ } else {
+ return new String[]{type.toString(value)};
+ }
}
- public void setValueFromString(String value) {
+ public Object convertStringToValue(String value){
try {
- Object object = type.fromStringValue(value);
- setValue(object);
+ return type.fromStringValue(value);
} catch(Exception he) {
- setNull();
+ return NULL_MARKER;
}
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java 2010-11-12
14:49:31 UTC (rev 26511)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/HQLQueryPage.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -22,6 +22,7 @@
package org.hibernate.console;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
@@ -75,12 +76,20 @@
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 ), parameter.getType());
} catch(NumberFormatException nfe) {
- query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
- }
+ Object value = parameter.getValue();
+ if (value != null && value.getClass().isArray()){
+ Object[] values = (Object[])value;
+ query2.setParameterList(parameter.getName(), Arrays.asList(values),
parameter.getType());
+ } else {
+ query2.setParameter(parameter.getName(), calcValue( parameter ),
parameter.getType());
+ }
+ }
}
}
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ParametersListDialog.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ParametersListDialog.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse/src/org/hibernate/console/ParametersListDialog.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -0,0 +1,152 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.console;
+
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.viewers.ListViewer;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Shell;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public class ParametersListDialog extends Dialog {
+
+ private ListViewer listViewer;
+
+ private ConsoleQueryParameter cqp;
+
+ private String[] values;
+
+ /**
+ * @param parentShell
+ */
+ public ParametersListDialog(Shell parentShell, ConsoleQueryParameter cqp) {
+ super(parentShell);
+ this.cqp = cqp;
+
+ }
+
+ @Override
+ protected Control createDialogArea(Composite container) {
+ Composite parent = (Composite) super.createDialogArea(container);
+ listViewer = new ListViewer(parent);
+ GridData gd = new GridData(GridData.FILL_BOTH);
+ gd.heightHint = convertHeightInCharsToPixels(15);
+ gd.widthHint = convertWidthInCharsToPixels(55);
+ org.eclipse.swt.widgets.List list = listViewer.getList();
+ list.setLayoutData(gd);
+ list.setFont(container.getFont());
+ /*List<String> input = new ArrayList<String>();
+ Object value = cqp.getValue();
+ if (value != null) {
+ if (cqp.isArrayValue()) {
+ Object[] values = (Object[]) value;
+ for (Object object : values) {
+ input.add(cqp.convertValueToString(object));
+ }
+ } else {
+ input.add(cqp.convertValueToString(value));
+ }
+ }*/
+ if (!cqp.isNull()){
+ list.setItems(cqp.getStringValues());
+ }
+ if (list.getItemCount() > 0){
+ list.select(list.getItemCount() - 1);
+ }
+ addButtons(parent);
+ return parent;
+ }
+
+ protected void configureShell(Shell shell) {
+ super.configureShell(shell);
+ shell.setText("Parameter Values"); //$NON-NLS-1$
+ }
+
+ private Button buttonAdd;
+ private Button buttonRemove;
+ private Button buttonModify;
+
+ private void addButtons(Composite parent) {
+ Composite composite = new Composite(parent, SWT.NULL);
+ FillLayout fillLayout = new FillLayout(SWT.HORIZONTAL);
+ fillLayout.spacing = 2;
+
+ composite.setLayout(fillLayout);
+
+ buttonAdd = new Button(composite, SWT.PUSH);
+ buttonAdd.setText("Add"); //$NON-NLS-1$
+
+ buttonModify = new Button(composite, SWT.PUSH);
+ buttonModify.setText("Modify"); //$NON-NLS-1$
+
+ buttonRemove = new Button(composite, SWT.PUSH);
+ buttonRemove.setText("Remove"); //$NON-NLS-1$
+
+ buttonAdd.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ InputDialog inputDialog = new InputDialog(null, "Add Element",
//$NON-NLS-1$
+ "New parameter: ", "", null); //$NON-NLS-1$ //$NON-NLS-2$
+ if (inputDialog.open() == Window.OK) {
+ listViewer.getList().add(inputDialog.getValue());
+ }
+ }
+ });
+
+ buttonModify.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ int line = listViewer.getList().getSelectionIndex();
+ if (line >=0 ){
+ InputDialog inputDialog = new InputDialog(null, "Add Element",
//$NON-NLS-1$
+ "New parameter: ", listViewer.getList().getItem(line), null);
//$NON-NLS-1$
+ if (inputDialog.open() == Window.OK) {
+ listViewer.getList().setItem(line, inputDialog.getValue());
+ }
+ }
+
+
+ }
+ });
+
+ buttonRemove.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ int line = listViewer.getList().getSelectionIndex();
+ if (line < 0) {
+ return;
+ } else {
+ listViewer.getList().remove(line);
+ }
+ }
+ });
+ }
+
+ @Override
+ protected void okPressed() {
+ values = listViewer.getList().getItems();
+ super.okPressed();
+ }
+
+ public String[] getValue() {
+ return values;
+ }
+
+}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersPage.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersPage.java 2010-11-12
14:49:31 UTC (rev 26511)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/QueryParametersPage.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -48,10 +48,12 @@
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
@@ -62,6 +64,7 @@
import org.eclipse.ui.part.Page;
import org.hibernate.console.ConsoleQueryParameter;
import org.hibernate.console.ImageConstants;
+import org.hibernate.console.ParametersListDialog;
import org.hibernate.console.QueryInputModel;
import org.hibernate.eclipse.console.HibernateConsoleMessages;
import org.hibernate.eclipse.console.QueryEditor;
@@ -122,8 +125,6 @@
toggleActive.setChecked(model.ignoreParameters());
tableViewer.getTable().setEnabled(!model.ignoreParameters());
tableViewer.setInput(model);
-
-
}
private void createStatusLabel() {
@@ -224,11 +225,27 @@
}
}
if ( VALUE_PROPERTY.equals( property ) ) {
- cqp.setValueFromString((String) value);
+ 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()) {
- cqp.setValueFromString( "" ); // best attempt to "unnull"
//$NON-NLS-1$
+ cqp.setValue(cqp.convertStringToValue( "" )); // best attempt to
"unnull" //$NON-NLS-1$
} else {
cqp.setNull();
}
@@ -253,7 +270,11 @@
}
}
if ( VALUE_PROPERTY.equals( property ) ) {
- return cqp.getValueAsString();
+ return cqp.getStringValues();
+ /*if (cqp.isArrayValue()){
+ return cqp.convertValueToString(((Object[])cqp.getValue())[0]);
+ }
+ return cqp.convertValueToString(cqp.getValue());*/
}
if ( NULL_PROPERTY.equals( property )) {
return Boolean.valueOf(cqp.isNull());
@@ -294,7 +315,45 @@
CellEditor[] editors = new CellEditor[columnProperties.length];
editors[0] = new TextCellEditor( queryParametersTable );
editors[1] = new ComboBoxCellEditor( queryParametersTable, valueTypes );
- editors[2] = new TextCellEditor( queryParametersTable );
+ editors[2] = new TextDialogCellEditor(queryParametersTable) {
+
+ private Button b;
+
+ @Override
+ protected Object openDialogBox(Control cellEditorWindow) {
+ Object firstElement =
((IStructuredSelection)tableViewer.getSelection()).getFirstElement();
+ if(firstElement instanceof ConsoleQueryParameter) {
+ ParametersListDialog pld = new ParametersListDialog(null, (ConsoleQueryParameter)
firstElement);
+ if (pld.open()==Window.OK){
+ return pld.getValue();//String[]
+ }
+ }
+ return getValue();//could be String or String[]
+ }
+
+ 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 );
@@ -323,7 +382,7 @@
case 1:
return cqp.getType().getName();
case 2:
- return cqp.getValueAsString();
+ return cqp.getStringValues()[0];
case 3:
return null; //cqp.isNull()?"X":"";
default:
Added:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/TextDialogCellEditor.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/TextDialogCellEditor.java
(rev 0)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/views/TextDialogCellEditor.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -0,0 +1,243 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributor:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.hibernate.eclipse.console.views;
+
+import java.text.MessageFormat;
+
+import org.eclipse.jface.viewers.TextCellEditor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.FocusListener;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.Font;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Layout;
+import org.eclipse.swt.widgets.Text;
+
+/**
+ * @author Dmitry Geraskov
+ *
+ */
+public abstract class TextDialogCellEditor extends TextCellEditor {
+
+ private Text text;
+
+ /**
+ * The button.
+ */
+ private Button button;
+
+ /**
+ * The editor control.
+ */
+ private Composite editor;
+
+ private String[] strValues = null;
+
+ private FocusListener buttonFocusListener;
+
+ protected TextDialogCellEditor(Composite parent) {
+ super(parent);
+ }
+
+ @Override
+ protected Control createControl(Composite parent) {
+ Font font = parent.getFont();
+ Color bg = parent.getBackground();
+
+ editor = new Composite(parent, getStyle());
+ editor.setFont(font);
+ editor.setBackground(bg);
+ editor.setLayout(new DialogCellLayout());
+
+ text = (Text) super.createControl(editor);
+ updateContents(strValues);
+
+ button = createButton(editor);
+ button.setFont(font);
+
+ button.addKeyListener(new KeyAdapter() {
+ /* (non-Javadoc)
+ * @see
org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
+ */
+ public void keyReleased(KeyEvent e) {
+ if (e.character == '\u001b') { // Escape
+ fireCancelEditor();
+ }
+ }
+ });
+
+ button.addFocusListener(getButtonFocusListener());
+
+ button.addSelectionListener(new SelectionAdapter() {
+ /* (non-Javadoc)
+ * @see
org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ public void widgetSelected(SelectionEvent event) {
+ // Remove the button's focus listener since it's guaranteed
+ // to lose focus when the dialog opens
+ button.removeFocusListener(getButtonFocusListener());
+
+ Object newValue = openDialogBox(editor);
+
+ // Re-add the listener once the dialog closes
+ button.addFocusListener(getButtonFocusListener());
+
+ if (newValue != null) {
+ boolean newValidState = isCorrect(newValue);
+ if (newValidState) {
+ markDirty();
+ doSetValue(newValue);
+ } else {
+ // try to insert the current value into the error message.
+ setErrorMessage(MessageFormat.format(getErrorMessage(),
+ new Object[] { newValue.toString() }));
+ }
+ fireApplyEditorValue();
+ }
+ }
+
+
+ });
+
+ setValueValid(true);
+
+ return editor;
+ }
+
+ @Override
+ protected void focusLost() {
+ if (isActivated()) {
+ if (button != null && !button.isDisposed() &&
!button.isFocusControl()) {
+ if (text != null && !text.isDisposed() &&
!text.isFocusControl()){
+ fireApplyEditorValue();
+ deactivate();
+ }
+ }
+ }
+ }
+
+ protected abstract Object openDialogBox(Control cellEditorWindow);
+
+ private FocusListener getButtonFocusListener() {
+ if (buttonFocusListener == null) {
+ buttonFocusListener = new FocusListener() {
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
+ */
+ public void focusGained(FocusEvent e) {
+
+ }
+
+ /* (non-Javadoc)
+ * @see
org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
+ */
+ public void focusLost(FocusEvent e) {
+ TextDialogCellEditor.this.focusLost();
+ }
+ };
+ }
+
+ return buttonFocusListener;
+ }
+
+ protected Button createButton(Composite parent) {
+ Button result = new Button(parent, SWT.DOWN);
+ result.setText("..."); //$NON-NLS-1$
+ return result;
+ }
+
+ public void deactivate() {
+ if (button != null && !button.isDisposed()) {
+ button.removeFocusListener(getButtonFocusListener());
+ }
+
+ super.deactivate();
+ }
+
+ @Override
+ protected void editOccured(ModifyEvent e) {
+ String text = this.text.getText();
+ if (strValues == null || strValues.length == 0){
+ strValues = new String[]{text};
+ } else {
+ strValues[0] = text;
+ }
+ super.editOccured(e);
+ }
+
+ protected Object doGetValue() {
+ return strValues;
+ }
+
+ protected void doSetValue(Object value) {
+ if (value == null) {
+ value = "";
+ this.strValues = new String[]{""};
+ } else if (value.getClass().isArray()){
+ this.strValues = (String[])value;
+ value = this.strValues[0];
+ }
+ super.doSetValue(value);
+ }
+
+ protected void updateContents(String[] value) {
+ if (text == null) {
+ return;
+ }
+
+ String strVal = "";//$NON-NLS-1$
+ if (value != null && value.length > 0) {
+ strVal = value[0];
+ }
+ text.setText(strVal);
+ }
+
+ private class DialogCellLayout extends Layout {
+ public void layout(Composite editor, boolean force) {
+ Rectangle bounds = editor.getClientArea();
+ Point size = button.computeSize(SWT.DEFAULT, SWT.DEFAULT, force);
+ if (text != null) {
+ text.setBounds(0, 0, bounds.width - size.x, bounds.height);
+ }
+ button.setBounds(bounds.width - size.x, 0, size.x, bounds.height);
+ }
+
+ public Point computeSize(Composite editor, int wHint, int hHint,
+ boolean force) {
+ if (wHint != SWT.DEFAULT && hHint != SWT.DEFAULT) {
+ return new Point(wHint, hHint);
+ }
+ Point contentsSize = text.computeSize(SWT.DEFAULT, SWT.DEFAULT,
+ force);
+ Point buttonSize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT,
+ force);
+ // Just return the button width to ensure the button is not clipped
+ // if the label is long.
+ // The label will just use whatever extra width there is
+ Point result = new Point(buttonSize.x, Math.max(contentsSize.y,
+ buttonSize.y));
+ return result;
+ }
+ }
+
+
+}
\ No newline at end of file
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java 2010-11-12
14:49:31 UTC (rev 26511)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/ConsoleConfigurationTest.java 2010-11-12
14:50:50 UTC (rev 26512)
@@ -1,14 +1,31 @@
package org.hibernate.eclipse.console.test;
+import java.lang.reflect.Field;
+import java.util.Map;
+
import junit.framework.TestCase;
+import org.hibernate.Hibernate;
+import org.hibernate.Query;
import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.cfg.Mappings;
import org.hibernate.console.ConcoleConfigurationAdapter;
import org.hibernate.console.ConsoleConfiguration;
+import org.hibernate.console.ConsoleQueryParameter;
import org.hibernate.console.HibernateConsoleRuntimeException;
import org.hibernate.console.KnownConfigurations;
+import org.hibernate.console.QueryInputModel;
import org.hibernate.console.QueryPage;
import org.hibernate.eclipse.console.test.launchcfg.TestConsoleConfigurationPreferences;
+import org.hibernate.impl.AbstractQueryImpl;
+import org.hibernate.mapping.Column;
+import org.hibernate.mapping.KeyValue;
+import org.hibernate.mapping.PrimaryKey;
+import org.hibernate.mapping.Property;
+import org.hibernate.mapping.RootClass;
+import org.hibernate.mapping.SimpleValue;
+import org.hibernate.mapping.Table;
public class ConsoleConfigurationTest extends TestCase {
@@ -99,7 +116,59 @@
QueryPage qp = consoleCfg.executeHQLQuery("from java.lang.Object --this is my
comment"); //$NON-NLS-1$
assertNotNull(qp);
}
+
+ public void testHQLListParameters() throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
+ consoleCfg.build();
+ Configuration c = consoleCfg.getConfiguration();
+ Mappings mappings = c.createMappings();
+ RootClass rc = new RootClass();
+ rc.setEntityName("java.awt.Button");
+ rc.setClassName( "java.awt.Button" );
+ Column column = new Column("label");
+ PrimaryKey pk = new PrimaryKey();
+ pk.addColumn(column);
+ Table table = new Table("faketable");
+ rc.setTable(table);
+ table.addColumn(column);
+ table.setPrimaryKey(pk);
+ Property fakeProp = new Property();
+ fakeProp.setName("label");
+ SimpleValue sv = new SimpleValue();
+ sv.addColumn(column);
+ sv.setTypeName("string");
+ sv.setTable(table);
+ fakeProp.setValue(sv);
+ rc.setIdentifierProperty(fakeProp);
+ rc.setIdentifier((KeyValue) fakeProp.getValue());
+ mappings.addClass(rc);
+ consoleCfg.buildSessionFactory();
+
+ ConsoleQueryParameter paramA = new ConsoleQueryParameter("a",
Hibernate.INTEGER,
+ new Integer[]{new Integer(1), new Integer(2)});
+ ConsoleQueryParameter paramB = new ConsoleQueryParameter("b",
Hibernate.INTEGER, new Integer(3));
+ ConsoleQueryParameter paramOrdered = new ConsoleQueryParameter("0",
Hibernate.INTEGER, new Integer(4));
+ QueryInputModel model = new QueryInputModel();
+ model.addParameter(paramA);
+ model.addParameter(paramB);
+ model.addParameter(paramOrdered);
+ QueryPage qp = consoleCfg.executeHQLQuery("select count(*) from java.awt.Button
where 1 in ( ?, :a, :b )", model); //$NON-NLS-1$
+ assertNotNull(qp);
+ try{
+ qp.getList();//execute the query
+ } catch (Exception e){
+ //ignore - there is fake mapping
+ }
+ Field qField = qp.getClass().getDeclaredField("query");
+ qField.setAccessible(true);
+ Query query = (Query) qField.get(qp);
+ assertTrue(query.getNamedParameters().length == 2); // a and b
+ Field listParam =
AbstractQueryImpl.class.getDeclaredField("namedParameterLists");
+ listParam.setAccessible(true);
+ Map namedParameterLists = (Map) listParam.get(query);
+ assertTrue(namedParameterLists.size() == 1);//"a" added as a list parameter
+ }
+
/*public void testCleanup() throws InterruptedException {
for(int cnt=0;cnt<10000;cnt++) {