Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix br #720

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand All @@ -36,7 +37,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>EntityFramework_Reverse_POCO_Generator</RootNamespace>
<AssemblyName>EntityFramework Reverse POCO Generator</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<GeneratePkgDefFile>false</GeneratePkgDefFile>
<IncludeAssemblyInVSIXContainer>false</IncludeAssemblyInVSIXContainer>
<IncludeDebugSymbolsInVSIXContainer>false</IncludeDebugSymbolsInVSIXContainer>
Expand Down
360 changes: 319 additions & 41 deletions EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions Generator.Tests.Unit/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,36 @@ public void SetUp()

// Stored procedure
[TestCase("ab", FilterType.StoredProcedure, false)]

// Enum
[TestCase("Enum.PriceType", FilterType.EnumTable, false)]
[TestCase("Enum.ProductType", FilterType.EnumTable, true)]
public void IsTypeExcluded(string name, FilterType filterType, bool expectedExclusion)
{
var item = CreateType(name, filterType);
var isExcluded = _sut.IsExcluded(item);
Assert.AreEqual(expectedExclusion, isExcluded);
}

private EntityName CreateType(string name, FilterType filterType)
private static EntityName CreateType(string name, FilterType filterType)
{
string[] split;
switch (filterType)
{
case FilterType.Schema:
return new Schema(name);
case FilterType.Table:
var split = name.Split('.');
split = name.Split('.');
return new Table(null, new Schema(split[0]), split[1], false);
case FilterType.Column:
return new Column { DbName = name };
case FilterType.StoredProcedure:
return new StoredProcedure { Schema = new Schema("dbo"), DbName = name };
case FilterType.EnumSchema:
return new EnumSchemaSource(name);
case FilterType.EnumTable:
split = name.Split('.');
return new EnumTableSource(new EnumSchemaSource(split[0]), split[1]);
default:
throw new ArgumentOutOfRangeException(nameof(filterType), filterType, null);
}
Expand Down
10 changes: 10 additions & 0 deletions Generator.Tests.Unit/TestDbContextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ public class TestContextFilter : SingleContextFilter
{
public TestContextFilter()
{
EnumerationSchemaFilters.AddRange(new List<IFilterType<EnumSchemaSource>>
{
new RegexIncludeFilter("^Enum$")
});

EnumerationTableFilters.AddRange(new List<IFilterType<EnumTableSource>>
{
new RegexExcludeFilter("^ProductType$")
});

SchemaFilters.AddRange(new List<IFilterType<Schema>>
{
// Only include the schemas 'dbo' and 'events'
Expand Down
10 changes: 10 additions & 0 deletions Generator/EnumSchemaSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Efrpg
{
public class EnumSchemaSource : EntityName
{
public EnumSchemaSource(string dbName)
{
DbName = dbName;
}
}
}
13 changes: 13 additions & 0 deletions Generator/EnumTableSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Efrpg
{
public class EnumTableSource : EntityName
{
public EnumSchemaSource Schema;

public EnumTableSource(EnumSchemaSource schema, string dbName)
{
Schema = schema;
DbName = dbName;
}
}
}
18 changes: 18 additions & 0 deletions Generator/Filtering/EnumerationSchemaFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Efrpg.Filtering
{
public class EnumerationSchemaFilter : IFilterType<EnumSchemaSource>
{
// Filtering of stored procedures using a function.
// Return true to exclude the stored procedure, return false to include it.
public bool IsExcluded(EnumSchemaSource enumSchemaSource)
{
// Example: Exclude any stored procedure in dbo schema with "order" in its name.
//if(sp.Schema.DbName.Equals("dbo", StringComparison.InvariantCultureIgnoreCase) && sp.NameHumanCase.ToLowerInvariant().Contains("order"))
// return false;

return false;
}
}
}
18 changes: 18 additions & 0 deletions Generator/Filtering/EnumerationTableFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Efrpg.Filtering
{
public class EnumerationTableFilter : IFilterType<EnumTableSource>
{
// Filtering of stored procedures using a function.
// Return true to exclude the stored procedure, return false to include it.
public bool IsExcluded(EnumTableSource enumTableSource)
{
// Example: Exclude any stored procedure in dbo schema with "order" in its name.
//if(sp.Schema.DbName.Equals("dbo", StringComparison.InvariantCultureIgnoreCase) && sp.NameHumanCase.ToLowerInvariant().Contains("order"))
// return false;

return false;
}
}
}
28 changes: 28 additions & 0 deletions Generator/Filtering/FilterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,29 @@ public static class FilterSettings
public static readonly List<IFilterType<Table>> TableFilters;
public static readonly List<IFilterType<Column>> ColumnFilters;
public static readonly List<IFilterType<StoredProcedure>> StoredProcedureFilters;
public static readonly List<IFilterType<EnumTableSource>> EnumerationTableFilters;
public static readonly List<IFilterType<EnumSchemaSource>> EnumerationSchemaFilters;

static FilterSettings()
{
SchemaFilters = new List<IFilterType<Schema>>();
TableFilters = new List<IFilterType<Table>>();
ColumnFilters = new List<IFilterType<Column>>();
StoredProcedureFilters = new List<IFilterType<StoredProcedure>>();
EnumerationTableFilters = new List<IFilterType<EnumTableSource>>();
EnumerationSchemaFilters = new List<IFilterType<EnumSchemaSource>>();
}



public static void Reset()
{
SchemaFilters .RemoveAll(x => true);
TableFilters .RemoveAll(x => true);
ColumnFilters .RemoveAll(x => true);
StoredProcedureFilters.RemoveAll(x => true);
EnumerationTableFilters.RemoveAll(x => true);
EnumerationSchemaFilters.RemoveAll(x => true);
}

public static void AddDefaults()
Expand All @@ -51,6 +59,8 @@ public static void AddDefaults()
AddDefaultTableFilters();
AddDefaultColumnFilters();
AddDefaultStoredProcedureFilters();
AddDefaultEnumerationTableFilters();
AddDefaultEnumerationSchemaFilters();
}

public static void CheckSettings()
Expand Down Expand Up @@ -121,5 +131,23 @@ public static void AddDefaultStoredProcedureFilters()
new HasNameFilter(FilterType.StoredProcedure)
});
}

public static void AddDefaultEnumerationTableFilters()
{
EnumerationTableFilters.AddRange(new List<IFilterType<EnumTableSource>>
{
// Add your own code to these custom filter classes
new EnumerationTableFilter(),
});
}

public static void AddDefaultEnumerationSchemaFilters()
{
EnumerationSchemaFilters.AddRange(new List<IFilterType<EnumSchemaSource>>
{
// Add your own code to these custom filter classes
new EnumerationSchemaFilter(),
});
}
}
}
4 changes: 3 additions & 1 deletion Generator/Filtering/FilterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public enum FilterType
Schema, // Can only be used on Schema
Table, // Can only used on Tables
Column, // Can only used on Columns
StoredProcedure // Can only used on Stored Procedures
StoredProcedure,// Can only used on Stored Procedures
EnumSchema, // Can only used on Enums
EnumTable // Can only used on Enums
}
}
31 changes: 27 additions & 4 deletions Generator/Filtering/SingleContextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class SingleContextFilter : DbContextFilter
protected readonly List<IFilterType<Table>> TableFilters;
protected readonly List<IFilterType<Column>> ColumnFilters;
protected readonly List<IFilterType<StoredProcedure>> StoredProcedureFilters;
protected readonly List<IFilterType<EnumTableSource>> EnumerationTableFilters;
protected readonly List<IFilterType<EnumSchemaSource>>EnumerationSchemaFilters;

