Skip to content

Commit 6256f26

Browse files
committed
Add support for NOT, MATCHES and LIKE conditions to AqlUtil.removeParameter
1 parent 888b0f7 commit 6256f26

File tree

2 files changed

+121
-9
lines changed

2 files changed

+121
-9
lines changed

aql/src/main/java/org/ehrbase/openehr/sdk/aql/util/AqlUtil.java

+34-9
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@
2424
import java.util.stream.Collectors;
2525
import java.util.stream.Stream;
2626
import org.ehrbase.openehr.sdk.aql.dto.AqlQuery;
27-
import org.ehrbase.openehr.sdk.aql.dto.condition.ComparisonOperatorCondition;
28-
import org.ehrbase.openehr.sdk.aql.dto.condition.LogicalOperatorCondition;
29-
import org.ehrbase.openehr.sdk.aql.dto.condition.WhereCondition;
27+
import org.ehrbase.openehr.sdk.aql.dto.condition.*;
3028
import org.ehrbase.openehr.sdk.aql.dto.containment.AbstractContainmentExpression;
3129
import org.ehrbase.openehr.sdk.aql.dto.containment.Containment;
3230
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentNotOperator;
3331
import org.ehrbase.openehr.sdk.aql.dto.containment.ContainmentSetOperator;
32+
import org.ehrbase.openehr.sdk.aql.dto.operand.LikeOperand;
33+
import org.ehrbase.openehr.sdk.aql.dto.operand.MatchesOperand;
3434
import org.ehrbase.openehr.sdk.aql.dto.operand.Operand;
3535
import org.ehrbase.openehr.sdk.aql.dto.operand.QueryParameter;
3636
import org.ehrbase.openehr.sdk.aql.dto.path.AndOperatorPredicate;
@@ -58,13 +58,14 @@ public static String removeParameter(String aql, String parameterName) {
5858
}
5959

6060
private static WhereCondition removeParameter(WhereCondition condition, String parameterName) {
61-
if (condition instanceof ComparisonOperatorCondition) {
62-
Operand value = ((ComparisonOperatorCondition) condition).getValue();
63-
if (value instanceof QueryParameter && Objects.equals(((QueryParameter) value).getName(), parameterName)) {
61+
if (condition instanceof ComparisonOperatorCondition comparisonOperatorCondition) {
62+
Operand value = comparisonOperatorCondition.getValue();
63+
if (value instanceof QueryParameter queryParameter
64+
&& Objects.equals(queryParameter.getName(), parameterName)) {
6465
return null;
6566
}
66-
} else if (condition instanceof LogicalOperatorCondition) {
67-
List<WhereCondition> values = ((LogicalOperatorCondition) condition).getValues();
67+
} else if (condition instanceof LogicalOperatorCondition logicalOperatorCondition) {
68+
List<WhereCondition> values = logicalOperatorCondition.getValues();
6869

6970
for (WhereCondition value : new ArrayList<>(values)) {
7071
values.remove(value);
@@ -80,8 +81,32 @@ private static WhereCondition removeParameter(WhereCondition condition, String p
8081
return null;
8182
} else if (values.size() == 1) {
8283
return values.get(0);
84+
}
85+
} else if (condition instanceof NotCondition notCondition) {
86+
var value = removeParameter(notCondition.getConditionDto(), parameterName);
87+
if (value != null) {
88+
notCondition.setConditionDto(value);
8389
} else {
84-
return condition;
90+
return null;
91+
}
92+
} else if (condition instanceof MatchesCondition matchesCondition) {
93+
List<MatchesOperand> values = matchesCondition.getValues();
94+
values.removeIf(value -> value instanceof QueryParameter queryParameter
95+
&& Objects.equals(queryParameter.getName(), parameterName));
96+
if (values.isEmpty()) {
97+
return null;
98+
}
99+
} else if (condition instanceof LikeCondition likeCondition) {
100+
LikeOperand value = likeCondition.getValue();
101+
if (value instanceof QueryParameter queryParameter
102+
&& Objects.equals(queryParameter.getName(), parameterName)) {
103+
return null;
104+
}
105+
} else if (condition instanceof ExistsCondition) {
106+
// doesn't have parameter
107+
} else {
108+
if (condition != null) {
109+
throw new IllegalStateException("Unexpected condition: " + condition);
85110
}
86111
}
87112

aql/src/test/java/org/ehrbase/openehr/sdk/aql/util/AqlUtilTest.java

+87
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,93 @@ public void removeParameterNoWhere() {
154154
assertThat(actual).isEqualToIgnoringCase(aql);
155155
}
156156

157+
@Test
158+
public void removeParameterInNotConditionInsideAndCondition() {
159+
String aql =
160+
"""
161+
SELECT c0 AS F1, e/ehr_id/value
162+
FROM EHR e
163+
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
164+
WHERE (
165+
e/ehr_id/value = $ehrid1
166+
AND NOT e/ehr_id/value = $ehrid2
167+
)""";
168+
String actual = AqlUtil.removeParameter(aql, "ehrid2");
169+
170+
assertThat(actual)
171+
.isEqualToIgnoringCase(
172+
"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");
173+
}
174+
175+
@Test
176+
public void removeParameterInNotCondition() {
177+
String aql =
178+
"""
179+
SELECT c0 AS F1, e/ehr_id/value
180+
FROM EHR e
181+
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
182+
WHERE NOT (
183+
e/ehr_id/value = $ehrid1
184+
AND e/ehr_id/value = $ehrid2
185+
)""";
186+
String actual = AqlUtil.removeParameter(aql, "ehrid2");
187+
188+
assertThat(actual)
189+
.isEqualToIgnoringCase(
190+
"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");
191+
}
192+
193+
@Test
194+
public void removeParameterInLikeCondition() {
195+
String aql =
196+
"""
197+
SELECT c0 AS F1, e/ehr_id/value
198+
FROM EHR e
199+
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
200+
WHERE
201+
e/ehr_id/value LIKE $ehrid1
202+
""";
203+
String actual = AqlUtil.removeParameter(aql, "ehrid1");
204+
205+
assertThat(actual)
206+
.isEqualToIgnoringCase(
207+
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
208+
}
209+
210+
@Test
211+
public void removeParameterInMatchesConditionMatchesIsKept() {
212+
String aql =
213+
"""
214+
SELECT c0 AS F1, e/ehr_id/value
215+
FROM EHR e
216+
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
217+
WHERE
218+
e/ehr_id/value MATCHES { $ehrid1, $ehrid2 }
219+
""";
220+
String actual = AqlUtil.removeParameter(aql, "ehrid1");
221+
222+
assertThat(actual)
223+
.isEqualToIgnoringCase(
224+
"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}");
225+
}
226+
227+
@Test
228+
public void removeParameterInMatchesConditionMatchesIsRemoved() {
229+
String aql =
230+
"""
231+
SELECT c0 AS F1, e/ehr_id/value
232+
FROM EHR e
233+
CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]
234+
WHERE
235+
e/ehr_id/value MATCHES { $ehrid1 }
236+
""";
237+
String actual = AqlUtil.removeParameter(aql, "ehrid1");
238+
239+
assertThat(actual)
240+
.isEqualToIgnoringCase(
241+
"SELECT c0 AS F1, e/ehr_id/value FROM EHR e CONTAINS COMPOSITION c0[openEHR-EHR-COMPOSITION.report.v1]");
242+
}
243+
157244
@Test
158245
public void containmentExpressionsByIdentifier() {
159246
AqlQuery aqlQuery = AqlQueryParser.parse(

0 commit comments

Comments
 (0)