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.
///
public static string GenerateCollectionReport(QueryBreakdownCollection collection, string? title = null)
=> CollectionMarkdownGenerator.GenerateCollectionReport(Adapt(collection), Format, title);
///
/// Generates a summary section for the collection.
///
public static string GenerateCollectionSummary(QueryBreakdownCollection collection)
=> CollectionMarkdownGenerator.GenerateCollectionSummary(Adapt(collection));
///
/// Generates a parameter analysis report.
///
public static string GenerateParameterAnalysis(QueryBreakdownCollection collection)
=> CollectionMarkdownGenerator.GenerateParameterAnalysis(Adapt(collection), Format);
///
/// Generates a Mermaid diagram showing parameter dependencies across queries.
///
public static string GenerateParameterDependencyDiagram(QueryBreakdownCollection collection)
=> CollectionMarkdownGenerator.GenerateParameterDependencyDiagram(Adapt(collection), Format);
///
/// Generates a detailed query composition report.
///
public static string GenerateQueryCompositionReport(QueryBreakdownCollection collection)
=> CollectionMarkdownGenerator.GenerateQueryCompositionReport(Adapt(collection), Format);
///
/// Generates a batch execution flow diagram.
///
public static string GenerateBatchFlowDiagram(QueryBreakdownCollection collection, bool includeTransaction = false)
=> CollectionMarkdownGenerator.GenerateBatchFlowDiagram(
Adapt(collection),
includeTransaction ? "BEGIN TRANSACTION" : null,
includeTransaction ? "COMMIT TRANSACTION" : null);
private static ICollectionMarkdownData Adapt(QueryBreakdownCollection collection) => new Adapter(collection);
private sealed class Adapter : ICollectionMarkdownData
{
private readonly QueryBreakdownCollection _c;
public Adapter(QueryBreakdownCollection c) { _c = c; }
public int QueryCount => _c.QueryBreakdowns.Count;
public int UniqueParameterCount => _c.GetAllUniqueParameters().Count();
public int TotalSelectedColumns => _c.GetTotalSelectedColumns();
public int UniqueTableCount => _c.GetUniqueTableReferences().Count();
public IReadOnlyList QueriesForReport => _c.QueryBreakdowns;
public IEnumerable ParameterRows
=> _c.GetParameterUsageReport().Select(p => new ParameterUsageRow
{
ParameterName = p.ParameterName,
IsUsedInAllQueries = p.IsUsedInAllQueries,
UsedInQueryCount = p.UsedInQueryCount,
TotalQueries = p.TotalQueries,
Value = p.Value
});
}
///
/// 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"
};
}
}