Skip to content

Commit

Permalink
Add schema filter to database diagram
Browse files Browse the repository at this point in the history
fixes #1977
  • Loading branch information
ErikEJ committed Oct 25, 2023
1 parent 3b37512 commit 84236cb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public PickServerDatabaseDialog(
{
grdRow1.Height = new GridLength(0);
grdRow2.Height = new GridLength(0);
grdRow3.Height = new GridLength(0);
}

viewModel.CodeGenerationMode = (int)codeGeneration;
Expand Down
8 changes: 6 additions & 2 deletions src/GUI/RevEng.Core.60/DgmlBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -13,8 +14,9 @@ public class DgmlBuilder
{
private readonly string connectionString;
private readonly ServiceProvider serviceProvider;
private readonly List<string> schemas = Enumerable.Empty<string>().ToList();

public DgmlBuilder(int databaseType, string connectionString)
public DgmlBuilder(int databaseType, string connectionString, List<string> schemas)
{
if (string.IsNullOrEmpty(connectionString))
{
Expand All @@ -29,6 +31,8 @@ public DgmlBuilder(int databaseType, string connectionString)
ConnectionString = connectionString,
};

this.schemas = schemas;

serviceProvider = new ServiceCollection().AddEfpt(options, new List<string>(), new List<string>(), new List<string>()).BuildServiceProvider();
}

Expand All @@ -48,7 +52,7 @@ private DatabaseModel GetModelInternal()
{
var dbModelFactory = serviceProvider.GetService<IDatabaseModelFactory>();

var dbModelOptions = new DatabaseModelFactoryOptions();
var dbModelOptions = new DatabaseModelFactoryOptions(schemas: schemas);
var dbModel = dbModelFactory!.Create(connectionString, dbModelOptions);

return dbModel;
Expand Down
9 changes: 7 additions & 2 deletions src/GUI/Shared/Handlers/ReverseEngineer/EfRevEngLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ public async Task<List<TableModel>> GetTablesAsync(string connectionString, Data
return await GetTablesInternalAsync(arguments);
}

public async Task<string> GetDgmlAsync(string connectionString, DatabaseType databaseType)
public async Task<string> GetDgmlAsync(string connectionString, DatabaseType databaseType, List<string> schemaList)
{
var arguments = "dgml " + ((int)databaseType).ToString() + " \"" + connectionString.Replace("\"", "\\\"") + "\"";
var arguments = "dgml " + ((int)databaseType).ToString() + " \"" + connectionString.Replace("\"", "\\\"") + "\" \"" + string.Join(",", schemaList) + "\"";

if (schemaList.Count == 0)
{
arguments = "dgml " + ((int)databaseType).ToString() + " \"" + connectionString.Replace("\"", "\\\"") + "\"";
}

var filePath = await GetDgmlInternalAsync(arguments);

Expand Down
30 changes: 18 additions & 12 deletions src/GUI/Shared/Handlers/ServerDgmlHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ public async Task GenerateAsync()

try
{
var connection = await ChooseDataBaseConnectionAsync();
var info = await ChooseDataBaseConnectionAsync();

if (connection == null)
if (info.DatabaseModel == null)
{
return;
}

var connectionString = connection.ConnectionString;
var connectionString = info.DatabaseModel.ConnectionString;

if (connection.DataConnection != null)
if (info.DatabaseModel.DataConnection != null)
{
connection.DataConnection.Open();
connectionString = DataProtection.DecryptString(connection.DataConnection.EncryptedConnectionString);
info.DatabaseModel.DataConnection.Open();
connectionString = DataProtection.DecryptString(info.DatabaseModel.DataConnection.EncryptedConnectionString);
}

var dgmlPath = await GetDgmlAsync(connectionString, connection.DatabaseType);
var dgmlPath = await GetDgmlAsync(connectionString, info.DatabaseModel.DatabaseType, info.Schemas);

await ShowDgmlAsync(dgmlPath);

Expand All @@ -57,7 +57,7 @@ public async Task GenerateAsync()
}
}

private async Task<DatabaseConnectionModel> ChooseDataBaseConnectionAsync()
private async Task<(DatabaseConnectionModel DatabaseModel, SchemaInfo[] Schemas)> ChooseDataBaseConnectionAsync()
{
var vsDataHelper = new VsDataHelper();
var databaseList = await vsDataHelper.GetDataConnectionsAsync(package);
Expand Down Expand Up @@ -90,16 +90,22 @@ private async Task<DatabaseConnectionModel> ChooseDataBaseConnectionAsync()
var pickDataSourceResult = psd.ShowAndAwaitUserResponse(true);
if (!pickDataSourceResult.ClosedByOK)
{
return null;
return (null, null);
}

return pickDataSourceResult.Payload.Connection;
return (pickDataSourceResult.Payload.Connection, pickDataSourceResult.Payload.Schemas);
}

private async Task<string> GetDgmlAsync(string connectionString, DatabaseType databaseType)
private async Task<string> GetDgmlAsync(string connectionString, DatabaseType databaseType, SchemaInfo[] schemas)
{
var schemaList = Enumerable.Empty<string>().ToList();
if (schemas != null)
{
schemaList = schemas.Select(s => s.Name).ToList();
}

var launcher = new EfRevEngLauncher(null, CodeGenerationMode.EFCore6);
return await launcher.GetDgmlAsync(connectionString, databaseType);
return await launcher.GetDgmlAsync(connectionString, databaseType, schemaList);
}

private async Task ShowDgmlAsync(string path)
Expand Down
11 changes: 9 additions & 2 deletions src/GUI/efreveng60/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ public static async System.Threading.Tasks.Task<int> Main(string[] args)
return 0;
}

if (args.Length == 3 && args[0] == "dgml" && int.TryParse(args[1], out int dbType))
if ((args.Length == 3 || args.Length == 4)
&& args[0] == "dgml" && int.TryParse(args[1], out int dbType))
{
var builder = new DgmlBuilder(dbType, args[2]);
var schemas = Enumerable.Empty<string>().ToList();
if (args.Length == 4)
{
schemas = args[3].Split(',', StringSplitOptions.RemoveEmptyEntries).Select(s => s).ToList();
}

var builder = new DgmlBuilder(dbType, args[2], schemas);

var buildResult = builder.GetDgmlFileName();

Expand Down
Binary file modified src/GUI/lib/efreveng60.exe.zip
Binary file not shown.

0 comments on commit 84236cb

Please sign in to comment.