forked from david-zoltan/dbdiagramio-mssql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcc7e00
commit afd5c85
Showing
19 changed files
with
329 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<ApplicationIcon>flow-diagram.ico</ApplicationIcon> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\DbDiagramIo.MsSql\DbDiagramIo.MsSql.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="CommandLineParser" Version="2.9.1" /> | ||
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
using CommandLine; | ||
using DbDiagramIo.Core; | ||
using DbDiagramIo.Core.Extensions; | ||
using DbDiagramIo.MsSql; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
Log.Logger = new LoggerConfiguration() | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
Log.Information("DbDiagramIo.CLI v{Version}", Assembly.GetExecutingAssembly().GetName().Version); | ||
|
||
Parser.Default.ParseArguments<Options>(args) | ||
.WithParsed(o => | ||
{ | ||
Log.Logger = new LoggerConfiguration() | ||
.MinimumLevel.Is(o.LogEventLevel) | ||
.WriteTo.Console() | ||
.CreateLogger(); | ||
|
||
if (!string.IsNullOrEmpty(o.ConnectionString)) | ||
{ | ||
var schemaReader = new MsSqlSchemaReader(o.ConnectionString); | ||
var schema = schemaReader.GetDbmlSchema(); | ||
var dbml = new StringBuilder(); | ||
|
||
var regexOptions = RegexOptions.IgnoreCase; | ||
var excludedSchemas = o.ExcludedSchemas?.Where(s => !string.IsNullOrEmpty(s)).ToList(); | ||
var excludedTables = o.ExcludedTables?.Where(s => !string.IsNullOrEmpty(s)).ToList(); | ||
|
||
Log.Debug("{Count} tables in total", schema.Tables.Count); | ||
|
||
schema.Tables | ||
.Where(t => excludedSchemas.AllSafe(s => !t.Schema.Like(s, regexOptions), true) && excludedTables.AllSafe(s => !t.Name.Like(s, regexOptions), true)) | ||
.ToList() | ||
.ForEach(t => dbml.AppendLine(t.ToDbml())); | ||
|
||
Log.Debug("{Count} foreign keys in total", schema.ForeignKeys.Count); | ||
|
||
schema.ForeignKeys | ||
.Where(f => o.ExcludedSchemas.AllSafe(s => !f.SourceTable.Schema.Like(s, regexOptions), true) && o.ExcludedTables.AllSafe(s => !f.SourceTable.Name.Like(s, regexOptions), true) | ||
&& o.ExcludedSchemas.AllSafe(s => !f.TargetTable.Schema.Like(s, regexOptions), true) && o.ExcludedTables.AllSafe(s => !f.TargetTable.Name.Like(s, regexOptions), true)) | ||
.ToList() | ||
.ForEach(t => dbml.AppendLine(t.ToDbml())); | ||
|
||
if (!string.IsNullOrEmpty(o.OutputFile)) | ||
{ | ||
var dirName = Path.GetDirectoryName(o.OutputFile); | ||
if (!string.IsNullOrEmpty(dirName) && !Directory.Exists(dirName)) | ||
{ | ||
Directory.CreateDirectory(dirName); | ||
} | ||
|
||
|
||
File.WriteAllText(o.OutputFile, dbml.ToString()); | ||
|
||
Log.Information("Dbml file created at {OutputFile}", o.OutputFile); | ||
} | ||
else | ||
{ | ||
Log.Information("Dbml output:"); | ||
Console.WriteLine(dbml.ToString()); | ||
} | ||
} | ||
else | ||
{ | ||
Log.Error("No connection string provided!"); | ||
} | ||
}); | ||
|
||
public class Options | ||
{ | ||
[Option('v', "verbosity", Required = false, HelpText = "Sets the serilog log level.", Default = LogEventLevel.Information)] | ||
public LogEventLevel LogEventLevel { get; set; } | ||
|
||
[Option('c', "connection-string", Required = true, HelpText = "The connection string to the database.")] | ||
public string ConnectionString { get; set; } = null!; | ||
|
||
[Option("exclude-schemas", Required = false, HelpText = "The schemas to exclude from the dbml output. You can use LIKE syntax here.", Separator = ';')] | ||
public IEnumerable<string>? ExcludedSchemas { get; set; } | ||
|
||
[Option("exclude-tables", Required = false, HelpText = "The schemas to exclude from the dbml output. You can use LIKE syntax here e.g. 'AspNet%' to exclude all tables starting with 'AspNet'.", Separator = ';')] | ||
public IEnumerable<string>? ExcludedTables { get; set; } | ||
|
||
[Option('o', "output-file", Required = false, HelpText = "The file to write the dbml output to. If not provided, the output will be written to the console.")] | ||
public string? OutputFile { get; set; } | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="3.1.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace DbDiagramIo.Core | ||
{ | ||
public static class EnumerableExtensions | ||
{ | ||
public static bool AllSafe<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate, bool valueIfNull = false) | ||
{ | ||
return source?.All(predicate) ?? valueIfNull; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace DbDiagramIo.Core.Extensions | ||
{ | ||
public static class StringExtensions | ||
{ | ||
public static bool Like(this string toSearch, string toFind, RegexOptions regexOptions = RegexOptions.None) | ||
{ | ||
return new Regex(@"\A" + new Regex(@"\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\") | ||
.Replace(toFind, ch => @"\" + ch) | ||
.Replace('_', '.') | ||
.Replace("%", ".*") + @"\z", RegexOptions.Singleline | regexOptions) | ||
.IsMatch(toSearch); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Collections.Generic; | ||
using DbDiagramIo.MsSql.Objects; | ||
using DbDiagramIo.MsSql.Objects.Base; | ||
|
||
namespace DbDiagramIo.MsSql | ||
{ | ||
public class DbmlSchema : IDbmlObject | ||
{ | ||
public HashSet<Table> Tables { get; } | ||
public HashSet<ForeignKey> ForeignKeys { get; } | ||
|
||
public DbmlSchema(HashSet<Table> tables, HashSet<ForeignKey> foreignKeys) | ||
{ | ||
Tables = tables; | ||
ForeignKeys = foreignKeys; | ||
} | ||
|
||
public string ToDbml() | ||
{ | ||
throw new System.NotImplementedException(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.