Skip to content

Commit 0e5846b

Browse files
committed
HHH-18604 Add json_array_insert
1 parent 8dfc2a5 commit 0e5846b

File tree

20 files changed

+439
-1
lines changed

20 files changed

+439
-1
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,6 +1643,7 @@ it is necessary to enable the `hibernate.query.hql.json_functions_enabled` confi
16431643
| `json_remove()` | Removes a value by JSON path within a JSON document
16441644
| `json_mergepatch()` | Merges JSON documents by performing an https://tools.ietf.org/html/rfc7396[RFC 7396] compliant merge
16451645
| `json_array_append()` | Appends to a JSON array of a JSON document by JSON path
1646+
| `json_array_insert()` | Inserts a value by JSON path to a JSON array within a JSON document
16461647
|===
16471648
16481649
@@ -2134,6 +2135,29 @@ include::{json-example-dir-hql}/JsonArrayAppendTest.java[tags=hql-json-array-app
21342135
21352136
WARNING: SAP HANA, DB2, H2 and HSQLDB do not support this function.
21362137
2138+
[[hql-json-array-insert-function]]
2139+
===== `json_array_insert()`
2140+
2141+
Inserts a value by JSON path to a JSON array within a JSON document.
2142+
The function takes 3 arguments, the json document, the json path and the value to append.
2143+
2144+
Although the exact behavior is database dependent, usually an error will be triggered if
2145+
the JSON path does not end with an array index access i.e. `$.a[0]`.
2146+
The zero based array index represents the position at which an element should be inserted in an array.
2147+
2148+
If the JSON path without the index does not resolve to a JSON array within the JSON document,
2149+
the document is not changed.
2150+
2151+
[[hql-json-array-insert-example]]
2152+
====
2153+
[source, java, indent=0]
2154+
----
2155+
include::{json-example-dir-hql}/JsonArrayInsertTest.java[tags=hql-json-array-insert-example]
2156+
----
2157+
====
2158+
2159+
WARNING: SAP HANA, DB2, H2 and HSQLDB do not support this function.
2160+
21372161
[[hql-user-defined-functions]]
21382162
==== Native and user-defined functions
21392163

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
514514
functionFactory.jsonInsert_postgresql();
515515
functionFactory.jsonMergepatch_postgresql();
516516
functionFactory.jsonArrayAppend_postgresql();
517+
functionFactory.jsonArrayInsert_postgresql();
517518

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
666666
functionFactory.jsonInsert_mysql();
667667
functionFactory.jsonMergepatch_mysql();
668668
functionFactory.jsonArrayAppend_mysql();
669+
functionFactory.jsonArrayInsert_mysql();
669670
}
670671
}
671672

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
323323
functionFactory.jsonInsert_oracle();
324324
functionFactory.jsonMergepatch_oracle();
325325
functionFactory.jsonArrayAppend_oracle();
326+
functionFactory.jsonArrayInsert_oracle();
326327
}
327328
}
328329

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
664664
functionFactory.jsonInsert_postgresql();
665665
functionFactory.jsonMergepatch_postgresql();
666666
functionFactory.jsonArrayAppend_postgresql();
667+
functionFactory.jsonArrayInsert_postgresql();
667668

668669
if ( getVersion().isSameOrAfter( 9, 4 ) ) {
669670
functionFactory.makeDateTimeTimestamp();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
411411
functionFactory.jsonReplace_sqlserver();
412412
functionFactory.jsonInsert_sqlserver();
413413
functionFactory.jsonArrayAppend_sqlserver();
414+
functionFactory.jsonArrayInsert_sqlserver();
414415
}
415416
if ( getVersion().isSameOrAfter( 14 ) ) {
416417
functionFactory.listagg_stringAggWithinGroup( "varchar(max)" );

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
481481
functionFactory.jsonInsert_postgresql();
482482
functionFactory.jsonMergepatch_postgresql();
483483
functionFactory.jsonArrayAppend_postgresql();
484+
functionFactory.jsonArrayInsert_postgresql();
484485

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
651651
functionFactory.jsonInsert_mysql();
652652
functionFactory.jsonMergepatch_mysql();
653653
functionFactory.jsonArrayAppend_mysql();
654+
functionFactory.jsonArrayInsert_mysql();
654655
}
655656

656657
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
414414
functionFactory.jsonInsert_oracle();
415415
functionFactory.jsonMergepatch_oracle();
416416
functionFactory.jsonArrayAppend_oracle();
417+
functionFactory.jsonArrayInsert_oracle();
417418
}
418419

419420
@Override

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ public void initializeFunctionRegistry(FunctionContributions functionContributio
625625
functionFactory.jsonInsert_postgresql();
626626
functionFactory.jsonMergepatch_postgresql();
627627
functionFactory.jsonArrayAppend_postgresql();
628+
functionFactory.jsonArrayInsert_postgresql();
628629

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

0 commit comments

Comments
 (0)