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);
}
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;
}
}
});
}
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()));
}
}
}
}
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
}
}
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()));
}
Aggregations