Files
sql-utilities/src/Strata.SqlTools.Markdown/LinqToSql/QueryBreakdownGenerator.cs
T
Thom LambandClaude Opus 4.7 6175dcde96
SonarQube Analysis / sonarqube (pull_request) Successful in 2m50s
chore(sonar)!: cascade CA1822 through Markdown dialect wrappers
Commit 5202d93 made the SqlServer-namespaced Markdown generator methods
static, which left the LinqToSql / PostgreSql / Snowflake wrapper
classes' instance methods delegating to nothing but a static call.
SonarQube re-flagged those 10 wrapper methods as CA1822 on the next
scan.

This sweep:

- Makes all 10 wrapper instance methods `static` (`dotnet format` driven).
- Makes `Markdown.LinqToSql.QueryBreakdownGenerator.GenerateCombinedDiagram`
  static preemptively — it composes two static helpers and would otherwise
  be the next-iteration cascade flag.
- Removes the now-dead `_baseGenerator` field and its initializing
  constructor from all six dialect wrappers (LinqToSql / PostgreSql /
  Snowflake × QueryBreakdownGenerator + SqlStatementGenerator). The
  classes keep their implicit parameterless constructor so `new
  Snowflake.QueryBreakdownGenerator()` still compiles.
- Updates the one test call site (`GenerateCombinedDiagram`) the fixer
  didn't catch to use type-name form.

BREAKING CHANGE: External NuGet consumers calling
`instance.Generate*Diagram(...)` on `Markdown.LinqToSql.*`,
`Markdown.PostgreSql.*`, or `Markdown.Snowflake.*` generators must
switch to type-name form, e.g. `Markdown.Snowflake.SqlStatementGenerator.GenerateSequenceDiagram(...)`.
The class types and parameterless constructors remain — only the call
syntax for these methods changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-27 14:38:58 -05:00

101 lines
3.6 KiB
C#

using Strata.SqlTools.Breakdowns.LinqToSql;
namespace Strata.SqlTools.Markdown.LinqToSql;
/// <summary>
/// Generates Mermaid diagram markdown from LINQ to SQL QueryBreakdown objects.
/// Creates flowchart visualizations showing the LINQ query structure and flow.
/// </summary>
public class QueryBreakdownGenerator
{
/// <summary>
/// Generates a Mermaid flowchart diagram from a LINQ to SQL QueryBreakdown.
/// </summary>
/// <param name="queryBreakdown">The LINQ QueryBreakdown to visualize.</param>
/// <param name="title">Optional title for the diagram.</param>
/// <returns>A string containing the Mermaid markdown diagram.</returns>
public static string GenerateMermaidDiagram(LinqQueryBreakdown queryBreakdown, string? title = null)
{
// Since LinqQueryBreakdown inherits from SqlServer.QueryBreakdown,
// we can use the base generator which works with the shared properties
return SqlServer.QueryBreakdownGenerator.GenerateMermaidDiagram(queryBreakdown, title);
}
/// <summary>
/// Generates a Mermaid diagram showing the LINQ method call chain.
/// </summary>
/// <param name="queryBreakdown">The LINQ QueryBreakdown to visualize.</param>
/// <param name="title">Optional title for the diagram.</param>
/// <returns>A string containing the Mermaid flowchart showing method calls.</returns>
public static string GenerateMethodChainDiagram(LinqQueryBreakdown queryBreakdown, string? title = null)
{
var sb = new System.Text.StringBuilder();
if (!string.IsNullOrWhiteSpace(title))
{
sb.AppendLine($"### {title}");
sb.AppendLine();
}
sb.AppendLine("```mermaid");
sb.AppendLine("flowchart LR");
sb.AppendLine();
if (queryBreakdown.MethodCallChain.Count == 0)
{
sb.AppendLine(" Start([IQueryable]) --> End([Result])");
}
else
{
sb.AppendLine(" Start([IQueryable])");
for (int i = 0; i < queryBreakdown.MethodCallChain.Count; i++)
{
var method = queryBreakdown.MethodCallChain[i];
var nodeId = $"M{i}";
var prevNodeId = i == 0 ? "Start" : $"M{i - 1}";
sb.AppendLine($" {nodeId}[\"{method}\"]");
sb.AppendLine($" {prevNodeId} --> {nodeId}");
}
var lastNodeId = $"M{queryBreakdown.MethodCallChain.Count - 1}";
sb.AppendLine($" {lastNodeId} --> End([Result])");
}
sb.AppendLine("```");
sb.AppendLine();
return sb.ToString();
}
/// <summary>
/// Generates a combined diagram showing both the query structure and method chain.
/// </summary>
/// <param name="queryBreakdown">The LINQ QueryBreakdown to visualize.</param>
/// <param name="title">Optional title for the diagram.</param>
/// <returns>A string containing both diagrams.</returns>
public static string GenerateCombinedDiagram(LinqQueryBreakdown queryBreakdown, string? title = null)
{
var sb = new System.Text.StringBuilder();
if (!string.IsNullOrWhiteSpace(title))
{
sb.AppendLine($"## {title}");
sb.AppendLine();
}
// Method chain
sb.AppendLine("### LINQ Method Chain");
sb.AppendLine();
sb.Append(GenerateMethodChainDiagram(queryBreakdown));
// SQL Structure
sb.AppendLine("### SQL Query Structure");
sb.AppendLine();
sb.Append(GenerateMermaidDiagram(queryBreakdown));
return sb.ToString();
}
}