From f5800a0388b9bd1cc946fd20c6f7403f74e8c9e3 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Mon, 15 Jan 2024 10:31:51 +0100 Subject: [PATCH] HHH-17435 Small fixes to trim() function --- .../hibernate/dialect/AbstractTransactSQLDialect.java | 6 +++--- .../org/hibernate/dialect/function/TrimFunction.java | 6 +++--- .../function/AnsiTrimEmulationFunctionTest.java | 10 +++++----- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java index 2b6e5252b9..d2af7fde99 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java @@ -183,15 +183,15 @@ public abstract class AbstractTransactSQLDialect extends Dialect { case LEADING: return isWhitespace ? "ltrim(?1)" - : "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)+1)"; + : "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)+1)"; case TRAILING: return isWhitespace ? "rtrim(?1)" - : "substring(?1,1,len(?1)-patindex('%[^'+?2+']%',reverse(?1))+1)"; + : "substring(?1,1,len(?1+'x')-1-patindex('%[^'+?2+']%',reverse(?1))+1)"; default: return isWhitespace ? "ltrim(rtrim(?1))" - : "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)"; + : "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)"; } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/TrimFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/TrimFunction.java index 1f39f70b72..3ee36151ee 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/TrimFunction.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/TrimFunction.java @@ -27,13 +27,13 @@ import org.hibernate.sql.ast.tree.SqlAstNode; import org.hibernate.sql.ast.tree.expression.Expression; import org.hibernate.sql.ast.tree.expression.Literal; import org.hibernate.sql.ast.tree.expression.TrimSpecification; -import org.hibernate.type.SqlTypes; import org.hibernate.type.StandardBasicTypes; import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.spi.TypeConfiguration; import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING; import static org.hibernate.query.sqm.produce.function.FunctionParameterType.TRIM_SPEC; +import static org.hibernate.type.SqlTypes.isCharacterType; /** * ANSI SQL-standard {@code trim()} function, which has a funny syntax @@ -100,9 +100,9 @@ public class TrimFunction extends AbstractSqmSelfRenderingFunctionDescriptor { final JdbcType jdbcType = ( (SqmParameterInterpretation) trimCharacter ).getExpressionType() .getSingleJdbcMapping() .getJdbcType(); - if ( jdbcType.getJdbcTypeCode() != SqlTypes.CHAR ) { + if ( !isCharacterType( jdbcType.getJdbcTypeCode() ) ) { throw new FunctionArgumentException( String.format( - "Expected parameter used as trim character to be Character typed, instead was [%s]", + "Expected parameter used as trim character to be character typed, instead was [%s]", jdbcType.getFriendlyName() ) ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/function/AnsiTrimEmulationFunctionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/function/AnsiTrimEmulationFunctionTest.java index bb895b1de9..38bea21f74 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/function/AnsiTrimEmulationFunctionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/dialect/function/AnsiTrimEmulationFunctionTest.java @@ -49,12 +49,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; @ServiceRegistry public class AnsiTrimEmulationFunctionTest { private static final String trimSource = "a.column"; - private static final String LEADING = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)+1)"; - private static final String TRAILING = "substring(?1,1,len(?1)-patindex('%[^'+?2+']%',reverse(?1))+1)"; - private static final String BOTH = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)"; + private static final String LEADING = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)+1)"; + private static final String TRAILING = "substring(?1,1,len(?1+'x')-1-patindex('%[^'+?2+']%',reverse(?1))+1)"; + private static final String BOTH = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)"; @Test - @RequiresDialect( SQLServerDialect.class ) +// @RequiresDialect( SQLServerDialect.class ) public void testBasicSqlServerProcessing(ServiceRegistryScope scope) { Dialect dialect = new SQLServerDialect(); TrimFunction function = new TrimFunction( dialect, new TypeConfiguration() ); @@ -78,7 +78,7 @@ public class AnsiTrimEmulationFunctionTest { } @Test - @RequiresDialect( SybaseDialect.class ) +// @RequiresDialect( SybaseDialect.class ) public void testBasicSybaseProcessing(ServiceRegistryScope scope) { Dialect dialect = new SybaseDialect(); TrimFunction function = new TrimFunction( dialect, new TypeConfiguration() );