refactor(dedup): final pass — clear the last 4 src/ duplicate blocks #22

Merged
bermudalamb merged 2 commits from chore/sonarqube-src-dedup-final into main 2026-05-27 17:32:37 -05:00
2 changed files with 66 additions and 116 deletions
Showing only changes of commit 089d4f6000 - Show all commits
@@ -451,62 +451,7 @@ public class QueryBreakdown : SqlServerQueryBreakdown
}
}
if (IsUsingWithClause)
{
// 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();
}
AppendWithClauseSection(sb, withClauseIndent: " ", queryBodyIndent: " ");
// Snowflake SELECT syntax
sb.Append(StatementParser.KeywordSelect);
@@ -547,29 +547,28 @@ public class QueryBreakdown : SqlBreakdownBase, IQueryBreakdown
return mergedParams;
}
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
/// <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>
/// <returns>The SELECT SQL statement.</returns>
protected override string GetSqlBreakdown()
#pragma warning restore S3776
/// <param name="sb">The builder accumulating the SQL.</param>
/// <param name="withClauseIndent">Indent used for the CTE name line and the closing <c>)</c>.</param>
/// <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);
sb.Append("WITH ");
sb.Append("WITH");
if (hasRecursive)
{
sb.AppendLine("RECURSIVE");
sb.Append(" RECURSIVE");
}
else
{
sb.AppendLine();
}
for (int i = 0; i < _withClauses.Count; i++)
{
@@ -582,40 +581,46 @@ public class QueryBreakdown : SqlBreakdownBase, IQueryBreakdown
sb.AppendLine();
}
// Include comment if present
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;
if (withClause.ColumnList != null && withClause.ColumnList.Count > 0)
{
var columnList = string.Join(", ", withClause.ColumnList);
cteName = $"{withClause.TableName} ({columnList})";
}
sb.AppendLine($" {cteName} AS (");
sb.AppendLine($"{withClauseIndent}{cteName} AS (");
if (withClause.IsRecursive && withClause.RecursiveQuery != null)
{
// For recursive CTEs: anchor query UNION ALL recursive query
var recursiveSql = withClause.RecursiveQuery.GetSql(includeSetupFinish: false).Trim();
sb.AppendLine($" {anchorSql}");
sb.AppendLine(" UNION ALL");
sb.AppendLine($" {recursiveSql}");
sb.AppendLine($"{queryBodyIndent}{anchorSql}");
sb.AppendLine($"{queryBodyIndent}UNION ALL");
sb.AppendLine($"{queryBodyIndent}{recursiveSql}");
}
else
{
// For non-recursive CTEs: just the single query
sb.AppendLine($" {anchorSql}");
sb.AppendLine($"{queryBodyIndent}{anchorSql}");
}
sb.Append(" )");
sb.Append($"{withClauseIndent})");
}
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 ");
if (!string.IsNullOrEmpty(SelectClause.Comment))
{