HHH-4700 - ClassCastException with parameter in CaseNode.getFirstThenNode()

This commit is contained in:
Steve Ebersole 2014-04-03 08:35:44 -05:00
parent 920e3cf2ab
commit efa5dc2f6c
8 changed files with 356 additions and 111 deletions

View File

@ -32,8 +32,8 @@ import org.hibernate.hql.internal.ast.tree.BetweenOperatorNode;
import org.hibernate.hql.internal.ast.tree.BinaryArithmeticOperatorNode;
import org.hibernate.hql.internal.ast.tree.BinaryLogicOperatorNode;
import org.hibernate.hql.internal.ast.tree.BooleanLiteralNode;
import org.hibernate.hql.internal.ast.tree.Case2Node;
import org.hibernate.hql.internal.ast.tree.CaseNode;
import org.hibernate.hql.internal.ast.tree.SearchedCaseNode;
import org.hibernate.hql.internal.ast.tree.SimpleCaseNode;
import org.hibernate.hql.internal.ast.tree.CollectionFunction;
import org.hibernate.hql.internal.ast.tree.ConstructorNode;
import org.hibernate.hql.internal.ast.tree.CountNode;
@ -171,9 +171,9 @@ public class SqlASTFactory extends ASTFactory implements HqlSqlTokenTypes {
case UNARY_PLUS:
return UnaryArithmeticNode.class;
case CASE2:
return Case2Node.class;
return SimpleCaseNode.class;
case CASE:
return CaseNode.class;
return SearchedCaseNode.class;
case PARAM:
case NAMED_PARAM:
return ParameterNode.class;

View File

@ -1,51 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.hql.internal.ast.tree;
import org.hibernate.hql.internal.ast.util.ColumnHelper;
import org.hibernate.type.Type;
import antlr.SemanticException;
/**
* Represents a case ... when .. then ... else ... end expression in a select.
*
* @author Gavin King
*/
public class Case2Node extends AbstractSelectExpression implements SelectExpression {
public Type getDataType() {
return getFirstThenNode().getDataType();
}
private SelectExpression getFirstThenNode() {
return (SelectExpression) getFirstChild().getNextSibling().getFirstChild().getNextSibling();
}
public void setScalarColumnText(int i) throws SemanticException {
ColumnHelper.generateSingleScalarColumn( this, i );
}
}

View File

@ -1,51 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.hql.internal.ast.tree;
import org.hibernate.hql.internal.ast.util.ColumnHelper;
import org.hibernate.type.Type;
import antlr.SemanticException;
/**
* Represents a case ... when .. then ... else ... end expression in a select.
*
* @author Gavin King
*/
public class CaseNode extends AbstractSelectExpression implements SelectExpression {
public Type getDataType() {
return getFirstThenNode().getDataType();
}
private SelectExpression getFirstThenNode() {
return (SelectExpression) getFirstChild().getFirstChild().getNextSibling();
}
public void setScalarColumnText(int i) throws SemanticException {
ColumnHelper.generateSingleScalarColumn( this, i );
}
}

View File

@ -133,13 +133,9 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
if ( function != null ) {
AST firstChild = exprList != null ? exprList.getFirstChild() : null;
Type functionReturnType = getSessionFactoryHelper()
.findFunctionReturnType( methodName, firstChild );
.findFunctionReturnType( methodName, function, firstChild );
setDataType( functionReturnType );
}
//TODO:
/*else {
methodName = (String) getWalker().getTokenReplacements().get( methodName );
}*/
}
public boolean isCollectionPropertyMethod() {

View File

@ -0,0 +1,88 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.hql.internal.ast.tree;
import org.hibernate.QueryException;
import org.hibernate.hql.internal.antlr.HqlSqlTokenTypes;
import org.hibernate.hql.internal.ast.util.ASTUtil;
import org.hibernate.hql.internal.ast.util.ColumnHelper;
import org.hibernate.type.Type;
import antlr.SemanticException;
import antlr.collections.AST;
/**
* Models what ANSI SQL terms a <tt>searched case expression</tt>. This is a <tt>CASE</tt> expression
* in the form<pre>
* CASE
* WHEN [firstCondition] THEN [firstResult]
* WHEN [secondCondition] THEN [secondResult]
* ELSE [defaultResult]
* END
* </pre>
*
* @author Gavin King
* @author Steve Ebersole
*/
public class SearchedCaseNode extends AbstractSelectExpression implements SelectExpression {
@Override
public Type getDataType() {
// option is used to hold each WHEN/ELSE in turn
AST option = getFirstChild();
while ( option != null ) {
final AST result;
if ( option.getType() == HqlSqlTokenTypes.WHEN ) {
result = option.getFirstChild().getNextSibling();
}
else if ( option.getType() == HqlSqlTokenTypes.ELSE ) {
result = option.getFirstChild();
}
else {
throw new QueryException(
"Unexpected node type :" +
ASTUtil.getTokenTypeName( HqlSqlTokenTypes.class, option.getType() ) +
"; expecting WHEN or ELSE"
);
}
if ( SqlNode.class.isInstance( result ) ) {
final Type nodeDataType = ( (SqlNode) result ).getDataType();
if ( nodeDataType != null ) {
return nodeDataType;
}
}
option = option.getNextSibling();
}
throw new QueryException( "Could not determine data type for searched case statement" );
}
@Override
public void setScalarColumnText(int i) throws SemanticException {
ColumnHelper.generateSingleScalarColumn( this, i );
}
}

View File

@ -0,0 +1,87 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.hql.internal.ast.tree;
import org.hibernate.QueryException;
import org.hibernate.hql.internal.antlr.HqlSqlTokenTypes;
import org.hibernate.hql.internal.ast.util.ASTUtil;
import org.hibernate.hql.internal.ast.util.ColumnHelper;
import org.hibernate.type.Type;
import antlr.SemanticException;
import antlr.collections.AST;
/**
* Models what ANSI SQL terms a simple case statement. This is a <tt>CASE</tt> expression in the form<pre>
* CASE [expression]
* WHEN [firstCondition] THEN [firstResult]
* WHEN [secondCondition] THEN [secondResult]
* ELSE [defaultResult]
* END
* </pre>
*
* @author Gavin King
* @author Steve Ebersole
*/
public class SimpleCaseNode extends AbstractSelectExpression implements SelectExpression {
public Type getDataType() {
final AST expression = getFirstChild();
// option is used to hold each WHEN/ELSE in turn
AST option = expression.getNextSibling();
while ( option != null ) {
final AST result;
if ( option.getType() == HqlSqlTokenTypes.WHEN ) {
result = option.getFirstChild().getNextSibling();
}
else if ( option.getType() == HqlSqlTokenTypes.ELSE ) {
result = option.getFirstChild();
}
else {
throw new QueryException(
"Unexpected node type :" +
ASTUtil.getTokenTypeName( HqlSqlTokenTypes.class, option.getType() ) +
"; expecting WHEN or ELSE"
);
}
if ( SqlNode.class.isInstance( result ) ) {
final Type nodeDataType = ( (SqlNode) result ).getDataType();
if ( nodeDataType != null ) {
return nodeDataType;
}
}
option = option.getNextSibling();
}
throw new QueryException( "Could not determine data type for simple case statement" );
}
public void setScalarColumnText(int i) throws SemanticException {
ColumnHelper.generateSingleScalarColumn( this, i );
}
}

View File

@ -0,0 +1,154 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.hql;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.junit.Assert.fail;
/**
* @author Steve Ebersole
*/
public class CaseStatementTest extends BaseCoreFunctionalTestCase {
@Entity(name = "Person")
public static class Person {
@Id
private Integer id;
private String name;
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class };
}
@Test
public void testSimpleCaseStatementFixture() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "select case p.name when 'Steve' then 'x' else 'y' end from Person p" )
.list();
t.commit();
s.close();
}
@Test
public void testSimpleCaseStatementWithParamResult() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "select case p.name when 'Steve' then :opt1 else p.name end from Person p" )
.setString( "opt1", "x" )
.list();
t.commit();
s.close();
}
@Test
public void testSimpleCaseStatementWithParamAllResults() {
Session s = openSession();
Transaction t = s.beginTransaction();
try {
s.createQuery( "select case p.name when 'Steve' then :opt1 else :opt2 end from Person p" )
.setString( "opt1", "x" )
.setString( "opt2", "y" )
.list();
fail( "was expecting an exception" );
}
catch (QueryException expected) {
// expected
}
s.createQuery( "select case p.name when 'Steve' then cast( :opt1 as string ) else cast( :opt2 as string) end from Person p" )
.setString( "opt1", "x" )
.setString( "opt2", "y" )
.list();
t.commit();
s.close();
}
@Test
public void testSearchedCaseStatementFixture() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "select case when p.name = 'Steve' then 'x' else 'y' end from Person p" )
.list();
t.commit();
s.close();
}
@Test
public void testSearchedCaseStatementWithParamResult() {
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( "select case when p.name = 'Steve' then :opt1 else p.name end from Person p" )
.setString( "opt1", "x" )
.list();
t.commit();
s.close();
}
@Test
public void testSearchedCaseStatementWithAllParamResults() {
Session s = openSession();
Transaction t = s.beginTransaction();
try {
s.createQuery( "select case when p.name = 'Steve' then :opt1 else :opt2 end from Person p" )
.setString( "opt1", "x" )
.setString( "opt2", "y" )
.list();
fail( "was expecting an exception" );
}
catch (QueryException expected) {
// expected
}
s.createQuery( "select case when p.name = 'Steve' then cast( :opt1 as string) else :opt2 end from Person p" )
.setString( "opt1", "x" )
.setString( "opt2", "y" )
.list();
t.commit();
s.close();
}
}

View File

@ -73,6 +73,28 @@ public class BasicSimpleCaseTest extends BaseEntityManagerFunctionalTestCase {
}
@Test
public void testCaseInOrderBy2() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Customer> query = builder.createQuery( Customer.class );
Root<Customer> root = query.from( Customer.class );
query.select( root );
Path<String> emailPath = root.get( "email" );
SimpleCase<String, String> orderCase = builder.selectCase( emailPath );
orderCase = orderCase.when( "test@test.com", "a" );
orderCase = orderCase.when( "test2@test.com", "b" );
query.orderBy( builder.asc( orderCase.otherwise( "c" ) ) );
em.createQuery( query );
}
@Entity(name = "Customer")
@Table(name = "customer")
public static class Customer {