Skip to content

Commit 11d134a

Browse files
committed
HHH-18604 Fix some issues with older database versions
1 parent ddadad2 commit 11d134a

File tree

7 files changed

+40
-16
lines changed

7 files changed

+40
-16
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,9 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
512512
functionFactory.jsonRemove_cockroachdb();
513513
functionFactory.jsonReplace_postgresql();
514514
functionFactory.jsonInsert_postgresql();
515-
functionFactory.jsonMergepatch_postgresql();
516-
functionFactory.jsonArrayAppend_postgresql();
515+
// No support for WITH clause in subquery: https://github.com/cockroachdb/cockroach/issues/131011
516+
// functionFactory.jsonMergepatch_postgresql();
517+
functionFactory.jsonArrayAppend_postgresql( false );
517518
functionFactory.jsonArrayInsert_postgresql();
518519

519520
// Postgres uses # instead of ^ for XOR

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,11 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
662662
functionFactory.jsonRemove_postgresql();
663663
functionFactory.jsonReplace_postgresql();
664664
functionFactory.jsonInsert_postgresql();
665-
functionFactory.jsonMergepatch_postgresql();
666-
functionFactory.jsonArrayAppend_postgresql();
665+
if ( getVersion().isSameOrAfter( 13 ) ) {
666+
// Requires support for WITH clause in subquery which only 13+ provides
667+
functionFactory.jsonMergepatch_postgresql();
668+
}
669+
functionFactory.jsonArrayAppend_postgresql( getVersion().isSameOrAfter( 13 ) );
667670
functionFactory.jsonArrayInsert_postgresql();
668671

669672
if ( getVersion().isSameOrAfter( 9, 4 ) ) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,9 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
479479
functionFactory.jsonRemove_cockroachdb();
480480
functionFactory.jsonReplace_postgresql();
481481
functionFactory.jsonInsert_postgresql();
482-
functionFactory.jsonMergepatch_postgresql();
483-
functionFactory.jsonArrayAppend_postgresql();
482+
// No support for WITH clause in subquery: https://github.com/cockroachdb/cockroach/issues/131011
483+
// functionFactory.jsonMergepatch_postgresql();
484+
functionFactory.jsonArrayAppend_postgresql( false );
484485
functionFactory.jsonArrayInsert_postgresql();
485486

486487
// Postgres uses # instead of ^ for XOR

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,11 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
623623
functionFactory.jsonRemove_postgresql();
624624
functionFactory.jsonReplace_postgresql();
625625
functionFactory.jsonInsert_postgresql();
626-
functionFactory.jsonMergepatch_postgresql();
627-
functionFactory.jsonArrayAppend_postgresql();
626+
if ( getVersion().isSameOrAfter( 13 ) ) {
627+
// Requires support for WITH clause in subquery which only 13+ provides
628+
functionFactory.jsonMergepatch_postgresql();
629+
}
630+
functionFactory.jsonArrayAppend_postgresql( getVersion().isSameOrAfter( 13 ) );
628631
functionFactory.jsonArrayInsert_postgresql();
629632

630633
functionFactory.makeDateTimeTimestamp();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,8 +4018,8 @@ public void jsonMergepatch_oracle() {
40184018
/**
40194019
* PostgreSQL json_array_append() function
40204020
*/
4021-
public void jsonArrayAppend_postgresql() {
4022-
functionRegistry.register( "json_array_append", new PostgreSQLJsonArrayAppendFunction( typeConfiguration ) );
4021+
public void jsonArrayAppend_postgresql(boolean supportsLax) {
4022+
functionRegistry.register( "json_array_append", new PostgreSQLJsonArrayAppendFunction( supportsLax, typeConfiguration ) );
40234023
}
40244024

40254025
/**

hibernate-core/src/main/java/org/hibernate/dialect/function/json/PostgreSQLJsonArrayAppendFunction.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
*/
2222
public class PostgreSQLJsonArrayAppendFunction extends AbstractJsonArrayAppendFunction {
2323

24-
public PostgreSQLJsonArrayAppendFunction(TypeConfiguration typeConfiguration) {
24+
private final boolean supportsLax;
25+
26+
public PostgreSQLJsonArrayAppendFunction(boolean supportsLax, TypeConfiguration typeConfiguration) {
2527
super( typeConfiguration );
28+
this.supportsLax = supportsLax;
2629
}
2730

2831
@Override
@@ -34,7 +37,14 @@ public void render(
3437
final Expression json = (Expression) arguments.get( 0 );
3538
final Expression jsonPath = (Expression) arguments.get( 1 );
3639
final SqlAstNode value = arguments.get( 2 );
37-
sqlAppender.appendSql( "(select jsonb_set_lax(t.d,t.p,(t.d)#>t.p||" );
40+
sqlAppender.appendSql( "(select " );
41+
if ( supportsLax ) {
42+
sqlAppender.appendSql( "jsonb_set_lax" );
43+
}
44+
else {
45+
sqlAppender.appendSql( "case when t.d#>t.p is not null then jsonb_set" );
46+
}
47+
sqlAppender.appendSql( "(t.d,t.p,(t.d)#>t.p||" );
3848
if ( value instanceof Literal && ( (Literal) value ).getLiteralValue() == null ) {
3949
sqlAppender.appendSql( "null::jsonb" );
4050
}
@@ -47,7 +57,14 @@ public void render(
4757
}
4858
sqlAppender.appendSql( ')' );
4959
}
50-
sqlAppender.appendSql( ",false,'return_target') from (values(" );
60+
sqlAppender.appendSql( ",false" );
61+
if ( supportsLax ) {
62+
sqlAppender.appendSql( ",'return_target')" );
63+
}
64+
else {
65+
sqlAppender.appendSql( ") else t.d end" );
66+
}
67+
sqlAppender.appendSql( " from (values(" );
5168
final boolean needsCast = !isJsonType( json );
5269
if ( needsCast ) {
5370
sqlAppender.appendSql( "cast(" );

hibernate-core/src/main/java/org/hibernate/dialect/function/json/PostgreSQLJsonRemoveFunction.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public void render(
3333
SqlAstTranslator<?> translator) {
3434
final Expression json = (Expression) arguments.get( 0 );
3535
final Expression jsonPath = (Expression) arguments.get( 1 );
36-
sqlAppender.appendSql( "jsonb_set_lax(" );
3736
final boolean needsCast = !isJsonType( json ) && json instanceof JdbcParameter;
3837
if ( needsCast ) {
3938
sqlAppender.appendSql( "cast(" );
@@ -42,7 +41,7 @@ public void render(
4241
if ( needsCast ) {
4342
sqlAppender.appendSql( " as jsonb)" );
4443
}
45-
sqlAppender.appendSql( ',' );
44+
sqlAppender.appendSql( "#-" );
4645
List<JsonPathHelper.JsonPathElement> jsonPathElements =
4746
JsonPathHelper.parseJsonPathElements( translator.getLiteralValue( jsonPath ) );
4847
sqlAppender.appendSql( "array" );
@@ -63,7 +62,7 @@ else if ( pathElement instanceof JsonPathHelper.JsonParameterIndexAccess ) {
6362
}
6463
separator = ',';
6564
}
66-
sqlAppender.appendSql( "]::text[],null,true,'delete_key')" );
65+
sqlAppender.appendSql( "]::text[]" );
6766
}
6867

6968
private boolean isJsonType(Expression expression) {

0 commit comments

Comments
 (0)