Commit 1b381298fb9bcccb5fb53c12db1dadcfd9dbd05a
Committed by
Andrew Shvayka
1 parent
0aa16d28
Refactor predicate queries building
Showing
1 changed file
with
11 additions
and
10 deletions
@@ -16,7 +16,7 @@ | @@ -16,7 +16,7 @@ | ||
16 | package org.thingsboard.server.dao.sql.query; | 16 | package org.thingsboard.server.dao.sql.query; |
17 | 17 | ||
18 | import lombok.Data; | 18 | import lombok.Data; |
19 | -import org.springframework.util.StringUtils; | 19 | +import org.apache.commons.lang3.StringUtils; |
20 | import org.thingsboard.server.common.data.DataConstants; | 20 | import org.thingsboard.server.common.data.DataConstants; |
21 | import org.thingsboard.server.common.data.EntityType; | 21 | import org.thingsboard.server.common.data.EntityType; |
22 | import org.thingsboard.server.common.data.query.BooleanFilterPredicate; | 22 | import org.thingsboard.server.common.data.query.BooleanFilterPredicate; |
@@ -42,7 +42,6 @@ import java.util.HashMap; | @@ -42,7 +42,6 @@ import java.util.HashMap; | ||
42 | import java.util.HashSet; | 42 | import java.util.HashSet; |
43 | import java.util.List; | 43 | import java.util.List; |
44 | import java.util.Map; | 44 | import java.util.Map; |
45 | -import java.util.Objects; | ||
46 | import java.util.Optional; | 45 | import java.util.Optional; |
47 | import java.util.Set; | 46 | import java.util.Set; |
48 | import java.util.stream.Collectors; | 47 | import java.util.stream.Collectors; |
@@ -245,8 +244,9 @@ public class EntityKeyMapping { | @@ -245,8 +244,9 @@ public class EntityKeyMapping { | ||
245 | entityTypeStr = "'" + entityType.name() + "'"; | 244 | entityTypeStr = "'" + entityType.name() + "'"; |
246 | } | 245 | } |
247 | ctx.addStringParameter(alias + "_key_id", entityKey.getKey()); | 246 | ctx.addStringParameter(alias + "_key_id", entityKey.getKey()); |
248 | - String filterQuery = toQueries(ctx, entityFilter.getType()).filter(Objects::nonNull).collect( | ||
249 | - Collectors.joining(" and ")); | 247 | + String filterQuery = toQueries(ctx, entityFilter.getType()) |
248 | + .filter(StringUtils::isNotEmpty) | ||
249 | + .collect(Collectors.joining(" and ")); | ||
250 | if (StringUtils.isEmpty(filterQuery)) { | 250 | if (StringUtils.isEmpty(filterQuery)) { |
251 | filterQuery = ""; | 251 | filterQuery = ""; |
252 | } else { | 252 | } else { |
@@ -293,8 +293,10 @@ public class EntityKeyMapping { | @@ -293,8 +293,10 @@ public class EntityKeyMapping { | ||
293 | } | 293 | } |
294 | 294 | ||
295 | public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) { | 295 | public static String buildQuery(QueryContext ctx, List<EntityKeyMapping> mappings, EntityFilterType filterType) { |
296 | - return mappings.stream().flatMap(mapping -> mapping.toQueries(ctx, filterType)).filter(Objects::nonNull).collect( | ||
297 | - Collectors.joining(" AND ")); | 296 | + return mappings.stream() |
297 | + .flatMap(mapping -> mapping.toQueries(ctx, filterType)) | ||
298 | + .filter(StringUtils::isNotEmpty) | ||
299 | + .collect(Collectors.joining(" AND ")); | ||
298 | } | 300 | } |
299 | 301 | ||
300 | public static List<EntityKeyMapping> prepareKeyMapping(EntityDataQuery query) { | 302 | public static List<EntityKeyMapping> prepareKeyMapping(EntityDataQuery query) { |
@@ -461,9 +463,8 @@ public class EntityKeyMapping { | @@ -461,9 +463,8 @@ public class EntityKeyMapping { | ||
461 | ComplexFilterPredicate predicate, EntityFilterType filterType) { | 463 | ComplexFilterPredicate predicate, EntityFilterType filterType) { |
462 | String result = predicate.getPredicates().stream() | 464 | String result = predicate.getPredicates().stream() |
463 | .map(keyFilterPredicate -> this.buildPredicateQuery(ctx, alias, key, keyFilterPredicate, filterType)) | 465 | .map(keyFilterPredicate -> this.buildPredicateQuery(ctx, alias, key, keyFilterPredicate, filterType)) |
464 | - .filter(Objects::nonNull).collect(Collectors.joining( | ||
465 | - " " + predicate.getOperation().name() + " " | ||
466 | - )); | 466 | + .filter(StringUtils::isNotEmpty) |
467 | + .collect(Collectors.joining(" " + predicate.getOperation().name() + " ")); | ||
467 | if (!result.trim().isEmpty()) { | 468 | if (!result.trim().isEmpty()) { |
468 | result = "( " + result + " )"; | 469 | result = "( " + result + " )"; |
469 | } | 470 | } |
@@ -520,7 +521,7 @@ public class EntityKeyMapping { | @@ -520,7 +521,7 @@ public class EntityKeyMapping { | ||
520 | String paramName = getNextParameterName(field); | 521 | String paramName = getNextParameterName(field); |
521 | String value = stringFilterPredicate.getValue().getValue(); | 522 | String value = stringFilterPredicate.getValue().getValue(); |
522 | if (value.isEmpty()) { | 523 | if (value.isEmpty()) { |
523 | - return null; | 524 | + return ""; |
524 | } | 525 | } |
525 | String stringOperationQuery = ""; | 526 | String stringOperationQuery = ""; |
526 | if (stringFilterPredicate.isIgnoreCase()) { | 527 | if (stringFilterPredicate.isIgnoreCase()) { |