Skip to content

Commit f5800a0

Browse files
mbelladebeikov
authored andcommitted
HHH-17435 Small fixes to trim() function
1 parent 8250f13 commit f5800a0

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/AbstractTransactSQLDialect.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ public static String replaceLtrimRtrim(TrimSpec specification, boolean isWhitesp
183183
case LEADING:
184184
return isWhitespace
185185
? "ltrim(?1)"
186-
: "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)+1)";
186+
: "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)+1)";
187187
case TRAILING:
188188
return isWhitespace
189189
? "rtrim(?1)"
190-
: "substring(?1,1,len(?1)-patindex('%[^'+?2+']%',reverse(?1))+1)";
190+
: "substring(?1,1,len(?1+'x')-1-patindex('%[^'+?2+']%',reverse(?1))+1)";
191191
default:
192192
return isWhitespace
193193
? "ltrim(rtrim(?1))"
194-
: "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)";
194+
: "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)";
195195
}
196196
}
197197

hibernate-core/src/main/java/org/hibernate/dialect/function/TrimFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import org.hibernate.sql.ast.tree.expression.Expression;
2828
import org.hibernate.sql.ast.tree.expression.Literal;
2929
import org.hibernate.sql.ast.tree.expression.TrimSpecification;
30-
import org.hibernate.type.SqlTypes;
3130
import org.hibernate.type.StandardBasicTypes;
3231
import org.hibernate.type.descriptor.jdbc.JdbcType;
3332
import org.hibernate.type.spi.TypeConfiguration;
3433

3534
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING;
3635
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.TRIM_SPEC;
36+
import static org.hibernate.type.SqlTypes.isCharacterType;
3737

3838
/**
3939
* ANSI SQL-standard {@code trim()} function, which has a funny syntax
@@ -100,9 +100,9 @@ private static boolean isWhitespace(SqlAstNode trimCharacter) {
100100
final JdbcType jdbcType = ( (SqmParameterInterpretation) trimCharacter ).getExpressionType()
101101
.getSingleJdbcMapping()
102102
.getJdbcType();
103-
if ( jdbcType.getJdbcTypeCode() != SqlTypes.CHAR ) {
103+
if ( !isCharacterType( jdbcType.getJdbcTypeCode() ) ) {
104104
throw new FunctionArgumentException( String.format(
105-
"Expected parameter used as trim character to be Character typed, instead was [%s]",
105+
"Expected parameter used as trim character to be character typed, instead was [%s]",
106106
jdbcType.getFriendlyName()
107107
) );
108108
}

hibernate-core/src/test/java/org/hibernate/orm/test/dialect/function/AnsiTrimEmulationFunctionTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949
@ServiceRegistry
5050
public class AnsiTrimEmulationFunctionTest {
5151
private static final String trimSource = "a.column";
52-
private static final String LEADING = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)+1)";
53-
private static final String TRAILING = "substring(?1,1,len(?1)-patindex('%[^'+?2+']%',reverse(?1))+1)";
54-
private static final String BOTH = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1)-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)";
52+
private static final String LEADING = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)+1)";
53+
private static final String TRAILING = "substring(?1,1,len(?1+'x')-1-patindex('%[^'+?2+']%',reverse(?1))+1)";
54+
private static final String BOTH = "substring(?1,patindex('%[^'+?2+']%',?1),len(?1+'x')-1-patindex('%[^'+?2+']%',?1)-patindex('%[^'+?2+']%',reverse(?1))+2)";
5555

5656
@Test
57-
@RequiresDialect( SQLServerDialect.class )
57+
// @RequiresDialect( SQLServerDialect.class )
5858
public void testBasicSqlServerProcessing(ServiceRegistryScope scope) {
5959
Dialect dialect = new SQLServerDialect();
6060
TrimFunction function = new TrimFunction( dialect, new TypeConfiguration() );
@@ -78,7 +78,7 @@ public void testBasicSqlServerProcessing(ServiceRegistryScope scope) {
7878
}
7979

8080
@Test
81-
@RequiresDialect( SybaseDialect.class )
81+
// @RequiresDialect( SybaseDialect.class )
8282
public void testBasicSybaseProcessing(ServiceRegistryScope scope) {
8383
Dialect dialect = new SybaseDialect();
8484
TrimFunction function = new TrimFunction( dialect, new TypeConfiguration() );

0 commit comments

Comments
 (0)