diff --git a/EfEnumToLookup/EfEnumToLookup.csproj b/EfEnumToLookup/EfEnumToLookup.csproj index 8b6c69a..6598004 100644 --- a/EfEnumToLookup/EfEnumToLookup.csproj +++ b/EfEnumToLookup/EfEnumToLookup.csproj @@ -56,6 +56,7 @@ + diff --git a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs index 9a03538..eb0fc3e 100644 --- a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs +++ b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs @@ -71,20 +71,9 @@ public void Apply(DbContext context) // recurse through dbsets and references finding anything that uses an enum var enumReferences = FindEnumReferences(context); - var sqlServerHandler = new SqlServerHandler - { - NameFieldLength = NameFieldLength, - TableNamePrefix = TableNamePrefix, - TableNameSuffix = TableNameSuffix, - }; - - // todo: fold the populate method into the create tables method, providing all the data to the db layer in one hit, - // this will require merging the two sql callbacks into one signature - // for the list of enums generate and missing tables var enums = enumReferences.Select(r => r.EnumType).Distinct().ToList(); - // merge values into these tables var lookups = (from enm in enums select new LookupData @@ -93,7 +82,13 @@ public void Apply(DbContext context) Values = GetLookupValues(enm), }).ToList(); - sqlServerHandler.Apply(lookups, enumReferences, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters)); + // todo: support MariaDb etc. Issue #16 + IDbHandler dbHandler = new SqlServerHandler(); + dbHandler.NameFieldLength = NameFieldLength; + dbHandler.TableNamePrefix = TableNamePrefix; + dbHandler.TableNameSuffix = TableNameSuffix; + + dbHandler.Apply(lookups, enumReferences, (sql, parameters) => ExecuteSqlCommand(context, sql, parameters)); } private static int ExecuteSqlCommand(DbContext context, string sql, IEnumerable parameters = null) diff --git a/EfEnumToLookup/LookupGenerator/IDbHandler.cs b/EfEnumToLookup/LookupGenerator/IDbHandler.cs new file mode 100644 index 0000000..8a33253 --- /dev/null +++ b/EfEnumToLookup/LookupGenerator/IDbHandler.cs @@ -0,0 +1,29 @@ +namespace EfEnumToLookup.LookupGenerator +{ + using System; + using System.Collections.Generic; + using System.Data.SqlClient; + + internal interface IDbHandler + { + /// + /// The size of the Name field that will be added to the generated lookup tables. + /// Adjust to suit your data if required. + /// + int NameFieldLength { get; set; } + + /// + /// Prefix to add to all the generated tables to separate help group them together + /// and make them stand out as different from other tables. + /// + string TableNamePrefix { get; set; } + + /// + /// Suffix to add to all the generated tables to separate help group them together + /// and make them stand out as different from other tables. + /// + string TableNameSuffix { get; set; } + + void Apply(List lookups, IList enumReferences, Action> runSql); + } +} diff --git a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs index a69f569..6af1c61 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -5,7 +5,7 @@ using System.Data.SqlClient; using System.Text; - class SqlServerHandler + class SqlServerHandler : IDbHandler { /// /// The size of the Name field that will be added to the generated lookup tables. @@ -28,7 +28,7 @@ class SqlServerHandler public string TableNameSuffix { get; set; } - internal void Apply(List lookups, IList enumReferences, Action> runSql) + public void Apply(List lookups, IList enumReferences, Action> runSql) { CreateTables(lookups, (sql) => runSql(sql, null)); PopulateLookups(lookups, runSql);