Skip to content

Commit 48692a6

Browse files
committed
#780 Filter sequences to just those required. Thanks to @MarkLFT
1 parent 1d63b3f commit 48692a6

File tree

65 files changed

+759
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+759
-174
lines changed

EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,7 +4387,16 @@
43874387

43884388
foreach (var filterKeyValuePair in FilterList.GetFilters())
43894389
{
4390-
filterKeyValuePair.Value.Sequences.AddRange(sequences);
4390+
// Only add sequences where the table is used by this filter
4391+
foreach (var seq in sequences
4392+
.Where(seq => seq.TableMapping
4393+
.Any(tableMapping => filterKeyValuePair.Value.Tables
4394+
.Any(x =>
4395+
x.Schema.DbName.Equals(tableMapping.TableSchema, StringComparison.InvariantCultureIgnoreCase) &&
4396+
x.DbName.Equals(tableMapping.TableName, StringComparison.InvariantCultureIgnoreCase)))))
4397+
{
4398+
filterKeyValuePair.Value.Sequences.Add(seq);
4399+
}
43914400
}
43924401
}
43934402
catch (Exception x)
@@ -13181,25 +13190,41 @@ and limitations under the License.
1318113190

1318213191
cmd.CommandText = sql;
1318313192

13193+
RawSequence rs = null;
13194+
1318413195
using (var rdr = cmd.ExecuteReader())
1318513196
{
13197+
// Sequences
1318613198
while (rdr.Read())
1318713199
{
1318813200
var dataType = rdr["DataType"].ToString().Trim().ToLower();
13189-
13190-
var index = new RawSequence
13191-
(
13192-
rdr["Schema"].ToString().Trim(),
13193-
rdr["Name"].ToString().Trim(),
13194-
GetPropertyType(dataType),
13195-
rdr["StartValue"].ToString(),
13196-
rdr["IncrementValue"].ToString(),
13197-
rdr["MinValue"].ToString(),
13198-
rdr["MaxValue"].ToString(),
13199-
GetReaderBool(rdr, "IsCycleEnabled") ?? false
13200-
);
13201-
result.Add(index);
13201+
var schema = rdr["Schema"].ToString().Trim();
13202+
var name = rdr["Name"].ToString().Trim();
13203+
13204+
if (rs == null || rs.Schema != schema || rs.Name != name)
13205+
{
13206+
rs = new RawSequence
13207+
(
13208+
schema,
13209+
name,
13210+
GetPropertyType(dataType),
13211+
rdr["StartValue"].ToString(),
13212+
rdr["IncrementValue"].ToString(),
13213+
rdr["MinValue"].ToString(),
13214+
rdr["MaxValue"].ToString(),
13215+
GetReaderBool(rdr, "IsCycleEnabled") ?? false
13216+
);
13217+
13218+
result.Add(rs);
13219+
}
13220+
13221+
rs.TableMapping.Add(new RawSequenceTableMapping(
13222+
rdr["TableSchema"].ToString().Trim(),
13223+
rdr["TableName"].ToString().Trim()));
1320213224
}
13225+
13226+
if (!result.Any())
13227+
return result;
1320313228
}
1320413229
}
1320513230

@@ -14446,6 +14471,8 @@ ORDER BY SchemaName, TableName, TriggerName;";
1444614471
public readonly bool hasMinValue;
1444714472
public readonly bool hasMaxValue;
1444814473

