Skip to content

Commit

Permalink
Fixes #1365
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Aug 15, 2022
1 parent f98845d commit baa124e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Removed restriction preventing [Lookup] requiring all foreign key columns being from the same table [#1331](https://github.com/HicServices/RDMP/issues/1307)
- If there are multiple IsPrimaryExtractionTable involved in a query then the one with the IsExtractionIdentifier column (if any) will be picked (previously QueryBuildingException was thrown) [#1365](https://github.com/HicServices/RDMP/issues/1365)

### Added
- Added 'Set Description' command to [AggregateConfiguration] context menu
Expand Down
40 changes: 37 additions & 3 deletions Rdmp.Core/QueryBuilding/SqlQueryBuilderHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ public static List<ITableInfo> GetTablesUsedInQuery(ISqlQueryBuilder qb, out ITa
if (primaryExtractionTable == null)
primaryExtractionTable = table;
else
throw new QueryBuildingException("There are multiple tables marked as IsPrimaryExtractionTable:" +
primaryExtractionTable.Name + "(ID=" + primaryExtractionTable.ID +
") and " + table.Name + "(ID=" + table.ID + ")");
primaryExtractionTable = PickBestPrimaryExtractionTable(qb,primaryExtractionTable, table);
}
}

Expand Down Expand Up @@ -268,6 +266,42 @@ public static List<ITableInfo> GetTablesUsedInQuery(ISqlQueryBuilder qb, out ITa
return toReturn;
}

/// <summary>
/// Picks between two <see cref="ITableInfo"/> both of which are <see cref="TableInfo.IsPrimaryExtractionTable"/> and returns
/// the 'winner' (best to start joining from). Throws <see cref="QueryBuildingException"/> if there is no clear better one
/// </summary>
/// <param name="qb"></param>
/// <param name="t1"></param>
/// <param name="t2"></param>
/// <returns></returns>
/// <exception cref="QueryBuildingException"></exception>
private static ITableInfo PickBestPrimaryExtractionTable(ISqlQueryBuilder qb, ITableInfo t1, TableInfo t2)
{
// what tables have IsExtractionIdentifier column(s)?
var extractionIdentifierTables = qb.SelectColumns
.Where(c => c.IColumn?.IsExtractionIdentifier ?? false)
.Select(t => t.UnderlyingColumn?.TableInfo_ID)
.Where(id => id != null)
.ToArray();

if(extractionIdentifierTables.Length == 1)
{
var id = extractionIdentifierTables[0];

if (id == t1.ID)
return t1;

if (id == t2.ID)
return t2;

// IsExtractionIdentifier column is from neither of these tables, bad times
}

throw new QueryBuildingException("There are multiple tables marked as IsPrimaryExtractionTable:" +
t1.Name + "(ID=" + t1.ID +
") and " + t2.Name + "(ID=" + t2.ID + ")");
}

private static List<ITableInfo> AddOpportunisticJoins(List<ITableInfo> toReturn, List<IFilter> filters)
{
//there must be at least one TableInfo here to do this... but we are going to look up all available JoinInfos from these tables to identify opportunistic joins
Expand Down

0 comments on commit baa124e

Please sign in to comment.