HHH-18497 Add special xmlquery and xmlexists functions for DB2 10.5

This commit is contained in:
Christian Beikov 2024-09-23 14:55:24 +02:00
parent 3b07ed91c3
commit 450bca6265
5 changed files with 126 additions and 4 deletions

View File

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

View File

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

View File

@ -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
*/

View File

@ -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<? extends SqlAstNode> 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\")" );
}
}

View File

@ -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<? extends SqlAstNode> 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\")" );
}
}