Skip to content
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

Add support for NOT, MATCHES and LIKE conditions to AqlUtil.removeParameter #657

Merged
merged 1 commit into from
Nov 27, 2024
Merged
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
43 changes: 34 additions & 9 deletions aql/src/main/java/org/ehrbase/openehr/sdk/aql/util/AqlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.ehrbase.openehr.sdk.aql.dto.AqlQuery;
import org.ehrbase.openehr.sdk.aql.dto.condition.ComparisonOperatorCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.LogicalOperatorCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.WhereCondition;
import org.ehrbase.openehr.sdk.aql.dto.condition.*;
import org.ehrbase.openehr.sdk.aql.dto.containment.AbstractContainmentExpression;
import org.ehrbase.openehr.sdk.aql.dto.containment.Containment;
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentNotOperator;
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentSetOperator;
import org.ehrbase.openehr.sdk.aql.dto.operand.LikeOperand;
import org.ehrbase.openehr.sdk.aql.dto.operand.MatchesOperand;
import org.ehrbase.openehr.sdk.aql.dto.operand.Operand;
import org.ehrbase.openehr.sdk.aql.dto.operand.QueryParameter;
import org.ehrbase.openehr.sdk.aql.dto.path.AndOperatorPredicate;
Expand Down Expand Up @@ -58,13 +58,14 @@ public static String removeParameter(String aql, String parameterName) {
}

private static WhereCondition removeParameter(WhereCondition condition, String parameterName) {
if (condition instanceof ComparisonOperatorCondition) {
Operand value = ((ComparisonOperatorCondition) condition).getValue();
if (value instanceof QueryParameter && Objects.equals(((QueryParameter) value).getName(), parameterName)) {
if (condition instanceof ComparisonOperatorCondition comparisonOperatorCondition) {
Operand value = comparisonOperatorCondition.getValue();
if (value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName)) {
return null;
}
} else if (condition instanceof LogicalOperatorCondition) {
List<WhereCondition> values = ((LogicalOperatorCondition) condition).getValues();
} else if (condition instanceof LogicalOperatorCondition logicalOperatorCondition) {
List<WhereCondition> values = logicalOperatorCondition.getValues();

for (WhereCondition value : new ArrayList<>(values)) {
values.remove(value);
Expand All @@ -80,8 +81,32 @@ private static WhereCondition removeParameter(WhereCondition condition, String p
return null;
} else if (values.size() == 1) {
return values.get(0);
}
} else if (condition instanceof NotCondition notCondition) {
var value = removeParameter(notCondition.getConditionDto(), parameterName);
if (value != null) {
notCondition.setConditionDto(value);
} else {
return condition;
return null;
}
} else if (condition instanceof MatchesCondition matchesCondition) {
List<MatchesOperand> values = matchesCondition.getValues();
values.removeIf(value -> value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName));
if (values.isEmpty()) {
return null;
}
} else if (condition instanceof LikeCondition likeCondition) {
LikeOperand value = likeCondition.getValue();
if (value instanceof QueryParameter queryParameter
&& Objects.equals(queryParameter.getName(), parameterName)) {
return null;
}
} else if (condition instanceof ExistsCondition) {
// doesn't have parameter
} else {
if (condition != null) {
throw new IllegalStateException("Unexpected condition: " + condition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,93 @@ public void removeParameterNoWhere() {
assertThat(actual).isEqualToIgnoringCase(aql);
}

@Test
public void removeParameterInNotConditionInsideAndCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE (
e/ehr_id/value = $ehrid1
AND NOT e/ehr_id/value = $ehrid2
)""";
String actual = AqlUtil.removeParameter(aql, "ehrid2");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE e/ehr_id/value = $ehrid1");
}

@Test
public void removeParameterInNotCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE NOT (
e/ehr_id/value = $ehrid1
AND e/ehr_id/value = $ehrid2
)""";
String actual = AqlUtil.removeParameter(aql, "ehrid2");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE NOT e/ehr_id/value = $ehrid1");
}

@Test
public void removeParameterInLikeCondition() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value LIKE $ehrid1
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
}

@Test
public void removeParameterInMatchesConditionMatchesIsKept() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value MATCHES { $ehrid1, $ehrid2 }
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1] WHERE e/ehr_id/value MATCHES {$ehrid2}");
}

@Test
public void removeParameterInMatchesConditionMatchesIsRemoved() {
String aql =
"""
SELECT c0 AS F1, e/ehr_id/value
FROM EHR e
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
WHERE
e/ehr_id/value MATCHES { $ehrid1 }
""";
String actual = AqlUtil.removeParameter(aql, "ehrid1");

assertThat(actual)
.isEqualToIgnoringCase(
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
}

@Test
public void containmentExpressionsByIdentifier() {
AqlQuery aqlQuery = AqlQueryParser.parse(
Expand Down