HHH-16699 promote repeat() function

emulate it on Oracle using rpad()

unsupported on Derby
This commit is contained in:
Gavin 2023-05-25 20:11:17 +02:00 committed by Gavin King
parent b2dfe7148a
commit be2319b59c
4 changed files with 43 additions and 2 deletions

View File

@ -265,6 +265,7 @@ public class OracleDialect extends Dialect {
functionFactory.addMonths();
functionFactory.monthsBetween();
functionFactory.everyAny_minMaxCase();
functionFactory.repeat_rpad();
functionFactory.radians_acos();
functionFactory.degrees_acos();

View File

@ -710,6 +710,15 @@ public class CommonFunctionFactory {
.register();
}
public void repeat_rpad() {
functionRegistry.patternDescriptorBuilder( "repeat", "rpad(?1,?2*length(?1),?1)" )
.setInvariantType(stringType)
.setExactArgumentCount( 2 )
.setParameterTypes(STRING, INTEGER)
.setArgumentListSignature( "(STRING string, INTEGER times)" )
.register();
}
public void leftRight() {
functionRegistry.namedDescriptorBuilder( "left" )
.setInvariantType(stringType)

View File

@ -709,6 +709,27 @@ public class FunctionTests {
);
}
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsRepeat.class)
public void testRepeatFunction(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
assertThat( session.createQuery("select repeat('hello', 3)", String.class).getSingleResult(),
is("hellohellohello") );
assertThat( session.createQuery("select repeat(?1, 3)", String.class)
.setParameter(1, "hello")
.getSingleResult(),
is("hellohellohello") );
//HSQLDB doesn't like the second parameter
// assertThat( session.createQuery("select repeat(?1, ?2)", String.class)
// .setParameter(1, "hello")
// .setParameter(2, 3)
// .getSingleResult(),
// is("hellohellohello") );
}
);
}
@Test
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsReplace.class)
public void testReplaceFunction(SessionFactoryScope scope) {
@ -716,8 +737,10 @@ public class FunctionTests {
session -> {
session.createQuery("select replace(e.theString, 'hello', 'goodbye') from EntityOfBasics e", String.class)
.list();
assertThat( session.createQuery("select replace('hello world', 'hello', 'goodbye')", String.class).getSingleResult(), is("goodbye world") );
assertThat( session.createQuery("select replace('hello world', 'o', 'ooo')", String.class).getSingleResult(), is("hellooo wooorld") );
assertThat( session.createQuery("select replace('hello world', 'hello', 'goodbye')", String.class).getSingleResult(),
is("goodbye world") );
assertThat( session.createQuery("select replace('hello world', 'o', 'ooo')", String.class).getSingleResult(),
is("hellooo wooorld") );
}
);

View File

@ -318,6 +318,14 @@ abstract public class DialectFeatureChecks {
}
}
public static class SupportsRepeat implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
dialect = DialectDelegateWrapper.extractRealDialect( dialect );
// Derby doesn't support the `REPLACE` function
return !( dialect instanceof DerbyDialect );
}
}
public static class SupportsTemporaryTable implements DialectFeatureCheck {
public boolean apply(Dialect dialect) {
return dialect.supportsTemporaryTables();