Skip to content

Commit 7454032

Browse files
rchigvintsevmp911de
authored andcommitted
DATAJDBC-514 - Add infrastructure for query derivation in Spring Data Relational.
Original pull request: spring-projects/spring-data-r2dbc#295.
1 parent 7802f6e commit 7454032

File tree

6 files changed

+701
-0
lines changed

6 files changed

+701
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.relational.core.dialect;
17+
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
22+
import org.springframework.lang.Nullable;
23+
24+
/**
25+
* Helper class encapsulating an escape character for LIKE queries and the actually usage of it in escaping
26+
* {@link String}s.
27+
*
28+
* @author Roman Chigvintsev
29+
* @author Mark Paluch
30+
* @since 2.0
31+
*/
32+
public class Escaper {
33+
34+
public static final Escaper DEFAULT = Escaper.of('\\');
35+
36+
private final char escapeCharacter;
37+
private final List<String> toReplace;
38+
39+
private Escaper(char escapeCharacter, List<String> toReplace) {
40+
41+
if (toReplace.contains(Character.toString(escapeCharacter))) {
42+
throw new IllegalArgumentException(
43+
String.format("'%s' and cannot be used as escape character as it should be replaced", escapeCharacter));
44+
}
45+
46+
this.escapeCharacter = escapeCharacter;
47+
this.toReplace = toReplace;
48+
}
49+
50+
/**
51+
* Creates new instance of this class with the given escape character.
52+
*
53+
* @param escapeCharacter escape character
54+
* @return new instance of {@link Escaper}.
55+
* @throws IllegalArgumentException if escape character is one of special characters ('_' and '%')
56+
*/
57+
public static Escaper of(char escapeCharacter) {
58+
return new Escaper(escapeCharacter, Arrays.asList("_", "%"));
59+
}
60+
61+
/**
62+
* Apply the {@link Escaper} to the given {@code chars}.
63+
*
64+
* @param chars characters/char sequences that should be escaped.
65+
* @return
66+
*/
67+
public Escaper withRewriteFor(String... chars) {
68+
69+
List<String> toReplace = new ArrayList<>(this.toReplace.size() + chars.length);
70+
toReplace.addAll(this.toReplace);
71+
toReplace.addAll(Arrays.asList(chars));
72+
73+
return new Escaper(this.escapeCharacter, toReplace);
74+
}
75+
76+
/**
77+
* Returns the escape character.
78+
*
79+
* @return the escape character to use.
80+
*/
81+
public char getEscapeCharacter() {
82+
return escapeCharacter;
83+
}
84+
85+
/**
86+
* Escapes all special like characters ({@code _}, {@code %}) using the configured escape character.
87+
*
88+
* @param value value to be escaped
89+
* @return escaped value
90+
*/
91+
@Nullable
92+
public String escape(@Nullable String value) {
93+
94+
if (value == null) {
95+
return null;
96+
}
97+
98+
return toReplace.stream().reduce(value, (it, character) -> it.replace(character, escapeCharacter + character));
99+
}
100+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.relational.repository.query;
17+
18+
import org.springframework.data.relational.core.query.Criteria;
19+
import org.springframework.data.relational.core.sql.Expression;
20+
import org.springframework.data.repository.query.parser.Part;
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* Simple factory to contain logic to create {@link Criteria}s from {@link Part}s.
25+
*
26+
* @author Roman Chigvintsev
27+
*/
28+
class CriteriaFactory {
29+
30+
private final ParameterMetadataProvider parameterMetadataProvider;
31+
32+
/**
33+
* Creates new instance of this class with the given {@link ParameterMetadataProvider}.
34+
*
35+
* @param parameterMetadataProvider parameter metadata provider (must not be {@literal null})
36+
*/
37+
public CriteriaFactory(ParameterMetadataProvider parameterMetadataProvider) {
38+
Assert.notNull(parameterMetadataProvider, "Parameter metadata provider must not be null!");
39+
this.parameterMetadataProvider = parameterMetadataProvider;
40+
}
41+
42+
/**
43+
* Creates {@link Criteria} for the given {@link Part}.
44+
*
45+
* @param part method name part (must not be {@literal null})
46+
* @return {@link Criteria} instance
47+
* @throws IllegalArgumentException if part type is not supported
48+
*/
49+
public Criteria createCriteria(Part part) {
50+
Part.Type type = part.getType();
51+
52+
String propertyName = part.getProperty().getSegment();
53+
Class<?> propertyType = part.getProperty().getType();
54+
55+
Criteria.CriteriaStep criteriaStep = Criteria.where(propertyName);
56+
57+
if (type == Part.Type.IS_NULL || type == Part.Type.IS_NOT_NULL) {
58+
return part.getType() == Part.Type.IS_NULL ? criteriaStep.isNull() : criteriaStep.isNotNull();
59+
}
60+
61+
if (type == Part.Type.TRUE || type == Part.Type.FALSE) {
62+
return part.getType() == Part.Type.TRUE ? criteriaStep.isTrue() : criteriaStep.isFalse();
63+
}
64+
65+
switch (type) {
66+
case BETWEEN: {
67+
ParameterMetadata geParamMetadata = parameterMetadataProvider.next(part);
68+
ParameterMetadata leParamMetadata = parameterMetadataProvider.next(part);
69+
return criteriaStep.greaterThanOrEquals(geParamMetadata.getValue()).and(propertyName)
70+
.lessThanOrEquals(leParamMetadata.getValue());
71+
}
72+
case AFTER:
73+
case GREATER_THAN: {
74+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
75+
return criteriaStep.greaterThan(paramMetadata.getValue());
76+
}
77+
case GREATER_THAN_EQUAL: {
78+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
79+
return criteriaStep.greaterThanOrEquals(paramMetadata.getValue());
80+
}
81+
case BEFORE:
82+
case LESS_THAN: {
83+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
84+
return criteriaStep.lessThan(paramMetadata.getValue());
85+
}
86+
case LESS_THAN_EQUAL: {
87+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
88+
return criteriaStep.lessThanOrEquals(paramMetadata.getValue());
89+
}
90+
case IN:
91+
case NOT_IN: {
92+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
93+
Criteria criteria = part.getType() == Part.Type.IN ? criteriaStep.in(paramMetadata.getValue())
94+
: criteriaStep.notIn(paramMetadata.getValue());
95+
return criteria.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, part.getProperty().getType()));
96+
}
97+
case STARTING_WITH:
98+
case ENDING_WITH:
99+
case CONTAINING:
100+
case NOT_CONTAINING:
101+
case LIKE:
102+
case NOT_LIKE: {
103+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
104+
Criteria criteria = part.getType() == Part.Type.NOT_LIKE || part.getType() == Part.Type.NOT_CONTAINING
105+
? criteriaStep.notLike(paramMetadata.getValue())
106+
: criteriaStep.like(paramMetadata.getValue());
107+
return criteria
108+
.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType()));
109+
}
110+
case SIMPLE_PROPERTY: {
111+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
112+
if (paramMetadata.getValue() == null) {
113+
return criteriaStep.isNull();
114+
}
115+
return criteriaStep.is(paramMetadata.getValue())
116+
.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType()));
117+
}
118+
case NEGATING_SIMPLE_PROPERTY: {
119+
ParameterMetadata paramMetadata = parameterMetadataProvider.next(part);
120+
return criteriaStep.not(paramMetadata.getValue())
121+
.ignoreCase(shouldIgnoreCase(part) && checkCanUpperCase(part, propertyType, paramMetadata.getType()));
122+
}
123+
default:
124+
throw new IllegalArgumentException("Unsupported keyword " + type);
125+
}
126+
}
127+
128+
/**
129+
* Checks whether comparison should be done in case-insensitive way.
130+
*
131+
* @param part method name part (must not be {@literal null})
132+
* @return {@literal true} if comparison should be done in case-insensitive way
133+
*/
134+
private boolean shouldIgnoreCase(Part part) {
135+
return part.shouldIgnoreCase() == Part.IgnoreCaseType.ALWAYS
136+
|| part.shouldIgnoreCase() == Part.IgnoreCaseType.WHEN_POSSIBLE;
137+
}
138+
139+
/**
140+
* Checks whether "upper-case" conversion can be applied to the given {@link Expression}s in case the underlying
141+
* {@link Part} requires ignoring case.
142+
*
143+
* @param part method name part (must not be {@literal null})
144+
* @param expressionTypes types of the given expressions (must not be {@literal null} or empty)
145+
* @throws IllegalStateException if {@link Part} requires ignoring case but "upper-case" conversion cannot be applied
146+
* to at least one of the given {@link Expression}s
147+
*/
148+
private boolean checkCanUpperCase(Part part, Class<?>... expressionTypes) {
149+
Assert.notEmpty(expressionTypes, "Expression types must not be null or empty");
150+
boolean strict = part.shouldIgnoreCase() == Part.IgnoreCaseType.ALWAYS;
151+
for (Class<?> expressionType : expressionTypes) {
152+
if (!canUpperCase(expressionType)) {
153+
if (strict) {
154+
throw new IllegalStateException("Unable to ignore case of " + expressionType.getName()
155+
+ " type, the property '" + part.getProperty().getSegment() + "' must reference a string");
156+
}
157+
return false;
158+
}
159+
}
160+
return true;
161+
}
162+
163+
private boolean canUpperCase(Class<?> expressionType) {
164+
return expressionType == String.class;
165+
}
166+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 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+
* https://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.relational.repository.query;
17+
18+
import org.springframework.lang.Nullable;
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* Helper class for holding information about query parameter.
23+
*
24+
* @since 2.0
25+
*/
26+
class ParameterMetadata {
27+
28+
private final String name;
29+
private final @Nullable Object value;
30+
private final Class<?> type;
31+
32+
public ParameterMetadata(String name, @Nullable Object value, Class<?> type) {
33+
34+
Assert.notNull(type, "Parameter type must not be null");
35+
this.name = name;
36+
this.value = value;
37+
this.type = type;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
@Nullable
45+
public Object getValue() {
46+
return value;
47+
}
48+
49+
public Class<?> getType() {
50+
return type;
51+
}
52+
}

0 commit comments

Comments
 (0)