Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for parameterized @Query including ValueExpressions #505

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-ldap</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-453-SNAPSHOT</version>

<name>Spring Data LDAP</name>
<description>Spring Data integration for LDAP</description>
Expand All @@ -21,6 +21,7 @@
<spring-ldap>3.2.6</spring-ldap>
<springdata.commons>3.4.0-SNAPSHOT</springdata.commons>
<java-module-name>spring.data.ldap</java-module-name>
<unboundid-ldapsdk>7.0.1</unboundid-ldapsdk>
</properties>

<developers>
Expand Down Expand Up @@ -109,6 +110,20 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-test</artifactId>
<version>${spring-ldap}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.unboundid</groupId>
<artifactId>unboundid-ldapsdk</artifactId>
<version>${unboundid-ldapsdk}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.data.repository.query.ValueExpressionDelegate;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import static org.springframework.ldap.query.LdapQueryBuilder.*;

import org.springframework.data.expression.ValueEvaluationContext;
import org.springframework.data.expression.ValueEvaluationContextProvider;
import org.springframework.data.ldap.repository.Query;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.EntityInstantiators;
import org.springframework.data.repository.query.ValueExpressionDelegate;
import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.query.LdapQuery;
import org.springframework.util.Assert;
Expand All @@ -35,6 +38,8 @@
public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {

private final Query queryAnnotation;
private final ValueExpressionDelegate valueExpressionDelegate;
private final StringBasedQuery stringBasedQuery;

/**
* Construct a new instance.
Expand All @@ -44,26 +49,58 @@ public class AnnotatedLdapRepositoryQuery extends AbstractLdapRepositoryQuery {
* @param ldapOperations the LdapOperations instance to use.
* @param mappingContext must not be {@literal null}.
* @param instantiators must not be {@literal null}.
* @deprecated use the constructor with {@link ValueExpressionDelegate}
*/
@Deprecated(since = "3.4")
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
EntityInstantiators instantiators) {

this(queryMethod, entityType, ldapOperations, mappingContext, instantiators, ValueExpressionDelegate.create());
}

/**
* Construct a new instance.
*
* @param queryMethod the QueryMethod.
* @param entityType the managed class.
* @param ldapOperations the LdapOperations instance to use.
* @param mappingContext must not be {@literal null}.
* @param instantiators must not be {@literal null}.
* @param valueExpressionDelegate must not be {@literal null}
* @since 3.4
*/
public AnnotatedLdapRepositoryQuery(LdapQueryMethod queryMethod, Class<?> entityType, LdapOperations ldapOperations,
MappingContext<? extends PersistentEntity<?, ?>, ? extends PersistentProperty<?>> mappingContext,
EntityInstantiators instantiators, ValueExpressionDelegate valueExpressionDelegate) {

super(queryMethod, entityType, ldapOperations, mappingContext, instantiators);

Assert.notNull(queryMethod.getQueryAnnotation(), "Annotation must be present");
Assert.hasLength(queryMethod.getQueryAnnotation().value(), "Query filter must be specified");

queryAnnotation = queryMethod.getRequiredQueryAnnotation();
String queryValue = queryAnnotation.value();
this.valueExpressionDelegate = valueExpressionDelegate;
stringBasedQuery = new StringBasedQuery(queryValue, queryMethod.getParameters(), valueExpressionDelegate);
}

