HHH-4586 - Jpa2.0 Criteria API lower function missing parameter

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18008 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-11-19 01:34:35 +00:00
parent 7587c09f00
commit a3d1a3c59f
7 changed files with 86 additions and 1 deletions

View File

@ -25,6 +25,7 @@ package org.hibernate.ejb.criteria.expression.function;
import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.expression.ExpressionImpl; import org.hibernate.ejb.criteria.expression.ExpressionImpl;
/** /**
@ -57,4 +58,9 @@ public class CastFunction<T,Y> extends BasicFunctionExpression<T> implements Fun
Helper.possibleParameter( getCastSource(), registry ); Helper.possibleParameter( getCastSource(), registry );
} }
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
// HHH-4590
// todo : how to handle these, espeically if in the select...
return super.render( renderingContext );
}
} }

View File

@ -27,7 +27,9 @@ import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.expression.LiteralExpression; import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.ExpressionImplementor;
/** /**
* Models the ANSI SQL <tt>LOCATE</tt> function. * Models the ANSI SQL <tt>LOCATE</tt> function.
@ -95,4 +97,19 @@ public class LocateFunction extends BasicFunctionExpression<Integer> {
Helper.possibleParameter( getStart(), registry ); Helper.possibleParameter( getStart(), registry );
Helper.possibleParameter( getString(), registry ); Helper.possibleParameter( getString(), registry );
} }
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
buffer.append( "locate(" )
.append( ( (ExpressionImplementor) getPattern() ).render( renderingContext ) )
.append( ',' )
.append( ( (ExpressionImplementor) getString() ).render( renderingContext ) );
if ( getStart() != null ) {
buffer.append( ',' )
.append( ( (ExpressionImplementor) getStart() ).render( renderingContext ) );
}
buffer.append( ')' );
return buffer.toString();
}
} }

View File

@ -30,7 +30,9 @@ import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.ParameterContainer; import org.hibernate.ejb.criteria.ParameterContainer;
import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.expression.LiteralExpression; import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.ExpressionImplementor;
/** /**
* Support for functions with parameters. * Support for functions with parameters.
@ -86,4 +88,15 @@ public class ParameterizedFunctionExpression<X>
} }
} }
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
buffer.append( getFunctionName() )
.append( '(' );
for ( Expression argument : argumentExpressions ) {
buffer.append( ( (ExpressionImplementor) argument ).render( renderingContext ) );
}
buffer.append( ')' );
return buffer.toString();
}
} }

View File

@ -27,7 +27,9 @@ import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.expression.LiteralExpression; import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.ExpressionImplementor;
/** /**
* Models the ANSI SQL <tt>SUBSTRING</tt> function. * Models the ANSI SQL <tt>SUBSTRING</tt> function.
@ -102,4 +104,17 @@ public class SubstringFunction extends BasicFunctionExpression<String> {
Helper.possibleParameter( getValue(), registry ); Helper.possibleParameter( getValue(), registry );
} }
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
buffer.append( "substring(" )
.append( ( (ExpressionImplementor) getValue() ).render( renderingContext ) )
.append( ',' )
.append( ( (ExpressionImplementor) getStart() ).render( renderingContext ) );
if ( getLength() != null ) {
buffer.append( ',' )
.append( ( (ExpressionImplementor) getLength() ).render( renderingContext ) );
}
buffer.append( ')' );
return buffer.toString();
}
} }

View File

@ -27,7 +27,9 @@ import javax.persistence.criteria.Expression;
import javax.persistence.criteria.CriteriaBuilder.Trimspec; import javax.persistence.criteria.CriteriaBuilder.Trimspec;
import org.hibernate.ejb.criteria.ParameterRegistry; import org.hibernate.ejb.criteria.ParameterRegistry;
import org.hibernate.ejb.criteria.CriteriaBuilderImpl; import org.hibernate.ejb.criteria.CriteriaBuilderImpl;
import org.hibernate.ejb.criteria.CriteriaQueryCompiler;
import org.hibernate.ejb.criteria.expression.LiteralExpression; import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.ExpressionImplementor;
/** /**
* Models the ANSI SQL <tt>TRIM</tt> function. * Models the ANSI SQL <tt>TRIM</tt> function.
@ -110,4 +112,16 @@ public class TrimFunction extends BasicFunctionExpression<String> {
Helper.possibleParameter( getTrimSource(), registry ); Helper.possibleParameter( getTrimSource(), registry );
} }
@Override
public String render(CriteriaQueryCompiler.RenderingContext renderingContext) {
return new StringBuilder()
.append( "trim(" )
.append( trimspec.name() )
.append( ' ' )
.append( ( (ExpressionImplementor) trimCharacter ).render( renderingContext ) )
.append( " from " )
.append( ( (ExpressionImplementor) trimSource ).render( renderingContext ) )
.append( ')' )
.toString();
}
} }

View File

@ -28,6 +28,7 @@ import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.test.TestCase; import org.hibernate.ejb.test.TestCase;
@ -65,4 +66,23 @@ public class ComponentCriteriaTest extends TestCase {
em.getTransaction().commit(); em.getTransaction().commit();
em.close(); em.close();
} }
public void testParameterizedFunctions() {
// HHH-4586
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
CriteriaBuilder cb = em.getCriteriaBuilder();
// lower
CriteriaQuery<Client> cq = cb.createQuery( Client.class );
Root<Client> root = cq.from( Client.class );
cq.where( cb.equal( cb.lower( root.get( Client_.name ).get( Name_.lastName ) ),"test" ) );
em.createQuery( cq ).getResultList();
// upper
cq = cb.createQuery( Client.class );
root = cq.from( Client.class );
cq.where( cb.equal( cb.upper( root.get( Client_.name ).get( Name_.lastName ) ),"test" ) );
em.createQuery( cq ).getResultList();
em.getTransaction().commit();
em.close();
}
} }

View File

@ -35,7 +35,7 @@ log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ### ### log JDBC bind parameters ###
#log4j.logger.org.hibernate.type=info #log4j.logger.org.hibernate.type=info
log4j.logger.org.hibernate.type=debug log4j.logger.org.hibernate.type=trace
### log schema export/update ### ### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug log4j.logger.org.hibernate.tool.hbm2ddl=debug