Skip to content

Commit

Permalink
Merge pull request #118 from starnowski/feature/116_merge_to_master
Browse files Browse the repository at this point in the history
#116 merge to master
  • Loading branch information
starnowski committed Mar 12, 2024
2 parents 258dc09 + 5712ee6 commit 4d4bd21
Show file tree
Hide file tree
Showing 16 changed files with 457 additions and 32 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ https://www.markdownguide.org/basic-syntax/


* [Unreleased](#unreleased)
* [0.3.1](#031---2024-03-11)
* [0.3.0](#030---2024-01-03)
* [0.2.1](#021---2023-06-20)
* [0.2.0](#020---2023-06-13)
Expand All @@ -19,6 +20,18 @@ https://www.markdownguide.org/basic-syntax/

## [Unreleased]

## [0.3.1] - 2024-03-11

### Added

- Added a set of functions that define which function should be rendered with a schema reference. [116](https://github.com/starnowski/posjsonhelper/issues/116)
- Added com.github.starnowski.posjsonhelper.core.Context#functionsThatShouldBeExecutedWithSchemaReference property [116](https://github.com/starnowski/posjsonhelper/issues/116)
- Added com.github.starnowski.posjsonhelper.hibernate6.descriptor.NamedSqmFunctionWithSchemaReferenceDescriptor type [116](https://github.com/starnowski/posjsonhelper/issues/116)

### Changed

- Changed base type for com.github.starnowski.posjsonhelper.hibernate6.descriptor.AbstractJsonbArrayStringsExistPredicateDescriptor to com.github.starnowski.posjsonhelper.hibernate6.descriptor.NamedSqmFunctionWithSchemaReferenceDescriptor [116](https://github.com/starnowski/posjsonhelper/issues/116)

## [0.3.0] - 2024-01-03

### Added
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ You can use it just by adding it as a dependency in the project descriptor file
<dependency>
<groupId>com.github.starnowski.posjsonhelper</groupId>
<artifactId>hibernate5</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
```

Expand All @@ -48,7 +48,7 @@ You can use it just by adding it as a dependency in the project descriptor file
<dependency>
<groupId>com.github.starnowski.posjsonhelper</groupId>
<artifactId>hibernate6</artifactId>
<version>0.3.0</version>
<version>0.3.1</version>
</dependency>
```

Expand Down Expand Up @@ -530,6 +530,15 @@ For more details and examples with the IN operator or how to use numeric values
|com.github.starnowski.posjsonhelper.hibernate6.functiondescriptorregisterfactory.types | (Used only in Hibernate 6) System property that stores list of com.github.starnowski.posjsonhelper.hibernate6.descriptor.FunctionDescriptorRegisterFactoriesSupplier types that should be loaded. Instead of loading types that can be found on the classpath for package "com.github.starnowski.posjsonhelper". Types on the list are separated by comma character ".". |
|com.github.starnowski.posjsonhelper.hibernate6.functiondescriptorregisterfactory.types.excluded | (Used only in Hibernate 6) System property that stores list of com.github.starnowski.posjsonhelper.hibernate6.descriptor.FunctionDescriptorRegisterFactoriesSupplier types that should be excluded from loading. If "com.github.starnowski.posjsonhelper.hibernate6.functiondescriptorregisterfactory.types" property is also specified then "com.github.starnowski.posjsonhelper.hibernate6.functiondescriptorregisterfactory.types.excluded" has higher priority. Types on the list are separated by comma character ".". |

### Hibernate 6 version compatibility

Compatibility matrix with Hibernate 6.

| Posjsonhelper | Hibernate 6 |
|---------------|-------------|
| 0.3.0 | 6.4.0.Final |
| 0.2.0 - 0.2.1 | 6.1.5.Final |

# Reporting issues
* Any new issues please report in [GitHub site](https://github.com/starnowski/posjsonhelper/issues)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
/**
* Posjsonhelper library is an open-source project that adds support of
* Hibernate query for https://www.postgresql.org/docs/10/functions-json.html)
*
* Copyright (C) 2023 Szymon Tarnowski
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
* Posjsonhelper library is an open-source project that adds support of
* Hibernate query for https://www.postgresql.org/docs/10/functions-json.html)
* <p>
* Copyright (C) 2023 Szymon Tarnowski
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package com.github.starnowski.posjsonhelper.core;

import java.util.HashSet;
import java.util.Set;

import static com.github.starnowski.posjsonhelper.core.Constants.DEFAULT_JSONB_ALL_ARRAY_STRINGS_EXIST_FUNCTION_NAME;
import static com.github.starnowski.posjsonhelper.core.Constants.DEFAULT_JSONB_ANY_ARRAY_STRINGS_EXIST_FUNCTION_NAME;

Expand All @@ -44,19 +47,36 @@ public class Context {
* By default, the property is initialized with the null value.
*/
private final String schema;
/**
* The set of the SQL functions that should be executed with reference to schema specified by property {@link #schema}.
* This feature may be useful when a function not built into Postgres should be executable with schema IDs other than the public schema.
*/
private final Set<String> functionsThatShouldBeExecutedWithSchemaReference;

public Context(String jsonbAllArrayStringsExistFunctionReference, String jsonbAnyArrayStringsExistFunctionReference, String schema) {
public Context(String jsonbAllArrayStringsExistFunctionReference, String jsonbAnyArrayStringsExistFunctionReference, String schema,
Set<String> functionsThatShouldBeExecutedWithSchemaReference) {
this.jsonbAllArrayStringsExistFunctionReference = jsonbAllArrayStringsExistFunctionReference;
this.jsonbAnyArrayStringsExistFunctionReference = jsonbAnyArrayStringsExistFunctionReference;
this.schema = schema;
this.functionsThatShouldBeExecutedWithSchemaReference = functionsThatShouldBeExecutedWithSchemaReference;
}

public static ContextBuilder builder() {
return new ContextBuilder();
}

/**
* Returns copy of value of property {@link #functionsThatShouldBeExecutedWithSchemaReference}.
*
* @return copy of value of property {@link #functionsThatShouldBeExecutedWithSchemaReference}
*/
public Set<String> getFunctionsThatShouldBeExecutedWithSchemaReference() {
return functionsThatShouldBeExecutedWithSchemaReference == null ? null : new HashSet<>(functionsThatShouldBeExecutedWithSchemaReference);
}

/**
* Returns value of property {@link #schema}
*
* @return value of property {@link #schema}
*/
public String getSchema() {
Expand All @@ -65,6 +85,7 @@ public String getSchema() {

/**
* Returns value of property {@link #jsonbAllArrayStringsExistFunctionReference}
*
* @return value of property {@link #jsonbAllArrayStringsExistFunctionReference}
*/
public String getJsonbAllArrayStringsExistFunctionReference() {
Expand All @@ -73,6 +94,7 @@ public String getJsonbAllArrayStringsExistFunctionReference() {

/**
* Returns value of property {@link #jsonbAnyArrayStringsExistFunctionReference}
*
* @return value of property {@link #jsonbAnyArrayStringsExistFunctionReference}
*/
public String getJsonbAnyArrayStringsExistFunctionReference() {
Expand All @@ -83,11 +105,18 @@ public static class ContextBuilder {
private String jsonbAllArrayStringsExistFunctionReference = DEFAULT_JSONB_ALL_ARRAY_STRINGS_EXIST_FUNCTION_NAME;
private String jsonbAnyArrayStringsExistFunctionReference = DEFAULT_JSONB_ANY_ARRAY_STRINGS_EXIST_FUNCTION_NAME;
private String schema;
private Set<String> functionsThatShouldBeExecutedWithSchemaReference;

public ContextBuilder withFunctionsThatShouldBeExecutedWithSchemaReference(Set<String> functionsThatShouldBeExecutedWithSchemaReference) {
this.functionsThatShouldBeExecutedWithSchemaReference = functionsThatShouldBeExecutedWithSchemaReference;
return this;
}

public ContextBuilder withContext(Context context) {
return withJsonbAllArrayStringsExistFunctionReference(context.getJsonbAllArrayStringsExistFunctionReference())
.withJsonbAnyArrayStringsExistFunctionReference(context.getJsonbAnyArrayStringsExistFunctionReference())
.withSchema(context.getSchema());
.withSchema(context.getSchema())
.withFunctionsThatShouldBeExecutedWithSchemaReference(context.getFunctionsThatShouldBeExecutedWithSchemaReference());
}

public ContextBuilder withSchema(String schema) {
Expand All @@ -106,7 +135,7 @@ public ContextBuilder withJsonbAnyArrayStringsExistFunctionReference(String json
}

public Context build() {
return new Context(this.jsonbAllArrayStringsExistFunctionReference, this.jsonbAnyArrayStringsExistFunctionReference, schema);
return new Context(jsonbAllArrayStringsExistFunctionReference, jsonbAnyArrayStringsExistFunctionReference, schema, functionsThatShouldBeExecutedWithSchemaReference);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.github.starnowski.posjsonhelper.hibernate6.descriptor;

import com.github.starnowski.posjsonhelper.core.Context;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.sqm.function.NamedSqmFunctionDescriptor;
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.predicate.Predicate;
import org.hibernate.sql.ast.tree.select.SortSpecification;

import java.util.List;

/**
* Function descriptor that renders function execution with schema reference depending on whether the function name
* exists in the {@link Context#functionsThatShouldBeExecutedWithSchemaReference}.
* If not schema reference is not added during rendering.
*/
public class NamedSqmFunctionWithSchemaReferenceDescriptor extends NamedSqmFunctionDescriptor {

protected final Context context;

public NamedSqmFunctionWithSchemaReferenceDescriptor(String functionName, Context context) {
super(functionName, false, null, null);
this.context = context;
}

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, ReturnableType<?> returnType, SqlAstTranslator<?> translator) {
renderOptionalSchemaReference(sqlAppender);
super.render(sqlAppender, sqlAstArguments, returnType, translator);
}

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, List<SortSpecification> withinGroup, ReturnableType<?> returnType, SqlAstTranslator<?> translator) {
renderOptionalSchemaReference(sqlAppender);
super.render(sqlAppender, sqlAstArguments, filter, withinGroup, returnType, translator);
}

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, ReturnableType<?> returnType, SqlAstTranslator<?> translator) {
renderOptionalSchemaReference(sqlAppender);
super.render(sqlAppender, sqlAstArguments, filter, returnType, translator);
}

@Override
public void render(SqlAppender sqlAppender, List<? extends SqlAstNode> sqlAstArguments, Predicate filter, Boolean respectNulls, Boolean fromFirst, ReturnableType<?> returnType, SqlAstTranslator<?> walker) {
renderOptionalSchemaReference(sqlAppender);
super.render(sqlAppender, sqlAstArguments, filter, respectNulls, fromFirst, returnType, walker);
}

protected void renderOptionalSchemaReference(SqlAppender sqlAppender) {
if (context.getFunctionsThatShouldBeExecutedWithSchemaReference() != null && context.getFunctionsThatShouldBeExecutedWithSchemaReference().contains(getName())) {
sqlAppender.appendSql(context.getSchema());
sqlAppender.appendSql(".");
}
}
}
Loading

0 comments on commit 4d4bd21

Please sign in to comment.