private bool _hasMergedIncludeFilters;

public SingleContextFilter()
Expand All @@ -24,10 +27,12 @@ public SingleContextFilter()
IncludeScalarValuedFunctions = FilterSettings.IncludeScalarValuedFunctions;
IncludeStoredProcedures = IncludeScalarValuedFunctions || IncludeTableValuedFunctions || FilterSettings.IncludeStoredProcedures;

SchemaFilters = FilterSettings.SchemaFilters;
TableFilters = FilterSettings.TableFilters;
ColumnFilters = FilterSettings.ColumnFilters;
StoredProcedureFilters = FilterSettings.StoredProcedureFilters;
SchemaFilters = FilterSettings.SchemaFilters;
TableFilters = FilterSettings.TableFilters;
ColumnFilters = FilterSettings.ColumnFilters;
StoredProcedureFilters = FilterSettings.StoredProcedureFilters;
EnumerationTableFilters = FilterSettings.EnumerationTableFilters;
EnumerationSchemaFilters = FilterSettings.EnumerationSchemaFilters;
_hasMergedIncludeFilters = false;

EnumDefinitions = new List<EnumDefinition>();
Expand All @@ -46,9 +51,24 @@ public override bool IsExcluded(EntityName item)
if (schema != null)
return SchemaFilters.Any(filter => filter.IsExcluded(schema));

