Fix entities filtering by kv when search value is empty (#4515)
* Fix entities filtering by kv when search value is empty * Refactor
This commit is contained in:
parent
e618f2382a
commit
fbf2d3efdb
@ -243,23 +243,21 @@ public class EntityKeyMapping {
|
|||||||
} else {
|
} else {
|
||||||
entityTypeStr = "'" + entityType.name() + "'";
|
entityTypeStr = "'" + entityType.name() + "'";
|
||||||
}
|
}
|
||||||
ctx.addStringParameter(alias + "_key_id", entityKey.getKey());
|
ctx.addStringParameter(getKeyId(), entityKey.getKey());
|
||||||
String filterQuery = toQueries(ctx, entityFilter.getType())
|
String filterQuery = toQueries(ctx, entityFilter.getType())
|
||||||
.filter(StringUtils::isNotEmpty)
|
.filter(StringUtils::isNotEmpty)
|
||||||
.collect(Collectors.joining(" and "));
|
.collect(Collectors.joining(" and "));
|
||||||
if (StringUtils.isEmpty(filterQuery)) {
|
if (StringUtils.isNotEmpty(filterQuery)) {
|
||||||
filterQuery = "";
|
|
||||||
} else {
|
|
||||||
filterQuery = " AND (" + filterQuery + ")";
|
filterQuery = " AND (" + filterQuery + ")";
|
||||||
}
|
}
|
||||||
if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) {
|
if (entityKey.getType().equals(EntityKeyType.TIME_SERIES)) {
|
||||||
String join = hasFilter() ? "inner join" : "left join";
|
String join = (hasFilter() && hasFilterValues(ctx)) ? "inner join" : "left join";
|
||||||
return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s",
|
return String.format("%s ts_kv_latest %s ON %s.entity_id=entities.id AND %s.key = (select key_id from ts_kv_dictionary where key = :%s_key_id) %s",
|
||||||
join, alias, alias, alias, alias, filterQuery);
|
join, alias, alias, alias, alias, filterQuery);
|
||||||
} else {
|
} else {
|
||||||
String query;
|
String query;
|
||||||
if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) {
|
if (!entityKey.getType().equals(EntityKeyType.ATTRIBUTE)) {
|
||||||
String join = hasFilter() ? "inner join" : "left join";
|
String join = (hasFilter() && hasFilterValues(ctx)) ? "inner join" : "left join";
|
||||||
query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ",
|
query = String.format("%s attribute_kv %s ON %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id ",
|
||||||
join, alias, alias, alias, entityTypeStr, alias, alias);
|
join, alias, alias, alias, entityTypeStr, alias, alias);
|
||||||
String scope;
|
String scope;
|
||||||
@ -272,7 +270,7 @@ public class EntityKeyMapping {
|
|||||||
}
|
}
|
||||||
query = String.format("%s AND %s.attribute_type='%s' %s", query, alias, scope, filterQuery);
|
query = String.format("%s AND %s.attribute_type='%s' %s", query, alias, scope, filterQuery);
|
||||||
} else {
|
} else {
|
||||||
String join = hasFilter() ? "join LATERAL" : "left join LATERAL";
|
String join = (hasFilter() && hasFilterValues(ctx)) ? "join LATERAL" : "left join LATERAL";
|
||||||
query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id %s " +
|
query = String.format("%s (select * from attribute_kv %s WHERE %s.entity_id=entities.id AND %s.entity_type=%s AND %s.attribute_key=:%s_key_id %s " +
|
||||||
"ORDER BY %s.last_update_ts DESC limit 1) as %s ON true",
|
"ORDER BY %s.last_update_ts DESC limit 1) as %s ON true",
|
||||||
join, alias, alias, alias, entityTypeStr, alias, alias, filterQuery, alias, alias);
|
join, alias, alias, alias, entityTypeStr, alias, alias, filterQuery, alias, alias);
|
||||||
@ -281,15 +279,26 @@ public class EntityKeyMapping {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean hasFilterValues(QueryContext ctx) {
|
||||||
|
return Arrays.stream(ctx.getParameterNames()).anyMatch(parameterName -> {
|
||||||
|
return !parameterName.equals(getKeyId()) && parameterName.startsWith(alias);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getKeyId() {
|
||||||
|
return alias + "_key_id";
|
||||||
|
}
|
||||||
|
|
||||||
public static String buildSelections(List<EntityKeyMapping> mappings, EntityFilterType filterType, EntityType entityType) {
|
public static String buildSelections(List<EntityKeyMapping> mappings, EntityFilterType filterType, EntityType entityType) {
|
||||||
return mappings.stream().map(mapping -> mapping.toSelection(filterType, entityType)).collect(
|
return mappings.stream().map(mapping -> mapping.toSelection(filterType, entityType)).collect(
|
||||||
Collectors.joining(", "));
|
Collectors.joining(", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String buildLatestJoins(QueryContext ctx, EntityFilter entityFilter, EntityType entityType, List<EntityKeyMapping> latestMappings, boolean countQuery) {
|
public static String buildLatestJoins(QueryContext ctx, EntityFilter entityFilter, EntityType entityType, List<EntityKeyMapping> latestMappings, boolean countQuery) {
|
||||||
return latestMappings.stream().filter(mapping -> !countQuery || mapping.hasFilter())
|
return latestMappings.stream()
|
||||||
.map(mapping -> mapping.toLatestJoin(ctx, entityFilter, entityType)).collect(
|
.filter(mapping -> !countQuery || mapping.hasFilter())
|
||||||
Collectors.joining(" "));
|
.map(mapping -> mapping.toLatestJoin(ctx, entityFilter, entityType))
|
||||||
|
.collect(Collectors.joining(" "));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) {
|
public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user