|
| 1 | +/* |
| 2 | + * Copyright 2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.repository.query.parser; |
| 17 | + |
| 18 | +import lombok.NonNull; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | + |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.BiFunction; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +import org.springframework.util.Assert; |
| 29 | + |
| 30 | +/** |
| 31 | + * Source of {@link SpelExtractor} encapsulating configuration often common for all queries. |
| 32 | + * |
| 33 | + * @author Jens Schauder |
| 34 | + * @author Gerrit Meier |
| 35 | + */ |
| 36 | +@RequiredArgsConstructor |
| 37 | +public class SpelQueryContext { |
| 38 | + |
| 39 | + private final static String SPEL_PATTERN_STRING = "([:?])#\\{([^}]+)}"; |
| 40 | + private final static Pattern SPEL_PATTERN = Pattern.compile(SPEL_PATTERN_STRING); |
| 41 | + |
| 42 | + /** |
| 43 | + * A function from the index of a SpEL expression in a query and the actual SpEL expression to the parameter name to |
| 44 | + * be used in place of the SpEL expression. A typical implementation is expected to look like |
| 45 | + * <code>(index, spel) -> "__some_placeholder_" + index</code> |
| 46 | + */ |
| 47 | + @NonNull private final BiFunction<Integer, String, String> parameterNameSource; |
| 48 | + |
| 49 | + /** |
| 50 | + * A function from a prefix used to demarcate a SpEL expression in a query and a parameter name as returned from |
| 51 | + * {@link #parameterNameSource} to a {@literal String} to be used as a replacement of the SpEL in the query. The |
| 52 | + * returned value should normally be interpretable as a bind parameter by the underlying persistence mechanism. A |
| 53 | + * typical implementation is expected to look like <code>(prefix, name) -> prefix + name</code> or |
| 54 | + * <code>(prefix, name) -> "{" + name + "}"</code> |
| 55 | + */ |
| 56 | + @NonNull private final BiFunction<String, String, String> replacementSource; |
| 57 | + |
| 58 | + /** |
| 59 | + * Parses the query for SpEL expressions using the pattern |
| 60 | + * |
| 61 | + * <pre> |
| 62 | + * <prefix>#{<spel>} |
| 63 | + * </pre> |
| 64 | + * |
| 65 | + * with prefix being the character ':' or '?'. Parsing honors quoted {@literal String}s enclosed in single or double |
| 66 | + * quotation marks. |
| 67 | + * |
| 68 | + * @param query a query containing SpEL expressions in the format described above. Must not be {@literal null}. |
| 69 | + * @return A {@link SpelExtractor} which makes the query with SpEL expressions replaced by bind parameters and a map |
| 70 | + * from bind parameter to SpEL expression available. Guaranteed to be not {@literal null}. |
| 71 | + */ |
| 72 | + public SpelExtractor parse(String query) { |
| 73 | + return new SpelExtractor(query); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Parses a query string, identifies the contained SpEL expressions, replaces them with bind parameters and offers a |
| 78 | + * {@link Map} from those bind parameters to the spel expression. |
| 79 | + * <p> |
| 80 | + * The parser detects quoted parts of the query string and does not detect SpEL expressions inside such quoted parts |
| 81 | + * of the query. |
| 82 | + * |
| 83 | + * @author Jens Schauder |
| 84 | + */ |
| 85 | + public class SpelExtractor { |
| 86 | + |
| 87 | + private static final int PREFIX_GROUP_INDEX = 1; |
| 88 | + private static final int EXPRESSION_GROUP_INDEX = 2; |
| 89 | + |
| 90 | + private final String query; |
| 91 | + private final Map<String, String> expressions; |
| 92 | + |
| 93 | + /** |
| 94 | + * Creates a SpelExtractor from a query String. |
| 95 | + * |
| 96 | + * @param query Must not be {@literal null}. |
| 97 | + */ |
| 98 | + private SpelExtractor(String query) { |
| 99 | + |
| 100 | + Assert.notNull(query, "Query must not be null"); |
| 101 | + |
| 102 | + HashMap<String, String> expressions = new HashMap<>(); |
| 103 | + |
| 104 | + Matcher matcher = SPEL_PATTERN.matcher(query); |
| 105 | + |
| 106 | + StringBuilder resultQuery = new StringBuilder(); |
| 107 | + |
| 108 | + QuotationMap quotedAreas = new QuotationMap(query); |
| 109 | + |
| 110 | + int expressionCounter = 0; |
| 111 | + int matchedUntil = 0; |
| 112 | + |
| 113 | + while (matcher.find()) { |
| 114 | + |
| 115 | + if (quotedAreas.isQuoted(matcher.start())) { |
| 116 | + |
| 117 | + resultQuery.append(query.substring(matchedUntil, matcher.end())); |
| 118 | + |
| 119 | + } else { |
| 120 | + |
| 121 | + String spelExpression = matcher.group(EXPRESSION_GROUP_INDEX); |
| 122 | + String prefix = matcher.group(PREFIX_GROUP_INDEX); |
| 123 | + |
| 124 | + String parameterName = parameterNameSource.apply(expressionCounter, spelExpression); |
| 125 | + String replacement = replacementSource.apply(prefix, parameterName); |
| 126 | + |
| 127 | + resultQuery.append(query.substring(matchedUntil, matcher.start())); |
| 128 | + resultQuery.append(replacement); |
| 129 | + |
| 130 | + expressions.put(parameterName, spelExpression); |
| 131 | + expressionCounter++; |
| 132 | + } |
| 133 | + |
| 134 | + matchedUntil = matcher.end(); |
| 135 | + } |
| 136 | + |
| 137 | + resultQuery.append(query.substring(matchedUntil)); |
| 138 | + |
| 139 | + this.expressions = Collections.unmodifiableMap(expressions); |
| 140 | + this.query = resultQuery.toString(); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * The query with all the SpEL expressions replaced with bind parameters. |
| 145 | + * |
| 146 | + * @return Guaranteed to be not {@literal null}. |
| 147 | + */ |
| 148 | + public String query() { |
| 149 | + return query; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * A {@literal Map} from parameter name to SpEL expression. |
| 154 | + * |
| 155 | + * @return Guaranteed to be not {@literal null}. |
| 156 | + */ |
| 157 | + public Map<String, String> parameterNameToSpelMap() { |
| 158 | + return expressions; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments