Skip to content

Commit

Permalink
refactor: renamed class AnalyticsQueryOperator to NegatableQueryOpera…
Browse files Browse the repository at this point in the history
…tor [DHIS2-16187]
  • Loading branch information
gnespolino committed Nov 30, 2023
1 parent d0e12c6 commit 3387fab
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
@Getter
@RequiredArgsConstructor(access = PRIVATE)
public class DimensionParamItem {
private final AnalyticsQueryOperator operator;
private final NegatableQueryOperator operator;

private final List<String> values;

Expand All @@ -59,7 +59,7 @@ public static List<DimensionParamItem> ofStrings(List<String> items) {

if (firstElement.contains(DIMENSION_NAME_SEP)) { // Has operator.
String[] parts = firstElement.split(DIMENSION_NAME_SEP);
AnalyticsQueryOperator queryOperator = getOperator(parts[0].trim());
NegatableQueryOperator queryOperator = getOperator(parts[0].trim());
return singletonList(
new DimensionParamItem(
queryOperator,
Expand All @@ -70,14 +70,14 @@ public static List<DimensionParamItem> ofStrings(List<String> items) {
}
}

private static AnalyticsQueryOperator getOperator(String operator) {
private static NegatableQueryOperator getOperator(String operator) {
if (operator.startsWith("!")) {
return getOperator(operator.substring(1)).negate();
}
return Arrays.stream(QueryOperator.values())
.filter(queryOperator -> equalsIgnoreCase(queryOperator.name(), operator))
.findFirst()
.map(AnalyticsQueryOperator::of)
.map(NegatableQueryOperator::of)
.orElseThrow(() -> new IllegalQueryException(E2035, operator));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.common.QueryOperator;

/**
* Represents a query operator that can be negated. It holds Internally a {@link QueryOperator} and
* a boolean flag that indicates if the operator is negated.
*/
@Data
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class AnalyticsQueryOperator {
public class NegatableQueryOperator {

private final QueryOperator queryOperator;
private final boolean negated;

public AnalyticsQueryOperator negate() {
return new AnalyticsQueryOperator(queryOperator, !negated);
public NegatableQueryOperator negate() {
return new NegatableQueryOperator(queryOperator, !negated);
}

public static AnalyticsQueryOperator of(QueryOperator queryOperator) {
return new AnalyticsQueryOperator(queryOperator, false);
public static NegatableQueryOperator of(QueryOperator queryOperator) {
return new NegatableQueryOperator(queryOperator, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import org.hisp.dhis.analytics.common.ValueTypeMapping;
import org.hisp.dhis.analytics.common.params.dimension.AnalyticsQueryOperator;
import org.hisp.dhis.analytics.common.params.dimension.NegatableQueryOperator;
import org.hisp.dhis.analytics.tei.query.context.sql.QueryContext;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.common.QueryOperator;
Expand All @@ -58,15 +58,15 @@
public class BinaryConditionRenderer extends BaseRenderable {
private final Renderable left;

private final AnalyticsQueryOperator analyticsQueryOperator;
private final NegatableQueryOperator negatableQueryOperator;

private final Renderable right;

public static BinaryConditionRenderer fieldsEqual(
String leftAlias, String left, String rightAlias, String right) {
return BinaryConditionRenderer.of(
Field.of(leftAlias, () -> left, EMPTY),
AnalyticsQueryOperator.of(EQ),
NegatableQueryOperator.of(EQ),
Field.of(rightAlias, () -> right, EMPTY));
}

Expand All @@ -78,24 +78,24 @@ public static BinaryConditionRenderer of(
QueryContext queryContext) {
return BinaryConditionRenderer.of(
field,
AnalyticsQueryOperator.of(queryOperator),
NegatableQueryOperator.of(queryOperator),
ConstantValuesRenderer.of(values, valueTypeMapping, queryContext));
}

public static BinaryConditionRenderer of(
Renderable field,
AnalyticsQueryOperator analyticsQueryOperator,
NegatableQueryOperator negatableQueryOperator,
List<String> values,
ValueTypeMapping valueTypeMapping,
QueryContext queryContext) {
return BinaryConditionRenderer.of(
field,
analyticsQueryOperator,
negatableQueryOperator,
ConstantValuesRenderer.of(values, valueTypeMapping, queryContext));
}

public static Renderable of(Renderable left, QueryOperator queryOperator, Renderable right) {
return BinaryConditionRenderer.of(left, AnalyticsQueryOperator.of(queryOperator), right);
return BinaryConditionRenderer.of(left, NegatableQueryOperator.of(queryOperator), right);
}

private static final Collection<QueryOperator> comparisonOperators =
Expand Down Expand Up @@ -145,11 +145,11 @@ private Renderable getCondition(QueryOperator queryOperator) {
@Nonnull
@Override
public String render() {
if (analyticsQueryOperator.isNegated()) {
return NotConditionRenderer.of(getCondition(analyticsQueryOperator.getQueryOperator()))
if (negatableQueryOperator.isNegated()) {
return NotConditionRenderer.of(getCondition(negatableQueryOperator.getQueryOperator()))
.render();
}
return getCondition(analyticsQueryOperator.getQueryOperator()).render();
return getCondition(negatableQueryOperator.getQueryOperator()).render();
}

/** This class is responsible for mapping a "like" {@link QueryOperator} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.hisp.dhis.analytics.common.ValueTypeMapping;
import org.hisp.dhis.analytics.common.params.dimension.AnalyticsQueryOperator;
import org.hisp.dhis.analytics.common.params.dimension.NegatableQueryOperator;
import org.hisp.dhis.analytics.tei.query.context.sql.QueryContext;
import org.hisp.dhis.analytics.tei.query.context.sql.SqlParameterManager;
import org.hisp.dhis.common.IllegalQueryException;
Expand All @@ -70,7 +70,7 @@ void testInWithSingleValueProduceCorrectSql() {
@Test
void testNegatedInWithSingleValueProduceCorrectSql() {
genericTestExecutor(
AnalyticsQueryOperator.of(IN).negate(),
NegatableQueryOperator.of(IN).negate(),
List.of("v1"),
ValueTypeMapping.STRING,
"not (\"field\" = :1)",
Expand Down Expand Up @@ -302,15 +302,15 @@ private void genericTestExecutor(
String expectedSql,
List<Consumer<QueryContext>> queryContextConsumers) {
genericTestExecutor(
AnalyticsQueryOperator.of(operator),
NegatableQueryOperator.of(operator),
values,
valueTypeMapping,
expectedSql,
queryContextConsumers);
}

private void genericTestExecutor(
AnalyticsQueryOperator analyticsQueryOperator,
NegatableQueryOperator negatableQueryOperator,
List<String> values,
ValueTypeMapping valueTypeMapping,
String expectedSql,
Expand All @@ -319,7 +319,7 @@ private void genericTestExecutor(
QueryContext queryContext = QueryContext.of(null, sqlParameterManager);
String render =
BinaryConditionRenderer.of(
of("field"), analyticsQueryOperator, values, valueTypeMapping, queryContext)
of("field"), negatableQueryOperator, values, valueTypeMapping, queryContext)
.render();
assertEquals(expectedSql, render);
queryContextConsumers.forEach(
Expand Down

0 comments on commit 3387fab

Please sign in to comment.