From 72b693d91cca3880bd95f8f73483594bd0857086 Mon Sep 17 00:00:00 2001 From: Svein Helge Date: Fri, 31 Jul 2015 00:33:30 +0200 Subject: [PATCH 1/5] New column with name Description added to the table. --- EfEnumToLookup/LookupGenerator/EnumParser.cs | 9 +-- .../LookupGenerator/EnumToLookup.cs | 22 ++++-- EfEnumToLookup/LookupGenerator/IDbHandler.cs | 14 ++-- EfEnumToLookup/LookupGenerator/LookupValue.cs | 3 +- .../LookupGenerator/SqlServerHandler.cs | 34 +++++---- .../EfEnumToLookupTests.csproj | 3 + EfEnumToLookupTests/Tests/EnumParserTests.cs | 6 +- EfEnumToLookupTests/Tests/TestStuff.cs | 11 +-- ExampleUsage/EnumExample.cs | 69 +++++++++++++++++-- ExampleUsage/ExampleUsage.csproj | 7 +- 10 files changed, 130 insertions(+), 48 deletions(-) diff --git a/EfEnumToLookup/LookupGenerator/EnumParser.cs b/EfEnumToLookup/LookupGenerator/EnumParser.cs index 3bd9351..9ecbe48 100644 --- a/EfEnumToLookup/LookupGenerator/EnumParser.cs +++ b/EfEnumToLookup/LookupGenerator/EnumParser.cs @@ -57,7 +57,8 @@ public IEnumerable GetLookupValues(Type lookup) { Id = (int)numericValue, Name = EnumName(value), - }); + Description = EnumDescriptionValue(value) + }); } return values; } @@ -70,12 +71,6 @@ public IEnumerable GetLookupValues(Type lookup) /// private string EnumName(Enum value) { - var description = EnumDescriptionValue(value); - if (description != null) - { - return description; - } - var name = value.ToString(); if (SplitWords) diff --git a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs index f8138ef..546cabd 100644 --- a/EfEnumToLookup/LookupGenerator/EnumToLookup.cs +++ b/EfEnumToLookup/LookupGenerator/EnumToLookup.cs @@ -31,7 +31,8 @@ public EnumToLookup() { // set default behaviour, can be overridden by setting properties on object before calling Apply() NameFieldLength = 255; - TableNamePrefix = "Enum_"; + DescriptionFieldLength = 255; + TableNamePrefix = "Enum_"; _enumParser = new EnumParser { SplitWords = true }; } @@ -51,12 +52,18 @@ public bool SplitWords /// public 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. - /// Defaults to "Enum_" set to null or "" to not have any prefix. + /// + /// The size of the Description field that will be added to the generated lookup tables. + /// Adjust to suit your data if required, defaults to 255. /// - public string TableNamePrefix { get; set; } + public int DescriptionFieldLength { 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. + /// Defaults to "Enum_" set to null or "" to not have any prefix. + /// + public string TableNamePrefix { get; set; } /// /// Suffix to add to all the generated tables to separate help group them together @@ -110,7 +117,8 @@ private IDbHandler GetDbHandler() IDbHandler dbHandler = new SqlServerHandler { NameFieldLength = NameFieldLength, - TableNamePrefix = TableNamePrefix, + DescriptionFieldLength = DescriptionFieldLength, + TableNamePrefix = TableNamePrefix, TableNameSuffix = TableNameSuffix, }; return dbHandler; diff --git a/EfEnumToLookup/LookupGenerator/IDbHandler.cs b/EfEnumToLookup/LookupGenerator/IDbHandler.cs index c26c47c..2f4ca31 100644 --- a/EfEnumToLookup/LookupGenerator/IDbHandler.cs +++ b/EfEnumToLookup/LookupGenerator/IDbHandler.cs @@ -12,11 +12,17 @@ internal interface IDbHandler /// 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. + /// + /// The size of the Description field that will be added to the generated lookup tables. + /// Adjust to suit your data if required. /// - string TableNamePrefix { get; set; } + int DescriptionFieldLength { 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 diff --git a/EfEnumToLookup/LookupGenerator/LookupValue.cs b/EfEnumToLookup/LookupGenerator/LookupValue.cs index ac1c0ae..4b62398 100644 --- a/EfEnumToLookup/LookupGenerator/LookupValue.cs +++ b/EfEnumToLookup/LookupGenerator/LookupValue.cs @@ -4,5 +4,6 @@ internal class LookupValue { public int Id { get; set; } public string Name { get; set; } - } + public string Description { get; set; } + } } diff --git a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs index 7830b6b..ed8cdc4 100644 --- a/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs +++ b/EfEnumToLookup/LookupGenerator/SqlServerHandler.cs @@ -13,12 +13,18 @@ class SqlServerHandler : IDbHandler /// public 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. - /// Defaults to "Enum_" set to null or "" to not have any prefix. + /// + /// The size of the Description field that will be added to the generated lookup tables. + /// Adjust to suit your data if required, defaults to 255. /// - public string TableNamePrefix { get; set; } + public int DescriptionFieldLength { 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. + /// Defaults to "Enum_" set to null or "" to not have any prefix. + /// + public string TableNamePrefix { get; set; } /// /// Suffix to add to all the generated tables to separate help group them together @@ -58,14 +64,14 @@ private string CreateTables(IEnumerable enums) foreach (var lookup in enums) { sql.AppendFormat( - @"IF OBJECT_ID('{0}', 'U') IS NULL + @"IF OBJECT_ID('{0}', 'U') IS NULL begin - CREATE TABLE [{0}] (Id {2} CONSTRAINT PK_{0} PRIMARY KEY, Name nvarchar({1})); + CREATE TABLE [{0}] (Id {2} CONSTRAINT PK_{0} PRIMARY KEY, Name nvarchar({1}), Description nvarchar({3})); exec sys.sp_addextendedproperty @name=N'MS_Description', @level0type=N'SCHEMA', @level0name=N'dbo', @level1type=N'TABLE', @level1name=N'{0}', @value=N'Automatically generated. Contents will be overwritten on app startup. Table & contents generated by https://github.com/timabell/ef-enum-to-lookup'; end ", - TableName(lookup.Name), NameFieldLength, NumericSqlType(lookup.NumericType)); + TableName(lookup.Name), NameFieldLength, NumericSqlType(lookup.NumericType), DescriptionFieldLength); } return sql.ToString(); } @@ -88,7 +94,7 @@ private string AddForeignKeys(IEnumerable refs) private string PopulateLookups(IEnumerable lookupData) { var sql = new StringBuilder(); - sql.AppendLine(string.Format("CREATE TABLE #lookups (Id int, Name nvarchar({0}) COLLATE database_default);", NameFieldLength)); + sql.AppendLine(string.Format("CREATE TABLE #lookups (Id int, Name nvarchar({0}), Description nvarchar({1}) COLLATE database_default);", NameFieldLength, DescriptionFieldLength)); foreach (var lookup in lookupData) { sql.AppendLine(PopulateLookup(lookup)); @@ -102,7 +108,7 @@ private string PopulateLookup(LookupData lookup) var sql = new StringBuilder(); foreach (var value in lookup.Values) { - sql.AppendFormat("INSERT INTO #lookups (Id, Name) VALUES ({0}, N'{1}');\r\n", value.Id, SanitizeSqlString(value.Name)); + sql.AppendFormat("INSERT INTO #lookups (Id, Name, Description) VALUES ({0}, N'{1}', N'{2}');\r\n", value.Id, SanitizeSqlString(value.Name), SanitizeSqlString(value.Description)); } sql.AppendLine(string.Format(@" @@ -111,12 +117,12 @@ MERGE INTO [{0}] dst WHEN MATCHED AND src.Name <> dst.Name THEN UPDATE SET Name = src.Name WHEN NOT MATCHED THEN - INSERT (Id, Name) - VALUES (src.Id, src.Name) + INSERT (Id, Name, Description) + VALUES (src.Id, src.Name, src.Description) WHEN NOT MATCHED BY SOURCE THEN DELETE ;" - , TableName(lookup.Name))); + , TableName(lookup.Name))); sql.AppendLine("TRUNCATE TABLE #lookups;"); return sql.ToString(); @@ -124,7 +130,7 @@ WHEN NOT MATCHED BY SOURCE THEN private static string SanitizeSqlString(string value) { - return value.Replace("'", "''"); + return value == null ? null : value.Replace("'", "''"); } private string TableName(string enumName) diff --git a/EfEnumToLookupTests/EfEnumToLookupTests.csproj b/EfEnumToLookupTests/EfEnumToLookupTests.csproj index c1764a2..b202401 100644 --- a/EfEnumToLookupTests/EfEnumToLookupTests.csproj +++ b/EfEnumToLookupTests/EfEnumToLookupTests.csproj @@ -100,6 +100,9 @@ EfEnumToLookup + + +