diff --git a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java index d2b1d7655d..7fd2861e7d 100644 --- a/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java +++ b/hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/DB2LegacyDialect.java @@ -446,8 +446,14 @@ public class DB2LegacyDialect extends Dialect { functionFactory.xmlforest(); functionFactory.xmlconcat(); functionFactory.xmlpi(); - functionFactory.xmlquery_db2(); - functionFactory.xmlexists(); + if ( getDB2Version().isSameOrAfter( 11 ) ) { + functionFactory.xmlquery_db2(); + functionFactory.xmlexists(); + } + else { + functionFactory.xmlquery_db2_legacy(); + functionFactory.xmlexists_db2_legacy(); + } functionFactory.xmlagg(); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java index f749dee6c7..1cab2faf38 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java @@ -431,8 +431,14 @@ public class DB2Dialect extends Dialect { functionFactory.xmlforest(); functionFactory.xmlconcat(); functionFactory.xmlpi(); - functionFactory.xmlquery_db2(); - functionFactory.xmlexists(); + if ( getDB2Version().isSameOrAfter( 11 ) ) { + functionFactory.xmlquery_db2(); + functionFactory.xmlexists(); + } + else { + functionFactory.xmlquery_db2_legacy(); + functionFactory.xmlexists_db2_legacy(); + } functionFactory.xmlagg(); } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java index 9a92da77cc..11752fc442 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/CommonFunctionFactory.java @@ -157,6 +157,8 @@ import org.hibernate.dialect.function.xml.H2XmlConcatFunction; import org.hibernate.dialect.function.xml.H2XmlElementFunction; import org.hibernate.dialect.function.xml.H2XmlForestFunction; import org.hibernate.dialect.function.xml.H2XmlPiFunction; +import org.hibernate.dialect.function.xml.LegacyDB2XmlExistsFunction; +import org.hibernate.dialect.function.xml.LegacyDB2XmlQueryFunction; import org.hibernate.dialect.function.xml.PostgreSQLXmlQueryFunction; import org.hibernate.dialect.function.xml.SQLServerXmlAggFunction; import org.hibernate.dialect.function.xml.SQLServerXmlConcatFunction; @@ -4237,6 +4239,13 @@ public class CommonFunctionFactory { functionRegistry.register( "xmlquery", new XmlQueryFunction( false, typeConfiguration ) ); } + /** + * DB2 10.5 xmlquery() function + */ + public void xmlquery_db2_legacy() { + functionRegistry.register( "xmlquery", new LegacyDB2XmlQueryFunction( typeConfiguration ) ); + } + /** * PostgreSQL xmlquery() function */ @@ -4265,6 +4274,13 @@ public class CommonFunctionFactory { functionRegistry.register( "xmlexists", new SQLServerXmlExistsFunction( typeConfiguration ) ); } + /** + * DB2 10.5 xmlexists() function + */ + public void xmlexists_db2_legacy() { + functionRegistry.register( "xmlexists", new LegacyDB2XmlExistsFunction( typeConfiguration ) ); + } + /** * Standard xmlagg() function */ diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlExistsFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlExistsFunction.java new file mode 100644 index 0000000000..8210a4cd9f --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlExistsFunction.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.dialect.function.xml; + +import java.util.List; + +import org.hibernate.dialect.function.json.ExpressionTypeHelper; +import org.hibernate.query.ReturnableType; +import org.hibernate.sql.ast.SqlAstTranslator; +import org.hibernate.sql.ast.spi.SqlAppender; +import org.hibernate.sql.ast.tree.SqlAstNode; +import org.hibernate.sql.ast.tree.expression.Expression; +import org.hibernate.type.spi.TypeConfiguration; + +/** + * DB2 10.5 xmlexists function. + */ +public class LegacyDB2XmlExistsFunction extends XmlExistsFunction { + + public LegacyDB2XmlExistsFunction(TypeConfiguration typeConfiguration) { + super( typeConfiguration ); + } + + @Override + public void render( + SqlAppender sqlAppender, + List sqlAstArguments, + ReturnableType returnType, + SqlAstTranslator walker) { + final String xquery = walker.getLiteralValue( (Expression) sqlAstArguments.get( 0 ) ); + final Expression xmlDocument = (Expression) sqlAstArguments.get( 1 ); + final boolean needsCast = !ExpressionTypeHelper.isXml( xmlDocument ); + sqlAppender.appendSql( "xmlexists(" ); + sqlAppender.appendSingleQuoteEscapedString( "$d" + xquery ); + sqlAppender.appendSql( " passing " ); + if ( needsCast ) { + sqlAppender.appendSql( "xmlparse(document " ); + } + sqlAstArguments.get( 1 ).accept( walker ); + if ( needsCast ) { + sqlAppender.appendSql( ')' ); + } + sqlAppender.appendSql( " as \"d\")" ); + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlQueryFunction.java b/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlQueryFunction.java new file mode 100644 index 0000000000..b3931a5da9 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/dialect/function/xml/LegacyDB2XmlQueryFunction.java @@ -0,0 +1,47 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * Copyright Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.dialect.function.xml; + +import java.util.List; + +import org.hibernate.dialect.function.json.ExpressionTypeHelper; +import org.hibernate.query.ReturnableType; +import org.hibernate.sql.ast.SqlAstTranslator; +import org.hibernate.sql.ast.spi.SqlAppender; +import org.hibernate.sql.ast.tree.SqlAstNode; +import org.hibernate.sql.ast.tree.expression.Expression; +import org.hibernate.type.spi.TypeConfiguration; + +/** + * DB2 10.5 xmlquery function. + */ +public class LegacyDB2XmlQueryFunction extends XmlQueryFunction { + + public LegacyDB2XmlQueryFunction(TypeConfiguration typeConfiguration) { + super( false, typeConfiguration ); + } + + @Override + public void render( + SqlAppender sqlAppender, + List sqlAstArguments, + ReturnableType returnType, + SqlAstTranslator walker) { + final String xquery = walker.getLiteralValue( (Expression) sqlAstArguments.get( 0 ) ); + final Expression xmlDocument = (Expression) sqlAstArguments.get( 1 ); + final boolean needsCast = !ExpressionTypeHelper.isXml( xmlDocument ); + sqlAppender.appendSql( "xmlquery(" ); + sqlAppender.appendSingleQuoteEscapedString( "$d" + xquery ); + sqlAppender.appendSql( " passing " ); + if ( needsCast ) { + sqlAppender.appendSql( "xmlparse(document " ); + } + sqlAstArguments.get( 1 ).accept( walker ); + if ( needsCast ) { + sqlAppender.appendSql( ')' ); + } + sqlAppender.appendSql( " as \"d\")" ); + } +}