chore: initial git load of code space

This commit is contained in:
Thom Lamb
2026-05-12 08:52:33 -05:00
parent 9abada692f
commit 5e467bcc9c
384 changed files with 65960 additions and 2 deletions
@@ -0,0 +1,207 @@
using System.Text;
using Strata.SqlTools.Breakdowns.SqlServer;
using Strata.SqlTools.SqlBreakdown.Interfaces;
namespace Strata.SqlTools.Markdown.SqlServer;
/// <summary>
/// Generates Mermaid diagram markdown from SQL QueryBreakdown objects.
/// Creates flowchart visualizations showing the query structure and flow.
/// </summary>
public class QueryBreakdownGenerator
{
/// <summary>
/// Generates a Mermaid flowchart diagram from a QueryBreakdown.
/// </summary>
/// <param name="queryBreakdown">The QueryBreakdown to visualize.</param>
/// <param name="title">Optional title for the diagram.</param>
/// <returns>A string containing the Mermaid markdown diagram.</returns>
public string GenerateMermaidDiagram(QueryBreakdown queryBreakdown, string? title = null)
{
var sb = new StringBuilder();
// Add title if provided
if (!string.IsNullOrWhiteSpace(title))
{
sb.AppendLine($"### {title}");
sb.AppendLine();
}
// Start Mermaid flowchart
sb.AppendLine("```mermaid");
sb.AppendLine("flowchart TD");
sb.AppendLine();
int nodeId = 1;
// Start node
sb.AppendLine($" Start([Query Start]) --> Node{nodeId}");
sb.AppendLine();
// WITH clause (CTE)
if (queryBreakdown.IsUsingWithClause)
{
sb.AppendLine($" Node{nodeId}[\"WITH Clause<br/>Common Table Expressions\"]");
foreach (var withClause in queryBreakdown.WithClauses)
{
sb.AppendLine($" Node{nodeId} --> CTE{nodeId}[\"{EscapeMermaidText(withClause.TableName)}\"]");
nodeId++;
}
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// SELECT clause
if (!string.IsNullOrEmpty(queryBreakdown.SelectClause.Clause))
{
var selectText = TruncateText(queryBreakdown.SelectClause.Clause, 50);
sb.AppendLine($" Node{nodeId}[\"SELECT<br/>{EscapeMermaidText(selectText)}\"]");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// FROM clause
if (queryBreakdown.IsUsingFromClause && !string.IsNullOrWhiteSpace(queryBreakdown.FromClause?.Clause))
{
var fromText = TruncateText(queryBreakdown.FromClause.Clause, 50);
sb.AppendLine($" Node{nodeId}[\"FROM<br/>{EscapeMermaidText(fromText)}\"]");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// WHERE clause
if (queryBreakdown.IsUsingWhereClause && !string.IsNullOrWhiteSpace(queryBreakdown.WhereClause?.Clause))
{
var whereText = TruncateText(queryBreakdown.WhereClause.Clause, 50);
sb.AppendLine($" Node{nodeId}{{\"WHERE<br/>{EscapeMermaidText(whereText)}\"}}");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// GROUP BY clause
if (queryBreakdown.IsUsingGroupByClause && !string.IsNullOrWhiteSpace(queryBreakdown.GroupByClause?.Clause))
{
var groupByText = TruncateText(queryBreakdown.GroupByClause.Clause, 50);
sb.AppendLine($" Node{nodeId}[\"GROUP BY<br/>{EscapeMermaidText(groupByText)}\"]");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// HAVING clause
if (queryBreakdown.IsUsingHavingClause && !string.IsNullOrWhiteSpace(queryBreakdown.HavingClause?.Clause))
{
var havingText = TruncateText(queryBreakdown.HavingClause.Clause, 50);
sb.AppendLine($" Node{nodeId}{{\"HAVING<br/>{EscapeMermaidText(havingText)}\"}}");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// ORDER BY clause
if (queryBreakdown.IsUsingOrderByClause && !string.IsNullOrWhiteSpace(queryBreakdown.OrderByClause?.Clause))
{
var orderByText = TruncateText(queryBreakdown.OrderByClause.Clause, 50);
sb.AppendLine($" Node{nodeId}[\"ORDER BY<br/>{EscapeMermaidText(orderByText)}\"]");
sb.AppendLine($" Node{nodeId - 1} --> Node{nodeId}");
sb.AppendLine();
nodeId++;
}
// End node
sb.AppendLine($" Node{nodeId - 1} --> End([Query End])");
// End Mermaid diagram
sb.AppendLine("```");
return sb.ToString();
}
/// <summary>
/// Generates a Mermaid flowchart diagram from any SQL breakdown implementing ISqlBreakdown.
/// </summary>
/// <param name="sqlBreakdown">The SQL breakdown to visualize.</param>
/// <param name="title">Optional title for the diagram.</param>
/// <returns>A string containing the Mermaid markdown diagram.</returns>
public string GenerateMermaidDiagram(ISqlBreakdown sqlBreakdown, string? title = null)
{
// If it's a QueryBreakdown, use the specialized method
if (sqlBreakdown is QueryBreakdown qb)
{
return GenerateMermaidDiagram(qb, title);
}
// For other SQL breakdowns, generate a simple diagram
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(title))
{
sb.AppendLine($"### {title}");
sb.AppendLine();
}
sb.AppendLine("```mermaid");
sb.AppendLine("flowchart TD");
sb.AppendLine();
sb.AppendLine(" Start([SQL Statement Start])");
if (sqlBreakdown.IsUsingSetupClause)
{
sb.AppendLine(" Start --> Setup[\"Setup Clauses\"]");
sb.AppendLine(" Setup --> Main[\"Main Statement\"]");
}
else
{
sb.AppendLine(" Start --> Main[\"Main Statement\"]");
}
if (sqlBreakdown.IsUsingFinishClause)
{
sb.AppendLine(" Main --> Finish[\"Finish Clauses\"]");
sb.AppendLine(" Finish --> End([SQL Statement End])");
}
else
{
sb.AppendLine(" Main --> End([SQL Statement End])");
}
sb.AppendLine("```");
return sb.ToString();
}
/// <summary>
/// Escapes text for Mermaid diagram labels to prevent syntax errors.
/// </summary>
private string EscapeMermaidText(string text)
{
return text
.Replace("\"", "&quot;")
.Replace("[", "&#91;")
.Replace("]", "&#93;")
.Replace("{", "&#123;")
.Replace("}", "&#125;")
.Replace("(", "&#40;")
.Replace(")", "&#41;")
.Replace("<", "&lt;")
.Replace(">", "&gt;");
}
/// <summary>
/// Truncates text to a maximum length and adds ellipsis if needed.
/// </summary>
private string TruncateText(string text, int maxLength)
{
if (string.IsNullOrWhiteSpace(text) || text.Length <= maxLength)
{
return text;
}
return text.Substring(0, maxLength) + "...";
}
}