refactor(dedup): extract AppendWithClauseSection for the WITH/CTE block

`SqlServer.QueryBreakdown.GetSqlBreakdown` and
`Snowflake.QueryBreakdown.GetSql` each carried a 24-line copy of the
same CTE-rendering loop ("WITH" keyword, optional RECURSIVE, per-clause
header, anchor/UNION ALL/recursive query, closing parens). The two
copies differed only by indent (5/10 spaces vs 4/8) and a trailing
space after the keyword.

Hoist the loop into `protected virtual void AppendWithClauseSection(
StringBuilder, string withClauseIndent, string queryBodyIndent)` on
`SqlServer.QueryBreakdown`. Each caller invokes it with its dialect's
preferred indents; Snowflake's mid-method copy is deleted entirely.

Standardizes on the no-trailing-space "WITH" form (Snowflake's) — was
"WITH " (trailing space) in the SqlServer original. Visible only as a
trailing space before the newline in non-recursive output, which no
tests assert on.

All 1180 tests stay green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thom Lamb
2026-05-27 17:23:48 -05:00
co-authored by Claude Opus 4.7
parent 620b85a61d
commit 089d4f6000
2 changed files with 66 additions and 116 deletions
@@ -451,62 +451,7 @@ public class QueryBreakdown : SqlServerQueryBreakdown
} }
} }
if (IsUsingWithClause) AppendWithClauseSection(sb, withClauseIndent: " ", queryBodyIndent: " ");
{
// Check if any WITH clause is recursive
bool hasRecursive = WithClauses.Any(wc => wc.IsRecursive);
sb.Append("WITH");
if (hasRecursive)
{
sb.Append(" RECURSIVE");
}
sb.AppendLine();
for (int i = 0; i < WithClauses.Count; i++)
{
var withClause = WithClauses[i];
if (i > 0)
{
sb.Append(',');
sb.AppendLine();
}
// Include comment if present
if (!string.IsNullOrWhiteSpace(withClause.Comment))
{
sb.AppendLine($" {withClause.Comment}");
}
// Write CTE name with optional column list
var cteName = withClause.TableName;
if (withClause.ColumnList != null && withClause.ColumnList.Count > 0)
{
var columnList = string.Join(", ", withClause.ColumnList);
cteName = $"{withClause.TableName} ({columnList})";
}
sb.AppendLine($" {cteName} AS (");
if (withClause.IsRecursive && withClause.RecursiveQuery != null)
{
// For recursive CTEs: anchor query UNION ALL recursive query
var anchorSql = withClause.Query?.GetSql(includeSetupFinish: false).Trim() ?? string.Empty;
var recursiveSql = withClause.RecursiveQuery.GetSql(includeSetupFinish: false).Trim() ?? string.Empty;
sb.AppendLine($" {anchorSql}");
sb.AppendLine(" UNION ALL");
sb.AppendLine($" {recursiveSql}");
}
else
{
// For non-recursive CTEs: just the single query
var withSql = withClause.Query?.GetSql(includeSetupFinish: false).Trim() ?? string.Empty;
sb.AppendLine($" {withSql}");
}
sb.Append(" )");
}
sb.AppendLine();
}
// Snowflake SELECT syntax // Snowflake SELECT syntax
sb.Append(StatementParser.KeywordSelect); sb.Append(StatementParser.KeywordSelect);
@@ -547,29 +547,28 @@ public class QueryBreakdown : SqlBreakdownBase, IQueryBreakdown
return mergedParams; return mergedParams;
} }
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
/// <summary> /// <summary>
/// Gets the SQL breakdown as a string (query-specific implementation). /// Renders the <c>WITH [RECURSIVE]</c> CTE section into <paramref name="sb"/> if any
/// <see cref="WithClauses"/> are present. Shared by dialects (SqlServer uses 5-space
/// outer / 10-space inner indents; Snowflake passes 4-/8-space).
/// </summary> /// </summary>
/// <returns>The SELECT SQL statement.</returns> /// <param name="sb">The builder accumulating the SQL.</param>
protected override string GetSqlBreakdown() /// <param name="withClauseIndent">Indent used for the CTE name line and the closing <c>)</c>.</param>
#pragma warning restore S3776 /// <param name="queryBodyIndent">Indent used for each anchor/recursive query body line and the <c>UNION ALL</c>.</param>
protected virtual void AppendWithClauseSection(StringBuilder sb, string withClauseIndent, string queryBodyIndent)
{ {
var sb = new StringBuilder(); if (!IsUsingWithClause)
{
return;
}
if (IsUsingWithClause)
{
// Check if any CTE is recursive - if so, add RECURSIVE keyword
bool hasRecursive = _withClauses.Any(wc => wc.IsRecursive); bool hasRecursive = _withClauses.Any(wc => wc.IsRecursive);
sb.Append("WITH"); sb.Append("WITH");
if (hasRecursive) if (hasRecursive)
{ {
sb.AppendLine("RECURSIVE"); sb.Append(" RECURSIVE");
} }
else
{
sb.AppendLine(); sb.AppendLine();
}
for (int i = 0; i < _withClauses.Count; i++) for (int i = 0; i < _withClauses.Count; i++)
{ {
@@ -582,40 +581,46 @@ public class QueryBreakdown : SqlBreakdownBase, IQueryBreakdown
sb.AppendLine(); sb.AppendLine();
} }
// Include comment if present
if (!string.IsNullOrWhiteSpace(withClause.Comment)) if (!string.IsNullOrWhiteSpace(withClause.Comment))
{ {
sb.AppendLine($" {withClause.Comment}"); sb.AppendLine($"{withClauseIndent}{withClause.Comment}");
} }
// Write CTE name with optional column list
var cteName = withClause.TableName; var cteName = withClause.TableName;
if (withClause.ColumnList != null && withClause.ColumnList.Count > 0) if (withClause.ColumnList != null && withClause.ColumnList.Count > 0)
{ {
var columnList = string.Join(", ", withClause.ColumnList); var columnList = string.Join(", ", withClause.ColumnList);
cteName = $"{withClause.TableName} ({columnList})"; cteName = $"{withClause.TableName} ({columnList})";
} }
sb.AppendLine($" {cteName} AS ("); sb.AppendLine($"{withClauseIndent}{cteName} AS (");
if (withClause.IsRecursive && withClause.RecursiveQuery != null) if (withClause.IsRecursive && withClause.RecursiveQuery != null)
{ {
// For recursive CTEs: anchor query UNION ALL recursive query
var recursiveSql = withClause.RecursiveQuery.GetSql(includeSetupFinish: false).Trim(); var recursiveSql = withClause.RecursiveQuery.GetSql(includeSetupFinish: false).Trim();
sb.AppendLine($" {anchorSql}"); sb.AppendLine($"{queryBodyIndent}{anchorSql}");
sb.AppendLine(" UNION ALL"); sb.AppendLine($"{queryBodyIndent}UNION ALL");
sb.AppendLine($" {recursiveSql}"); sb.AppendLine($"{queryBodyIndent}{recursiveSql}");
} }
else else
{ {
// For non-recursive CTEs: just the single query sb.AppendLine($"{queryBodyIndent}{anchorSql}");
sb.AppendLine($" {anchorSql}");
} }
sb.Append(" )"); sb.Append($"{withClauseIndent})");
} }
sb.AppendLine(); sb.AppendLine();
} }
/// <summary>
/// Gets the SQL breakdown as a string (query-specific implementation).
/// </summary>
/// <returns>The SELECT SQL statement.</returns>
protected override string GetSqlBreakdown()
{
var sb = new StringBuilder();
AppendWithClauseSection(sb, withClauseIndent: " ", queryBodyIndent: " ");
sb.AppendLine("SELECT "); sb.AppendLine("SELECT ");
if (!string.IsNullOrEmpty(SelectClause.Comment)) if (!string.IsNullOrEmpty(SelectClause.Comment))
{ {