Skip to content

Commit 450bca6

Browse files
committed
HHH-18497 Add special xmlquery and xmlexists functions for DB2 10.5
1 parent 3b07ed9 commit 450bca6

File tree

5 files changed

+126
-4
lines changed

5 files changed

+126
-4
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,8 +446,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
446446
functionFactory.xmlforest();
447447
functionFactory.xmlconcat();
448448
functionFactory.xmlpi();
449-
functionFactory.xmlquery_db2();
450-
functionFactory.xmlexists();
449+
if ( getDB2Version().isSameOrAfter( 11 ) ) {
450+
functionFactory.xmlquery_db2();
451+
functionFactory.xmlexists();
452+
}
453+
else {
454+
functionFactory.xmlquery_db2_legacy();
455+
functionFactory.xmlexists_db2_legacy();
456+
}
451457
functionFactory.xmlagg();
452458
}
453459

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,14 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
431431
functionFactory.xmlforest();
432432
functionFactory.xmlconcat();
433433
functionFactory.xmlpi();
434-
functionFactory.xmlquery_db2();
435-
functionFactory.xmlexists();
434+
if ( getDB2Version().isSameOrAfter( 11 ) ) {
435+
functionFactory.xmlquery_db2();
436+
functionFactory.xmlexists();
437+
}
438+
else {
439+
functionFactory.xmlquery_db2_legacy();
440+
functionFactory.xmlexists_db2_legacy();
441+
}
436442
functionFactory.xmlagg();
437443
}
438444

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@
157157
import org.hibernate.dialect.function.xml.H2XmlElementFunction;
158158
import org.hibernate.dialect.function.xml.H2XmlForestFunction;
159159
import org.hibernate.dialect.function.xml.H2XmlPiFunction;
160+
import org.hibernate.dialect.function.xml.LegacyDB2XmlExistsFunction;
161+
import org.hibernate.dialect.function.xml.LegacyDB2XmlQueryFunction;
160162
import org.hibernate.dialect.function.xml.PostgreSQLXmlQueryFunction;
161163
import org.hibernate.dialect.function.xml.SQLServerXmlAggFunction;
162164
import org.hibernate.dialect.function.xml.SQLServerXmlConcatFunction;
@@ -4237,6 +4239,13 @@ public void xmlquery_db2() {
42374239
functionRegistry.register( "xmlquery", new XmlQueryFunction( false, typeConfiguration ) );
42384240
}
42394241

4242+
/**
4243+
* DB2 10.5 xmlquery() function
4244+
*/
4245+
public void xmlquery_db2_legacy() {
4246+
functionRegistry.register( "xmlquery", new LegacyDB2XmlQueryFunction( typeConfiguration ) );
4247+
}
4248+
42404249
/**
42414250
* PostgreSQL xmlquery() function
42424251
*/
@@ -4265,6 +4274,13 @@ public void xmlexists_sqlserver() {
42654274
functionRegistry.register( "xmlexists", new SQLServerXmlExistsFunction( typeConfiguration ) );
42664275
}
42674276

4277+
/**
4278+
* DB2 10.5 xmlexists() function
4279+
*/
4280+
public void xmlexists_db2_legacy() {
4281+
functionRegistry.register( "xmlexists", new LegacyDB2XmlExistsFunction( typeConfiguration ) );
4282+
}
4283+
42684284
/**
42694285
* Standard xmlagg() function
42704286
*/
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.dialect.function.xml;
6+
7+
import java.util.List;
8+
9+
import org.hibernate.dialect.function.json.ExpressionTypeHelper;
10+
import org.hibernate.query.ReturnableType;
11+
import org.hibernate.sql.ast.SqlAstTranslator;
12+
import org.hibernate.sql.ast.spi.SqlAppender;
13+
import org.hibernate.sql.ast.tree.SqlAstNode;
14+
import org.hibernate.sql.ast.tree.expression.Expression;
15+
import org.hibernate.type.spi.TypeConfiguration;
16+
17+
/**
18+
* DB2 10.5 xmlexists function.
19+
*/
20+
public class LegacyDB2XmlExistsFunction extends XmlExistsFunction {
21+
22+
public LegacyDB2XmlExistsFunction(TypeConfiguration typeConfiguration) {
23+
super( typeConfiguration );
24+
}
25+
26+
@Override
27+
public void render(
28+
SqlAppender sqlAppender,
29+
List<? extends SqlAstNode> sqlAstArguments,
30+
ReturnableType<?> returnType,
31+
SqlAstTranslator<?> walker) {
32+
final String xquery = walker.getLiteralValue( (Expression) sqlAstArguments.get( 0 ) );
33+
final Expression xmlDocument = (Expression) sqlAstArguments.get( 1 );
34+
final boolean needsCast = !ExpressionTypeHelper.isXml( xmlDocument );
35+
sqlAppender.appendSql( "xmlexists(" );
36+
sqlAppender.appendSingleQuoteEscapedString( "$d" + xquery );
37+
sqlAppender.appendSql( " passing " );
38+
if ( needsCast ) {
39+
sqlAppender.appendSql( "xmlparse(document " );
40+
}
41+
sqlAstArguments.get( 1 ).accept( walker );
42+
if ( needsCast ) {
43+
sqlAppender.appendSql( ')' );
44+
}
45+
sqlAppender.appendSql( " as \"d\")" );
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.dialect.function.xml;
6+
7+
import java.util.List;
8+
9+
import org.hibernate.dialect.function.json.ExpressionTypeHelper;
10+
import org.hibernate.query.ReturnableType;
11+
import org.hibernate.sql.ast.SqlAstTranslator;
12+
import org.hibernate.sql.ast.spi.SqlAppender;
13+
import org.hibernate.sql.ast.tree.SqlAstNode;
14+
import org.hibernate.sql.ast.tree.expression.Expression;
15+
import org.hibernate.type.spi.TypeConfiguration;
16+
17+
/**
18+
* DB2 10.5 xmlquery function.
19+
*/
20+
public class LegacyDB2XmlQueryFunction extends XmlQueryFunction {
21+
22+
public LegacyDB2XmlQueryFunction(TypeConfiguration typeConfiguration) {
23+
super( false, typeConfiguration );
24+
}
25+
26+
@Override
27+
public void render(
28+
SqlAppender sqlAppender,
29+
List<? extends SqlAstNode> sqlAstArguments,
30+
ReturnableType<?> returnType,
31+
SqlAstTranslator<?> walker) {
32+
final String xquery = walker.getLiteralValue( (Expression) sqlAstArguments.get( 0 ) );
33+
final Expression xmlDocument = (Expression) sqlAstArguments.get( 1 );
34+
final boolean needsCast = !ExpressionTypeHelper.isXml( xmlDocument );
35+
sqlAppender.appendSql( "xmlquery(" );
36+
sqlAppender.appendSingleQuoteEscapedString( "$d" + xquery );
37+
sqlAppender.appendSql( " passing " );
38+
if ( needsCast ) {
39+
sqlAppender.appendSql( "xmlparse(document " );
40+
}
41+
sqlAstArguments.get( 1 ).accept( walker );
42+
if ( needsCast ) {
43+
sqlAppender.appendSql( ')' );
44+
}
45+
sqlAppender.appendSql( " as \"d\")" );
46+
}
47+
}

0 commit comments

Comments
 (0)