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 | 16 | package org.thingsboard.server.dao.sql.query; |
17 | 17 | |
18 | 18 | import lombok.Data; |
19 | -import org.springframework.util.StringUtils; | |
19 | +import org.apache.commons.lang3.StringUtils; | |
20 | 20 | import org.thingsboard.server.common.data.DataConstants; |
21 | 21 | import org.thingsboard.server.common.data.EntityType; |
22 | 22 | import org.thingsboard.server.common.data.query.BooleanFilterPredicate; |
... | ... | @@ -42,7 +42,6 @@ import java.util.HashMap; |
42 | 42 | import java.util.HashSet; |
43 | 43 | import java.util.List; |
44 | 44 | import java.util.Map; |
45 | -import java.util.Objects; | |
46 | 45 | import java.util.Optional; |
47 | 46 | import java.util.Set; |
48 | 47 | import java.util.stream.Collectors; |
... | ... | @@ -245,8 +244,9 @@ public class EntityKeyMapping { |
245 | 244 | entityTypeStr = "'" + entityType.name() + "'"; |
246 | 245 | } |
247 | 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 | 250 | if (StringUtils.isEmpty(filterQuery)) { |
251 | 251 | filterQuery = ""; |
252 | 252 | } else { |
... | ... | @@ -293,8 +293,10 @@ public class EntityKeyMapping { |
293 | 293 | } |
294 | 294 | |
295 | 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 | 302 | public static List<EntityKeyMapping> prepareKeyMapping(EntityDataQuery query) { |
... | ... | @@ -461,9 +463,8 @@ public class EntityKeyMapping { |
461 | 463 | ComplexFilterPredicate predicate, EntityFilterType filterType) { |
462 | 464 | String result = predicate.getPredicates().stream() |
463 | 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 | 468 | if (!result.trim().isEmpty()) { |
468 | 469 | result = "( " + result + " )"; |
469 | 470 | } |
... | ... | @@ -520,7 +521,7 @@ public class EntityKeyMapping { |
520 | 521 | String paramName = getNextParameterName(field); |
521 | 522 | String value = stringFilterPredicate.getValue().getValue(); |
522 | 523 | if (value.isEmpty()) { |
523 | - return null; | |
524 | + return ""; | |
524 | 525 | } |
525 | 526 | String stringOperationQuery = ""; |
526 | 527 | if (stringFilterPredicate.isIgnoreCase()) { | ... | ... |