HHH-7387 - Integrate Draft 6 of the JPA 2.1 spec : FUNCTION keyword

This commit is contained in:
Steve Ebersole 2012-06-21 17:39:49 -05:00
parent 0a37440431
commit 8b87ae8830
5 changed files with 147 additions and 53 deletions

View File

@ -195,6 +195,9 @@ tokens
public void processMemberOf(Token n,AST p,ASTPair currentAST) { }
protected String unquote(String text) {
return text.substring( 1, text.length() - 1 );
}
}
statement
@ -599,10 +602,13 @@ quantifiedExpression
;
// level 0 - expression atom
// ident qualifier ('.' ident ), array index ( [ expr ] ),
// method call ( '.' ident '(' exprList ') )
// * ident qualifier ('.' ident )
// * array index ( [ expr ] )
// * method call ( '.' ident '(' exprList ') )
// * function : differentiated from method call via explicit keyword
atom
: primaryExpression
: { LT(1).getText().equalsIgnoreCase("function") && LA(2) == OPEN && LA(3) == QUOTED_STRING }? jpaFunctionSyntax
| primaryExpression
(
DOT^ identifier
( options { greedy=true; } :
@ -611,6 +617,14 @@ atom
)*
;
jpaFunctionSyntax!
: i:IDENT OPEN n:QUOTED_STRING COMMA a:exprList CLOSE {
#i.setType( METHOD_CALL );
#i.setText( #i.getText() + " (" + #n.getText() + ")" );
#jpaFunctionSyntax = #( #i, [IDENT, unquote( #n.getText() )], #a );
}
;
// level 0 - the basic element of an expression
primaryExpression
: identPrimary ( options {greedy=true;} : DOT^ "class" )?

View File

@ -35,7 +35,6 @@ import org.hibernate.hql.internal.antlr.SqlTokenTypes;
import org.hibernate.hql.internal.ast.TypeDiscriminatorMetadata;
import org.hibernate.hql.internal.ast.util.ASTUtil;
import org.hibernate.hql.internal.ast.util.ColumnHelper;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.persister.collection.CollectionPropertyNames;
import org.hibernate.persister.collection.QueryableCollection;
import org.hibernate.type.Type;
@ -46,8 +45,7 @@ import org.hibernate.type.Type;
* @author josh
*/
public class MethodNode extends AbstractSelectExpression implements FunctionNode {
private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, MethodNode.class.getName());
private static final Logger LOG = Logger.getLogger( MethodNode.class.getName() );
private String methodName;
private FromElement fromElement;
@ -55,25 +53,60 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
private SQLFunction function;
private boolean inSelect;
@Override
public boolean isScalar() throws SemanticException {
// Method expressions in a SELECT should always be considered scalar.
return true;
}
@Override
public SQLFunction getSQLFunction() {
return function;
}
@Override
public Type getFirstArgumentType() {
AST argument = getFirstChild();
while ( argument != null ) {
if ( argument instanceof SqlNode ) {
final Type type = ( (SqlNode) argument ).getDataType();
if ( type != null ) {
return type;
}
argument = argument.getNextSibling();
}
}
return null;
}
public void resolve(boolean inSelect) throws SemanticException {
// Get the function name node.
AST name = getFirstChild();
initializeMethodNode( name, inSelect );
AST exprList = name.getNextSibling();
AST nameNode = getFirstChild();
AST exprListNode = nameNode.getNextSibling();
initializeMethodNode( nameNode, inSelect );
// If the expression list has exactly one expression, and the type of the expression is a collection
// then this might be a collection function, such as index(c) or size(c).
if ( ASTUtil.hasExactlyOneChild( exprList ) ) {
if ( ASTUtil.hasExactlyOneChild( exprListNode ) ) {
if ( "type".equals( methodName ) ) {
typeDiscriminator( exprList.getFirstChild() );
typeDiscriminator( exprListNode.getFirstChild() );
return;
}
if ( isCollectionPropertyMethod() ) {
collectionProperty( exprList.getFirstChild(), name );
collectionProperty( exprListNode.getFirstChild(), nameNode );
return;
}
}
dialectFunction( exprList );
dialectFunction( exprListNode );
}
public void initializeMethodNode(AST name, boolean inSelect) {
name.setType( SqlTokenTypes.METHOD_NAME );
String text = name.getText();
methodName = text.toLowerCase(); // Use the lower case function name.
this.inSelect = inSelect; // Remember whether we're in a SELECT clause or not.
}
private void typeDiscriminator(AST path) throws SemanticException {
@ -90,24 +123,6 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
setType( SqlTokenTypes.SQL_TOKEN );
}
public SQLFunction getSQLFunction() {
return function;
}
public Type getFirstArgumentType() {
AST argument = getFirstChild();
while ( argument != null ) {
if ( argument instanceof SqlNode ) {
final Type type = ( (SqlNode) argument ).getDataType();
if ( type != null ) {
return type;
}
argument = argument.getNextSibling();
}
}
return null;
}
private void dialectFunction(AST exprList) {
function = getSessionFactoryHelper().findSQLFunction( methodName );
if ( function != null ) {
@ -126,17 +141,6 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
return CollectionProperties.isAnyCollectionProperty( methodName );
}
public void initializeMethodNode(AST name, boolean inSelect) {
name.setType( SqlTokenTypes.METHOD_NAME );
String text = name.getText();
methodName = text.toLowerCase(); // Use the lower case function name.
this.inSelect = inSelect; // Remember whether we're in a SELECT clause or not.
}
private String getMethodName() {
return methodName;
}
private void collectionProperty(AST path, AST name) throws SemanticException {
if ( path == null ) {
throw new SemanticException( "Collection function " + name.getText() + " has no path!" );
@ -149,14 +153,8 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
resolveCollectionProperty( expr );
}
@Override
public boolean isScalar() throws SemanticException {
// Method expressions in a SELECT should always be considered scalar.
return true;
}
public void resolveCollectionProperty(AST expr) throws SemanticException {
String propertyName = CollectionProperties.getNormalizedPropertyName( getMethodName() );
protected void resolveCollectionProperty(AST expr) throws SemanticException {
String propertyName = CollectionProperties.getNormalizedPropertyName( methodName );
if ( expr instanceof FromReferenceNode ) {
FromReferenceNode collectionNode = ( FromReferenceNode ) expr;
// If this is 'elements' then create a new FROM element.
@ -236,7 +234,7 @@ public class MethodNode extends AbstractSelectExpression implements FunctionNode
public String getDisplayText() {
return "{" +
"method=" + getMethodName() +
"method=" + methodName +
",selectColumns=" + ( selectColumns == null ?
null : Arrays.asList( selectColumns ) ) +
",fromElement=" + fromElement.getTableAlias() +

View File

@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.jpa.ql;
import org.hibernate.Session;
import org.junit.Test;
import org.hibernate.test.jpa.AbstractJPATest;
/**
* Test use of the JPA 2.1 FUNCTION keyword.
*
* @author Steve Ebersole
*/
public class FunctionKeywordTest extends AbstractJPATest {
@Test
public void basicFixture() {
Session s = openSession();
s.createQuery( "select i from Item i where substring( i.name, 1, 3 ) = 'abc'" )
.list();
s.close();
}
@Test
public void basicTest() {
Session s = openSession();
s.createQuery( "select i from Item i where function( 'substring', i.name, 1, 3 ) = 'abc'" )
.list();
s.close();
}
}

View File

@ -91,8 +91,10 @@ public class ParameterizedFunctionExpression<X>
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
buffer.append( getFunctionName() ).append( '(' );
StringBuilder buffer = new StringBuilder()
.append( "function('" )
.append( getFunctionName() )
.append( "', " );
renderArguments( buffer, renderingContext );
buffer.append( ')' );
return buffer.toString();

View File

@ -40,6 +40,7 @@ import org.hibernate.ejb.metamodel.Alias;
import org.hibernate.ejb.metamodel.Country;
import org.hibernate.ejb.metamodel.CreditCard;
import org.hibernate.ejb.metamodel.Customer;
import org.hibernate.ejb.metamodel.Customer_;
import org.hibernate.ejb.metamodel.Info;
import org.hibernate.ejb.metamodel.LineItem;
import org.hibernate.ejb.metamodel.MetamodelImpl;
@ -237,4 +238,29 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
em.getTransaction().commit();
em.close();
}
@Test
public void testFunctionDialectFunctions() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
CriteriaQuery<Long> criteria = cb.createQuery( Long.class );
criteria.select( cb.count( cb.literal( 1 ) ) );
Root<Customer> root = criteria.from( Customer.class );
criteria.where(
cb.equal(
cb.function(
"substring",
String.class,
cb.literal( 1 ),
root.get( Customer_.name ),
cb.literal( 1 )
),
cb.literal( "a" )
)
);
em.createQuery( criteria ).getResultList();
em.getTransaction().commit();
em.close();
}
}