14474+
public List<RawSequenceTableMapping> TableMapping;
14475+
1444914476
public RawSequence(string schema, string name, string dataType, string startValue, string incrementValue, string minValue, string maxValue, bool isCycleEnabled)
1445014477
{
1445114478
Schema = schema;
@@ -14456,11 +14483,23 @@ ORDER BY SchemaName, TableName, TriggerName;";
1445614483
MinValue = minValue;
1445714484
MaxValue = maxValue;
1445814485
IsCycleEnabled = isCycleEnabled ? "true" : "false";
14486+
TableMapping = new List<RawSequenceTableMapping>();
1445914487

1446014488
hasMinValue = MinMaxValueCache.GetMinValue(dataType) != minValue;
1446114489
hasMaxValue = MinMaxValueCache.GetMaxValue(dataType) != maxValue;
1446214490
}
1446314491
}
14492+
public class RawSequenceTableMapping
14493+
{
14494+
public readonly string TableSchema;
14495+
public readonly string TableName;
14496+
14497+
public RawSequenceTableMapping(string tableSchema, string tableName)
14498+
{
14499+
TableSchema = tableSchema;
14500+
TableName = tableName;
14501+
}
14502+
}
1446414503
public class RawStoredProcedure
1446514504
{
1446614505
public readonly string Schema;
@@ -15263,19 +15302,24 @@ SELECT * FROM MultiContext.ForeignKey;";
1526315302
protected override string SequenceSQL()
1526415303
{
1526515304
return @"
15266-
SELECT SCHEMA_NAME(seq.schema_id) AS [Schema],
15267-
seq.name AS [Name],
15268-
usrt.name AS DataType,
15269-
ISNULL(seq.start_value, N'') AS StartValue,
15270-
ISNULL(seq.increment, N'') AS IncrementValue,
15271-
ISNULL(seq.minimum_value, N'') AS MinValue,
15272-
ISNULL(seq.maximum_value, N'') AS MaxValue,
15273-
ISNULL(CAST(seq.is_cycling AS BIT), 0) AS IsCycleEnabled,
15274-
seq.cache_size AS CacheSize
15275-
FROM sys.sequences seq
15276-
LEFT OUTER JOIN sys.types usrt
15277-
ON usrt.user_type_id = seq.user_type_id
15278-
ORDER BY [Name], [Schema];";
15305+
SELECT SCHEMA_NAME(seq.schema_id) [Schema],
15306+
seq.name Name,
15307+
usrt.name DataType,
15308+
ISNULL(seq.start_value, N'') StartValue,
15309+
ISNULL(seq.increment, N'') IncrementValue,
15310+
ISNULL(seq.minimum_value, N'') MinValue,
15311+
ISNULL(seq.maximum_value, N'') MaxValue,
15312+
ISNULL(CAST(seq.is_cycling AS BIT), 0) IsCycleEnabled,
15313+
seq.cache_size CacheSize,
15314+
OBJECT_SCHEMA_NAME(o.parent_object_id) TableSchema,
15315+
OBJECT_NAME(o.parent_object_id) TableName
15316+
FROM sys.sequences seq
15317+
LEFT OUTER JOIN sys.types usrt
15318+
ON usrt.user_type_id = seq.user_type_id
15319+
CROSS APPLY sys.dm_sql_referencing_entities(OBJECT_SCHEMA_NAME(seq.object_id) + '.' + seq.name, 'OBJECT') r
15320+
JOIN sys.objects o
15321+
ON o.object_id = r.referencing_id
15322+
ORDER BY seq.schema_id, seq.name;";
1527915323
}
1528015324

1528115325
protected override string TriggerSQL()

Generator.Tests.Integration/TestComparison/EfrpgTestIncludeFilter_SqlServer_EfCore3_FkLegacy.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
249249
{
250250
base.OnModelCreating(modelBuilder);
251251

252-
modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
253-
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
254-
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
255-
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
256-
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
257-
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);
258-
259252
modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
260253
modelBuilder.ApplyConfiguration(new CarConfiguration());
261254
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());

Generator.Tests.Integration/TestComparison/EfrpgTestIncludeFilter_SqlServer_EfCore5_FkLegacy.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
251251
{
252252
base.OnModelCreating(modelBuilder);
253253

254-
modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
255-
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
256-
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
257-
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
258-
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
259-
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);
260-
261254
modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
262255
modelBuilder.ApplyConfiguration(new CarConfiguration());
263256
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());

Generator.Tests.Integration/TestComparison/EfrpgTestIncludeFilter_SqlServer_EfCore6_FkLegacy.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
251251
{
252252
base.OnModelCreating(modelBuilder);
253253

254-
modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
255-
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
256-
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
257-
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
258-
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
259-
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);
260-
261254
modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
262255
modelBuilder.ApplyConfiguration(new CarConfiguration());
263256
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());

Generator.Tests.Integration/TestComparison/EfrpgTestIncludeFilter_SqlServer_EfCore7_FkLegacy.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
251251
{
252252
base.OnModelCreating(modelBuilder);
253253

254-
modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
255-
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
256-
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
257-
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
258-
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
259-
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);
260-
261254
modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
262255
modelBuilder.ApplyConfiguration(new CarConfiguration());
263256
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());

