Skip to content

Commit

Permalink
fix: Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Dec 4, 2024
1 parent 15cb92a commit 85c2c92
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ protected Skip skipIndex(ValueType valueType, boolean hasOptionSet) {
}

/**
* Returns a select expression, potentially with a cast statement, based on the given value type.
* Returns a column expression, potentially with a cast statement, based on the given value type.
*
* @param valueType the {@link ValueType} to represent as database column type.
* @param columnExpression the expression or name of the column to be selected.
* @return a select expression appropriate for the given value type and context.
*/
protected String getSelectExpression(ValueType valueType, String columnExpression) {
protected String getColumnExpression(ValueType valueType, String columnExpression) {
if (valueType.isDecimal()) {
return getCastExpression(columnExpression, NUMERIC_REGEXP, sqlBuilder.dataTypeDouble());
} else if (valueType.isInteger()) {
Expand Down Expand Up @@ -224,7 +224,7 @@ protected List<AnalyticsTableColumn> getColumnForAttribute(TrackedEntityAttribut

String valueColumn = String.format("%s.%s", quote(attribute.getUid()), "value");
DataType dataType = getColumnType(attribute.getValueType(), isSpatialSupport());
String selectExpression = getSelectExpression(attribute.getValueType(), valueColumn);
String selectExpression = getColumnExpression(attribute.getValueType(), valueColumn);
String dataFilterClause = getDataFilterClause(attribute);
Skip skipIndex = skipIndex(attribute.getValueType(), attribute.hasOptionSet());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,16 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
List<AnalyticsTableColumn> columns = new ArrayList<>();

DataType dataType = getColumnType(dataElement.getValueType(), isSpatialSupport());
String columnExpression =
String jsonExpression =
sqlBuilder.jsonExtractNested("eventdatavalues", dataElement.getUid(), "value");
String selectExpression = getSelectExpression(dataElement.getValueType(), columnExpression);
String columnExpression = getColumnExpression(dataElement.getValueType(), jsonExpression);
String dataFilterClause = getDataFilterClause(dataElement);
String sql = String.format("%s as %s", selectExpression, quote(dataElement.getUid()));
String selectExpression =
String.format("%s as %s", columnExpression, quote(dataElement.getUid()));
Skip skipIndex = skipIndex(dataElement.getValueType(), dataElement.hasOptionSet());

if (withLegendSet) {
return getColumnFromDataElementWithLegendSet(dataElement, selectExpression, dataFilterClause);
return getColumnFromDataElementWithLegendSet(dataElement, columnExpression, dataFilterClause);
}

if (dataElement.getValueType().isOrganisationUnit()) {
Expand All @@ -509,7 +510,7 @@ private List<AnalyticsTableColumn> getColumnForDataElement(
.name(dataElement.getUid())
.dimensionType(AnalyticsDimensionType.DYNAMIC)
.dataType(dataType)
.selectExpression(sql)
.selectExpression(selectExpression)
.skipIndex(skipIndex)
.build());

Expand Down Expand Up @@ -590,7 +591,7 @@ private List<AnalyticsTableColumn> getAttributeColumns(Program program) {
*/
private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet(
TrackedEntityAttribute attribute) {
String selectClause = getSelectExpression(attribute.getValueType(), "value");
String columnExpression = getColumnExpression(attribute.getValueType(), "value");
String numericClause = getNumericClause();
String query =
"""
Expand All @@ -605,11 +606,11 @@ private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet(
.map(
ls -> {
String column = attribute.getUid() + PartitionUtils.SEP + ls.getUid();
String sql =
String selectExpression =
replaceQualify(
query,
Map.of(
"selectClause", selectClause,
"selectClause", columnExpression,
"legendSetId", String.valueOf(ls.getId()),
"column", column,
"attributeId", String.valueOf(attribute.getId()),
Expand All @@ -618,7 +619,7 @@ private List<AnalyticsTableColumn> getColumnForAttributeWithLegendSet(
return AnalyticsTableColumn.builder()
.name(column)
.dataType(CHARACTER_11)
.selectExpression(sql)
.selectExpression(selectExpression)
.build();
})
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ then cast(eventdatavalues #>> '{GieVkTxp4HH, value}' as double precision) \
else null end""";

String actual =
manager.getSelectExpression(ValueType.NUMBER, "eventdatavalues #>> '{GieVkTxp4HH, value}'");
manager.getColumnExpression(ValueType.NUMBER, "eventdatavalues #>> '{GieVkTxp4HH, value}'");

assertEquals(expected, actual);
}
Expand All @@ -88,7 +88,7 @@ void testGetSelectExpressionBoolean() {
case when eventdatavalues #>> '{Xl3voRRcmpo, value}' = 'true' then 1 when eventdatavalues #>> '{Xl3voRRcmpo, value}' = 'false' then 0 else null end""";

String actual =
manager.getSelectExpression(
manager.getColumnExpression(
ValueType.BOOLEAN, "eventdatavalues #>> '{Xl3voRRcmpo, value}'");

assertEquals(expected, actual);
Expand All @@ -103,7 +103,7 @@ then cast(eventdatavalues #>> '{AL04Wbutskk, value}' as timestamp) \
else null end""";

String actual =
manager.getSelectExpression(ValueType.DATE, "eventdatavalues #>> '{AL04Wbutskk, value}'");
manager.getColumnExpression(ValueType.DATE, "eventdatavalues #>> '{AL04Wbutskk, value}'");

assertEquals(expected, actual);
}
Expand All @@ -115,7 +115,7 @@ void testGetSelectExpressionText() {
eventdatavalues #>> '{FwUzmc49Pcr, value}'""";

String actual =
manager.getSelectExpression(ValueType.TEXT, "eventdatavalues #>> '{FwUzmc49Pcr, value}'");
manager.getColumnExpression(ValueType.TEXT, "eventdatavalues #>> '{FwUzmc49Pcr, value}'");

assertEquals(expected, actual);
}
Expand All @@ -129,7 +129,7 @@ void testGetSelectExpressionGeometry() {
ST_GeomFromGeoJSON('{"type":"Point", "coordinates":' || (eventdatavalues #>> '{C6bh7GevJfH, value}') || ', "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}}')""";

String actual =
manager.getSelectExpression(
manager.getColumnExpression(
ValueType.GEOJSON, "eventdatavalues #>> '{C6bh7GevJfH, value}'");

assertEquals(expected, actual);
Expand Down

0 comments on commit 85c2c92

Please sign in to comment.