HHH-17435 Small fixes to trim() function

This commit is contained in:
Marco Belladelli 2024-01-15 10:31:51 +01:00 committed by Christian Beikov
parent 8250f13d77
commit f5800a0388
3 changed files with 11 additions and 11 deletions

View File

@ -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)";
}
}

View File

@ -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()
) );
}

View File

@ -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() );