diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java index 9f53bfd51add..55a58883bd63 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/event/data/AbstractJdbcEventAnalyticsManager.java @@ -49,8 +49,8 @@ import static org.hisp.dhis.analytics.event.data.EnrollmentQueryHelper.getHeaderColumns; import static org.hisp.dhis.analytics.event.data.EnrollmentQueryHelper.getOrgUnitLevelColumns; import static org.hisp.dhis.analytics.event.data.EnrollmentQueryHelper.getPeriodColumns; -import static org.hisp.dhis.analytics.table.JdbcEventAnalyticsTableManager.OU_GEOMETRY_COL_SUFFIX; -import static org.hisp.dhis.analytics.table.JdbcEventAnalyticsTableManager.OU_NAME_COL_SUFFIX; +import static org.hisp.dhis.analytics.table.AbstractEventJdbcTableManager.OU_GEOMETRY_COL_SUFFIX; +import static org.hisp.dhis.analytics.table.AbstractEventJdbcTableManager.OU_NAME_COL_SUFFIX; import static org.hisp.dhis.analytics.util.AnalyticsUtils.replaceStringBetween; import static org.hisp.dhis.analytics.util.AnalyticsUtils.throwIllegalQueryEx; import static org.hisp.dhis.analytics.util.AnalyticsUtils.withExceptionHandling; @@ -375,8 +375,9 @@ private void addDimensionSelectColumns( singleQuote(period.getIsoDate()) + " as " + period.getPeriodType().getName()); } else { throw new IllegalStateException( - "Program indicator non-default boundary query must have " - + "exactly one period, or no periods and a period filter"); + """ + Program indicator non-default boundary query must have \" + exactly one period, or no periods and a period filter"""); } }); } @@ -455,7 +456,6 @@ private ColumnAndAlias getColumnAndAlias( } else if (queryItem.getValueType() == ValueType.NUMBER && !isGroupByClause) { ColumnAndAlias columnAndAlias = getColumnAndAlias(queryItem, isAggregated, queryItem.getItemName()); - return ColumnAndAlias.ofColumnAndAlias( columnAndAlias.getColumn(), defaultIfNull(columnAndAlias.getAlias(), queryItem.getItemName())); @@ -532,14 +532,10 @@ protected Optional getAlias(QueryItem queryItem) { @Transactional(readOnly = true, propagation = REQUIRES_NEW) public Grid getAggregatedEventData(EventQueryParams params, Grid grid, int maxLimit) { String aggregateClause = getAggregateClause(params); + String columns = StringUtils.join(getSelectColumns(params, true), ","); String sql = - TextUtils.removeLastComma( - "select " - + aggregateClause - + " as value," - + StringUtils.join(getSelectColumns(params, true), ",") - + " "); + TextUtils.removeLastComma("select " + aggregateClause + " as value," + columns + " "); // --------------------------------------------------------------------- // Criteria diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java index 537b473493e5..fa49efb4500f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/model/AnalyticsTableColumn.java @@ -80,10 +80,6 @@ public class AnalyticsTableColumn { /** Date of creation of the underlying data dimension. */ private final Date created; - // // ------------------------------------------------------------------------- - // // Logic - // // ------------------------------------------------------------------------- - /** Indicates whether this column is not null. */ public boolean isNotNull() { return Nullable.NOT_NULL == nullable; diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryBuilders.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryBuilders.java index e9768189d49c..23ae04e3abd6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryBuilders.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/query/context/sql/SqlQueryBuilders.java @@ -61,22 +61,22 @@ select json_agg(json_build_object( // incidentDate is kept to support a deprecated field, will be removed when FE will only // use occurreddate """ - select json_agg( - json_build_object( - 'programUid', en.program, - 'enrollmentUid', en.enrollment, - 'enrollmentDate', en.enrollmentdate, - 'incidentDate', en.occurreddate, - 'occurredDate', en.occurreddate, - 'completedDate', en.completeddate, - 'orgUnitUid', en.ou, - 'orgUnitName', en.ouname, - 'orgUnitCode', en.oucode, - 'orgUnitNameHierarchy', en.ounamehierarchy, - 'enrollmentStatus', en.enrollmentstatus, - 'events', ${eventQuery})) - from analytics_te_enrollment_${trackedEntityType} en - where en.trackedentity = t_1.trackedentity""", + select json_agg( + json_build_object( + 'programUid', en.program, + 'enrollmentUid', en.enrollment, + 'enrollmentDate', en.enrollmentdate, + 'incidentDate', en.occurreddate, + 'occurredDate', en.occurreddate, + 'completedDate', en.completeddate, + 'orgUnitUid', en.ou, + 'orgUnitName', en.ouname, + 'orgUnitCode', en.oucode, + 'orgUnitNameHierarchy', en.ounamehierarchy, + 'enrollmentStatus', en.enrollmentstatus, + 'events', ${eventQuery})) + from analytics_te_enrollment_${trackedEntityType} en + where en.trackedentity = t_1.trackedentity""", Map.of("eventQuery", coalesceToEmptyArray(EVENT_QUERY))); private static final String JSON_AGGREGATION_QUERY = coalesceToEmptyArray(ENROLLMENT_QUERY); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/DisplayNameUtils.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/DisplayNameUtils.java index 5377f0807253..b7f47fbce85d 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/DisplayNameUtils.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/util/DisplayNameUtils.java @@ -124,22 +124,22 @@ public static String getDisplayName( isNotEmpty.apply(surname), isNotEmpty.apply(firstName), isEmpty.apply(username), - formatNames(sqlBuilder, surname, "', '", firstName), + sqlBuilder.concat(surname, "', '", firstName), // FirstName and Username isEmpty.apply(surname), isNotEmpty.apply(firstName), isNotEmpty.apply(username), - formatNames(sqlBuilder, firstName, "' ('", username, "')'"), + sqlBuilder.concat(firstName, "' ('", username, "')'"), // Surname and Username isNotEmpty.apply(surname), isEmpty.apply(firstName), isNotEmpty.apply(username), - formatNames(sqlBuilder, surname, "' ('", username, "')'"), + sqlBuilder.concat(surname, "' ('", username, "')'"), // All fields - formatNames(sqlBuilder, surname, "', '", firstName, "' ('", username, "')'"), + sqlBuilder.concat(surname, "', '", firstName, "' ('", username, "')'"), columnAlias); } @@ -149,8 +149,4 @@ private static String extractJsonValue( String jsonExtracted = sqlBuilder.jsonExtract(json, path); return sqlBuilder.trim(jsonExtracted); } - - private static String formatNames(SqlBuilder sqlBuilder, String... elements) { - return sqlBuilder.concat(elements); - } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java index 57fce6c172ac..bc36af519d40 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/table/JdbcTrackedEntityEventsAnalyticsTableManagerTest.java @@ -52,8 +52,8 @@ import org.hisp.dhis.category.CategoryService; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.dataapproval.DataApprovalLevelService; +import org.hisp.dhis.db.sql.PostgreSqlAnalyticsSqlBuilder; import org.hisp.dhis.db.sql.PostgreSqlBuilder; -import org.hisp.dhis.db.sql.PostgresAnalyticsSqlBuilder; import org.hisp.dhis.organisationunit.OrganisationUnitService; import org.hisp.dhis.period.PeriodDataProvider; import org.hisp.dhis.resourcetable.ResourceTableService; @@ -82,7 +82,7 @@ class JdbcTrackedEntityEventsAnalyticsTableManagerTest { @Spy private PostgreSqlBuilder sqlBuilder; - @Spy private PostgresAnalyticsSqlBuilder analyticsSqlBuilder; + @Spy private PostgreSqlAnalyticsSqlBuilder analyticsSqlBuilder; @Mock private PartitionManager partitionManager; diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/AnalyticsSqlBuilderProvider.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/AnalyticsSqlBuilderProvider.java index 9f7aac27027c..3c6728876f0d 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/AnalyticsSqlBuilderProvider.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/AnalyticsSqlBuilderProvider.java @@ -33,7 +33,7 @@ import org.hisp.dhis.db.sql.AnalyticsSqlBuilder; import org.hisp.dhis.db.sql.ClickhouseAnalyticsSqlBuilder; import org.hisp.dhis.db.sql.DorisAnalyticsSqlBuilder; -import org.hisp.dhis.db.sql.PostgresAnalyticsSqlBuilder; +import org.hisp.dhis.db.sql.PostgreSqlAnalyticsSqlBuilder; import org.hisp.dhis.external.conf.DhisConfigurationProvider; import org.springframework.stereotype.Service; @@ -70,7 +70,7 @@ private AnalyticsSqlBuilder getSqlBuilder(SqlBuilderSettings config) { return switch (database) { case DORIS -> new DorisAnalyticsSqlBuilder(); case CLICKHOUSE -> new ClickhouseAnalyticsSqlBuilder(); - default -> new PostgresAnalyticsSqlBuilder(); + default -> new PostgreSqlAnalyticsSqlBuilder(); }; } } diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgresAnalyticsSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlAnalyticsSqlBuilder.java similarity index 97% rename from dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgresAnalyticsSqlBuilder.java rename to dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlAnalyticsSqlBuilder.java index 1e1e5dd5fbea..115b7b022c30 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgresAnalyticsSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlAnalyticsSqlBuilder.java @@ -27,7 +27,7 @@ */ package org.hisp.dhis.db.sql; -public class PostgresAnalyticsSqlBuilder implements AnalyticsSqlBuilder { +public class PostgreSqlAnalyticsSqlBuilder implements AnalyticsSqlBuilder { /** * Returns a subquery that expand the event datavalue jsonb with two additional fields: diff --git a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java index 92a14270f8cc..df43166bd281 100644 --- a/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java +++ b/dhis-2/dhis-support/dhis-support-sql/src/main/java/org/hisp/dhis/db/sql/PostgreSqlBuilder.java @@ -278,6 +278,8 @@ public String dateDifference(String startDate, String endDate, DateUnit dateUnit String.format( "(extract(epoch from (cast(%s as timestamp) - cast(%s as timestamp))) / 60)", endDate, startDate); + case WEEKS -> + String.format("((cast(%s as date) - cast(%s as date)) / 7)", endDate, startDate); case MONTHS -> String.format( "((date_part('year',age(cast(%s as date), cast(%s as date)))) * 12 + " @@ -286,8 +288,6 @@ public String dateDifference(String startDate, String endDate, DateUnit dateUnit case YEARS -> String.format( "(date_part('year',age(cast(%s as date), cast(%s as date))))", endDate, startDate); - case WEEKS -> - String.format("((cast(%s as date) - cast(%s as date)) / 7)", endDate, startDate); }; }