diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java index 5e29d96d017..53ea7026f6f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/JdbcEnrollmentAnalyticsManager.java @@ -153,9 +153,6 @@ public void getEnrollments(EventQueryParams params, Grid grid, int maxLimit) { String sql; if (params.isAggregatedEnrollments()) { sql = getAggregatedEnrollmentsSql(grid.getHeaders(), params); - } else if (!params.getItems().isEmpty() && shouldUseCTE(params)) { - // TODO no longer needed - sql = buildEnrollmentQueryWithCTE(params, params.getItems().get(0)); } else { sql = buildEnrollmentQueryWithCte2(params); } @@ -171,14 +168,6 @@ public void getEnrollments(EventQueryParams params, Grid grid, int maxLimit) { } } - private boolean shouldUseCTE(EventQueryParams params) { - if (params.getItems().isEmpty()) { - return false; - } - QueryItem item = params.getItems().get(0); - return item.hasProgram() && item.hasProgramStage(); - } - /** * Adds enrollments to the given grid based on the given parameters and SQL statement. * @@ -330,7 +319,7 @@ public long getEnrollmentCount(EventQueryParams params) { withExceptionHandling( () -> jdbcTemplate.queryForObject(finalSqlValue, Long.class), params.isMultipleQueries()) - .orElse(0l); + .orElse(0L); } return count; @@ -1182,47 +1171,6 @@ private String resolveOrderByOffset(int offset) { return "asc"; } - private String buildEnrollmentQueryWithCTE(EventQueryParams params, QueryItem item) { - - StringBuilder sql = new StringBuilder(); - List items = params.getItems(); - - // 1. Build CTEs for each unique program stage + offset combination - sql.append(buildAllRankedEventsCTEs(items)); - - // 2. Build main query - sql.append("select "); - - // 2.1 Add basic columns - sql.append(getBasicSelectColumns()); - - // 2.2 Add value columns for each item - String valueColumns = buildValueColumns(items); - if (!valueColumns.isEmpty()) { - sql.append(", ").append(valueColumns); - } - - // 2.3 Add FROM clause with all necessary joins - sql.append(buildFromClauseWithJoins(params, items)); - - // 2.4 Add WHERE clause - String whereClause = getWhereClause(params).trim(); - if (!whereClause.isEmpty()) { - // Remove any leading "and" and ensure only one "where" keyword - whereClause = whereClause.replaceFirst("^and\\s+", ""); - whereClause = whereClause.replaceFirst("^where\\s+", ""); - if (!whereClause.isEmpty()) { - sql.append(" where ").append(whereClause); - } - } - - // 2.5 Add ORDER BY and paging - sql.append(getSortClause(params)); - sql.append(getPagingClause(params, 5000)); - - return sql.toString(); - } - private String buildAllRankedEventsCTEs(List items) { StringBuilder ctes = new StringBuilder(); Set processedCombinations = new HashSet<>();