@Override
protected LdapQuery createQuery(LdapParameterAccessor parameters) {

ValueEvaluationContextProvider valueContextProvider = valueExpressionDelegate
.createValueContextProvider(getQueryMethod().getParameters());

ValueEvaluationContext evaluationContext = valueContextProvider
.getEvaluationContext(parameters.getBindableParameterValues(), stringBasedQuery.getExpressionDependencies());

String boundQuery = stringBasedQuery.bindQuery(parameters,
new ContextualValueExpressionEvaluator(valueExpressionDelegate, evaluationContext));

return query().base(queryAnnotation.base()) //
.searchScope(queryAnnotation.searchScope()) //
.countLimit(queryAnnotation.countLimit()) //
.timeLimit(queryAnnotation.timeLimit()) //
.filter(queryAnnotation.value(), parameters.getBindableParameterValues());
.filter(boundQuery);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.ldap.repository.query;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.data.mapping.model.ValueExpressionEvaluator;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Value object capturing the binding context to provide {@link #getBindingValues() binding values} for queries.
*
* @author Mark Paluch
* @since 3.4
*/
class BindingContext {

private final LdapParameters parameters;

private final ParameterAccessor parameterAccessor;

private final List<ParameterBinding> bindings;

private final ValueExpressionEvaluator evaluator;

/**
* Create new {@link BindingContext}.
*/
BindingContext(LdapParameters parameters, ParameterAccessor parameterAccessor,
List<ParameterBinding> bindings, ValueExpressionEvaluator evaluator) {

this.parameters = parameters;
this.parameterAccessor = parameterAccessor;
this.bindings = bindings;
this.evaluator = evaluator;
}

/**
* @return {@literal true} when list of bindings is not empty.
*/
private boolean hasBindings() {
return !bindings.isEmpty();
}

/**
* Bind values provided by {@link LdapParameterAccessor} to placeholders in {@link BindingContext} while
* considering potential conversions and parameter types.
*
* @return {@literal null} if given {@code raw} value is empty.
*/
public List<Object> getBindingValues() {

if (!hasBindings()) {
return Collections.emptyList();
}

List<Object> parameters = new ArrayList<>(bindings.size());

for (ParameterBinding binding : bindings) {
Object parameterValueForBinding = getParameterValueForBinding(binding);
parameters.add(parameterValueForBinding);
}

return parameters;
}

/**
* Return the value to be used for the given {@link ParameterBinding}.
*
* @param binding must not be {@literal null}.
* @return the value used for the given {@link ParameterBinding}.
*/
@Nullable
private Object getParameterValueForBinding(ParameterBinding binding) {

if (binding.isExpression()) {
return evaluator.evaluate(binding.getRequiredExpression());
}

return binding.isNamed()
? parameterAccessor.getBindableValue(getParameterIndex(parameters, binding.getRequiredParameterName()))
: parameterAccessor.getBindableValue(binding.getParameterIndex());
}

private int getParameterIndex(LdapParameters parameters, String parameterName) {

return parameters.stream() //
.filter(cassandraParameter -> cassandraParameter //
.getName().filter(s -> s.equals(parameterName)) //
.isPresent()) //
.mapToInt(Parameter::getIndex) //
.findFirst() //
.orElseThrow(() -> new IllegalArgumentException(
String.format("Invalid parameter name; Cannot resolve parameter [%s]", parameterName)));
}

/**
* A generic parameter binding with name or position information.
*
* @author Mark Paluch
*/
static class ParameterBinding {

private final int parameterIndex;
private final @Nullable String expression;
private final @Nullable String parameterName;

private ParameterBinding(int parameterIndex, @Nullable String expression, @Nullable String parameterName) {

this.parameterIndex = parameterIndex;
this.expression = expression;
this.parameterName = parameterName;
}

static ParameterBinding expression(String expression, boolean quoted) {
return new ParameterBinding(-1, expression, null);
}

static ParameterBinding indexed(int parameterIndex) {
return new ParameterBinding(parameterIndex, null, null);
}

static ParameterBinding named(String name) {
return new ParameterBinding(-1, null, name);
}

boolean isNamed() {
return (parameterName != null);
}

int getParameterIndex() {
return parameterIndex;
}

String getParameter() {
return ("?" + (isExpression() ? "expr" : "") + parameterIndex);
}

String getRequiredExpression() {

Assert.state(expression != null, "ParameterBinding is not an expression");
return expression;
}

boolean isExpression() {
return (this.expression != null);
}

String getRequiredParameterName() {

Assert.state(parameterName != null, "ParameterBinding is not named");

return parameterName;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.ldap.repository.query;

import org.springframework.data.expression.ValueEvaluationContext;
import org.springframework.data.expression.ValueExpression;
import org.springframework.data.expression.ValueExpressionParser;
import org.springframework.data.mapping.model.ValueExpressionEvaluator;

/**
* @author Marcin Grzejszczak
* @author Mark Paluch
*/
class ContextualValueExpressionEvaluator implements ValueExpressionEvaluator {

private final ValueExpressionParser parser;

public ContextualValueExpressionEvaluator(ValueExpressionParser parser, ValueEvaluationContext evaluationContext) {
this.parser = parser;
this.evaluationContext = evaluationContext;
}

private final ValueEvaluationContext evaluationContext;

@SuppressWarnings("unchecked")
@Override
public <T> T evaluate(String expressionString) {
ValueExpression expression = parser.parse(expressionString);
return (T) expression.evaluate(evaluationContext);
}
}
Loading