diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java index f6ac6eddb560..f00f880375ec 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractEventJdbcTableManager.java @@ -220,14 +220,14 @@ protected void populateTableInternal(AnalyticsTablePartition partition, String f } /** - * The select statement used by the table population. + * The select subquery statement. * * @param attribute the {@link TrackedEntityAttribute}. * @param columnExpression the column expression. * @param dataClause the data type related clause like "NUMERIC". * @return a select statement. */ - protected String selectForInsert( + protected String getSelectSubquery( TrackedEntityAttribute attribute, String columnExpression, String dataClause) { return replaceQualify( """ diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java index 7a451dc31086..31999aeab2e4 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEnrollmentAnalyticsTableManager.java @@ -239,7 +239,7 @@ private List getTrackedEntityAttributeColumns(Program prog if (attribute.getValueType().isOrganisationUnit()) { String fromTypeSql = "ou.name from organisationunit ou where ou.uid = (select value"; - String ouNameSql = selectForInsert(attribute, fromTypeSql, dataClause); + String ouNameSql = getSelectSubquery(attribute, fromTypeSql, dataClause); columns.add( AnalyticsTableColumn.builder() diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java index 9182cfc490d5..e28f4314b7e3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcEventAnalyticsTableManager.java @@ -421,7 +421,12 @@ private List getColumns(Program program) { return filterDimensionColumns(columns); } - protected AnalyticsTableColumn getPartitionColumn() { + /** + * Returns a partition column. + * + * @return an {@link AnalyticsTableColumn}. + */ + private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") .dataType(INTEGER) @@ -513,6 +518,13 @@ private List getColumnForDataElement( : columns; } + /** + * Returns a list of columns. + * + * @param dataElement the {@link DataElement}. + * @param dataFilterClause the data filter SQL clause. + * @return a list of {@link AnalyticsTableColumn}. + */ private List getColumnForOrgUnitDataElement( DataElement dataElement, String dataFilterClause) { List columns = new ArrayList<>(); @@ -561,12 +573,12 @@ private List getAttributeColumns(Program program) { List columns = new ArrayList<>(); columns.addAll( program.getNonConfidentialTrackedEntityAttributes().stream() - .map(tea -> getColumnForTrackedEntityAttribute(tea, false)) + .map(this::getColumnForAttribute) .flatMap(Collection::stream) .toList()); columns.addAll( program.getNonConfidentialTrackedEntityAttributesWithLegendSet().stream() - .map(tea -> getColumnForTrackedEntityAttribute(tea, true)) + .map(this::getColumnForAttributeWithLegendSet) .flatMap(Collection::stream) .toList()); return columns; @@ -579,14 +591,13 @@ private List getAttributeColumns(Program program) { * @param withLegendSet indicates whether the attribute has a legend set. * @return a list of {@link AnaylyticsTableColumn}. */ - private List getColumnForTrackedEntityAttribute( - TrackedEntityAttribute attribute, boolean withLegendSet) { + private List getColumnForAttribute(TrackedEntityAttribute attribute) { List columns = new ArrayList<>(); DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport()); String selectExpression = getSelectExpressionForAttribute(attribute.getValueType(), "value"); String dataExpression = getDataFilterClause(attribute); - String sql = selectForInsert(attribute, selectExpression, dataExpression); + String sql = getSelectSubquery(attribute, selectExpression, dataExpression); Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet()); if (attribute.getValueType().isOrganisationUnit()) { @@ -602,7 +613,7 @@ private List getColumnForTrackedEntityAttribute( .skipIndex(skipIndex) .build()); - return withLegendSet ? getColumnForAttributeWithLegendSet(attribute) : columns; + return columns; } /** @@ -663,7 +674,7 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( if (isSpatialSupport()) { String fromType = "ou.geometry " + fromClause; - String geoSql = selectForInsert(attribute, fromType, dataFilterClause); + String geoSql = getSelectSubquery(attribute, fromType, dataFilterClause); columns.add( AnalyticsTableColumn.builder() .name((attribute.getUid() + OU_GEOMETRY_COL_SUFFIX)) @@ -675,7 +686,7 @@ private List getColumnsForOrgUnitTrackedEntityAttribute( } String fromTypeSql = "ou.name " + fromClause; - String ouNameSql = selectForInsert(attribute, fromTypeSql, dataFilterClause); + String ouNameSql = getSelectSubquery(attribute, fromTypeSql, dataFilterClause); columns.add( AnalyticsTableColumn.builder() diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java index bec6c798dbba..afbfdf81dbe6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityAnalyticsTableManager.java @@ -237,15 +237,12 @@ private List getColumns( private Stream getAllTrackedEntityAttributes( TrackedEntityType trackedEntityType, Map> programsByTetUid) { - // Given TET has program(s) defined. if (programsByTetUid.containsKey(trackedEntityType.getUid())) { - // Programs defined for TET -> get attr from program and TET. return getAllTrackedEntityAttributesByPrograms( trackedEntityType, programsByTetUid.get(trackedEntityType.getUid())); } - // No programs defined for TET -> get only attributes from TET. return getAllTrackedEntityAttributesByEntityType(trackedEntityType); } @@ -297,9 +294,7 @@ private String castBasedOnType(ValueType valueType, String columnName) { private Stream getAllTrackedEntityAttributesByPrograms( TrackedEntityType trackedEntityType, List programs) { return Stream.concat( - /* all attributes of programs */ trackedEntityAttributeService.getProgramTrackedEntityAttributes(programs).stream(), - /* all attributes of the trackedEntityType */ getAllTrackedEntityAttributesByEntityType(trackedEntityType)) .distinct(); } @@ -374,8 +369,8 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti sql.append( replaceQualify( """ - \s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \ - and ${teaUid}.trackedentityattributeid = ${teaId}""", + \s left join ${trackedentityattributevalue} ${teaUid} on ${teaUid}.trackedentityid=te.trackedentityid \ + and ${teaUid}.trackedentityattributeid = ${teaId}""", Map.of( "teaUid", quote(tea.getUid()), "teaId", String.valueOf(tea.getId()))))); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java index 95a57aaf8168..07ef5fe4df1b 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEnrollmentsAnalyticsTableManager.java @@ -296,6 +296,11 @@ private List getFixedCols() { return columns; } + /** + * Returns a list of geospatial columns. + * + * @return a list of {@link AnalyticsTableColumn}. + */ private List getGeospatialCols() { return List.of( @@ -319,6 +324,11 @@ private List getGeospatialCols() { .build()); } + /** + * Returns a partition column. + * + * @return an {@link AnalyticsTableColumn}. + */ private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year") diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java index 904374494c13..f3055b6db7bc 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManager.java @@ -193,9 +193,8 @@ public class JdbcTrackedEntityEventsAnalyticsTableManager extends AbstractJdbcTa .selectExpression("ous.level") .build()); - private static final String AND = " and ("; - private final TrackedEntityTypeService trackedEntityTypeService; + private final AnalyticsSqlBuilder analyticsSqlBuilder; public JdbcTrackedEntityEventsAnalyticsTableManager( @@ -340,7 +339,8 @@ private List getDataYears(AnalyticsTableUpdateParams params, TrackedEnt "tetId", String.valueOf(tet.getId())))); if (params.getFromDate() != null) { - sql.append(AND + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'"); + sql.append( + " and (" + eventDateExpression + ") >= '" + toMediumDate(params.getFromDate()) + "'"); } List availableDataYears = @@ -451,6 +451,11 @@ private String getPartitionClause(AnalyticsTablePartition partition) { : emptyIfTrue(partitionFilter, sqlBuilder.supportsDeclarativePartitioning()); } + /** + * Returns a partition column. + * + * @return an {@link AnalyticsTableColumn}. + */ private AnalyticsTableColumn getPartitionColumn() { return AnalyticsTableColumn.builder() .name("year")