Generator.Tests.Integration/TestComparison/EfrpgTest_SqlServer_Ef6_FkLegacy.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public interface IEfrpgTestDbContext : IDisposable
9696
DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
9797
DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
9898
DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
99+
DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
99100
DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
100101
DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
101102
DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
@@ -399,6 +400,7 @@ public class EfrpgTestDbContext : DbContext, IEfrpgTestDbContext
399400
public DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
400401
public DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
401402
public DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
403+
public DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
402404
public DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
403405
public DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
404406
public DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
@@ -565,6 +567,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
565567
modelBuilder.Configurations.Add(new PkOrdinalTestConfiguration());
566568
modelBuilder.Configurations.Add(new PropertyTypesToAddConfiguration());
567569
modelBuilder.Configurations.Add(new SequenceTestConfiguration());
570+
modelBuilder.Configurations.Add(new SequenceTestPartTwoConfiguration());
568571
modelBuilder.Configurations.Add(new SmallDecimalTestConfiguration());
569572
modelBuilder.Configurations.Add(new SmallDecimalTestViewConfiguration());
570573
modelBuilder.Configurations.Add(new Stafford_BooConfiguration());
@@ -833,6 +836,7 @@ public static DbModelBuilder CreateModel(DbModelBuilder modelBuilder, string sch
833836
modelBuilder.Configurations.Add(new PkOrdinalTestConfiguration(schema));
834837
modelBuilder.Configurations.Add(new PropertyTypesToAddConfiguration(schema));
835838
modelBuilder.Configurations.Add(new SequenceTestConfiguration(schema));
839+
modelBuilder.Configurations.Add(new SequenceTestPartTwoConfiguration(schema));
836840
modelBuilder.Configurations.Add(new SmallDecimalTestConfiguration(schema));
837841
modelBuilder.Configurations.Add(new SmallDecimalTestViewConfiguration(schema));
838842
modelBuilder.Configurations.Add(new Stafford_BooConfiguration(schema));
@@ -2322,6 +2326,7 @@ public class FakeEfrpgTestDbContext : IEfrpgTestDbContext
23222326
public DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
23232327
public DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
23242328
public DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
2329+
public DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
23252330
public DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
23262331
public DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
23272332
public DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
@@ -2428,6 +2433,7 @@ public FakeEfrpgTestDbContext()
24282433
PkOrdinalTests = new FakeDbSet<PkOrdinalTest>("C3", "C1");
24292434
PropertyTypesToAdds = new FakeDbSet<PropertyTypesToAdd>("Id");
24302435
SequenceTests = new FakeDbSet<SequenceTest>("Id");
2436+
SequenceTestPartTwoes = new FakeDbSet<SequenceTestPartTwo>("Id");
24312437
SmallDecimalTests = new FakeDbSet<SmallDecimalTest>("Id");
24322438
SmallDecimalTestViews = new FakeDbSet<SmallDecimalTestView>("FkId", "Description");
24332439
Stafford_Boos = new FakeDbSet<Stafford_Boo>("Id");
@@ -4567,6 +4573,14 @@ public class SequenceTest
45674573
public decimal CntByNumeric { get; set; } // CntByNumeric
45684574
}
45694575

4576+
// SequenceTestPartTwo
4577+
public class SequenceTestPartTwo
4578+
{
4579+
public int Id { get; set; } // Id (Primary key)
4580+
public long CntByBigInt { get; set; } // CntByBigInt
4581+
public byte CntByTinyInt { get; set; } // CntByTinyInt
4582+
}
4583+
45704584
// SmallDecimalTest
45714585
public class SmallDecimalTest
45724586
{
@@ -6441,6 +6455,25 @@ public SequenceTestConfiguration(string schema)
64416455
}
64426456
}
64436457

6458+
// SequenceTestPartTwo
6459+
public class SequenceTestPartTwoConfiguration : EntityTypeConfiguration<SequenceTestPartTwo>
6460+
{
6461+
public SequenceTestPartTwoConfiguration()
6462+
: this("dbo")
6463+
{
6464+
}
6465+
6466+
public SequenceTestPartTwoConfiguration(string schema)
6467+
{
6468+
ToTable("SequenceTestPartTwo", schema);
6469+
HasKey(x => x.Id);
6470+
6471+
Property(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
6472+
Property(x => x.CntByBigInt).HasColumnName(@"CntByBigInt").HasColumnType("bigint").IsRequired();
6473+
Property(x => x.CntByTinyInt).HasColumnName(@"CntByTinyInt").HasColumnType("tinyint").IsRequired();
6474+
}
6475+
}
6476+
64446477
// SmallDecimalTest
64456478
public class SmallDecimalTestConfiguration : EntityTypeConfiguration<SmallDecimalTest>
64466479
{

0 commit comments

Comments
 (0)