refactor(dedup): second pass at src/ duplication — clear 10 of 13 remaining blocks #21

Merged
bermudalamb merged 4 commits from chore/sonarqube-src-dedup-pass2 into main 2026-05-27 17:14:01 -05:00
4 changed files with 39 additions and 52 deletions
Showing only changes of commit 507620cc04 - Show all commits
@@ -143,21 +143,12 @@ public class InsertBreakdown : SqlServerInsertBreakdown
return false;
}
// Parse INSERT statement using regex
var insertMatch = System.Text.RegularExpressions.Regex.Match(sql,
@"INSERT\s+INTO\s+([^\(\s]+)\s*\(([^\)]*)\)\s*VALUES\s*\(([^\)]*)\)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline, Strata.SqlTools.SqlBreakdown.Utilities.RegexDefaults.MatchTimeout);
if (!insertMatch.Success)
if (!Strata.SqlTools.Statements.SqlServer.ParsePreparation.TryMatchInsertSql(
sql, out var tableName, out var columnsClause, out var valuesClause, out errorMessage))
{
errorMessage = "Could not parse INSERT statement. Expected format: INSERT INTO table (columns) VALUES (values)";
return false;
}
var tableName = insertMatch.Groups[1].Value.Trim();
var columnsClause = insertMatch.Groups[2].Value.Trim();
var valuesClause = insertMatch.Groups[3].Value.Trim();
result = new InsertBreakdown(tableName, columnsClause, valuesClause, isMicrosoftSql: false)
{
SetupClauses = setupClauses,
@@ -44,35 +44,8 @@ public class UpdateBreakdown : SqlServerUpdateBreakdown
WhereClause.Comment = whereComments.Count > 0 ? string.Join(" ", whereComments) : null;
}
/// <summary>
/// Gets the SQL breakdown as a string for Snowflake.
/// </summary>
/// <returns>The UPDATE SQL statement.</returns>
protected override string GetSqlBreakdown()
{
var sb = new StringBuilder();
sb.AppendLine("UPDATE ");
sb.AppendLine($" {TableName.Clause}");
sb.AppendLine("SET ");
sb.AppendLine($" {SetClause.Clause}");
if (IsUsingFromClause)
{
// Snowflake supports FROM clause in UPDATE
sb.AppendLine("FROM ");
sb.AppendLine($" {FromClause.Clause}");
}
if (IsUsingWhereClause)
{
sb.AppendLine("WHERE ");
sb.AppendLine($" {WhereClause.Clause}");
}
return sb.ToString();
}
// GetSqlBreakdown() inherited from SqlServer.UpdateBreakdown — Snowflake's UPDATE syntax
// (including the optional FROM clause) is identical at the formatter level, so no override needed.
#region Parse Methods
@@ -152,22 +152,12 @@ public class InsertBreakdown : SqlBreakdownBase
return false;
}
// Parse INSERT statement using regex
// Pattern: INSERT INTO table (columns) VALUES (values)
var insertMatch = System.Text.RegularExpressions.Regex.Match(sql,
@"INSERT\s+INTO\s+([^\(\s]+)\s*\(([^\)]*)\)\s*VALUES\s*\(([^\)]*)\)",
System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline, Strata.SqlTools.SqlBreakdown.Utilities.RegexDefaults.MatchTimeout);
if (!insertMatch.Success)
if (!Strata.SqlTools.Statements.SqlServer.ParsePreparation.TryMatchInsertSql(
sql, out var tableName, out var columnsClause, out var valuesClause, out errorMessage))
{
errorMessage = "Could not parse INSERT statement. Expected format: INSERT INTO table (columns) VALUES (values)";
return false;
}
var tableName = insertMatch.Groups[1].Value.Trim();
var columnsClause = insertMatch.Groups[2].Value.Trim();
var valuesClause = insertMatch.Groups[3].Value.Trim();
result = new InsertBreakdown(tableName, columnsClause, valuesClause)
{
SetupClauses = setupClauses,
@@ -66,4 +66,37 @@ public static class ParsePreparation
return true;
}
/// <summary>
/// Runs the shared INSERT-statement match used by SqlServer / Snowflake (and any future
/// dialect that accepts the same <c>INSERT INTO table (cols) VALUES (vals)</c> grammar).
/// </summary>
/// <param name="sql">The SQL passed through <see cref="TryRunPrelude"/>.</param>
/// <param name="tableName">On success, the matched table name (trimmed).</param>
/// <param name="columnsClause">On success, the matched column list (trimmed).</param>
/// <param name="valuesClause">On success, the matched values list (trimmed).</param>
/// <param name="errorMessage">On failure, a human-readable parse-error message.</param>
/// <returns><c>true</c> if the regex matched and the three groups are populated; <c>false</c> otherwise.</returns>
public static bool TryMatchInsertSql(string sql, out string tableName, out string columnsClause, out string valuesClause, out string errorMessage)
{
tableName = null!;
columnsClause = null!;
valuesClause = null!;
errorMessage = null!;
var insertMatch = Regex.Match(sql,
@"INSERT\s+INTO\s+([^\(\s]+)\s*\(([^\)]*)\)\s*VALUES\s*\(([^\)]*)\)",
RegexOptions.IgnoreCase | RegexOptions.Singleline, RegexDefaults.MatchTimeout);
if (!insertMatch.Success)
{
errorMessage = "Could not parse INSERT statement. Expected format: INSERT INTO table (columns) VALUES (values)";
return false;
}
tableName = insertMatch.Groups[1].Value.Trim();
columnsClause = insertMatch.Groups[2].Value.Trim();
valuesClause = insertMatch.Groups[3].Value.Trim();
return true;
}
}