/// <summary> /// If this query has a principal query, this method returns the SQL of the principal query in the form of an /// INNER JOIN to restrict the result to those entities that are related to the principal query /// </summary> private string PreparePrincipalQuerySql(QxCompilationContext ctx) { string principalQuerySql = ""; if (PrincipalQuery != null) { // Get the inner sql and append 4 spaces before each line for aesthetics string innerSql = PrincipalQuery.PrepareStatementAsPrincipal( ctx.Sources, ctx.Variables, ctx.Parameters, IsAncestorExpand, PathToCollectionPropertyInPrincipal, ctx.UserId, ctx.Today); innerSql = innerSql.IndentLines(); if (IsAncestorExpand) { principalQuerySql = $@"INNER JOIN ( {innerSql} ) As [S] ON [S].[Node].IsDescendantOf([P].[Node]) = 1 AND [S].[Node] <> [P].[Node]"; } else { // This works since when there is a principal query, there is no WHERE clause principalQuerySql = $@"WHERE [P].[{ForeignKeyToPrincipalQuery}] IN ( {innerSql} )"; } } return(principalQuerySql); }
/// <summary> /// If this query has a principal query, this method returns the SQL of the principal query in the form of an /// INNER JOIN to restrict the result to those entities that are related to the principal query /// </summary> private string PreparePrincipalQuery(Func <Type, string> sources, SqlStatementParameters ps, int userId, DateTime?userToday) { string principalQuerySql = ""; if (PrincipalQuery != null) { // Get the inner sql and append 4 spaces before each line for aesthetics string innerSql = PrincipalQuery.PrepareStatementAsPrincipal(sources, ps, IsAncestorExpand, PathToCollectionPropertyInPrincipal, userId, userToday); innerSql = QueryTools.IndentLines(innerSql); if (IsAncestorExpand) { principalQuerySql = $@"INNER JOIN ( {innerSql} ) As [S] ON [S].[Node].IsDescendantOf([P].[Node]) = 1 AND [S].[Node] <> [P].[Node]"; } else { principalQuerySql = $@"INNER JOIN ( {innerSql} ) As [S] ON [S].[Id] = [P].[{ForeignKeyToPrincipalQuery}]"; } } return(principalQuerySql); }