Skip to content

Commit

Permalink
## 5.3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
vbilopav committed Jul 23, 2023
1 parent a61fb0e commit 9d37ad7
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 95 deletions.
144 changes: 92 additions & 52 deletions PgRoutiner/Builder/Md/MarkdownDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public class MarkdownDocument
private static string CommentIs => $" is {Param};";
private static string StartTag(string on, string name) => $"{Open}{CommentStatement}{on} {name}{CommentIs}{Close}";
private static string EndTag => $"{Open}end{Close}";
private static string Hashtag(string name) => $"<a id=\"user-content-{name}\" href=\"#{name}\">#</a>";
//private static string Hashtag(string name) => $"<a id=\"user-content-{name}\" href=\"#{name}\">#</a>";
private static string Hashtag(string name) => $"<a id=\"user-content-{name}\" href=\"#user-content-{name}\">#</a>";

public MarkdownDocument(Current settings, NpgsqlConnection connection)
{
Expand Down Expand Up @@ -115,6 +116,7 @@ public string BuildDiff(string file)
.ToDictionary(
t => $"\"{schema}\".{t.Signature.Replace(t.Name, $"\"{t.Name}\"")}",
t => t.Comment);

dict.ToList().ForEach(x => comments.Add(x.Key, x.Value));
}

