Skip to content

Commit

Permalink
#780 Filter sequences to just those required. Thanks to @MarkLFT
Browse files Browse the repository at this point in the history
  • Loading branch information
sjh37 committed Mar 2, 2023
1 parent 1d63b3f commit 48692a6
Show file tree
Hide file tree
Showing 65 changed files with 759 additions and 174 deletions.
98 changes: 71 additions & 27 deletions EntityFramework.Reverse.POCO.Generator/EF.Reverse.POCO.v3.ttinclude
Original file line number Diff line number Diff line change
Expand Up @@ -4387,7 +4387,16 @@

foreach (var filterKeyValuePair in FilterList.GetFilters())
{
filterKeyValuePair.Value.Sequences.AddRange(sequences);
// Only add sequences where the table is used by this filter
foreach (var seq in sequences
.Where(seq => seq.TableMapping
.Any(tableMapping => filterKeyValuePair.Value.Tables
.Any(x =>
x.Schema.DbName.Equals(tableMapping.TableSchema, StringComparison.InvariantCultureIgnoreCase) &&
x.DbName.Equals(tableMapping.TableName, StringComparison.InvariantCultureIgnoreCase)))))
{
filterKeyValuePair.Value.Sequences.Add(seq);
}
}
}
catch (Exception x)
Expand Down Expand Up @@ -13181,25 +13190,41 @@ and limitations under the License.

cmd.CommandText = sql;

RawSequence rs = null;

using (var rdr = cmd.ExecuteReader())
{
// Sequences
while (rdr.Read())
{
var dataType = rdr["DataType"].ToString().Trim().ToLower();

var index = new RawSequence
(
rdr["Schema"].ToString().Trim(),
rdr["Name"].ToString().Trim(),
GetPropertyType(dataType),
rdr["StartValue"].ToString(),
rdr["IncrementValue"].ToString(),
rdr["MinValue"].ToString(),
rdr["MaxValue"].ToString(),
GetReaderBool(rdr, "IsCycleEnabled") ?? false
);
result.Add(index);
var schema = rdr["Schema"].ToString().Trim();
var name = rdr["Name"].ToString().Trim();

if (rs == null || rs.Schema != schema || rs.Name != name)
{
rs = new RawSequence
(
schema,
name,
GetPropertyType(dataType),
rdr["StartValue"].ToString(),
rdr["IncrementValue"].ToString(),
rdr["MinValue"].ToString(),
rdr["MaxValue"].ToString(),
GetReaderBool(rdr, "IsCycleEnabled") ?? false
);

result.Add(rs);
}

rs.TableMapping.Add(new RawSequenceTableMapping(
rdr["TableSchema"].ToString().Trim(),
rdr["TableName"].ToString().Trim()));
}

if (!result.Any())
return result;
}
}

Expand Down Expand Up @@ -14446,6 +14471,8 @@ ORDER BY SchemaName, TableName, TriggerName;";
public readonly bool hasMinValue;
public readonly bool hasMaxValue;

public List<RawSequenceTableMapping> TableMapping;

