Skip to content

Commit

Permalink
jakartaee/persistence#417 - Add left(), right(), and replace() functi…
Browse files Browse the repository at this point in the history
…ons to JPQL/criteria

jakartaee/persistence#356 - Add extract() to CriteriaBuilder

Signed-off-by: Tomáš Kraus <[email protected]>
  • Loading branch information
Tomas-Kraus committed Sep 12, 2023
1 parent bfb0f22 commit a056e0e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
import org.eclipse.persistence.jpa.JpaCriteriaBuilder;
import org.eclipse.persistence.queries.ReportQuery;

import static org.eclipse.persistence.internal.jpa.querydef.InternalSelection.currentNode;

public class CriteriaBuilderImpl implements JpaCriteriaBuilder, Serializable {

public static final String CONCAT = "concat";
Expand Down Expand Up @@ -176,7 +178,7 @@ public Order asc(Expression<?> expression) {
// TODO-API-3.2 - Nulls added to OrderImpl and ObjectLevelReadQuery in CriteriaQueryImpl, but no tests exist
@Override
public Order asc(Expression<?> expression, Nulls nullPrecedence) {
if (((InternalSelection)expression).getCurrentNode() == null){
if (currentNode(expression) == null){
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("OPERATOR_EXPRESSION_IS_CONJUNCTION"));
}
return new OrderImpl(expression, true, nullPrecedence);
Expand All @@ -190,7 +192,7 @@ public Order desc(Expression<?> expression){
// TODO-API-3.2 - Nulls added to OrderImpl and ObjectLevelReadQuery in CriteriaQueryImpl, but no tests exist
@Override
public Order desc(Expression<?> expression, Nulls nullPrecedence) {
if (((InternalSelection)expression).getCurrentNode() == null){
if (currentNode(expression) == null){
throw new IllegalArgumentException(ExceptionLocalization.buildMessage("OPERATOR_EXPRESSION_IS_CONJUNCTION"));
}
return new OrderImpl(expression, false, nullPrecedence);
Expand Down Expand Up @@ -2161,49 +2163,69 @@ public Expression<Integer> length(Expression<String> x){
// TODO-API-3.2
@Override
public Expression<String> left(Expression<String> expression, int len) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).left(len), buildList(expression), "left");
}

// TODO-API-3.2
@Override
public Expression<String> right(Expression<String> expression, int len) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
public Expression<String> left(Expression<String> expression, Expression<Integer> len) {
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).left(currentNode(len)), buildList(expression), "left");
}

// TODO-API-3.2
@Override
public Expression<String> left(Expression<String> expression, Expression<Integer> len) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
public Expression<String> right(Expression<String> expression, int len) {
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).right(len), buildList(expression), "right");
}

// TODO-API-3.2
@Override
public Expression<String> right(Expression<String> expression, Expression<Integer> len) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).right(currentNode(len)), buildList(expression), "right");
}

// TODO-API-3.2
@Override
public Expression<String> replace(Expression<String> expression, Expression<String> substring, Expression<String> replacement) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).replace(currentNode(substring), currentNode(replacement)),
buildList(expression), "replace");
}

// TODO-API-3.2
@Override
public Expression<String> replace(Expression<String> expression, String substring, Expression<String> replacement) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).replace(substring, currentNode(replacement)),
buildList(expression), "replace");
}

// TODO-API-3.2
@Override
public Expression<String> replace(Expression<String> expression, Expression<String> substring, String replacement) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).replace(currentNode(substring), replacement),
buildList(expression), "replace");
}

// TODO-API-3.2
@Override
public Expression<String> replace(Expression<String> expression, String substring, String replacement) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, ClassConstants.STRING,
currentNode(expression).replace(substring, replacement),
buildList(expression), "replace");
}

/**
Expand Down Expand Up @@ -2339,10 +2361,14 @@ public Expression<java.time.LocalTime> localTime() {
return new ExpressionImpl(metamodel, ClassConstants.LOCAL_TIME, new ExpressionBuilder().localTime());
}

// TODO-API-3.2
// TODO-API-3.2 - not sure whether (Class<N>) temporal.getJavaType() is sufficient for result class
@Override
@SuppressWarnings("unchecked")
public <N, T extends Temporal> Expression<N> extract(TemporalField<N, T> field, Expression<T> temporal) {
throw new UnsupportedOperationException("Jakarta Persistence 3.2 API was not implemented yet");
return new FunctionExpressionImpl<>(
this.metamodel, (Class<N>) temporal.getJavaType(),
// JPA API: field.toString() returns name of the part to be extracted
currentNode(temporal).extract(field.toString()), buildList(temporal), "extract");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//
package org.eclipse.persistence.internal.jpa.querydef;

import jakarta.persistence.criteria.Expression;

/**
* <p>
* <b>Purpose</b>: Represents a Selection in the Criteria API implementation hierarchy.
Expand All @@ -28,7 +30,7 @@
* @author gyorke
* @since EclipseLink 1.2
*/
public interface InternalSelection{
public interface InternalSelection {

void findRootAndParameters(CommonAbstractCriteriaImpl criteriaQuery);

Expand All @@ -38,4 +40,9 @@ public interface InternalSelection{
boolean isRoot();
boolean isConstructor();

// Shortcut to return current expression node
static org.eclipse.persistence.expressions.Expression currentNode(Expression<?> expression) {
return ((InternalSelection)expression).getCurrentNode();
}

}

0 comments on commit a056e0e

Please sign in to comment.