var enumSchemaSource = item as EnumSchemaSource;
if (enumSchemaSource != null)
{
return EnumerationSchemaFilters.Any(filter => filter.IsExcluded(enumSchemaSource));
}


var enumTableSource = item as EnumTableSource;
if (enumTableSource != null)
{
return EnumerationTableFilters.Any(filter => filter.IsExcluded(enumTableSource)) || EnumerationSchemaFilters.Any(filter => filter.IsExcluded(enumTableSource.Schema));
}

var table = item as Table;
if (table != null)
{
return TableFilters.Any(filter => filter.IsExcluded(table)) || SchemaFilters.Any(filter => filter.IsExcluded(table.Schema));
}

var column = item as Column;
if (column != null)
Expand Down Expand Up @@ -162,6 +182,9 @@ private void MergeIncludeFilters()
MergeIncludeFilters(TableFilters);
MergeIncludeFilters(ColumnFilters);
MergeIncludeFilters(StoredProcedureFilters);

MergeIncludeFilters(EnumerationSchemaFilters);
MergeIncludeFilters(EnumerationTableFilters);
}

private static void MergeIncludeFilters<T>(List<IFilterType<T>> filters)
Expand Down
4 changes: 4 additions & 0 deletions Generator/Generator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Compile Include="AssemblyHelper.cs" />
<Compile Include="EfrpgVersion.cs" />
<Compile Include="EnumerationMember.cs" />
<Compile Include="Filtering\EnumerationSchemaFilter.cs" />
<Compile Include="EnumSchemaSource.cs" />
<Compile Include="EnumTableSource.cs" />
<Compile Include="FileManagement\VisualStudioFileManager.cs" />
<Compile Include="FileManagement\CustomFileManager.cs" />
<Compile Include="FileManagement\EntityFrameworkTemplateFileManager.cs" />
Expand All @@ -79,6 +82,7 @@
<Compile Include="Filtering\IFilter.cs" />
<Compile Include="Filtering\MultiContextFilter.cs" />
<Compile Include="Filtering\MultiContextNameNormalisation.cs" />
<Compile Include="Filtering\EnumerationTableFilter.cs" />
<Compile Include="ForeignKeyNamingStrategy.cs" />
<Compile Include="ForeignKeyStrategies\BaseForeignKeyNamingStrategy.cs" />
<Compile Include="ForeignKeyStrategies\ForeignKeyNamingStrategyFactory.cs" />
Expand Down
48 changes: 48 additions & 0 deletions Generator/Generators/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public void LoadTables()
//foreach (var ri in rawIndexes.OrderBy(x => x.TableName).ThenBy(x => x.IndexName)) Console.WriteLine(ri.Dump());
//foreach (var rfk in rawForeignKeys) Console.WriteLine(rfk.Dump());

AddTablesToEnums(rawTables);
AddTablesToFilters(rawTables);
IdentifyUniqueForeignKeys(rawIndexes, rawForeignKeys);
AddIndexesToFilters(rawIndexes);
Expand All @@ -305,6 +306,53 @@ public void LoadTables()
}
}

private void AddTablesToEnums(List<RawTable> rawTables)
{
if (rawTables == null || !rawTables.Any())
return;

var tablesNames = rawTables
.Select(x => new { x.SchemaName, x.TableName, x.IsView })
.Distinct()
.OrderBy(x => x.SchemaName)
.ThenBy(x => x.TableName)
.ToList();

foreach (var filterKeyValuePair in FilterList.GetFilters())
{
var filter = filterKeyValuePair.Value;

foreach (var tn in tablesNames)
{
var exclude = false;

var enumSchemaSource = new EnumSchemaSource(tn.SchemaName);
if (filter.IsExcluded(enumSchemaSource))
{
exclude = true;
}

var enumTableSource = new EnumTableSource(enumSchemaSource, tn.TableName);
if (filter.IsExcluded(enumTableSource))
{
exclude = true;
}

if (exclude)
{
continue;
}

var enumeration = DatabaseReader.ReadEnum(enumTableSource);

if (enumeration == null)
return; // No enums in database

filterKeyValuePair.Value.Enums.Add(enumeration);
}
}
}

public static void IdentifyUniqueForeignKeys(List<RawIndex> rawIndexes, List<RawForeignKey> rawForeignKeys)
{
if (rawIndexes == null || rawForeignKeys == null || !rawIndexes.Any() || !rawForeignKeys.Any())
Expand Down
Loading