public RawSequence(string schema, string name, string dataType, string startValue, string incrementValue, string minValue, string maxValue, bool isCycleEnabled)
{
Schema = schema;
Expand All @@ -14456,11 +14483,23 @@ ORDER BY SchemaName, TableName, TriggerName;";
MinValue = minValue;
MaxValue = maxValue;
IsCycleEnabled = isCycleEnabled ? "true" : "false";
TableMapping = new List<RawSequenceTableMapping>();

hasMinValue = MinMaxValueCache.GetMinValue(dataType) != minValue;
hasMaxValue = MinMaxValueCache.GetMaxValue(dataType) != maxValue;
}
}
public class RawSequenceTableMapping
{
public readonly string TableSchema;
public readonly string TableName;

public RawSequenceTableMapping(string tableSchema, string tableName)
{
TableSchema = tableSchema;
TableName = tableName;
}
}
public class RawStoredProcedure
{
public readonly string Schema;
Expand Down Expand Up @@ -15263,19 +15302,24 @@ SELECT * FROM MultiContext.ForeignKey;";
protected override string SequenceSQL()
{
return @"
SELECT SCHEMA_NAME(seq.schema_id) AS [Schema],
seq.name AS [Name],
usrt.name AS DataType,
ISNULL(seq.start_value, N'') AS StartValue,
ISNULL(seq.increment, N'') AS IncrementValue,
ISNULL(seq.minimum_value, N'') AS MinValue,
ISNULL(seq.maximum_value, N'') AS MaxValue,
ISNULL(CAST(seq.is_cycling AS BIT), 0) AS IsCycleEnabled,
seq.cache_size AS CacheSize
FROM sys.sequences seq
LEFT OUTER JOIN sys.types usrt
ON usrt.user_type_id = seq.user_type_id
ORDER BY [Name], [Schema];";
SELECT SCHEMA_NAME(seq.schema_id) [Schema],
seq.name Name,
usrt.name DataType,
ISNULL(seq.start_value, N'') StartValue,
ISNULL(seq.increment, N'') IncrementValue,
ISNULL(seq.minimum_value, N'') MinValue,
ISNULL(seq.maximum_value, N'') MaxValue,
ISNULL(CAST(seq.is_cycling AS BIT), 0) IsCycleEnabled,
seq.cache_size CacheSize,
OBJECT_SCHEMA_NAME(o.parent_object_id) TableSchema,
OBJECT_NAME(o.parent_object_id) TableName
FROM sys.sequences seq
LEFT OUTER JOIN sys.types usrt
ON usrt.user_type_id = seq.user_type_id
CROSS APPLY sys.dm_sql_referencing_entities(OBJECT_SCHEMA_NAME(seq.object_id) + '.' + seq.name, 'OBJECT') r
JOIN sys.objects o
ON o.object_id = r.referencing_id
ORDER BY seq.schema_id, seq.name;";
}

protected override string TriggerSQL()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);

modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
modelBuilder.ApplyConfiguration(new CarConfiguration());
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);

modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
modelBuilder.ApplyConfiguration(new CarConfiguration());
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);

modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
modelBuilder.ApplyConfiguration(new CarConfiguration());
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.HasSequence<int>("CountBy1", "dbo").StartsAt(1).IncrementsBy(1).IsCyclic(false);
modelBuilder.HasSequence<long>("CountByBigInt", "dbo").StartsAt(22).IncrementsBy(234).IsCyclic(true).HasMin(1).HasMax(9876543);
modelBuilder.HasSequence<decimal>("CountByDecimal", "dbo").StartsAt(593).IncrementsBy(82).IsCyclic(false).HasMin(5).HasMax(777777);
modelBuilder.HasSequence<decimal>("CountByNumeric", "dbo").StartsAt(789).IncrementsBy(987).IsCyclic(false).HasMin(345).HasMax(999999999999999999);
modelBuilder.HasSequence<short>("CountBySmallInt", "dbo").StartsAt(44).IncrementsBy(456).IsCyclic(true);
modelBuilder.HasSequence<byte>("CountByTinyInt", "dbo").StartsAt(33).IncrementsBy(3).IsCyclic(false);

