Skip to content

Commit

Permalink
fix: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Nov 25, 2024
1 parent 16445cd commit 879b90c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ValueType> NO_INDEX_VAL_TYPES =
Set.of(ValueType.TEXT, ValueType.LONG_TEXT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -699,10 +698,10 @@ private List<AnalyticsTableColumn> 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()
Expand Down Expand Up @@ -737,7 +736,7 @@ private List<AnalyticsTableColumn> 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");

Expand Down

0 comments on commit 879b90c

Please sign in to comment.