Skip to content

Commit 051bc78

Browse files
committed
HHH-18604 Add json_insert and json_replace function
1 parent 36066a0 commit 051bc78

30 files changed

+1033
-3
lines changed

documentation/src/main/asciidoc/userguide/chapters/query/hql/QueryLanguage.adoc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,42 @@ include::{json-example-dir-hql}/JsonRemoveTest.java[tags=hql-json-remove-example
20472047
20482048
WARNING: SAP HANA, DB2, H2 and HSQLDB do not support this function.
20492049
2050+
[[hql-json-replace-function]]
2051+
===== `json_replace()`
2052+
2053+
Replaces a value by JSON path within a JSON document.
2054+
The function takes 3 arguments, the json document, the json path and the new value to set.
2055+
A value will not be inserted if the key is missing, only the values of existing keys are replaced.
2056+
2057+
[[hql-json-replace-example]]
2058+
====
2059+
[source, java, indent=0]
2060+
----
2061+
include::{json-example-dir-hql}/JsonReplaceTest.java[tags=hql-json-replace-example]
2062+
----
2063+
====
2064+
2065+
WARNING: SAP HANA, DB2, H2 and HSQLDB do not support this function.
2066+
2067+
[[hql-json-insert-function]]
2068+
===== `json_insert()`
2069+
2070+
Inserts a value by JSON path in a JSON document.
2071+
The function takes 3 arguments, the json document, the json path and the value to insert.
2072+
When the JSON document contains a value for a JSON path, no insertion happens,
2073+
unless the value is an array, in which case the value will be appended to that array.
2074+
If no value exists for a JSON path, the value will be inserted under the key as specified through the JSON path.
2075+
2076+
[[hql-json-insert-example]]
2077+
====
2078+
[source, java, indent=0]
2079+
----
2080+
include::{json-example-dir-hql}/JsonInsertTest.java[tags=hql-json-insert-example]
2081+
----
2082+
====
2083+
2084+
WARNING: SAP HANA, DB2, H2 and HSQLDB do not support this function.
2085+
20502086
[[hql-user-defined-functions]]
20512087
==== Native and user-defined functions
20522088

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
509509
functionFactory.jsonArrayAgg_postgresql( false );
510510
functionFactory.jsonObjectAgg_postgresql( false );
511511
functionFactory.jsonSet_postgresql();
512-
functionFactory.jsonRemove_postgresql();
512+
functionFactory.jsonRemove_cockroachdb();
513+
functionFactory.jsonReplace_postgresql();
514+
functionFactory.jsonInsert_postgresql();
513515

514516
// Postgres uses # instead of ^ for XOR
515517
functionContributions.getFunctionRegistry().patternDescriptorBuilder( "bitxor", "(?1#?2)" )

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
662662
functionFactory.jsonObjectAgg_mysql();
663663
functionFactory.jsonSet_mysql();
664664
functionFactory.jsonRemove_mysql();
665+
functionFactory.jsonReplace_mysql();
666+
functionFactory.jsonInsert_mysql();
665667
}
666668
}
667669

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
319319
functionFactory.jsonObjectAgg_oracle();
320320
functionFactory.jsonSet_oracle();
321321
functionFactory.jsonRemove_oracle();
322+
functionFactory.jsonReplace_oracle();
323+
functionFactory.jsonInsert_oracle();
322324
}
323325
}
324326

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
660660
}
661661
functionFactory.jsonSet_postgresql();
662662
functionFactory.jsonRemove_postgresql();
663+
functionFactory.jsonReplace_postgresql();
664+
functionFactory.jsonInsert_postgresql();
663665

664666
if ( getVersion().isSameOrAfter( 9, 4 ) ) {
665667
functionFactory.makeDateTimeTimestamp();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
408408
functionFactory.jsonArray_sqlserver();
409409
functionFactory.jsonSet_sqlserver();
410410
functionFactory.jsonRemove_sqlserver();
411+
functionFactory.jsonReplace_sqlserver();
412+
functionFactory.jsonInsert_sqlserver();
411413
}
412414
if ( getVersion().isSameOrAfter( 14 ) ) {
413415
functionFactory.listagg_stringAggWithinGroup( "varchar(max)" );

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,9 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
476476
functionFactory.jsonArrayAgg_postgresql( false );
477477
functionFactory.jsonObjectAgg_postgresql( false );
478478
functionFactory.jsonSet_postgresql();
479-
functionFactory.jsonRemove_postgresql();
479+
functionFactory.jsonRemove_cockroachdb();
480+
functionFactory.jsonReplace_postgresql();
481+
functionFactory.jsonInsert_postgresql();
480482

481483
// Postgres uses # instead of ^ for XOR
482484
functionContributions.getFunctionRegistry().patternDescriptorBuilder( "bitxor", "(?1#?2)" )

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
647647
functionFactory.jsonObjectAgg_mysql();
648648
functionFactory.jsonSet_mysql();
649649
functionFactory.jsonRemove_mysql();
650+
functionFactory.jsonReplace_mysql();
651+
functionFactory.jsonInsert_mysql();
650652
}
651653

652654
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
410410
functionFactory.jsonObjectAgg_oracle();
411411
functionFactory.jsonSet_oracle();
412412
functionFactory.jsonRemove_oracle();
413+
functionFactory.jsonReplace_oracle();
414+
functionFactory.jsonInsert_oracle();
413415
}
414416

415417
@Override

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,8 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
621621
}
622622
functionFactory.jsonSet_postgresql();
623623
functionFactory.jsonRemove_postgresql();
624+
functionFactory.jsonReplace_postgresql();
625+
functionFactory.jsonInsert_postgresql();
624626

625627
functionFactory.makeDateTimeTimestamp();
626628
// Note that PostgreSQL doesn't support the OVER clause for ordered set-aggregate functions

0 commit comments

Comments
 (0)