Skip to content

Commit

Permalink
chore: propagate SqlBuilder to Antlr Expression visitor [DHIS-16705] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
luciano-fiandesio authored Dec 3, 2024
1 parent 96ee381 commit 8050437
Show file tree
Hide file tree
Showing 17 changed files with 297 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@
/** Provides methods targeting the generation of SQL statements for periods and time fields. */
public abstract class TimeFieldSqlRenderer {
protected final SqlBuilder sqlBuilder;
protected final StatementBuilder statementBuilder = new DefaultStatementBuilder();
protected final StatementBuilder statementBuilder;

protected TimeFieldSqlRenderer(SqlBuilder sqlBuilder) {
this.sqlBuilder = sqlBuilder;
this.statementBuilder = new DefaultStatementBuilder(sqlBuilder);
}

/**
Expand Down Expand Up @@ -173,7 +174,7 @@ private void collectDateRangeSqlConditions(
/**
* Returns a string representing the SQL condition for the given {@link ColumnWithDateRange}.
*
* @param dateRangeColumn
* @param dateRangeColumn the {@link ColumnWithDateRange}
* @return the SQL statement
*/
private String getDateRangeCondition(ColumnWithDateRange dateRangeColumn) {
Expand Down
4 changes: 4 additions & 0 deletions dhis-2/dhis-services/dhis-service-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
<groupId>org.hisp.dhis</groupId>
<artifactId>dhis-support-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.hisp.dhis</groupId>
<artifactId>dhis-support-sql</artifactId>
</dependency>
<!-- Other -->
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
import org.hisp.dhis.constant.Constant;
import org.hisp.dhis.constant.ConstantService;
import org.hisp.dhis.dataset.DataSet;
import org.hisp.dhis.db.sql.SqlBuilder;
import org.hisp.dhis.expression.dataitem.DimItemDataElementAndOperand;
import org.hisp.dhis.expression.dataitem.DimItemIndicator;
import org.hisp.dhis.expression.dataitem.DimItemProgramAttribute;
Expand Down Expand Up @@ -179,6 +180,8 @@ public class DefaultExpressionService implements ExpressionService {

private final I18nManager i18nManager;

private final SqlBuilder sqlBuilder;

// -------------------------------------------------------------------------
// Static data
// -------------------------------------------------------------------------
Expand Down Expand Up @@ -279,20 +282,23 @@ public DefaultExpressionService(
DimensionService dimensionService,
IdentifiableObjectManager idObjectManager,
I18nManager i18nManager,
CacheProvider cacheProvider) {
CacheProvider cacheProvider,
SqlBuilder sqlBuilder) {
checkNotNull(expressionStore);
checkNotNull(constantService);
checkNotNull(dimensionService);
checkNotNull(idObjectManager);
checkNotNull(i18nManager);
checkNotNull(cacheProvider);
checkNotNull(sqlBuilder);

this.expressionStore = expressionStore;
this.constantService = constantService;
this.dimensionService = dimensionService;
this.idObjectManager = idObjectManager;
this.i18nManager = i18nManager;
this.constantMapCache = cacheProvider.createAllConstantsCache();
this.sqlBuilder = sqlBuilder;
}

// -------------------------------------------------------------------------
Expand Down Expand Up @@ -726,6 +732,7 @@ private CommonExpressionVisitor newVisitor(
.params(params)
.info(params.getExpressionInfo())
.state(initialParsingState)
.sqlBuilder(sqlBuilder)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,7 @@
import static org.hisp.dhis.expression.ExpressionParams.DEFAULT_EXPRESSION_PARAMS;
import static org.hisp.dhis.parser.expression.ExpressionItem.ITEM_GET_DESCRIPTIONS;
import static org.hisp.dhis.parser.expression.ExpressionItem.ITEM_GET_SQL;
import static org.hisp.dhis.parser.expression.ParserUtils.COMMON_EXPRESSION_ITEMS;
import static org.hisp.dhis.parser.expression.ProgramExpressionParams.DEFAULT_PROGRAM_EXPRESSION_PARAMS;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.AVG;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.A_BRACE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.COUNT;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_CONDITION;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_COUNT;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_COUNT_IF_CONDITION;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_COUNT_IF_VALUE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_DAYS_BETWEEN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_HAS_VALUE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_MAX_VALUE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_MINUTES_BETWEEN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_MIN_VALUE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_MONTHS_BETWEEN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_OIZP;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_RELATIONSHIP_COUNT;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_WEEKS_BETWEEN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_YEARS_BETWEEN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_ZING;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.D2_ZPVC;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.HASH_BRACE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.MAX;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.MIN;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.PS_EVENTDATE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.STAGE_OFFSET;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.STDDEV;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.SUM;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.VARIANCE;
import static org.hisp.dhis.parser.expression.antlr.ExpressionParser.V_BRACE;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
Expand All @@ -74,6 +45,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.hisp.dhis.analytics.DataType;
import org.hisp.dhis.antlr.Parser;
Expand All @@ -84,42 +56,15 @@
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.common.IdentifiableObjectStore;
import org.hisp.dhis.commons.util.TextUtils;
import org.hisp.dhis.db.sql.SqlBuilder;
import org.hisp.dhis.expression.ExpressionParams;
import org.hisp.dhis.expression.ExpressionService;
import org.hisp.dhis.i18n.I18nManager;
import org.hisp.dhis.parser.expression.CommonExpressionVisitor;
import org.hisp.dhis.parser.expression.ExpressionItem;
import org.hisp.dhis.parser.expression.ExpressionItemMethod;
import org.hisp.dhis.parser.expression.ProgramExpressionParams;
import org.hisp.dhis.parser.expression.function.RepeatableProgramStageOffset;
import org.hisp.dhis.parser.expression.function.VectorAvg;
import org.hisp.dhis.parser.expression.function.VectorCount;
import org.hisp.dhis.parser.expression.function.VectorMax;
import org.hisp.dhis.parser.expression.function.VectorMin;
import org.hisp.dhis.parser.expression.function.VectorStddevSamp;
import org.hisp.dhis.parser.expression.function.VectorSum;
import org.hisp.dhis.parser.expression.function.VectorVariance;
import org.hisp.dhis.parser.expression.literal.SqlLiteral;
import org.hisp.dhis.program.dataitem.ProgramItemAttribute;
import org.hisp.dhis.program.dataitem.ProgramItemPsEventdate;
import org.hisp.dhis.program.dataitem.ProgramItemStageElement;
import org.hisp.dhis.program.function.D2Condition;
import org.hisp.dhis.program.function.D2Count;
import org.hisp.dhis.program.function.D2CountIfCondition;
import org.hisp.dhis.program.function.D2CountIfValue;
import org.hisp.dhis.program.function.D2DaysBetween;
import org.hisp.dhis.program.function.D2HasValue;
import org.hisp.dhis.program.function.D2MaxValue;
import org.hisp.dhis.program.function.D2MinValue;
import org.hisp.dhis.program.function.D2MinutesBetween;
import org.hisp.dhis.program.function.D2MonthsBetween;
import org.hisp.dhis.program.function.D2Oizp;
import org.hisp.dhis.program.function.D2RelationshipCount;
import org.hisp.dhis.program.function.D2WeeksBetween;
import org.hisp.dhis.program.function.D2YearsBetween;
import org.hisp.dhis.program.function.D2Zing;
import org.hisp.dhis.program.function.D2Zpvc;
import org.hisp.dhis.program.variable.ProgramVariableItem;
import org.hisp.dhis.system.util.SqlUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
Expand All @@ -146,6 +91,10 @@ public class DefaultProgramIndicatorService implements ProgramIndicatorService {

private final Cache<String> analyticsSqlCache;

private final SqlBuilder sqlBuilder;

@Getter private final ImmutableMap<Integer, ExpressionItem> programIndicatorItems;

public DefaultProgramIndicatorService(
ProgramIndicatorStore programIndicatorStore,
@Qualifier("org.hisp.dhis.program.ProgramIndicatorGroupStore")
Expand All @@ -155,7 +104,8 @@ public DefaultProgramIndicatorService(
ExpressionService expressionService,
DimensionService dimensionService,
I18nManager i18nManager,
CacheProvider cacheProvider) {
CacheProvider cacheProvider,
SqlBuilder sqlBuilder) {
checkNotNull(programIndicatorStore);
checkNotNull(programIndicatorGroupStore);
checkNotNull(programStageService);
Expand All @@ -164,6 +114,7 @@ public DefaultProgramIndicatorService(
checkNotNull(dimensionService);
checkNotNull(i18nManager);
checkNotNull(cacheProvider);
checkNotNull(sqlBuilder);

this.programIndicatorStore = programIndicatorStore;
this.programIndicatorGroupStore = programIndicatorGroupStore;
Expand All @@ -173,57 +124,10 @@ public DefaultProgramIndicatorService(
this.dimensionService = dimensionService;
this.i18nManager = i18nManager;
this.analyticsSqlCache = cacheProvider.createAnalyticsSqlCache();
}

public static final ImmutableMap<Integer, ExpressionItem> PROGRAM_INDICATOR_ITEMS =
ImmutableMap.<Integer, ExpressionItem>builder()

// Common functions

.putAll(COMMON_EXPRESSION_ITEMS)

// Program functions

.put(D2_CONDITION, new D2Condition())
.put(D2_COUNT, new D2Count())
.put(D2_COUNT_IF_CONDITION, new D2CountIfCondition())
.put(D2_COUNT_IF_VALUE, new D2CountIfValue())
.put(D2_DAYS_BETWEEN, new D2DaysBetween())
.put(D2_HAS_VALUE, new D2HasValue())
.put(D2_MAX_VALUE, new D2MaxValue())
.put(D2_MINUTES_BETWEEN, new D2MinutesBetween())
.put(D2_MIN_VALUE, new D2MinValue())
.put(D2_MONTHS_BETWEEN, new D2MonthsBetween())
.put(D2_OIZP, new D2Oizp())
.put(D2_RELATIONSHIP_COUNT, new D2RelationshipCount())
.put(D2_WEEKS_BETWEEN, new D2WeeksBetween())
.put(D2_YEARS_BETWEEN, new D2YearsBetween())
.put(D2_ZING, new D2Zing())
.put(D2_ZPVC, new D2Zpvc())
this.sqlBuilder = sqlBuilder;

// Program functions for custom aggregation

.put(AVG, new VectorAvg())
.put(COUNT, new VectorCount())
.put(MAX, new VectorMax())
.put(MIN, new VectorMin())
.put(STDDEV, new VectorStddevSamp())
.put(SUM, new VectorSum())
.put(VARIANCE, new VectorVariance())

// Data items

.put(HASH_BRACE, new ProgramItemStageElement())
.put(A_BRACE, new ProgramItemAttribute())
.put(PS_EVENTDATE, new ProgramItemPsEventdate())

// Program variables

.put(V_BRACE, new ProgramVariableItem())

// . functions
.put(STAGE_OFFSET, new RepeatableProgramStageOffset())
.build();
this.programIndicatorItems = new ExpressionMapBuilder(sqlBuilder).getExpressionItemMap();
}

// -------------------------------------------------------------------------
// ProgramIndicator CRUD
Expand Down Expand Up @@ -518,10 +422,11 @@ private CommonExpressionVisitor newVisitor(
.programStageService(programStageService)
.i18nSupplier(Suppliers.memoize(i18nManager::getI18n))
.constantMap(expressionService.getConstantMap())
.itemMap(PROGRAM_INDICATOR_ITEMS)
.itemMap(programIndicatorItems)
.itemMethod(itemMethod)
.params(params)
.progParams(progParams)
.sqlBuilder(sqlBuilder)
.build();
}

Expand Down
Loading

0 comments on commit 8050437

Please sign in to comment.