@@ -522,26 +522,54 @@ private <SNAPSHOT extends RecordTemplate> SNAPSHOT constructSnapshot(@Nonnull fi
522
522
* INNER JOIN source_entity_table st ON st.urn = rt.sourceEntityUrn
523
523
* WHERE destination entity filters AND source entity filters AND relationship filters</p>
524
524
*
525
+ * <p> or if relationshipLookUpContext.isIncludeNonCurrentRelationships is true </p>
526
+ *
527
+ * <p>SELECT * FROM (
528
+ * SELECT rt.*, ROW_NUMBER() OVER (PARTITION BY rt.source, rt.metadata$type, rt.destination ORDER BY rt.lastmodifiedon DESC) AS row_num
529
+ * FROM relationship_table rt
530
+ * INNER JOIN destination_entity_table dt ON dt.urn = rt.destinationEntityUrn
531
+ * INNER JOIN source_entity_table st ON st.urn = rt.sourceEntityUrn
532
+ * WHERE destination entity filters AND source entity filters AND relationship filters)
533
+ * ranked_rows WHERE row_num = 1</p>
534
+ *
525
535
* @param relationshipTableName relationship table name
526
536
* @param relationshipFilter filter on relationship
527
537
* @param sourceTableName source entity table name
528
538
* @param sourceEntityFilter filter on source entity.
529
539
* @param destTableName destination entity table name. Always null if building relationship with non-mg
530
540
* entity.
531
541
* @param destinationEntityFilter filter on destination entity.
532
- * @param limit max number of records to return. If < 0, will return all records.
533
- * @param offset offset to start from. If < 0, will start from 0.
542
+ * @param limit max number of records to return. If less than 0, will return all records.
543
+ * @param offset offset to start from. If less than 0, will start from 0.
534
544
*/
535
545
@ Nonnull
536
- private String buildFindRelationshipSQL (
537
- @ Nonnull final String relationshipTableName , @ Nonnull final LocalRelationshipFilter relationshipFilter ,
538
- @ Nullable final String sourceTableName , @ Nullable final LocalRelationshipFilter sourceEntityFilter ,
539
- @ Nullable final String destTableName , @ Nullable final LocalRelationshipFilter destinationEntityFilter ,
540
- int limit , int offset , RelationshipLookUpContext relationshipLookUpContext ) {
546
+ @ VisibleForTesting
547
+ public String buildFindRelationshipSQL (@ Nonnull final String relationshipTableName ,
548
+ @ Nonnull final LocalRelationshipFilter relationshipFilter , @ Nullable final String sourceTableName ,
549
+ @ Nullable final LocalRelationshipFilter sourceEntityFilter , @ Nullable final String destTableName ,
550
+ @ Nullable final LocalRelationshipFilter destinationEntityFilter , int limit , int offset ,
551
+ RelationshipLookUpContext relationshipLookUpContext ) {
541
552
542
553
boolean includeNonCurrentRelationships = relationshipLookUpContext .isIncludeNonCurrentRelationships ();
543
554
StringBuilder sqlBuilder = new StringBuilder ();
544
- sqlBuilder .append ("SELECT rt.* FROM " ).append (relationshipTableName ).append (" rt " );
555
+
556
+ if (includeNonCurrentRelationships ) {
557
+ sqlBuilder .append ("SELECT * FROM (" );
558
+ }
559
+
560
+ sqlBuilder .append ("SELECT rt.*" );
561
+
562
+ if (includeNonCurrentRelationships ) {
563
+ final boolean isNonDollarVirtualColumnsEnabled = _eBeanDAOConfig .isNonDollarVirtualColumnsEnabled ();
564
+ final String metadataTypeColName = isNonDollarVirtualColumnsEnabled ? "metadata0type" : "metadata$type" ;
565
+ final boolean hasMetadataTypeCol = _schemaValidatorUtil .columnExists (relationshipTableName , metadataTypeColName );
566
+
567
+ sqlBuilder .append (", ROW_NUMBER() OVER (PARTITION BY rt.source" )
568
+ .append (hasMetadataTypeCol ? ", rt." + metadataTypeColName : "" )
569
+ .append (", rt.destination ORDER BY rt.lastmodifiedon DESC) AS row_num" );
570
+ }
571
+
572
+ sqlBuilder .append (" FROM " ).append (relationshipTableName ).append (" rt " );
545
573
546
574
List <Pair <LocalRelationshipFilter , String >> filters = new ArrayList <>();
547
575
@@ -612,7 +640,8 @@ private String buildFindRelationshipSQL(
612
640
}
613
641
if (whereClauseBuilder .length () != 0 ) {
614
642
if (includeNonCurrentRelationships ) {
615
- sqlBuilder .append ("WHERE " ).append (whereClauseBuilder .toString ().replaceFirst ("^AND\\ s+" , "" ));
643
+ String where = whereClauseBuilder .toString ().replaceFirst ("\\ s*AND\\ s+" , "" );
644
+ sqlBuilder .append ("WHERE " ).append (where );
616
645
} else {
617
646
sqlBuilder .append ("WHERE " ).append (whereClauseBuilder );
618
647
}
@@ -628,6 +657,11 @@ private String buildFindRelationshipSQL(
628
657
sqlBuilder .append (" OFFSET " ).append (offset );
629
658
}
630
659
}
660
+
661
+ if (includeNonCurrentRelationships ) {
662
+ sqlBuilder .append (") ranked_rows WHERE row_num = 1" );
663
+ }
664
+
631
665
return sqlBuilder .toString ();
632
666
}
633
667
0 commit comments