Commit 15743bcf9198f9abe181d55996470692a173a3ce
1 parent
931c7409
Fetch Last Level Only + Distinct values
Showing
1 changed file
with
9 additions
and
6 deletions
@@ -225,9 +225,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | @@ -225,9 +225,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | ||
225 | " INNER JOIN related_entities re ON" + | 225 | " INNER JOIN related_entities re ON" + |
226 | " r.$in_id = re.$out_id and r.$in_type = re.$out_type and" + | 226 | " r.$in_id = re.$out_id and r.$in_type = re.$out_type and" + |
227 | " relation_type_group = 'COMMON' %s)" + | 227 | " relation_type_group = 'COMMON' %s)" + |
228 | - " SELECT re.$out_id entity_id, re.$out_type entity_type, re.lvl lvl" + | 228 | + " SELECT re.$out_id entity_id, re.$out_type entity_type, max(re.lvl) lvl" + |
229 | " from related_entities re" + | 229 | " from related_entities re" + |
230 | - " %s ) entity"; | 230 | + " %s GROUP BY entity_id, entity_type) entity"; |
231 | private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from"); | 231 | private static final String HIERARCHICAL_TO_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "to").replace("$out", "from"); |
232 | private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to"); | 232 | private static final String HIERARCHICAL_FROM_QUERY_TEMPLATE = HIERARCHICAL_QUERY_TEMPLATE.replace("$in", "from").replace("$out", "to"); |
233 | 233 | ||
@@ -457,8 +457,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | @@ -457,8 +457,6 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | ||
457 | 457 | ||
458 | private String entitySearchQuery(QueryContext ctx, EntitySearchQueryFilter entityFilter, EntityType entityType, List<String> types) { | 458 | private String entitySearchQuery(QueryContext ctx, EntitySearchQueryFilter entityFilter, EntityType entityType, List<String> types) { |
459 | EntityId rootId = entityFilter.getRootEntity(); | 459 | EntityId rootId = entityFilter.getRootEntity(); |
460 | - //TODO: fetch last level only. | ||
461 | - //TODO: fetch distinct records. | ||
462 | String lvlFilter = getLvlFilter(entityFilter.getMaxLevel()); | 460 | String lvlFilter = getLvlFilter(entityFilter.getMaxLevel()); |
463 | String selectFields = "SELECT tenant_id, customer_id, id, created_time, type, name, additional_info " | 461 | String selectFields = "SELECT tenant_id, customer_id, id, created_time, type, name, additional_info " |
464 | + (entityType.equals(EntityType.ENTITY_VIEW) ? "" : ", label ") | 462 | + (entityType.equals(EntityType.ENTITY_VIEW) ? "" : ", label ") |
@@ -470,7 +468,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | @@ -470,7 +468,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | ||
470 | whereFilter += " re.relation_type = :where_relation_type AND"; | 468 | whereFilter += " re.relation_type = :where_relation_type AND"; |
471 | } | 469 | } |
472 | whereFilter += " re." + (entityFilter.getDirection().equals(EntitySearchDirection.FROM) ? "to" : "from") + "_type = :where_entity_type"; | 470 | whereFilter += " re." + (entityFilter.getDirection().equals(EntitySearchDirection.FROM) ? "to" : "from") + "_type = :where_entity_type"; |
473 | - | 471 | + if (entityFilter.isFetchLastLevelOnly()) { |
472 | + whereFilter += " and re.lvl = " + entityFilter.getMaxLevel(); | ||
473 | + } | ||
474 | from = String.format(from, lvlFilter, whereFilter); | 474 | from = String.format(from, lvlFilter, whereFilter); |
475 | String query = "( " + selectFields + from + ")"; | 475 | String query = "( " + selectFields + from + ")"; |
476 | if (types != null && !types.isEmpty()) { | 476 | if (types != null && !types.isEmpty()) { |
@@ -500,7 +500,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | @@ -500,7 +500,7 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | ||
500 | ctx.addStringParameter("relation_root_type", rootId.getEntityType().name()); | 500 | ctx.addStringParameter("relation_root_type", rootId.getEntityType().name()); |
501 | 501 | ||
502 | StringBuilder whereFilter = new StringBuilder(); | 502 | StringBuilder whereFilter = new StringBuilder(); |
503 | - ; | 503 | + |
504 | boolean noConditions = true; | 504 | boolean noConditions = true; |
505 | if (entityFilter.getFilters() != null && !entityFilter.getFilters().isEmpty()) { | 505 | if (entityFilter.getFilters() != null && !entityFilter.getFilters().isEmpty()) { |
506 | boolean single = entityFilter.getFilters().size() == 1; | 506 | boolean single = entityFilter.getFilters().size() == 1; |
@@ -530,6 +530,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | @@ -530,6 +530,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { | ||
530 | .append("_type in (:where_entity_types").append(")"); | 530 | .append("_type in (:where_entity_types").append(")"); |
531 | ctx.addStringListParameter("where_entity_types", Arrays.stream(RELATION_QUERY_ENTITY_TYPES).map(EntityType::name).collect(Collectors.toList())); | 531 | ctx.addStringListParameter("where_entity_types", Arrays.stream(RELATION_QUERY_ENTITY_TYPES).map(EntityType::name).collect(Collectors.toList())); |
532 | } | 532 | } |
533 | + if (entityFilter.isFetchLastLevelOnly()) { | ||
534 | + whereFilter.append(" and re.lvl = ").append(entityFilter.getMaxLevel()); | ||
535 | + } | ||
533 | from = String.format(from, lvlFilter, whereFilter); | 536 | from = String.format(from, lvlFilter, whereFilter); |
534 | return "( " + selectFields + from + ")"; | 537 | return "( " + selectFields + from + ")"; |
535 | } | 538 | } |