From 879b90c11587404f39f0cb37a578f12bbaa73971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Helge=20=C3=98verland?= Date: Mon, 25 Nov 2024 09:36:48 +0100 Subject: [PATCH] fix: Update code --- .../table/AbstractEventJdbcTableManager.java | 29 ++++++++++++------- .../table/AbstractJdbcTableManager.java | 3 ++ .../table/JdbcAnalyticsTableManager.java | 3 +- .../table/JdbcEventAnalyticsTableManager.java | 11 ++++--- 4 files changed, 27 insertions(+), 19 deletions(-) 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 65ef1a37ab32..4f1ac6fe7b03 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 @@ -103,6 +103,13 @@ protected final String getDateClause() { return " and " + sqlBuilder.regexpMatch("value", "'" + DATE_REGEXP + "'"); } + /** + * Indicates whether creating an index should be skipped. + * + * @param valueType the {@link ValueType}. + * @param hasOptionSet whether an option set exists. + * @return a {@link Skip}. + */ protected Skip skipIndex(ValueType valueType, boolean hasOptionSet) { boolean skipIndex = NO_INDEX_VAL_TYPES.contains(valueType) && !hasOptionSet; return skipIndex ? Skip.SKIP : Skip.INCLUDE; @@ -137,39 +144,39 @@ protected String getSelectExpressionForAttribute(ValueType valueType, String col * Handles data element and tracked entity attribute select expressions. * * @param valueType the {@link ValueType} to represent as database column type. - * @param columnName the name of the column to be selected. + * @param columnExpression the expression or name of the column to be selected. * @param isTeaContext whether the selection is in the context of a tracked entity attribute. When * true, organization unit selections will include an additional subquery wrapper. - * @return A SQL select expression appropriate for the given value type and context. + * @return a select expression appropriate for the given value type and context. */ private String getSelectExpressionInternal( - ValueType valueType, String columnName, boolean isTeaContext) { + ValueType valueType, String columnExpression, boolean isTeaContext) { String doubleType = sqlBuilder.dataTypeDouble(); if (valueType.isDecimal()) { - return "cast(" + columnName + " as " + doubleType + ")"; + return "cast(" + columnExpression + " as " + doubleType + ")"; } else if (valueType.isInteger()) { - return "cast(" + columnName + " as bigint)"; + return "cast(" + columnExpression + " as bigint)"; } else if (valueType.isBoolean()) { return "case when " - + columnName + + columnExpression + " = 'true' then 1 when " - + columnName + + columnExpression + " = 'false' then 0 else null end"; } else if (valueType.isDate()) { - return "cast(" + columnName + " as " + sqlBuilder.dataTypeTimestamp() + ")"; + return "cast(" + columnExpression + " as " + sqlBuilder.dataTypeTimestamp() + ")"; } else if (valueType.isGeo() && isSpatialSupport()) { return "ST_GeomFromGeoJSON('{\"type\":\"Point\", \"coordinates\":' || (" - + columnName + + columnExpression + ") || ', \"crs\":{\"type\":\"name\", \"properties\":{\"name\":\"EPSG:4326\"}}}')"; } else if (valueType.isOrganisationUnit()) { String ouClause = isTeaContext ? "ou.uid from ${organisationunit} ou where ou.uid = (select ${columnName}" : "ou.uid from ${organisationunit} ou where ou.uid = ${columnName}"; - return replaceQualify(ouClause, Map.of("columnName", columnName)); + return replaceQualify(ouClause, Map.of("columnName", columnExpression)); } else { - return columnName; + return columnExpression; } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java index 2a4b27ff8c8b..8868f48387ae 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/AbstractJdbcTableManager.java @@ -86,6 +86,7 @@ import org.hisp.dhis.setting.SystemSettings; import org.hisp.dhis.setting.SystemSettingsProvider; import org.hisp.dhis.system.database.DatabaseInfoProvider; +import org.hisp.dhis.system.util.MathUtils; import org.hisp.dhis.util.DateUtils; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.JdbcTemplate; @@ -113,6 +114,8 @@ public abstract class AbstractJdbcTableManager implements AnalyticsTableManager protected static final String DATE_REGEXP = "^\\d{4}-\\d{2}-\\d{2}(\\s|T)?((\\d{2}:)(\\d{2}:)?(\\d{2}))?(|.(\\d{3})|.(\\d{3})Z)?$"; + protected static final String NUMERIC_REGEXP = MathUtils.NUMERIC_LENIENT_REGEXP; + protected static final Set NO_INDEX_VAL_TYPES = Set.of(ValueType.TEXT, ValueType.LONG_TEXT); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java index c426edc9e0e4..1671f437353f 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/table/JdbcAnalyticsTableManager.java @@ -40,7 +40,6 @@ import static org.hisp.dhis.db.model.DataType.VARCHAR_255; import static org.hisp.dhis.db.model.constraint.Nullable.NOT_NULL; import static org.hisp.dhis.db.model.constraint.Nullable.NULL; -import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; import static org.hisp.dhis.util.DateUtils.toLongDate; import com.google.common.collect.Sets; @@ -280,7 +279,7 @@ public void populateTable(AnalyticsTableUpdateParams params, AnalyticsTableParti String numericClause = skipDataTypeValidation ? "" - : "and " + sqlBuilder.regexpMatch("dv.value", "'" + NUMERIC_LENIENT_REGEXP + "'"); + : "and " + sqlBuilder.regexpMatch("dv.value", "'" + NUMERIC_REGEXP + "'"); String zeroValueCondition = includeZeroValues ? " or des.zeroissignificant = true" : ""; String zeroValueClause = replace( 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 0c3e85fe9e0a..060758c929d0 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 @@ -41,7 +41,6 @@ import static org.hisp.dhis.db.model.DataType.TEXT; import static org.hisp.dhis.period.PeriodDataProvider.DataSource.DATABASE; import static org.hisp.dhis.period.PeriodDataProvider.DataSource.SYSTEM_DEFINED; -import static org.hisp.dhis.system.util.MathUtils.NUMERIC_LENIENT_REGEXP; import static org.hisp.dhis.util.DateUtils.toLongDate; import static org.hisp.dhis.util.DateUtils.toMediumDate; @@ -699,10 +698,10 @@ private List getColumnFromDataElementWithLegendSet( DataElement dataElement, String selectExpression, String dataFilterClause) { String query = """ - (select l.uid from ${maplegend} l - inner join ${event} on l.startvalue <= ${select} - and l.endvalue > ${select} - and l.maplegendsetid=${legendSetId} + (select l.uid from ${maplegend} l \ + inner join ${event} on l.startvalue <= ${select} \ + and l.endvalue > ${select} \ + and l.maplegendsetid=${legendSetId} \ ${dataClause} where eventid = ev.eventid) as ${column}"""; return dataElement.getLegendSets().stream() @@ -737,7 +736,7 @@ private List getColumnFromDataElementWithLegendSet( */ private String getDataFilterClause(String uid, ValueType valueType) { if (valueType.isNumeric() || valueType.isDate()) { - String regex = valueType.isNumeric() ? NUMERIC_LENIENT_REGEXP : DATE_REGEXP; + String regex = valueType.isNumeric() ? NUMERIC_REGEXP : DATE_REGEXP; String jsonExpression = sqlBuilder.jsonExtractNested("eventdatavalues", uid, "value");