HHH-18604 Fix some issues with older database versions

This commit is contained in:
Christian Beikov 2024-09-19 15:11:37 +02:00
parent ddadad2dac
commit 11d134ae38
7 changed files with 40 additions and 16 deletions

View File

@ -512,8 +512,9 @@ public class CockroachLegacyDialect extends Dialect {
functionFactory.jsonRemove_cockroachdb();
functionFactory.jsonReplace_postgresql();
functionFactory.jsonInsert_postgresql();
functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql();
// No support for WITH clause in subquery: https://github.com/cockroachdb/cockroach/issues/131011
// functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql( false );
functionFactory.jsonArrayInsert_postgresql();
// Postgres uses # instead of ^ for XOR

View File

@ -662,8 +662,11 @@ public class PostgreSQLLegacyDialect extends Dialect {
functionFactory.jsonRemove_postgresql();
functionFactory.jsonReplace_postgresql();
functionFactory.jsonInsert_postgresql();
functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql();
if ( getVersion().isSameOrAfter( 13 ) ) {
// Requires support for WITH clause in subquery which only 13+ provides
functionFactory.jsonMergepatch_postgresql();
}
functionFactory.jsonArrayAppend_postgresql( getVersion().isSameOrAfter( 13 ) );
functionFactory.jsonArrayInsert_postgresql();
if ( getVersion().isSameOrAfter( 9, 4 ) ) {

View File

@ -479,8 +479,9 @@ public class CockroachDialect extends Dialect {
functionFactory.jsonRemove_cockroachdb();
functionFactory.jsonReplace_postgresql();
functionFactory.jsonInsert_postgresql();
functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql();
// No support for WITH clause in subquery: https://github.com/cockroachdb/cockroach/issues/131011
// functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql( false );
functionFactory.jsonArrayInsert_postgresql();
// Postgres uses # instead of ^ for XOR

View File

@ -623,8 +623,11 @@ public class PostgreSQLDialect extends Dialect {
functionFactory.jsonRemove_postgresql();
functionFactory.jsonReplace_postgresql();
functionFactory.jsonInsert_postgresql();
functionFactory.jsonMergepatch_postgresql();
functionFactory.jsonArrayAppend_postgresql();
if ( getVersion().isSameOrAfter( 13 ) ) {
// Requires support for WITH clause in subquery which only 13+ provides
functionFactory.jsonMergepatch_postgresql();
}
functionFactory.jsonArrayAppend_postgresql( getVersion().isSameOrAfter( 13 ) );
functionFactory.jsonArrayInsert_postgresql();
functionFactory.makeDateTimeTimestamp();

View File

@ -4018,8 +4018,8 @@ public class CommonFunctionFactory {
/**
* PostgreSQL json_array_append() function
*/
public void jsonArrayAppend_postgresql() {
functionRegistry.register( "json_array_append", new PostgreSQLJsonArrayAppendFunction( typeConfiguration ) );
public void jsonArrayAppend_postgresql(boolean supportsLax) {
functionRegistry.register( "json_array_append", new PostgreSQLJsonArrayAppendFunction( supportsLax, typeConfiguration ) );
}
/**

View File

@ -21,8 +21,11 @@ import org.hibernate.type.spi.TypeConfiguration;
*/
public class PostgreSQLJsonArrayAppendFunction extends AbstractJsonArrayAppendFunction {
public PostgreSQLJsonArrayAppendFunction(TypeConfiguration typeConfiguration) {
private final boolean supportsLax;
public PostgreSQLJsonArrayAppendFunction(boolean supportsLax, TypeConfiguration typeConfiguration) {
super( typeConfiguration );
this.supportsLax = supportsLax;
}
@Override
@ -34,7 +37,14 @@ public class PostgreSQLJsonArrayAppendFunction extends AbstractJsonArrayAppendFu
final Expression json = (Expression) arguments.get( 0 );
final Expression jsonPath = (Expression) arguments.get( 1 );
final SqlAstNode value = arguments.get( 2 );
sqlAppender.appendSql( "(select jsonb_set_lax(t.d,t.p,(t.d)#>t.p||" );
sqlAppender.appendSql( "(select " );
if ( supportsLax ) {
sqlAppender.appendSql( "jsonb_set_lax" );
}
else {
sqlAppender.appendSql( "case when t.d#>t.p is not null then jsonb_set" );
}
sqlAppender.appendSql( "(t.d,t.p,(t.d)#>t.p||" );
if ( value instanceof Literal && ( (Literal) value ).getLiteralValue() == null ) {
sqlAppender.appendSql( "null::jsonb" );
}
@ -47,7 +57,14 @@ public class PostgreSQLJsonArrayAppendFunction extends AbstractJsonArrayAppendFu
}
sqlAppender.appendSql( ')' );
}
sqlAppender.appendSql( ",false,'return_target') from (values(" );
sqlAppender.appendSql( ",false" );
if ( supportsLax ) {
sqlAppender.appendSql( ",'return_target')" );
}
else {
sqlAppender.appendSql( ") else t.d end" );
}
sqlAppender.appendSql( " from (values(" );
final boolean needsCast = !isJsonType( json );
if ( needsCast ) {
sqlAppender.appendSql( "cast(" );

View File

@ -33,7 +33,6 @@ public class PostgreSQLJsonRemoveFunction extends AbstractJsonRemoveFunction {
SqlAstTranslator<?> translator) {
final Expression json = (Expression) arguments.get( 0 );
final Expression jsonPath = (Expression) arguments.get( 1 );
sqlAppender.appendSql( "jsonb_set_lax(" );
final boolean needsCast = !isJsonType( json ) && json instanceof JdbcParameter;
if ( needsCast ) {
sqlAppender.appendSql( "cast(" );
@ -42,7 +41,7 @@ public class PostgreSQLJsonRemoveFunction extends AbstractJsonRemoveFunction {
if ( needsCast ) {
sqlAppender.appendSql( " as jsonb)" );
}
sqlAppender.appendSql( ',' );
sqlAppender.appendSql( "#-" );
List<JsonPathHelper.JsonPathElement> jsonPathElements =
JsonPathHelper.parseJsonPathElements( translator.getLiteralValue( jsonPath ) );
sqlAppender.appendSql( "array" );
@ -63,7 +62,7 @@ public class PostgreSQLJsonRemoveFunction extends AbstractJsonRemoveFunction {
}
separator = ',';
}
sqlAppender.appendSql( "]::text[],null,true,'delete_key')" );
sqlAppender.appendSql( "]::text[]" );
}
private boolean isJsonType(Expression expression) {