Skip to content

Commit

Permalink
[ISSUE #6916] support startsWith and endsWith in sql filter. (#6915)
Browse files Browse the repository at this point in the history
* support startsWith and endsWith in sql filter.

* add tests

* add tests
  • Loading branch information
yuz10 authored Jun 23, 2023
1 parent 9899be1 commit 867c296
Show file tree
Hide file tree
Showing 6 changed files with 833 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,174 @@ public static BooleanExpression createNotContains(Expression left, String search
return new NotContainsExpression(left, search);
}

static class StartsWithExpression extends UnaryExpression implements BooleanExpression {

String search;

public StartsWithExpression(Expression right, String search) {
super(right);
this.search = search;
}

public String getExpressionSymbol() {
return "STARTSWITH";
}

public Object evaluate(EvaluationContext message) throws Exception {

if (search == null || search.length() == 0) {
return Boolean.FALSE;
}

Object rv = this.getRight().evaluate(message);

if (rv == null) {
return Boolean.FALSE;
}

if (!(rv instanceof String)) {
return Boolean.FALSE;
}

return ((String)rv).startsWith(search) ? Boolean.TRUE : Boolean.FALSE;
}

public boolean matches(EvaluationContext message) throws Exception {
Object object = evaluate(message);
return object != null && object == Boolean.TRUE;
}
}

static class NotStartsWithExpression extends UnaryExpression implements BooleanExpression {

String search;

public NotStartsWithExpression(Expression right, String search) {
super(right);
this.search = search;
}

public String getExpressionSymbol() {
return "NOT STARTSWITH";
}

public Object evaluate(EvaluationContext message) throws Exception {

if (search == null || search.length() == 0) {
return Boolean.FALSE;
}

Object rv = this.getRight().evaluate(message);

if (rv == null) {
return Boolean.FALSE;
}

if (!(rv instanceof String)) {
return Boolean.FALSE;
}

return ((String)rv).startsWith(search) ? Boolean.FALSE : Boolean.TRUE;
}

public boolean matches(EvaluationContext message) throws Exception {
Object object = evaluate(message);
return object != null && object == Boolean.TRUE;
}
}

public static BooleanExpression createStartsWith(Expression left, String search) {
return new StartsWithExpression(left, search);
}

public static BooleanExpression createNotStartsWith(Expression left, String search) {
return new NotStartsWithExpression(left, search);
}

static class EndsWithExpression extends UnaryExpression implements BooleanExpression {

String search;

public EndsWithExpression(Expression right, String search) {
super(right);
this.search = search;
}

public String getExpressionSymbol() {
return "ENDSWITH";
}

public Object evaluate(EvaluationContext message) throws Exception {

if (search == null || search.length() == 0) {
return Boolean.FALSE;
}

Object rv = this.getRight().evaluate(message);

if (rv == null) {
return Boolean.FALSE;
}

if (!(rv instanceof String)) {
return Boolean.FALSE;
}

return ((String)rv).endsWith(search) ? Boolean.TRUE : Boolean.FALSE;
}

public boolean matches(EvaluationContext message) throws Exception {
Object object = evaluate(message);
return object != null && object == Boolean.TRUE;
}
}

static class NotEndsWithExpression extends UnaryExpression implements BooleanExpression {

String search;

public NotEndsWithExpression(Expression right, String search) {
super(right);
this.search = search;
}

public String getExpressionSymbol() {
return "NOT ENDSWITH";
}

public Object evaluate(EvaluationContext message) throws Exception {

if (search == null || search.length() == 0) {
return Boolean.FALSE;
}

Object rv = this.getRight().evaluate(message);

if (rv == null) {
return Boolean.FALSE;
}

if (!(rv instanceof String)) {
return Boolean.FALSE;
}

return ((String)rv).endsWith(search) ? Boolean.FALSE : Boolean.TRUE;
}

public boolean matches(EvaluationContext message) throws Exception {
Object object = evaluate(message);
return object != null && object == Boolean.TRUE;
}
}

public static BooleanExpression createEndsWith(Expression left, String search) {
return new EndsWithExpression(left, search);
}

public static BooleanExpression createNotEndsWith(Expression left, String search) {
return new NotEndsWithExpression(left, search);
}

@SuppressWarnings({"rawtypes", "unchecked"})
public static BooleanExpression createInFilter(Expression left, List elements) {

Expand Down
Loading

0 comments on commit 867c296

Please sign in to comment.