Skip to content

Commit 0f03fd6

Browse files
committed
HHH-3404 Add regexp_like support for HANA, Oracle and SQL Server
Also cleanup the tests and make case-insensitive regexp_like work with lower function if needed
1 parent 918ebc2 commit 0f03fd6

File tree

21 files changed

+401
-47
lines changed

21 files changed

+401
-47
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HANALegacyDialect.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
529529

530530
// functionFactory.xmlextract();
531531
}
532+
533+
functionFactory.regexpLike_like_regexp();
532534
}
533535

534536
/**

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/HSQLLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
292292
functionContributions.getTypeConfiguration(),
293293
SqlAstNodeRenderingMode.NO_PLAIN_PARAMETER
294294
) );
295+
functionFactory.regexpLike_hsql();
295296
}
296297

297298
/**

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/InformixDialect.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.hibernate.boot.model.FunctionContributions;
1717
import org.hibernate.boot.model.TypeContributions;
1818
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
19+
import org.hibernate.community.dialect.function.InformixRegexpLikeFunction;
1920
import org.hibernate.community.dialect.identity.InformixIdentityColumnSupport;
2021
import org.hibernate.community.dialect.pagination.FirstLimitHandler;
2122
import org.hibernate.community.dialect.pagination.SkipFirstLimitHandler;
@@ -411,11 +412,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
411412
functionContributions.getFunctionRegistry().register( "trim",
412413
new TrimFunction( this, typeConfiguration, SqlAstNodeRenderingMode.NO_UNTYPED ) );
413414

414-
//TODO: emulate support for the 'i' flag argument
415-
functionRegistry.namedDescriptorBuilder( "regexp_like", "regex_match" )
416-
.setParameterTypes( STRING, STRING )
417-
.setInvariantType( booleanBasicType )
418-
.register();
415+
functionRegistry.register( "regexp_like", new InformixRegexpLikeFunction( typeConfiguration ) );
419416
}
420417

421418
@Override

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/OracleLegacyDialect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
375375

376376
functionFactory.unnest_oracle();
377377
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), true, false );
378+
functionFactory.regexpLike_predicateFunction();
378379
}
379380

380381
/**

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SQLServerLegacyDialect.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,13 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
521521
functionFactory.generateSeries_recursive( getMaximumSeriesSize(), false, false );
522522
}
523523
}
524+
if ( getVersion().isBefore( 17 ) ) {
525+
// Before SQL Server 2025, this function wasn't supported
526+
functionContributions.getFunctionRegistry().removeFunctionDescriptor( "regexp_like" );
527+
}
528+
else {
529+
functionFactory.regexpLike_predicateFunction();
530+
}
524531
}
525532

526533
/**

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/SybaseASELegacyDialect.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
173173
functionFactory.unnest_sybasease();
174174
functionFactory.generateSeries_sybasease( getMaximumSeriesSize() );
175175
functionFactory.xmltable_sybasease();
176+
// Sybase ASE does not support this function
177+
functionContributions.getFunctionRegistry().removeFunctionDescriptor( "regexp_like" );
176178
}
177179

178180
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.community.dialect.function;
6+
7+
import org.hibernate.dialect.function.AbstractRegexpLikeFunction;
8+
import org.hibernate.metamodel.model.domain.ReturnableType;
9+
import org.hibernate.sql.ast.SqlAstTranslator;
10+
import org.hibernate.sql.ast.spi.SqlAppender;
11+
import org.hibernate.sql.ast.tree.SqlAstNode;
12+
import org.hibernate.sql.ast.tree.expression.Literal;
13+
import org.hibernate.type.spi.TypeConfiguration;
14+
15+
import java.util.List;
16+
17+
18+
/**
19+
* Informix has special integer constants as third argument.
20+
*/
21+
public class InformixRegexpLikeFunction extends AbstractRegexpLikeFunction {
22+
23+
public InformixRegexpLikeFunction(TypeConfiguration typeConfiguration) {
24+
super( typeConfiguration );
25+
}
26+
27+
@Override
28+
public void render(
29+
SqlAppender sqlAppender,
30+
List<? extends SqlAstNode> arguments,
31+
ReturnableType<?> returnType,
32+
SqlAstTranslator<?> walker) {
33+
final boolean caseSensitive;
34+
if ( arguments.size() > 2 ) {
35+
if ( !(arguments.get( 2 ) instanceof Literal literal)
36+
|| !(literal.getLiteralValue() instanceof String flags)
37+
|| !flags.equals( "i" ) ) {
38+
throw new IllegalArgumentException( "Informix only supports the case insensitive flag 'i' as literal but got." );
39+
}
40+
caseSensitive = false;
41+
}
42+
else {
43+
caseSensitive = true;
44+
}
45+
46+
sqlAppender.appendSql( "regex_match(" );
47+
arguments.get( 0 ).accept( walker );
48+
sqlAppender.appendSql( ',' );
49+
arguments.get( 1 ).accept( walker );
50+
if ( !caseSensitive ) {
51+
// 1 is extended POSIX regex which is the default, 3 is extended POSIX regex and case-insensitive
52+
// See https://www.ibm.com/docs/en/informix-servers/14.10.0?topic=routines-regex-match-function
53+
sqlAppender.appendSql( ",3" );
54+
}
55+
sqlAppender.appendSql( ')' );
56+
}
57+
58+
@Override
59+
public boolean isPredicate() {
60+
return false;
61+
}
62+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
533533
functionFactory.hex( "bintohex(?1)" );
534534
functionFactory.sha( "hash_sha256(to_binary(?1))" );
535535
functionFactory.md5( "hash_md5(to_binary(?1))" );
536+
functionFactory.regexpLike_like_regexp();
536537
}
537538

538539
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
239239
) );
240240

241241
functionFactory.hex( "hex(?1)" );
242+
functionFactory.regexpLike_hsql();
242243
}
243244

244245
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
434434
"extract",
435435
new OracleExtractFunction( this, typeConfiguration )
436436
);
437+
functionFactory.regexpLike_predicateFunction();
437438
}
438439

439440
/**

0 commit comments

Comments
 (0)