HHH-16699 add repeat() function to criteria API

This commit is contained in:
Gavin 2023-05-26 00:18:34 +02:00 committed by Gavin King
parent ed897c4208
commit 8c1839f7dc
4 changed files with 76 additions and 0 deletions

View File

@ -1196,6 +1196,36 @@ public interface HibernateCriteriaBuilder extends CriteriaBuilder {
Expression<Integer> length, Expression<Integer> length,
Expression<Character> padChar); Expression<Character> padChar);
/**
* Concatenate the given string expression with itself the given number of times.
*
* @param x the string expression to concatenate
* @param times the number of times it should be repeated
*
* @return repeat expression
*/
JpaFunction<String> repeat(Expression<String> x, Expression<Integer> times);
/**
* Concatenate the given string expression with itself the given number of times.
*
* @param x the string expression to concatenate
* @param times the number of times it should be repeated
*
* @return repeat expression
*/
JpaFunction<String> repeat(Expression<String> x, int times);
/**
* Concatenate the given string expression with itself the given number of times.
*
* @param x the string expression to concatenate
* @param times the number of times it should be repeated
*
* @return repeat expression
*/
JpaFunction<String> repeat(String x, Expression<Integer> times);
/** /**
* @see #left(Expression, Expression) * @see #left(Expression, Expression)
*/ */

View File

@ -1464,6 +1464,21 @@ public class HibernateCriteriaBuilderDelegate implements HibernateCriteriaBuilde
return criteriaBuilder.pad( ts, x, length, padChar ); return criteriaBuilder.pad( ts, x, length, padChar );
} }
@Override
public JpaFunction<String> repeat(Expression<String> x, Expression<Integer> times) {
return criteriaBuilder.repeat( x, times );
}
@Override
public JpaFunction<String> repeat(Expression<String> x, int times) {
return criteriaBuilder.repeat( x, times );
}
@Override
public JpaFunction<String> repeat(String x, Expression<Integer> times) {
return criteriaBuilder.repeat( x, times );
}
@Override @Override
public JpaFunction<String> left(Expression<String> x, int length) { public JpaFunction<String> left(Expression<String> x, int length) {
return criteriaBuilder.left( x, length ); return criteriaBuilder.left( x, length );

View File

@ -57,6 +57,7 @@ import org.hibernate.query.criteria.JpaCompoundSelection;
import org.hibernate.query.criteria.JpaCriteriaQuery; import org.hibernate.query.criteria.JpaCriteriaQuery;
import org.hibernate.query.criteria.JpaCteCriteriaAttribute; import org.hibernate.query.criteria.JpaCteCriteriaAttribute;
import org.hibernate.query.criteria.JpaExpression; import org.hibernate.query.criteria.JpaExpression;
import org.hibernate.query.criteria.JpaFunction;
import org.hibernate.query.criteria.JpaOrder; import org.hibernate.query.criteria.JpaOrder;
import org.hibernate.query.criteria.JpaPredicate; import org.hibernate.query.criteria.JpaPredicate;
import org.hibernate.query.criteria.JpaSearchOrder; import org.hibernate.query.criteria.JpaSearchOrder;
@ -2967,6 +2968,26 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext,
); );
} }
@Override
public JpaFunction<String> repeat(Expression<String> x, Expression<Integer> times) {
return getFunctionDescriptor( "repeat" ).generateSqmExpression(
asList( (SqmExpression<String>) x, (SqmExpression<Integer>) times ),
null,
getQueryEngine(),
getJpaMetamodel().getTypeConfiguration()
);
}
@Override
public JpaFunction<String> repeat(Expression<String> x, int times) {
return repeat( x, value( times ) );
}
@Override
public JpaFunction<String> repeat(String x, Expression<Integer> times) {
return repeat( value( x), times );
}
@Override @Override
public SqmFunction<String> left(Expression<String> x, int length) { public SqmFunction<String> left(Expression<String> x, int length) {
return left( x, value( length ) ); return left( x, value( length ) );

View File

@ -309,6 +309,16 @@ public class CriteriaBuilderNonStandardFunctionsTest {
} ); } );
} }
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsRepeat.class)
public void testRepeat(SessionFactoryScope scope) {
scope.inTransaction( session -> {
HibernateCriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<String> query = cb.createQuery( String.class ).select( cb.repeat("hello", cb.literal(3)) );
assertEquals( "hellohellohello", session.createQuery( query ).getSingleResult() );
} );
}
@Test @Test
@RequiresDialect(PostgreSQLDialect.class) @RequiresDialect(PostgreSQLDialect.class)
public void testCollatePostgreSQL(SessionFactoryScope scope) { public void testCollatePostgreSQL(SessionFactoryScope scope) {