forked from hibernate/hibernate-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
439 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...re/src/main/java/org/hibernate/dialect/function/json/AbstractJsonArrayInsertFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.dialect.function.json; | ||
|
||
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; | ||
import org.hibernate.query.sqm.function.FunctionKind; | ||
import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator; | ||
import org.hibernate.query.sqm.produce.function.FunctionParameterType; | ||
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators; | ||
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; | ||
import org.hibernate.type.SqlTypes; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
/** | ||
* Standard json_array_insert function. | ||
*/ | ||
public abstract class AbstractJsonArrayInsertFunction extends AbstractSqmSelfRenderingFunctionDescriptor { | ||
|
||
public AbstractJsonArrayInsertFunction(TypeConfiguration typeConfiguration) { | ||
super( | ||
"json_array_insert", | ||
FunctionKind.NORMAL, | ||
new ArgumentTypesValidator( | ||
StandardArgumentsValidators.exactly( 3 ), | ||
FunctionParameterType.IMPLICIT_JSON, | ||
FunctionParameterType.STRING, | ||
FunctionParameterType.ANY | ||
), | ||
StandardFunctionReturnTypeResolvers.invariant( | ||
typeConfiguration.getBasicTypeRegistry().resolve( String.class, SqlTypes.JSON ) | ||
), | ||
null | ||
); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...core/src/main/java/org/hibernate/dialect/function/json/OracleJsonArrayInsertFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.dialect.function.json; | ||
|
||
import java.util.List; | ||
|
||
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; | ||
|
||
/** | ||
* Oracle json_array_insert function. | ||
*/ | ||
public class OracleJsonArrayInsertFunction extends AbstractJsonArrayInsertFunction { | ||
|
||
public OracleJsonArrayInsertFunction(TypeConfiguration typeConfiguration) { | ||
super( typeConfiguration ); | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> arguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> translator) { | ||
final Expression json = (Expression) arguments.get( 0 ); | ||
final String jsonPath = translator.getLiteralValue( (Expression) arguments.get( 1 ) ); | ||
final SqlAstNode value = arguments.get( 2 ); | ||
sqlAppender.appendSql( "json_transform(" ); | ||
json.accept( translator ); | ||
sqlAppender.appendSql( ",insert " ); | ||
sqlAppender.appendSingleQuoteEscapedString( jsonPath ); | ||
sqlAppender.appendSql( '=' ); | ||
value.accept( translator ); | ||
sqlAppender.appendSql( " ignore on existing)" ); | ||
|
||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
.../src/main/java/org/hibernate/dialect/function/json/PostgreSQLJsonArrayInsertFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* SPDX-License-Identifier: LGPL-2.1-or-later | ||
* Copyright Red Hat Inc. and Hibernate Authors | ||
*/ | ||
package org.hibernate.dialect.function.json; | ||
|
||
import java.util.List; | ||
|
||
import org.hibernate.QueryException; | ||
import org.hibernate.metamodel.mapping.JdbcMappingContainer; | ||
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.sql.ast.tree.expression.Literal; | ||
import org.hibernate.type.spi.TypeConfiguration; | ||
|
||
/** | ||
* PostgreSQL json_array_insert function. | ||
*/ | ||
public class PostgreSQLJsonArrayInsertFunction extends AbstractJsonArrayInsertFunction { | ||
|
||
public PostgreSQLJsonArrayInsertFunction(TypeConfiguration typeConfiguration) { | ||
super( typeConfiguration ); | ||
} | ||
|
||
@Override | ||
public void render( | ||
SqlAppender sqlAppender, | ||
List<? extends SqlAstNode> arguments, | ||
ReturnableType<?> returnType, | ||
SqlAstTranslator<?> translator) { | ||
final Expression json = (Expression) arguments.get( 0 ); | ||
final Expression jsonPath = (Expression) arguments.get( 1 ); | ||
final SqlAstNode value = arguments.get( 2 ); | ||
sqlAppender.appendSql( "jsonb_insert(" ); | ||
final boolean needsCast = !isJsonType( json ); | ||
if ( needsCast ) { | ||
sqlAppender.appendSql( "cast(" ); | ||
} | ||
json.accept( translator ); | ||
if ( needsCast ) { | ||
sqlAppender.appendSql( " as jsonb)" ); | ||
} | ||
sqlAppender.appendSql( ',' ); | ||
List<JsonPathHelper.JsonPathElement> jsonPathElements = | ||
JsonPathHelper.parseJsonPathElements( translator.getLiteralValue( jsonPath ) ); | ||
sqlAppender.appendSql( "array" ); | ||
char separator = '['; | ||
for ( JsonPathHelper.JsonPathElement pathElement : jsonPathElements ) { | ||
sqlAppender.appendSql( separator ); | ||
if ( pathElement instanceof JsonPathHelper.JsonAttribute attribute ) { | ||
sqlAppender.appendSingleQuoteEscapedString( attribute.attribute() ); | ||
} | ||
else if ( pathElement instanceof JsonPathHelper.JsonParameterIndexAccess ) { | ||
final String parameterName = ( (JsonPathHelper.JsonParameterIndexAccess) pathElement ).parameterName(); | ||
throw new QueryException( "JSON path [" + jsonPath + "] uses parameter [" + parameterName + "] that is not passed" ); | ||
} | ||
else { | ||
sqlAppender.appendSql( '\'' ); | ||
sqlAppender.appendSql( ( (JsonPathHelper.JsonIndexAccess) pathElement ).index() ); | ||
sqlAppender.appendSql( '\'' ); | ||
} | ||
separator = ','; | ||
} | ||
sqlAppender.appendSql( "]::text[]," ); | ||
if ( value instanceof Literal && ( (Literal) value ).getLiteralValue() == null ) { | ||
sqlAppender.appendSql( "null::jsonb" ); | ||
} | ||
else { | ||
sqlAppender.appendSql( "to_jsonb(" ); | ||
value.accept( translator ); | ||
if ( value instanceof Literal literal && literal.getJdbcMapping().getJdbcType().isString() ) { | ||
// PostgreSQL until version 16 is not smart enough to infer the type of a string literal | ||
sqlAppender.appendSql( "::text" ); | ||
} | ||
sqlAppender.appendSql( ')' ); | ||
} | ||
sqlAppender.appendSql( ')' ); | ||
} | ||
|
||
private static boolean isJsonType(Expression expression) { | ||
final JdbcMappingContainer expressionType = expression.getExpressionType(); | ||
return expressionType != null && expressionType.getSingleJdbcMapping().getJdbcType().isJson(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.