HHH-18018 Use NO_PLAIN_PARAMETER for Derby functions that use the length function

This commit is contained in:
Christian Beikov 2024-05-01 02:56:24 +02:00
parent f379ae0652
commit d5da60da1c
4 changed files with 24 additions and 4 deletions

View File

@ -380,8 +380,8 @@ public class DerbyLegacyDialect extends Dialect {
functionFactory.power_expLn(); functionFactory.power_expLn();
functionFactory.round_floor(); functionFactory.round_floor();
functionFactory.trunc_floor(); functionFactory.trunc_floor();
functionFactory.octetLength_pattern( "length(?1)" ); functionFactory.octetLength_pattern( "length(?1)", SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionFactory.bitLength_pattern( "length(?1)*8" ); functionFactory.bitLength_pattern( "length(?1)*8", SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionContributions.getFunctionRegistry().register( functionContributions.getFunctionRegistry().register(
"concat", "concat",

View File

@ -77,6 +77,8 @@ import org.hibernate.type.spi.TypeConfiguration;
import jakarta.persistence.TemporalType; import jakarta.persistence.TemporalType;
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.INTEGER;
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING;
import static org.hibernate.type.SqlTypes.BINARY; import static org.hibernate.type.SqlTypes.BINARY;
import static org.hibernate.type.SqlTypes.BLOB; import static org.hibernate.type.SqlTypes.BLOB;
import static org.hibernate.type.SqlTypes.CHAR; import static org.hibernate.type.SqlTypes.CHAR;
@ -369,8 +371,8 @@ public class DerbyDialect extends Dialect {
functionFactory.power_expLn(); functionFactory.power_expLn();
functionFactory.round_floor(); functionFactory.round_floor();
functionFactory.trunc_floor(); functionFactory.trunc_floor();
functionFactory.octetLength_pattern( "length(?1)" ); functionFactory.octetLength_pattern( "length(?1)", SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionFactory.bitLength_pattern( "length(?1)*8" ); functionFactory.bitLength_pattern( "length(?1)*8", SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER );
functionContributions.getFunctionRegistry().register( functionContributions.getFunctionRegistry().register(
"concat", "concat",

View File

@ -814,6 +814,11 @@ public class CommonFunctionFactory {
.register(); .register();
} }
/**
* Emulate left via substr and right via substr and length.
* This function is for Apache Derby and uses {@link SqlAstNodeRenderingMode#NO_PLAIN_PARAMETER}
* for the right function emulation, because length in Apache Derby can't handle plain parameters.
*/
public void leftRight_substrLength() { public void leftRight_substrLength() {
functionRegistry.patternDescriptorBuilder( "left", "substr(?1,1,?2)" ) functionRegistry.patternDescriptorBuilder( "left", "substr(?1,1,?2)" )
.setInvariantType(stringType) .setInvariantType(stringType)
@ -826,6 +831,7 @@ public class CommonFunctionFactory {
.setExactArgumentCount( 2 ) .setExactArgumentCount( 2 )
.setParameterTypes(STRING, INTEGER) .setParameterTypes(STRING, INTEGER)
.setArgumentListSignature( "(STRING string, INTEGER length)" ) .setArgumentListSignature( "(STRING string, INTEGER length)" )
.setArgumentRenderingMode( SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER )
.register(); .register();
} }
@ -1638,10 +1644,15 @@ public class CommonFunctionFactory {
} }
public void octetLength_pattern(String pattern) { public void octetLength_pattern(String pattern) {
octetLength_pattern( pattern, SqlAstNodeRenderingMode.DEFAULT );
}
public void octetLength_pattern(String pattern, SqlAstNodeRenderingMode renderingMode) {
functionRegistry.patternDescriptorBuilder( "octet_length", pattern ) functionRegistry.patternDescriptorBuilder( "octet_length", pattern )
.setInvariantType(integerType) .setInvariantType(integerType)
.setExactArgumentCount( 1 ) .setExactArgumentCount( 1 )
.setParameterTypes(STRING_OR_CLOB) .setParameterTypes(STRING_OR_CLOB)
.setArgumentRenderingMode( renderingMode )
.register(); .register();
} }
@ -1661,10 +1672,15 @@ public class CommonFunctionFactory {
} }
public void bitLength_pattern(String pattern) { public void bitLength_pattern(String pattern) {
bitLength_pattern( pattern, SqlAstNodeRenderingMode.DEFAULT );
}
public void bitLength_pattern(String pattern, SqlAstNodeRenderingMode renderingMode) {
functionRegistry.patternDescriptorBuilder( "bit_length", pattern ) functionRegistry.patternDescriptorBuilder( "bit_length", pattern )
.setInvariantType(integerType) .setInvariantType(integerType)
.setExactArgumentCount( 1 ) .setExactArgumentCount( 1 )
.setParameterTypes(STRING_OR_CLOB) .setParameterTypes(STRING_OR_CLOB)
.setArgumentRenderingMode( renderingMode )
.register(); .register();
} }

View File

@ -691,6 +691,8 @@ public class FunctionTests {
.list(); .list();
assertThat( session.createQuery("select left('hello world', 5)", String.class).getSingleResult(), is("hello") ); assertThat( session.createQuery("select left('hello world', 5)", String.class).getSingleResult(), is("hello") );
assertThat( session.createQuery("select right('hello world', 5)", String.class).getSingleResult(), is("world") ); assertThat( session.createQuery("select right('hello world', 5)", String.class).getSingleResult(), is("world") );
assertThat( session.createQuery("select right(:data, 5)", String.class).setParameter( "data", "hello world" ).getSingleResult(), is("world") );
} }
); );
} }