modelBuilder.ApplyConfiguration(new Beta_Harish3485Configuration());
modelBuilder.ApplyConfiguration(new CarConfiguration());
modelBuilder.ApplyConfiguration(new CarToColourConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public interface IEfrpgTestDbContext : IDisposable
DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
Expand Down Expand Up @@ -399,6 +400,7 @@ public class EfrpgTestDbContext : DbContext, IEfrpgTestDbContext
public DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
public DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
public DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
public DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
public DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
public DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
public DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
Expand Down Expand Up @@ -565,6 +567,7 @@ protected override void OnModelCreating(DbModelBuilder modelBuilder)
modelBuilder.Configurations.Add(new PkOrdinalTestConfiguration());
modelBuilder.Configurations.Add(new PropertyTypesToAddConfiguration());
modelBuilder.Configurations.Add(new SequenceTestConfiguration());
modelBuilder.Configurations.Add(new SequenceTestPartTwoConfiguration());
modelBuilder.Configurations.Add(new SmallDecimalTestConfiguration());
modelBuilder.Configurations.Add(new SmallDecimalTestViewConfiguration());
modelBuilder.Configurations.Add(new Stafford_BooConfiguration());
Expand Down Expand Up @@ -833,6 +836,7 @@ public static DbModelBuilder CreateModel(DbModelBuilder modelBuilder, string sch
modelBuilder.Configurations.Add(new PkOrdinalTestConfiguration(schema));
modelBuilder.Configurations.Add(new PropertyTypesToAddConfiguration(schema));
modelBuilder.Configurations.Add(new SequenceTestConfiguration(schema));
modelBuilder.Configurations.Add(new SequenceTestPartTwoConfiguration(schema));
modelBuilder.Configurations.Add(new SmallDecimalTestConfiguration(schema));
modelBuilder.Configurations.Add(new SmallDecimalTestViewConfiguration(schema));
modelBuilder.Configurations.Add(new Stafford_BooConfiguration(schema));
Expand Down Expand Up @@ -2322,6 +2326,7 @@ public class FakeEfrpgTestDbContext : IEfrpgTestDbContext
public DbSet<PkOrdinalTest> PkOrdinalTests { get; set; } // pk_ordinal_test
public DbSet<PropertyTypesToAdd> PropertyTypesToAdds { get; set; } // PropertyTypesToAdd
public DbSet<SequenceTest> SequenceTests { get; set; } // SequenceTest
public DbSet<SequenceTestPartTwo> SequenceTestPartTwoes { get; set; } // SequenceTestPartTwo
public DbSet<SmallDecimalTest> SmallDecimalTests { get; set; } // SmallDecimalTest
public DbSet<SmallDecimalTestView> SmallDecimalTestViews { get; set; } // SmallDecimalTestView
public DbSet<Stafford_Boo> Stafford_Boos { get; set; } // Boo
Expand Down Expand Up @@ -2428,6 +2433,7 @@ public FakeEfrpgTestDbContext()
PkOrdinalTests = new FakeDbSet<PkOrdinalTest>("C3", "C1");
PropertyTypesToAdds = new FakeDbSet<PropertyTypesToAdd>("Id");
SequenceTests = new FakeDbSet<SequenceTest>("Id");
SequenceTestPartTwoes = new FakeDbSet<SequenceTestPartTwo>("Id");
SmallDecimalTests = new FakeDbSet<SmallDecimalTest>("Id");
SmallDecimalTestViews = new FakeDbSet<SmallDecimalTestView>("FkId", "Description");
Stafford_Boos = new FakeDbSet<Stafford_Boo>("Id");
Expand Down Expand Up @@ -4567,6 +4573,14 @@ public class SequenceTest
public decimal CntByNumeric { get; set; } // CntByNumeric
}

// SequenceTestPartTwo
public class SequenceTestPartTwo
{
public int Id { get; set; } // Id (Primary key)
public long CntByBigInt { get; set; } // CntByBigInt
public byte CntByTinyInt { get; set; } // CntByTinyInt
}

// SmallDecimalTest
public class SmallDecimalTest
{
Expand Down Expand Up @@ -6441,6 +6455,25 @@ public SequenceTestConfiguration(string schema)
}
}

// SequenceTestPartTwo
public class SequenceTestPartTwoConfiguration : EntityTypeConfiguration<SequenceTestPartTwo>
{
public SequenceTestPartTwoConfiguration()
: this("dbo")
{
}

public SequenceTestPartTwoConfiguration(string schema)
{
ToTable("SequenceTestPartTwo", schema);
HasKey(x => x.Id);

Property(x => x.Id).HasColumnName(@"Id").HasColumnType("int").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.CntByBigInt).HasColumnName(@"CntByBigInt").HasColumnType("bigint").IsRequired();
Property(x => x.CntByTinyInt).HasColumnName(@"CntByTinyInt").HasColumnType("tinyint").IsRequired();
}
}

// SmallDecimalTest
public class SmallDecimalTestConfiguration : EntityTypeConfiguration<SmallDecimalTest>
{
Expand Down
Loading

0 comments on commit 48692a6

Please sign in to comment.