Skip to content

Commit

Permalink
fix: minor deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
bobeal committed Sep 20, 2023
1 parent 8d0e09f commit f2e3131
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ class ScopeService(
temporalEntitiesQuery.withAggregatedValues -> {
val temporalQuery = temporalEntitiesQuery.temporalQuery
val aggrPeriodDuration = temporalQuery.aggrPeriodDuration
val allAggregates = temporalQuery.aggrMethods?.joinToString(",") {
val sqlAggregateExpression = aggrMethodToSqlAggregate(it, AttributeValueType.ARRAY)
"$sqlAggregateExpression as ${it.method}_value"
}
val allAggregates = temporalQuery.aggrMethods?.composeAggregationSelectClause(AttributeValueType.ARRAY)
// if retrieving a temporal entity, origin is calculated beforehand as timeAt is optional in this case
// if querying temporal entities, timeAt is mandatory and will be used if origin is null
if (aggrPeriodDuration != WHOLE_TIME_RANGE_DURATION) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,8 @@ class AttributeInstanceService(
) = when {
temporalQuery.aggrPeriodDuration != null -> {
val aggrPeriodDuration = temporalQuery.aggrPeriodDuration
val allAggregates = temporalQuery.aggrMethods?.joinToString(",") {
val sqlAggregateExpression =
aggrMethodToSqlAggregate(it, temporalEntityAttributes[0].attributeValueType)
"$sqlAggregateExpression as ${it.method}_value"
}
val allAggregates = temporalQuery.aggrMethods
?.composeAggregationSelectClause(temporalEntityAttributes[0].attributeValueType)
// if retrieving a temporal entity, origin is calculated beforehand as timeAt is optional in this case
// if querying temporal entities, timeAt is mandatory and will be used if origin is null
if (aggrPeriodDuration != WHOLE_TIME_RANGE_DURATION) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.egm.stellio.search.util

import com.egm.stellio.search.model.TemporalEntityAttribute
import com.egm.stellio.search.model.TemporalEntityAttribute.AttributeValueType
import com.egm.stellio.search.model.TemporalQuery

fun aggrMethodToSqlAggregate(
aggregate: TemporalQuery.Aggregate,
attributeValueType: TemporalEntityAttribute.AttributeValueType
attributeValueType: AttributeValueType
): String = when (attributeValueType) {
TemporalEntityAttribute.AttributeValueType.STRING -> sqlAggregationForJsonString(aggregate)
TemporalEntityAttribute.AttributeValueType.NUMBER -> sqlAggregateForJsonNumber(aggregate)
TemporalEntityAttribute.AttributeValueType.OBJECT -> sqlAggregateForJsonObject(aggregate)
TemporalEntityAttribute.AttributeValueType.ARRAY -> sqlAggregateForJsonArray(aggregate)
TemporalEntityAttribute.AttributeValueType.BOOLEAN -> sqlAggregateForJsonBoolean(aggregate)
TemporalEntityAttribute.AttributeValueType.DATETIME -> sqlAggregateForDateTime(aggregate)
TemporalEntityAttribute.AttributeValueType.DATE -> sqlAggregateForDate(aggregate)
TemporalEntityAttribute.AttributeValueType.TIME -> sqlAggregateForTime(aggregate)
TemporalEntityAttribute.AttributeValueType.URI -> sqlAggregateForURI(aggregate)
TemporalEntityAttribute.AttributeValueType.GEOMETRY -> "null"
AttributeValueType.STRING -> sqlAggregationForJsonString(aggregate)
AttributeValueType.NUMBER -> sqlAggregateForJsonNumber(aggregate)
AttributeValueType.OBJECT -> sqlAggregateForJsonObject(aggregate)
AttributeValueType.ARRAY -> sqlAggregateForJsonArray(aggregate)
AttributeValueType.BOOLEAN -> sqlAggregateForJsonBoolean(aggregate)
AttributeValueType.DATETIME -> sqlAggregateForDateTime(aggregate)
AttributeValueType.DATE -> sqlAggregateForDate(aggregate)
AttributeValueType.TIME -> sqlAggregateForTime(aggregate)
AttributeValueType.URI -> sqlAggregateForURI(aggregate)
AttributeValueType.GEOMETRY -> "null"
}

fun sqlAggregationForJsonString(aggregate: TemporalQuery.Aggregate): String = when (aggregate) {
Expand Down Expand Up @@ -95,3 +95,9 @@ fun sqlAggregateForURI(aggregate: TemporalQuery.Aggregate): String = when (aggre
TemporalQuery.Aggregate.DISTINCT_COUNT -> "count(distinct(value))"
else -> "null"
}

fun List<TemporalQuery.Aggregate>?.composeAggregationSelectClause(attributeValueType: AttributeValueType): String? =
this?.joinToString(",") {
val sqlAggregateExpression = aggrMethodToSqlAggregate(it, attributeValueType)
"$sqlAggregateExpression as ${it.method}_value"
}

0 comments on commit f2e3131

Please sign in to comment.