Skip to content

DATACMNS-1258 - Added infrastructure for SpEL handling in queries. #275

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 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-commons</artifactId>
<version>2.1.0.BUILD-SNAPSHOT</version>
<version>2.1.0.DATACMNS-1258-SNAPSHOT</version>

<name>Spring Data Core</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2018 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
*
* http://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.repository.query.parser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.springframework.data.domain.Range;
import org.springframework.lang.Nullable;

/**
* Value object to analyze a String to determine the parts of the String that are quoted and offers an API to query that
* information.
*
* @author Jens Schauder
* @since 2.0.3
*/
class QuotationMap {

private static final Collection<Character> QUOTING_CHARACTERS = Arrays.asList('"', '\'');

private final List<Range<Integer>> quotedRanges = new ArrayList<>();

public QuotationMap(@Nullable String query) {

if (query == null) {
return;
}

Character inQuotation = null;
int start = 0;

for (int i = 0; i < query.length(); i++) {

char currentChar = query.charAt(i);

if (QUOTING_CHARACTERS.contains(currentChar)) {

if (inQuotation == null) {

inQuotation = currentChar;
start = i;

} else if (currentChar == inQuotation) {

inQuotation = null;
quotedRanges.add(Range.of(Range.Bound.inclusive(start), Range.Bound.inclusive(i)));
}
}
}

if (inQuotation != null) {
throw new IllegalArgumentException(
String.format("The string <%s> starts a quoted range at %d, but never ends it.", query, start));
}
}

/**
* Checks if a given index is within a quoted range.
*
* @param index to check if it is part of a quoted range.
* @return whether the query contains a quoted range at {@literal index}.
*/
public boolean isQuoted(int index) {
return quotedRanges.stream().anyMatch(r -> r.contains(index));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2018 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
*
* http://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.repository.query.parser;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.parser.SpelQueryContext.SpelExtractor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Evaluates SpEL expressions as extracted by the SpelExtractor based on parameter information from a method and
* parameter values from a method call.
*
* @author Jens Schauder
* @author Gerrit Meier
*/
@RequiredArgsConstructor
public class SpelEvaluator {

private final static SpelExpressionParser PARSER = new SpelExpressionParser();

private final @NonNull QueryMethodEvaluationContextProvider evaluationContextProvider;
private final @NonNull Parameters<?, ?> parameters;

/**
* A map from parameter name to SpEL expression as returned by
* {@link SpelQueryContext.SpelExtractor#parameterNameToSpelMap()}.
*/
private final @NonNull SpelExtractor extractor;

/**
* Evaluate all the SpEL expressions in {@link #parameterNameToSpelMap} based on values provided as an argument.
*
* @param values Parameter values. Must not be {@literal null}.
* @return a map from parameter name to evaluated value. Guaranteed to be not {@literal null}.
*/
public Map<String, Object> evaluate(Object[] values) {

Assert.notNull(values, "Values must not be null.");

EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(parameters, values);

return extractor.parameters().collect(Collectors.toMap(//
it -> it.getKey(), //
it -> getSpElValue(evaluationContext, it.getValue()) //
));
}

@Nullable
private static Object getSpElValue(EvaluationContext evaluationContext, String expression) {
return PARSER.parseExpression(expression).getValue(evaluationContext, Object.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* Copyright 2018 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
*
* http://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.repository.query.parser;

import lombok.NonNull;
import lombok.RequiredArgsConstructor;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiFunction;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.util.Assert;

/**
* Source of {@link SpelExtractor} encapsulating configuration often common for all queries.
*
* @author Jens Schauder
* @author Gerrit Meier
*/
@RequiredArgsConstructor(staticName = "of")
public class SpelQueryContext {

private final static String SPEL_PATTERN_STRING = "([:?])#\\{([^}]+)}";
private final static Pattern SPEL_PATTERN = Pattern.compile(SPEL_PATTERN_STRING);

private final QueryMethodEvaluationContextProvider evaluationContextProvider;

/**
* A function from the index of a SpEL expression in a query and the actual SpEL expression to the parameter name to
* be used in place of the SpEL expression. A typical implementation is expected to look like
* <code>(index, spel) -> "__some_placeholder_" + index</code>
*/
private final @NonNull BiFunction<Integer, String, String> parameterNameSource;

/**
* A function from a prefix used to demarcate a SpEL expression in a query and a parameter name as returned from
* {@link #parameterNameSource} to a {@literal String} to be used as a replacement of the SpEL in the query. The
* returned value should normally be interpretable as a bind parameter by the underlying persistence mechanism. A
* typical implementation is expected to look like <code>(prefix, name) -> prefix + name</code> or
* <code>(prefix, name) -> "{" + name + "}"</code>
*/
private final @NonNull BiFunction<String, String, String> replacementSource;

/**
* Parses the query for SpEL expressions using the pattern
*
* <pre>
* &lt;prefix&gt;#{&lt;spel&gt;}
* </pre>
*
* with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double
* quotation marks.
*
* @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}.
* @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map
* from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}.
*/
SpelExtractor parse(String query) {
return new SpelExtractor(query);
}

public SpelEvaluator parse(String query, Parameters<?, ?> parameters) {

SpelExtractor extractor = parse(query);

return new SpelEvaluator(evaluationContextProvider, parameters, extractor);
}

/**
* Parses a query string, identifies the contained SpEL expressions, replaces them with bind parameters and offers a
* {@link Map} from those bind parameters to the spel expression.
* <p>
* The parser detects quoted parts of the query string and does not detect SpEL expressions inside such quoted parts
* of the query.
*
* @author Jens Schauder
* @author Oliver Gierke
* @since 2.1
*/
class SpelExtractor {

private static final int PREFIX_GROUP_INDEX = 1;
private static final int EXPRESSION_GROUP_INDEX = 2;

private final String query;
private final Map<String, String> expressions;

/**
* Creates a SpelExtractor from a query String.
*
* @param query must not be {@literal null}.
*/
SpelExtractor(String query) {

Assert.notNull(query, "Query must not be null");

Map<String, String> expressions = new HashMap<>();
Matcher matcher = SPEL_PATTERN.matcher(query);
StringBuilder resultQuery = new StringBuilder();
QuotationMap quotedAreas = new QuotationMap(query);

int expressionCounter = 0;
int matchedUntil = 0;

while (matcher.find()) {

if (quotedAreas.isQuoted(matcher.start())) {

resultQuery.append(query.substring(matchedUntil, matcher.end()));

} else {

String spelExpression = matcher.group(EXPRESSION_GROUP_INDEX);
String prefix = matcher.group(PREFIX_GROUP_INDEX);

String parameterName = parameterNameSource.apply(expressionCounter, spelExpression);
String replacement = replacementSource.apply(prefix, parameterName);

resultQuery.append(query.substring(matchedUntil, matcher.start()));
resultQuery.append(replacement);

expressions.put(parameterName, spelExpression);
expressionCounter++;
}

matchedUntil = matcher.end();
}

resultQuery.append(query.substring(matchedUntil));

this.expressions = Collections.unmodifiableMap(expressions);
this.query = resultQuery.toString();
}

/**
* The query with all the SpEL expressions replaced with bind parameters.
*
* @return Guaranteed to be not {@literal null}.
*/
String query() {
return query;
}

/**
* A {@literal Map} from parameter name to SpEL expression.
*
* @return Guaranteed to be not {@literal null}.
*/
Map<String, String> parameterNameToSpelMap() {
return expressions;
}

Stream<Entry<String, String>> parameters() {
return expressions.entrySet().stream();
}
}
}
Loading