Skip to content

Commit

Permalink
Revise String Query ParameterBinding.
Browse files Browse the repository at this point in the history
We now distinguish between the binding parameter target and its origin. The parameter target represents how the binding is bound to the query, the origin points to where the binding parameter comes from (method invocation argument or an expression).

The revised design removes the assumption that binding parameters must match their indices/names of the method call to introduce synthetic parameters for different binding variants while using the same underlying invocation parameters.

See #3041
  • Loading branch information
mp911de authored and gregturn committed Jul 21, 2023
1 parent c0a6ee7 commit 55813a8
Show file tree
Hide file tree
Showing 10 changed files with 695 additions and 612 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ static DeclaredQuery of(@Nullable String query, boolean nativeQuery) {
boolean isDefaultProjection();

/**
* Returns the {@link StringQuery.ParameterBinding}s registered.
* Returns the {@link ParameterBinding}s registered.
*/
List<StringQuery.ParameterBinding> getParameterBindings();
List<ParameterBinding> getParameterBindings();

/**
* Creates a new {@literal DeclaredQuery} representing a count query, i.e. a query returning the number of rows to be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public boolean isDefaultProjection() {
}

@Override
public List<StringQuery.ParameterBinding> getParameterBindings() {
public List<ParameterBinding> getParameterBindings() {
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import java.util.List;

import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.jpa.repository.query.ParameterBinding.BindingIdentifier;
import org.springframework.data.jpa.repository.query.ParameterBinding.ParameterOrigin;
import org.springframework.data.jpa.repository.query.ParameterMetadataProvider.ParameterMetadata;
import org.springframework.data.jpa.repository.query.StringQuery.ParameterBinding;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.Assert;
Expand All @@ -46,11 +47,10 @@ static ParameterBinder createBinder(JpaParameters parameters) {

Assert.notNull(parameters, "JpaParameters must not be null");

QueryParameterSetterFactory likeFactory = QueryParameterSetterFactory.forLikeRewrite(parameters);
QueryParameterSetterFactory setterFactory = QueryParameterSetterFactory.basic(parameters);
List<ParameterBinding> bindings = getBindings(parameters);

return new ParameterBinder(parameters, createSetters(bindings, likeFactory, setterFactory));
return new ParameterBinder(parameters, createSetters(bindings, setterFactory));
}

/**
Expand Down Expand Up @@ -97,11 +97,9 @@ static ParameterBinder createQueryAwareBinder(JpaParameters parameters, Declared
QueryParameterSetterFactory expressionSetterFactory = QueryParameterSetterFactory.parsing(parser,
evaluationContextProvider, parameters);

QueryParameterSetterFactory like = QueryParameterSetterFactory.forLikeRewrite(parameters);
QueryParameterSetterFactory basicSetterFactory = QueryParameterSetterFactory.basic(parameters);

return new ParameterBinder(parameters,
createSetters(bindings, query, expressionSetterFactory, like, basicSetterFactory),
return new ParameterBinder(parameters, createSetters(bindings, query, expressionSetterFactory, basicSetterFactory),
!query.usesPaging());
}

Expand All @@ -113,7 +111,11 @@ private static List<ParameterBinding> getBindings(JpaParameters parameters) {
for (JpaParameter parameter : parameters) {

if (parameter.isBindable()) {
result.add(new ParameterBinding(++bindableParameterIndex));
int index = ++bindableParameterIndex;
BindingIdentifier bindingIdentifier = parameter.getName().map(it -> BindingIdentifier.of(it, index))
.orElseGet(() -> BindingIdentifier.of(index));

result.add(new ParameterBinding(bindingIdentifier, ParameterOrigin.ofParameter(bindingIdentifier)));
}
}

Expand Down
Loading

0 comments on commit 55813a8

Please sign in to comment.