Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
(cherry picked from commit 9d45943844
)
This commit is contained in:
parent
dc3e4c7a66
commit
cc426ffd61
|
@ -17,12 +17,7 @@ public class LikeExpression extends BaseExpression {
|
||||||
super(expressionInterval, expressionText);
|
super(expressionInterval, expressionText);
|
||||||
this.internal = internal;
|
this.internal = internal;
|
||||||
// Converting to regex is not the most performant impl, but it works
|
// Converting to regex is not the most performant impl, but it works
|
||||||
this.pattern = Pattern.compile("^" +
|
this.pattern = convertLikePatternToRegex(pattern);
|
||||||
pattern.replaceAll("(?<!\\\\)\\%", ".*")
|
|
||||||
.replaceAll("(?<!\\\\)\\_", ".")
|
|
||||||
.replaceAll("\\\\\\%", "%")
|
|
||||||
.replaceAll("\\\\_", "_") + "$"
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -35,4 +30,38 @@ public class LikeExpression extends BaseExpression {
|
||||||
|
|
||||||
return pattern.matcher(value).matches();
|
return pattern.matcher(value).matches();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Pattern convertLikePatternToRegex(String pattern) {
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append("^\\Q");
|
||||||
|
|
||||||
|
for (int i = 0; i < pattern.length(); i++) {
|
||||||
|
if (pattern.charAt(i) == '\\' && i < pattern.length() - 1) {
|
||||||
|
if (pattern.charAt(i + 1) == '%') {
|
||||||
|
// \% case
|
||||||
|
builder.append('%');
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
} else if (pattern.charAt(i + 1) == '_') {
|
||||||
|
// \_ case
|
||||||
|
builder.append('_');
|
||||||
|
i++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (pattern.charAt(i) == '_') {
|
||||||
|
// replace with .
|
||||||
|
builder.append("\\E.\\Q");
|
||||||
|
} else if (pattern.charAt(i) == '%') {
|
||||||
|
// replace with .*
|
||||||
|
builder.append("\\E.*\\Q");
|
||||||
|
} else {
|
||||||
|
builder.append(pattern.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.append("\\E$");
|
||||||
|
|
||||||
|
return Pattern.compile(builder.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
name: Like expression
|
name: Like expression
|
||||||
tests:
|
tests:
|
||||||
- name: Exact match
|
- name: Exact match (1)
|
||||||
expression: "'abc' LIKE 'abc'"
|
expression: "'abc' LIKE 'abc'"
|
||||||
result: true
|
result: true
|
||||||
|
- name: Exact match (2)
|
||||||
|
expression: "'ab\\c' LIKE 'ab\\c'"
|
||||||
|
result: true
|
||||||
- name: Exact match (negate)
|
- name: Exact match (negate)
|
||||||
expression: "'abc' NOT LIKE 'abc'"
|
expression: "'abc' NOT LIKE 'abc'"
|
||||||
result: false
|
result: false
|
||||||
|
@ -25,6 +28,12 @@ tests:
|
||||||
- name: Percentage operator (6)
|
- name: Percentage operator (6)
|
||||||
expression: "'' LIKE 'abc'"
|
expression: "'' LIKE 'abc'"
|
||||||
result: false
|
result: false
|
||||||
|
- name: Percentage operator (7)
|
||||||
|
expression: "'.ab.cde.' LIKE '.%.%.'"
|
||||||
|
result: true
|
||||||
|
- name: Percentage operator (8)
|
||||||
|
expression: "'ab.cde' LIKE '.%.%.'"
|
||||||
|
result: false
|
||||||
|
|
||||||
- name: Underscore operator (1)
|
- name: Underscore operator (1)
|
||||||
expression: "'abc' LIKE 'a_b_c'"
|
expression: "'abc' LIKE 'a_b_c'"
|
||||||
|
@ -41,6 +50,12 @@ tests:
|
||||||
- name: Underscore operator (5)
|
- name: Underscore operator (5)
|
||||||
expression: "'azbzc' LIKE 'a_b_c'"
|
expression: "'azbzc' LIKE 'a_b_c'"
|
||||||
result: true
|
result: true
|
||||||
|
- name: Underscore operator (6)
|
||||||
|
expression: "'.a.b.' LIKE '._._.'"
|
||||||
|
result: true
|
||||||
|
- name: Underscore operator (7)
|
||||||
|
expression: "'abcd.' LIKE '._._.'"
|
||||||
|
result: false
|
||||||
|
|
||||||
- name: Escaped underscore wildcards (1)
|
- name: Escaped underscore wildcards (1)
|
||||||
expression: "'a_b_c' LIKE 'a\\_b\\_c'"
|
expression: "'a_b_c' LIKE 'a\\_b\\_c'"
|
||||||
|
|
Loading…
Reference in New Issue