refactor(dedup): share clause-with-comments ingestion (PG/Snowflake QueryBreakdown ctors)

The `QueryBreakdown(string select, string from, ...)` constructors on
PostgreSql.QueryBreakdown and Snowflake.QueryBreakdown each ran the
same six-line pattern twice (once per clause): call
`parser.ExtractSqlComments`, set the clause to the trimmed result,
join the comment list into the Comment property.

New `StatementParser.PopulateClauseWithComments(rawText, target)`
instance method does both halves. Each ctor now reads:

    parser.PopulateClauseWithComments(selectClause, SelectClause);
    parser.PopulateClauseWithComments(fromClause, FromClause);

Same behavior; the helper is a pure refactor of existing semantics.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Thom Lamb
2026-05-27 17:06:51 -05:00
co-authored by Claude Opus 4.7
parent 507620cc04
commit 85cc79d5a1
3 changed files with 20 additions and 16 deletions
@@ -36,14 +36,8 @@ public class QueryBreakdown : SqlServerQueryBreakdown
public QueryBreakdown(string selectClause, string fromClause, bool isMicrosoftSql = false) : base()
{
var parser = isMicrosoftSql ? Parser : PostgreSqlParserInstance;
var cleanSelect = parser.ExtractSqlComments(selectClause, out var selectComments);
SelectClause.Clause = cleanSelect.Trim();
SelectClause.Comment = selectComments.Count > 0 ? string.Join(" ", selectComments) : null;
var cleanFrom = parser.ExtractSqlComments(fromClause, out var fromComments);
FromClause.Clause = cleanFrom.Trim();
FromClause.Comment = fromComments.Count > 0 ? string.Join(" ", fromComments) : null;
parser.PopulateClauseWithComments(selectClause, SelectClause);
parser.PopulateClauseWithComments(fromClause, FromClause);
}
/// <summary>
@@ -39,14 +39,8 @@ public class QueryBreakdown : SqlServerQueryBreakdown
public QueryBreakdown(string selectClause, string fromClause, bool isMicrosoftSql = false) : base()
{
var parser = isMicrosoftSql ? Parser : SnowflakeParserInstance;
var cleanSelect = parser.ExtractSqlComments(selectClause, out var selectComments);
SelectClause.Clause = cleanSelect.Trim();
SelectClause.Comment = selectComments.Count > 0 ? string.Join(" ", selectComments) : null;
var cleanFrom = parser.ExtractSqlComments(fromClause, out var fromComments);
FromClause.Clause = cleanFrom.Trim();
FromClause.Comment = fromComments.Count > 0 ? string.Join(" ", fromComments) : null;
parser.PopulateClauseWithComments(selectClause, SelectClause);
parser.PopulateClauseWithComments(fromClause, FromClause);
}
/// <summary>
@@ -140,6 +140,22 @@ public class StatementParser
/// <param name="sql">The SQL statement containing comments.</param>
/// <param name="comments">The extracted comments as a list of strings.</param>
/// <returns>The SQL statement with comments removed.</returns>
/// <summary>
/// Runs <see cref="ExtractSqlComments"/> on <paramref name="rawText"/> and assigns the
/// cleaned text to <paramref name="target"/>'s <see cref="ISqlClause.Clause"/> (trimmed)
/// and the merged comments to its <see cref="ISqlClause.Comment"/>. Helper for
/// dialect-specific <c>QueryBreakdown</c> constructors that need to ingest
/// comment-bearing SQL fragments.
/// </summary>
/// <param name="rawText">The SQL fragment to clean.</param>
/// <param name="target">The clause to populate.</param>
public void PopulateClauseWithComments(string rawText, ISqlClause target)
{
var clean = ExtractSqlComments(rawText, out var comments);
target.Clause = clean.Trim();
target.Comment = comments.Count > 0 ? string.Join(" ", comments) : null;
}
#pragma warning disable S3776 // Cognitive Complexity of methods should not be too high
#pragma warning disable S127 // "for" loop stop conditions should be invariant
public virtual string ExtractSqlComments(string sql, out List<string> comments)