Commit 40a37eef934d38902c30fd2aface4b5218e886dd
1 parent
2dd2cae9
Fix for multiple conditions in relation query
Showing
1 changed file
with
17 additions
and
10 deletions
... | ... | @@ -201,6 +201,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
201 | 201 | entityTableMap.put(EntityType.TENANT, "tenant"); |
202 | 202 | } |
203 | 203 | |
204 | + public static EntityType[] RELATION_QUERY_ENTITY_TYPES = new EntityType[]{ | |
205 | + EntityType.TENANT, EntityType.CUSTOMER, EntityType.USER, EntityType.DASHBOARD, EntityType.ASSET, EntityType.DEVICE, EntityType.ENTITY_VIEW}; | |
206 | + | |
204 | 207 | private static final String HIERARCHICAL_QUERY_TEMPLATE = " FROM (WITH RECURSIVE related_entities(from_id, from_type, to_id, to_type, relation_type, lvl) AS (" + |
205 | 208 | " SELECT from_id, from_type, to_id, to_type, relation_type, 1 as lvl" + |
206 | 209 | " FROM relation" + |
... | ... | @@ -333,8 +336,8 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
333 | 336 | if (pageLink.getPageSize() > 0) { |
334 | 337 | dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex); |
335 | 338 | } |
336 | - log.error("QUERY: {}", dataQuery); | |
337 | - Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param))); | |
339 | +// log.error("QUERY: {}", dataQuery); | |
340 | +// Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param))); | |
338 | 341 | List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx); |
339 | 342 | return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements); |
340 | 343 | }); |
... | ... | @@ -467,20 +470,20 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
467 | 470 | ctx.addUuidParameter("relation_root_id", rootId.getId()); |
468 | 471 | ctx.addStringParameter("relation_root_type", rootId.getEntityType().name()); |
469 | 472 | |
470 | - StringBuilder whereFilter; | |
473 | + StringBuilder whereFilter = new StringBuilder(); | |
474 | + ; | |
475 | + boolean noConditions = true; | |
471 | 476 | if (entityFilter.getFilters() != null && !entityFilter.getFilters().isEmpty()) { |
472 | - whereFilter = new StringBuilder(); | |
473 | - boolean first = true; | |
474 | 477 | boolean single = entityFilter.getFilters().size() == 1; |
475 | 478 | int entityTypeFilterIdx = 0; |
476 | 479 | for (EntityTypeFilter etf : entityFilter.getFilters()) { |
477 | 480 | String etfCondition = buildEtfCondition(ctx, etf, entityFilter.getDirection(), entityTypeFilterIdx++); |
478 | 481 | if (!etfCondition.isEmpty()) { |
479 | - if (first) { | |
482 | + if (noConditions) { | |
480 | 483 | whereFilter.append(" WHERE "); |
481 | - first = false; | |
484 | + noConditions = false; | |
482 | 485 | } else { |
483 | - whereFilter.append(" AND "); | |
486 | + whereFilter.append(" OR "); | |
484 | 487 | } |
485 | 488 | if (!single) { |
486 | 489 | whereFilter.append(" ("); |
... | ... | @@ -491,8 +494,12 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
491 | 494 | } |
492 | 495 | } |
493 | 496 | } |
494 | - } else { | |
495 | - whereFilter = new StringBuilder(); | |
497 | + } | |
498 | + if (noConditions) { | |
499 | + whereFilter.append(" WHERE re.") | |
500 | + .append(entityFilter.getDirection().equals(EntitySearchDirection.FROM) ? "to" : "from") | |
501 | + .append("_type in (:where_entity_types").append(")"); | |
502 | + ctx.addStringListParameter("where_entity_types", Arrays.stream(RELATION_QUERY_ENTITY_TYPES).map(EntityType::name).collect(Collectors.toList())); | |
496 | 503 | } |
497 | 504 | from = String.format(from, lvlFilter, whereFilter); |
498 | 505 | return "( " + selectFields + from + ")"; | ... | ... |