HHH-9154 - keywords as parameter names

(cherry picked from commit 5aff092e2bd06bcfc18eb6eec9983b2f97237d64)

Conflicts:
	hibernate-core/src/main/antlr/hql.g
	hibernate-core/src/test/java/org/hibernate/test/hql/ASTParserLoadingTest.java
This commit is contained in:
Steve Ebersole 2014-05-15 09:05:22 -05:00
parent 044bf39e22
commit bd3fca9d12
4 changed files with 135 additions and 71 deletions

View File

@ -197,8 +197,16 @@ tokens
public void weakKeywords() throws TokenStreamException { }
public void processMemberOf(Token n,AST p,ASTPair currentAST) { }
/**
* Called after we have recognized ':'. The expectation is to handle converting
* any non-IDENT token where possibleID == true into an IDENT
*/
public void expectNamedParameterName() throws TokenStreamException {
}
public void processMemberOf(Token n,AST p,ASTPair currentAST) {
}
protected boolean validateSoftKeyword(String text) throws TokenStreamException {
return validateLookAheadText(1, text);
}
@ -633,8 +641,7 @@ quantifiedExpression
// ident qualifier ('.' ident ), array index ( [ expr ] ),
// method call ( '.' ident '(' exprList ') )
atom
: { validateSoftKeyword("cast") && LA(2) == OPEN }? castFunction
| primaryExpression
: primaryExpression
(
DOT^ identifier
( options { greedy=true; } :
@ -657,17 +664,18 @@ castTargetType
: identifier { handleDotIdent(); } ( options { greedy=true; } : DOT^ identifier )*
;
// level 0 - the basic element of an expression
primaryExpression
: identPrimary ( options {greedy=true;} : DOT^ "class" )?
| constant
| parameter
// TODO: Add parens to the tree so the user can control the operator evaluation order.
| OPEN! (expressionOrVector | subQuery) CLOSE!
: { validateSoftKeyword("cast") && LA(2) == OPEN }? castFunction
| identPrimary ( options {greedy=true;} : DOT^ "class" )?
| constant
| parameter
| OPEN! (expressionOrVector | subQuery) CLOSE!
;
parameter
: COLON^ identifier
: COLON^ { expectNamedParameterName(); } IDENT
| PARAM^ (NUM_INT)?
;

View File

@ -363,6 +363,25 @@ public final class HqlParser extends HqlBaseParser {
}
}
@Override
public void expectNamedParameterName() throws TokenStreamException {
// we expect the token following a COLON (':') to be the name of a named parameter.
// if the following token is anything other than IDENT we convert its type if possible.
// NOTE : the LT() call is more expensive than the LA() call; so we
// use LA() first to see if LT() is needed.
if ( LA( 1 ) != IDENT ) {
final HqlToken nextToken = (HqlToken) LT( 1 );
if ( nextToken.isPossibleID() ) {
LOG.debugf(
"Converting keyword [%s] following COLON to IDENT as an expected parameter name",
nextToken.getText()
);
nextToken.setType( IDENT );
}
}
}
@Override
public void handleDotIdent() throws TokenStreamException {
// This handles HHH-354, where there is a strange property name in a where clause.

View File

@ -23,15 +23,6 @@
*/
package org.hibernate.test.hql;
import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
@ -72,6 +63,19 @@ import org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.persister.entity.DiscriminatorType;
import org.hibernate.stat.QueryStatistics;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.ComponentType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.Type;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.test.any.IntegerPropertyValue;
import org.hibernate.test.any.PropertySet;
import org.hibernate.test.any.PropertyValue;
@ -81,21 +85,19 @@ import org.hibernate.test.cid.LineItem;
import org.hibernate.test.cid.LineItem.Id;
import org.hibernate.test.cid.Order;
import org.hibernate.test.cid.Product;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialect;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.transform.DistinctRootEntityResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.ComponentType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.Type;
import org.jboss.logging.Logger;
import org.junit.Test;
import org.jboss.logging.Logger;
import static org.hibernate.testing.junit4.ExtraAssertions.assertClassAssignability;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Tests the integration of the new AST parser into the loading of query results using
* the Hibernate persisters and loaders.
@ -262,45 +264,6 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
s.close();
}
@Test
@FailureExpected( jiraKey = "HHH-9154" )
public void testClassAsParameter() {
Session s = openSession();
s.beginTransaction();
Type[] types = s.createQuery( "select h.name from Human h" ).getReturnTypes();
assertEquals( 1, types.length );
assertTrue( types[0] instanceof ComponentType );
s.createQuery( "from Human h where h.name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human where name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class = h.name" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class <> h.name" ).setParameter( "class", new Name() ).list();
s.getTransaction().commit();
s.close();
}
@Test
@FailureExpected( jiraKey = "HHH-9154" )
public void testObjectAsParameter() {
Session s = openSession();
s.beginTransaction();
Type[] types = s.createQuery( "select h.name from Human h" ).getReturnTypes();
assertEquals( 1, types.length );
assertTrue( types[0] instanceof ComponentType );
s.createQuery( "from Human h where h.name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human where name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT = h.name" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT <> h.name" ).setParameter( "OBJECT", new Name() ).list();
s.getTransaction().commit();
s.close();
}
@Test
public void testComponentJoins() {
Session s = openSession();

View File

@ -0,0 +1,74 @@
/*
* 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 org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Isolated test for various usages of parameters
*
* @author Steve Ebersole
*/
public class ParameterTest extends BaseCoreFunctionalTestCase {
@Override
protected String[] getMappings() {
return new String[] { "hql/Animal.hbm.xml" };
}
@Test
@TestForIssue( jiraKey = "HHH-9154" )
public void testClassAsParameter() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Human h where h.name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human where name = :class" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class = h.name" ).setParameter( "class", new Name() ).list();
s.createQuery( "from Human h where :class <> h.name" ).setParameter( "class", new Name() ).list();
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9154" )
public void testObjectAsParameter() {
Session s = openSession();
s.beginTransaction();
s.createQuery( "from Human h where h.name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human where name = :OBJECT" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT = h.name" ).setParameter( "OBJECT", new Name() ).list();
s.createQuery( "from Human h where :OBJECT <> h.name" ).setParameter( "OBJECT", new Name() ).list();
s.getTransaction().commit();
s.close();
}
}