using Strata.SqlTools.Breakdowns.SqlServer; using Strata.SqlTools.Markdown.Common; namespace Strata.SqlTools.Markdown.SqlServer; /// /// Generates Markdown documentation from QueryBreakdownCollection objects. /// Creates comprehensive reports including collection summaries, parameter analysis, and batch flow visualization. /// public static class QueryBreakdownCollectionGenerator { private static readonly MarkdownDialectFormat Format = new() { ParameterTableLabel = name => $"@{name}", ParameterNodeLabel = name => $"@{name}", ParameterTypeName = GetParameterType, ParameterNodeFill = "#e1f5ff", QueryNodeFill = "#f3e5f5", IncludeHavingRow = true }; /// /// Generates a comprehensive collection report in Markdown format. /// /// The QueryBreakdownCollection to document. /// Optional title for the report. /// A string containing the Markdown documentation. public static string GenerateCollectionReport(QueryBreakdownCollection collection, string? title = null) => CollectionReportWriter.CollectionReport(title, new[] { GenerateCollectionSummary(collection), GenerateParameterAnalysis(collection), GenerateQueryCompositionReport(collection) }); /// /// Generates a summary section for the collection. /// /// The QueryBreakdownCollection to summarize. /// Markdown summary section. public static string GenerateCollectionSummary(QueryBreakdownCollection collection) => CollectionReportWriter.CollectionSummary( collection.QueryBreakdowns.Count, collection.GetAllUniqueParameters().Count(), collection.GetTotalSelectedColumns(), collection.GetUniqueTableReferences().Count()); /// /// Generates a parameter analysis report. /// /// The QueryBreakdownCollection to analyze. /// Markdown parameter analysis section. public static string GenerateParameterAnalysis(QueryBreakdownCollection collection) => CollectionReportWriter.ParameterAnalysis(MapParameters(collection), collection.QueryBreakdowns, Format); /// /// Generates a Mermaid diagram showing parameter dependencies across queries. /// /// The QueryBreakdownCollection to visualize. /// Mermaid diagram markdown. public static string GenerateParameterDependencyDiagram(QueryBreakdownCollection collection) => CollectionReportWriter.ParameterDependencyDiagram(collection.QueryBreakdowns, Format); /// /// Generates a detailed query composition report. /// /// The QueryBreakdownCollection to report on. /// Markdown composition report section. public static string GenerateQueryCompositionReport(QueryBreakdownCollection collection) => CollectionReportWriter.QueryCompositionReport(collection.QueryBreakdowns, Format); /// /// Generates a batch execution flow diagram. /// /// The QueryBreakdownCollection to visualize. /// Whether to show transaction wrapping. /// Mermaid diagram markdown. public static string GenerateBatchFlowDiagram(QueryBreakdownCollection collection, bool includeTransaction = false) => CollectionReportWriter.BatchFlowDiagram( collection.QueryBreakdowns.Count, includeTransaction ? "BEGIN TRANSACTION" : null, includeTransaction ? "COMMIT TRANSACTION" : null); /// /// Maps the collection's parameter usage report into the writer's dialect-agnostic rows. /// private static List MapParameters(QueryBreakdownCollection collection) => collection.GetParameterUsageReport() .Select(p => new ParameterUsageRow { ParameterName = p.ParameterName, IsUsedInAllQueries = p.IsUsedInAllQueries, UsedInQueryCount = p.UsedInQueryCount, TotalQueries = p.TotalQueries, Value = p.Value }) .ToList(); /// /// Gets the parameter type name from a parameter value. /// private static string GetParameterType(object? value) { return value switch { null => "NULL", bool => "BIT", byte => "TINYINT", short => "SMALLINT", int => "INT", long => "BIGINT", float => "REAL", double => "FLOAT", decimal => "DECIMAL", string => "NVARCHAR", DateTime => "DATETIME2", _ => "VARIANT" }; } }