Skip to content

Commit

Permalink
#71 - Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
starnowski committed Dec 20, 2023
1 parent 4dbb041 commit 8e8bd04
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ public List<Tweet> findBySinglePhraseInDescriptionForDefaultConfiguration(String
CriteriaQuery<Tweet> query = cb.createQuery(Tweet.class);
Root<Tweet> root = query.from(Tweet.class);
query.select(root);
List<SqmExpression<String>> arguments = new ArrayList<>();
arguments.add(new TSVectorFunction(root.get("shortContent"), (NodeBuilder) cb));
arguments.add(new PlainToTSQueryFunction((NodeBuilder) cb, null, phrase));
query.where(new TextOperatorFunction((NodeBuilder) cb, arguments, hibernateContext));
query.where(new TextOperatorFunction((NodeBuilder) cb, new TSVectorFunction(root.get("shortContent"), (NodeBuilder) cb), new PlainToTSQueryFunction((NodeBuilder) cb, null, phrase), hibernateContext));
return entityManager.createQuery(query).getResultList();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.starnowski.posjsonhelper.text.hibernate6.descriptor;

import com.github.starnowski.posjsonhelper.core.HibernateContext;
import com.github.starnowski.posjsonhelper.text.hibernate6.functions.TSVectorFunction;
import com.github.starnowski.posjsonhelper.text.hibernate6.operators.TextOperatorFunction;
import org.hibernate.query.ReturnableType;
import org.hibernate.query.spi.QueryEngine;
Expand Down Expand Up @@ -42,6 +43,6 @@ protected <T> SelfRenderingSqmFunction<T> generateSqmFunctionExpression(List<? e
for (int i = 0; i < arguments.size(); i++) {
args.add((SqmExpression<String>) arguments.get(i));
}
return (SelfRenderingSqmFunction<T>) new TextOperatorFunction(queryEngine.getCriteriaBuilder(), args, hibernateContext);
return (SelfRenderingSqmFunction<T>) new TextOperatorFunction(queryEngine.getCriteriaBuilder(), (TSVectorFunction) args.get(0), args.get(1), hibernateContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
package com.github.starnowski.posjsonhelper.text.hibernate6.operators;

import com.github.starnowski.posjsonhelper.core.HibernateContext;
import com.github.starnowski.posjsonhelper.text.hibernate6.functions.TSVectorFunction;
import jakarta.persistence.criteria.Path;
import org.hibernate.query.sqm.NodeBuilder;
import org.hibernate.query.sqm.function.FunctionRenderingSupport;
import org.hibernate.query.sqm.function.SelfRenderingSqmFunction;
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.SqmTypedNode;
import org.hibernate.query.sqm.tree.expression.SqmExpression;
import org.hibernate.type.StandardBasicTypes;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class TextOperatorFunction extends SelfRenderingSqmFunction<Boolean> implements Serializable {

private final HibernateContext context;
private final List<? extends SqmExpression<String>> arguments;
private final TSVectorFunction tsVectorFunction;
private final SqmExpression<String> argument;

public TextOperatorFunction(NodeBuilder nodeBuilder, List<? extends SqmExpression<String>> arguments, HibernateContext hibernateContext) {
public TextOperatorFunction(NodeBuilder nodeBuilder, TSVectorFunction tsVectorFunction, SqmExpression<String> argument, HibernateContext hibernateContext) {
super(nodeBuilder.getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(hibernateContext.getTextFunctionOperator()),
(FunctionRenderingSupport) nodeBuilder.getQueryEngine().getSqmFunctionRegistry().findFunctionDescriptor(hibernateContext.getTextFunctionOperator()),
//TODO Check if only two arguments are being passed!
//TODO First should be TSVector type
arguments,
contactParameters(tsVectorFunction, argument),
null,
null,
StandardFunctionReturnTypeResolvers.invariant(nodeBuilder.getTypeConfiguration().getBasicTypeRegistry().resolve(StandardBasicTypes.BOOLEAN)),
nodeBuilder,
hibernateContext.getTextFunctionOperator());
this.context = hibernateContext;
this.arguments = arguments;
this.tsVectorFunction = tsVectorFunction;
this.argument = argument;
}

public TextOperatorFunction copy(SqmCopyContext context) {
TextOperatorFunction existing = (TextOperatorFunction) context.getCopy(this);
if (existing != null) {
return existing;
} else {
TextOperatorFunction predicate = (TextOperatorFunction) context.registerCopy(this, new TextOperatorFunction(nodeBuilder(), this.arguments, this.context));
TextOperatorFunction predicate = (TextOperatorFunction) context.registerCopy(this, new TextOperatorFunction(nodeBuilder(), this.tsVectorFunction, this.argument, this.context));
this.copyTo(predicate, context);
return predicate;
}
}

private static List<? extends SqmExpression<String>> contactParameters(TSVectorFunction tsVectorFunction, SqmExpression<String> argument) {
if (tsVectorFunction == null) {
throw new IllegalArgumentException("TSVectorFunction argument can not be null");
}
List<SqmExpression<String>> result = new ArrayList<>();
result.add(tsVectorFunction);
result.add(argument);
return result;
}
}

0 comments on commit 8e8bd04

Please sign in to comment.