121 lines
5.1 KiB
C#
121 lines
5.1 KiB
C#
using Strata.SqlTools.Breakdowns.SqlServer;
|
|
using Strata.SqlTools.Markdown.Common;
|
|
|
|
namespace Strata.SqlTools.Markdown.SqlServer;
|
|
|
|
/// <summary>
|
|
/// Generates Markdown documentation from QueryBreakdownCollection objects.
|
|
/// Creates comprehensive reports including collection summaries, parameter analysis, and batch flow visualization.
|
|
/// </summary>
|
|
public static class QueryBreakdownCollectionGenerator
|
|
{
|
|
private static readonly MarkdownDialectFormat Format = new()
|
|
{
|
|
ParameterTableLabel = name => $"@{name}",
|
|
ParameterNodeLabel = name => $"@{name}",
|
|
ParameterTypeName = GetParameterType,
|
|
ParameterNodeFill = "#e1f5ff",
|
|
QueryNodeFill = "#f3e5f5",
|
|
IncludeHavingRow = true
|
|
};
|
|
|
|
/// <summary>
|
|
/// Generates a comprehensive collection report in Markdown format.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to document.</param>
|
|
/// <param name="title">Optional title for the report.</param>
|
|
/// <returns>A string containing the Markdown documentation.</returns>
|
|
public static string GenerateCollectionReport(QueryBreakdownCollection collection, string? title = null)
|
|
=> CollectionReportWriter.CollectionReport(title, new[]
|
|
{
|
|
GenerateCollectionSummary(collection),
|
|
GenerateParameterAnalysis(collection),
|
|
GenerateQueryCompositionReport(collection)
|
|
});
|
|
|
|
/// <summary>
|
|
/// Generates a summary section for the collection.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to summarize.</param>
|
|
/// <returns>Markdown summary section.</returns>
|
|
public static string GenerateCollectionSummary(QueryBreakdownCollection collection)
|
|
=> CollectionReportWriter.CollectionSummary(
|
|
collection.QueryBreakdowns.Count,
|
|
collection.GetAllUniqueParameters().Count(),
|
|
collection.GetTotalSelectedColumns(),
|
|
collection.GetUniqueTableReferences().Count());
|
|
|
|
/// <summary>
|
|
/// Generates a parameter analysis report.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to analyze.</param>
|
|
/// <returns>Markdown parameter analysis section.</returns>
|
|
public static string GenerateParameterAnalysis(QueryBreakdownCollection collection)
|
|
=> CollectionReportWriter.ParameterAnalysis(MapParameters(collection), collection.QueryBreakdowns, Format);
|
|
|
|
/// <summary>
|
|
/// Generates a Mermaid diagram showing parameter dependencies across queries.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to visualize.</param>
|
|
/// <returns>Mermaid diagram markdown.</returns>
|
|
public static string GenerateParameterDependencyDiagram(QueryBreakdownCollection collection)
|
|
=> CollectionReportWriter.ParameterDependencyDiagram(collection.QueryBreakdowns, Format);
|
|
|
|
/// <summary>
|
|
/// Generates a detailed query composition report.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to report on.</param>
|
|
/// <returns>Markdown composition report section.</returns>
|
|
public static string GenerateQueryCompositionReport(QueryBreakdownCollection collection)
|
|
=> CollectionReportWriter.QueryCompositionReport(collection.QueryBreakdowns, Format);
|
|
|
|
/// <summary>
|
|
/// Generates a batch execution flow diagram.
|
|
/// </summary>
|
|
/// <param name="collection">The QueryBreakdownCollection to visualize.</param>
|
|
/// <param name="includeTransaction">Whether to show transaction wrapping.</param>
|
|
/// <returns>Mermaid diagram markdown.</returns>
|
|
public static string GenerateBatchFlowDiagram(QueryBreakdownCollection collection, bool includeTransaction = false)
|
|
=> CollectionReportWriter.BatchFlowDiagram(
|
|
collection.QueryBreakdowns.Count,
|
|
includeTransaction ? "BEGIN TRANSACTION" : null,
|
|
includeTransaction ? "COMMIT TRANSACTION" : null);
|
|
|
|
/// <summary>
|
|
/// Maps the collection's parameter usage report into the writer's dialect-agnostic rows.
|
|
/// </summary>
|
|
private static List<ParameterUsageRow> 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();
|
|
|
|
/// <summary>
|
|
/// Gets the parameter type name from a parameter value.
|
|
/// </summary>
|
|
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"
|
|
};
|
|
}
|
|
}
|