Expand Down Expand Up @@ -192,31 +194,50 @@ private void BuildRoutines(StringBuilder content, StringBuilder header, List<str
{
break;
}
var routinesHeader = false;

var routineComments = connection.GetRoutineComments(settings, schema).ToList();

var count = routineComments.Count();
if (count > 0)
{
header.AppendLine();
header.AppendLine($"### {count} Routine{(count == 1 ? "" : "s")} in Schema \"{schema}\"");
header.AppendLine();
}

foreach (var result in routineComments)
{
if (!routinesHeader)
content.AppendLine();
content.AppendLine(
$"## {result.Type.First().ToString().ToUpper()}{result.Type[1..]} \"{schema}\".\"{result.Name}\"");

content.AppendLine();
content.AppendLine(StartTag(result.Type, $"\"{schema}\".{result.Signature.Replace(result.Name, $"\"{result.Name}\"")}"));
if (result.Comment != null)
{
content.AppendLine();
content.AppendLine("## Routines");
routinesHeader = true;
content.AppendLine(string.Join($"{NL}{NL}", result.Comment?.Trim() ?? ""));
}
content.AppendLine(EndTag);


content.AppendLine();
content.AppendLine(
$"### {result.Type.First().ToString().ToUpper()}{result.Type[1..]} `{schema}.{result.Signature}`");
var routineAnchor = result.Signature.ToLower()
.Replace("(", "")
.Replace(")", "")
.Replace(",", "")
.Replace("[", "")
.Replace("]", "")
.Replace(" ", "-");
if (!settings.MdSkipToc)
{
header.AppendLine($"- {result.Type.First().ToString().ToUpper()}{result.Type[1..]} [`{schema}.{result.Signature}`](#{result.Type.ToLower()}-{schema.ToLower()}{routineAnchor})");
header.AppendLine($"- {result.Type.First().ToString().ToUpper()}{result.Type[1..]} [`{schema}.{result.Name}`](#{result.Type.ToLower()}-{schema.ToLower()}{result.Name})");
}

content.AppendLine();
content.AppendLine($"- Signature `{schema}.{result.Signature}`");

if (result.Parameters.Length > 0)
{
content.AppendLine();
content.AppendLine($"- Parameters:");
foreach(var p in result.Parameters)
{
content.AppendLine($" - `{p}`");
}
}

content.AppendLine();
content.AppendLine($"- Returns `{result.Returns}`");
content.AppendLine();
Expand All @@ -238,14 +259,14 @@ private void BuildRoutines(StringBuilder content, StringBuilder header, List<str
content.AppendLine();
}
}

content.AppendLine($"- Language is `{result.Language}`");
content.AppendLine();
if (settings.MdIncludeSourceLinks)
{
var url = GetUrl(
string.Equals(result.Type.ToLowerInvariant(), "function", StringComparison.InvariantCulture) ?
DumpType.Functions : DumpType.Procedures,
string.Equals(result.Type.ToLowerInvariant(), "function", StringComparison.InvariantCulture) ?
DumpType.Functions : DumpType.Procedures,
schema, result.Name);

content.AppendLine($"- Source: [{url}]({url})");
Expand Down Expand Up @@ -291,15 +312,15 @@ private void BuildRoutines(StringBuilder content, StringBuilder header, List<str
content.AppendLine();
}

content.AppendLine(StartTag(result.Type, $"\"{schema}\".{result.Signature.Replace(result.Name, $"\"{result.Name}\"")}"));
if (result.Comment != null)
if (settings.MdIncludeRoutineDefinitions)
{
content.AppendLine(string.Join($"{NL}{NL}", result.Comment));
content.AppendLine($"- Definition:");
content.AppendLine();
content.AppendLine("```sql");
content.AppendLine(result.Definition);
content.AppendLine("```");
}

content.AppendLine(EndTag);

content.AppendLine();
content.AppendLine("<a href=\"#table-of-contents\" title=\"Table of Contents\">&#8673;</a>");
}
}
Expand All @@ -315,16 +336,24 @@ private void BuildViews(StringBuilder content, StringBuilder header, List<string
{
break;
}
var viewsHeader = false;
//var viewsHeader = false;
var viewComments = connection.GetViewComments(settings, schema).ToList();
var count = viewComments.Count();
if (count > 0)
{
header.AppendLine();
header.AppendLine($"### {count} View{(count == 1 ? "" : "s")} in Schema \"{schema}\"");
header.AppendLine();
}

foreach (var result in viewComments)
{
if (!viewsHeader)
{
content.AppendLine();
content.AppendLine("## Views");
viewsHeader = true;
}
//if (!viewsHeader)
//{
// content.AppendLine();
// content.AppendLine("## Views");
// viewsHeader = true;
//}

string comment = null;
if (result.Column == null)
Expand All @@ -345,7 +374,7 @@ private void BuildViews(StringBuilder content, StringBuilder header, List<string
content.AppendLine();
}

content.AppendLine($"### View `{schema}.{result.Table}`");
content.AppendLine($"## View \"{schema}\".\"{result.Table}\"");
if (!settings.MdSkipToc)
{
header.AppendLine($"- View [`{schema}.{result.Table}`](#view-{schema.ToLower()}{result.Table.ToLower()})");
Expand All @@ -354,7 +383,7 @@ private void BuildViews(StringBuilder content, StringBuilder header, List<string
content.AppendLine(StartTag("view", $"\"{schema}\".\"{result.Table}\""));
if (comment != null)
{
content.AppendLine(comment);
content.AppendLine(comment?.Trim() ?? "");
}

content.AppendLine(EndTag);
Expand Down Expand Up @@ -397,7 +426,7 @@ private void BuildViews(StringBuilder content, StringBuilder header, List<string
content.AppendLine(
$"| {(result.IsPk == true ? "**" : "")}`{result.Column}`{(result.IsPk == true ? "**" : "")} " +
$"| `{result.ColumnType}`{typeMarkup}" +
$"| {StartTag("column", $"\"{schema}\".\"{result.Table}\".\"{result.Column}\"")}{comment}{EndTag} |");
$"| {StartTag("column", $"\"{schema}\".\"{result.Table}\".\"{result.Column}\"")}{comment?.Trim() ?? ""}{EndTag} |");
}
}
}
Expand All @@ -412,7 +441,6 @@ private void BuildViews(StringBuilder content, StringBuilder header, List<string
private void BuildTables(StringBuilder content, StringBuilder header, List<string> schemas)
{
var any = false;
var tablesHeader = false;
string prevTable = null;
string lastSchema = null;

Expand Down Expand Up @@ -451,14 +479,17 @@ void WriteStats(string schema, string table)
string additionalTableComment = null;
Dictionary<string, string> additionalColumnComments = new();
var tableComments = connection.GetTableComments(settings, schema).ToList();

var count = tableComments.Count();
if (count > 0)
{
header.AppendLine();
header.AppendLine($"### {count} Table{(count == 1 ? "" : "s")} in Schema \"{schema}\"");
header.AppendLine();
}

foreach (var result in tableComments)
{
if (!tablesHeader)
{
content.AppendLine("## Tables");
tablesHeader = true;
}

if (result.Column == null && additionalCommentsSql != null)
{
additionalTableComment = null;
Expand All @@ -478,7 +509,7 @@ void WriteStats(string schema, string table)
{
if (result.Comment != null || additionalCommentsSql != null)
{
comment = string.Concat($"{NL}", $"{result.Comment}{NL}{additionalTableComment}".Trim());
comment = string.Concat($"{NL}", $"{result.Comment?.Trim() ?? ""}{NL}{additionalTableComment}".Trim());
}

if (prevTable != null)
Expand All @@ -500,7 +531,7 @@ void WriteStats(string schema, string table)
content.AppendLine();
}

content.AppendLine($"### Table `{schema}.{result.Table}`");
content.AppendLine($"## Table \"{schema}\".\"{result.Table}\"");
if (!settings.MdSkipToc)
{
header.AppendLine($"- Table [`{schema}.{result.Table}`](#table-{schema.ToLower()}{result.Table.ToLower()})");
Expand All @@ -510,7 +541,7 @@ void WriteStats(string schema, string table)

if (comment != null)
{
content.AppendLine(comment);
content.AppendLine(comment?.Trim() ?? "");
}
content.AppendLine(EndTag);

Expand Down Expand Up @@ -578,7 +609,7 @@ void WriteStats(string schema, string table)
$"| `{result.ColumnType}`{typeMarkup}" +
$"| {result.Nullable} " +
$"| {result.DefaultMarkup} " +
$"| {StartTag("column", $"\"{schema}\".\"{result.Table}\".\"{result.Column}\"")}{comment}{EndTag} |");
$"| {StartTag("column", $"\"{schema}\".\"{result.Table}\".\"{result.Column}\"")}{comment?.Trim() ?? ""}{EndTag} |");
}

}
Expand All @@ -605,14 +636,23 @@ private void BuildEnums(StringBuilder content, StringBuilder header, List<string
{
break;
}
var enumComments = connection.GetEnumComments(settings, schema).ToList();
var count = enumComments.Count();
if (count > 0)
{
header.AppendLine();
header.AppendLine($"### {count} Enum{(count == 1 ? "" : "s")} in Schema \"{schema}\"");
header.AppendLine();
}

var enumHeader = false;
foreach (var result in connection.GetEnumComments(settings, schema).ToList())
foreach (var result in enumComments)
{
anyEnums = true;
if (!enumHeader)
{
content.AppendLine();
content.AppendLine("## Enums");
content.AppendLine($"## Enums in Schema \"{schema}\"");
content.AppendLine();

if (settings.MdIncludeSourceLinks)
Expand Down Expand Up @@ -640,15 +680,15 @@ private void BuildEnums(StringBuilder content, StringBuilder header, List<string
content.AppendLine(
$"| {Hashtag(name)}`{schema}.{result.Name}` " +
$"| `{result.Values}` " +
$"| {StartTag("type", $"\"{schema}\".\"{result.Name}\"")}{result.Comment}{EndTag} " +
$"| {StartTag("type", $"\"{schema}\".\"{result.Name}\"")}{result.Comment?.Trim() ?? ""}{EndTag} " +
$"| [{url}]({url}) |");
}
else
{
content.AppendLine(
$"| {Hashtag(name)}`{schema}.{result.Name}` " +
$"| `{result.Values}` " +
$"| {StartTag("type", $"\"{schema}\".\"{result.Name}\"")}{result.Comment}{EndTag} |");
$"| {StartTag("type", $"\"{schema}\".\"{result.Name}\"")}{result.Comment?.Trim() ?? ""}{EndTag} |");
}

}
Expand Down Expand Up @@ -716,7 +756,7 @@ private void BuildHeader(StringBuilder header, List<string> schemas)
if (!settings.MdSkipToc)
{
header.AppendLine("## Table of Contents");
header.AppendLine();
//header.AppendLine();
}
}

Expand Down
Loading

0 comments on commit 9d37ad7

Please sign in to comment.