Skip to content

Commit

Permalink
#116 - Added NamedSqmFunctionWithSchemaReferenceDescriptor type
Browse files Browse the repository at this point in the history
  • Loading branch information
starnowski committed Mar 10, 2024
1 parent 26f2c8b commit ac24cf9
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 26 deletions.
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,37 @@ 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}.
* If {@link #functionsThatShouldBeExecutedWithSchemaReference} is null then the empty set is being returned.
*
* @return copy of value of property {@link #functionsThatShouldBeExecutedWithSchemaReference}
*/
public Set<String> getFunctionsThatShouldBeExecutedWithSchemaReference() {
return functionsThatShouldBeExecutedWithSchemaReference == null ? new HashSet<>() : new HashSet<>(functionsThatShouldBeExecutedWithSchemaReference);
}

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

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

/**
* Returns value of property {@link #jsonbAnyArrayStringsExistFunctionReference}
*
* @return value of property {@link #jsonbAnyArrayStringsExistFunctionReference}
*/
public String getJsonbAnyArrayStringsExistFunctionReference() {
Expand All @@ -83,6 +106,12 @@ 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())
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,55 @@
package com.github.starnowski.posjsonhelper.hibernate6.descriptor;

import com.github.starnowski.posjsonhelper.core.Context;
import com.github.starnowski.posjsonhelper.core.HibernateContext;
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;

public class NamedSqmFunctionWithSchemaReferenceDescriptor extends NamedSqmFunctionDescriptor {

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

@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().contains(getName())) {
sqlAppender.appendSql(context.getSchema());
sqlAppender.appendSql(".");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.HashSet;

import static java.util.Arrays.asList;

@Configuration
@Profile("different-schema-testing-function-execution-with-schema-reference")
public class PosjsonhelperWithDifferentSchemaConfigurationExecuteFunctionWithSchemaReference {
Expand All @@ -24,6 +28,9 @@ public Context getContext() {
.withSchema("non_public_schema")
.withJsonbAnyArrayStringsExistFunctionReference("poshelper_json_array_any_string")
.withJsonbAllArrayStringsExistFunctionReference("poshelper_json_array_all_string")
.withFunctionsThatShouldBeExecutedWithSchemaReference(new HashSet<>(
asList("poshelper_json_array_any_string", "poshelper_json_array_all_string")
))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
package com.github.starnowski.posjsonhelper.hibernate6.descriptor;

import com.github.starnowski.posjsonhelper.core.Context;
import com.github.starnowski.posjsonhelper.core.HibernateContext;
import com.github.starnowski.posjsonhelper.hibernate6.JsonBExtractPath;
import com.github.starnowski.posjsonhelper.hibernate6.operators.JsonArrayFunction;
Expand All @@ -40,12 +41,14 @@
*
* @param <T> child type for {@link AbstractJsonbArrayStringsExistPredicate}
*/
public abstract class AbstractJsonbArrayStringsExistPredicateDescriptor<T extends AbstractJsonbArrayStringsExistPredicate> extends NamedSqmFunctionDescriptor {
public abstract class AbstractJsonbArrayStringsExistPredicateDescriptor<T extends AbstractJsonbArrayStringsExistPredicate> extends NamedSqmFunctionWithSchemaReferenceDescriptor {

protected final HibernateContext hibernateContext;
protected final Context context;

public AbstractJsonbArrayStringsExistPredicateDescriptor(String name, HibernateContext hibernateContext) {
super(name, false, null, null);
public AbstractJsonbArrayStringsExistPredicateDescriptor(String name, Context context, HibernateContext hibernateContext) {
super(name, context, hibernateContext);
this.context = context;
this.hibernateContext = hibernateContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class JsonbAllArrayStringsExistPredicateDescriptor extends AbstractJsonbArrayStringsExistPredicateDescriptor<JsonbAllArrayStringsExistPredicate> {
public JsonbAllArrayStringsExistPredicateDescriptor(Context context, HibernateContext hibernateContext) {
super(context.getJsonbAllArrayStringsExistFunctionReference(), hibernateContext);
super(context.getJsonbAllArrayStringsExistFunctionReference(), context, hibernateContext);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
public class JsonbAnyArrayStringsExistPredicateDescriptor extends AbstractJsonbArrayStringsExistPredicateDescriptor<JsonbAnyArrayStringsExistPredicate> {
public JsonbAnyArrayStringsExistPredicateDescriptor(Context context, HibernateContext hibernateContext) {
super(context.getJsonbAnyArrayStringsExistFunctionReference(), hibernateContext);
super(context.getJsonbAnyArrayStringsExistFunctionReference(), context, hibernateContext);
}

@Override
Expand Down

0 comments on commit ac24cf9

Please sign in to comment.