From 1bec3d69743649f44a3d02e09945aabfa79ac208 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Mon, 10 Sep 2018 10:56:29 -0500 Subject: [PATCH] minor seed refactor ref #308 --- .../Extensions/SeedFilterListsDbContext.cs | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs index 58aa841af..248ae0e74 100644 --- a/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs +++ b/src/FilterLists.Data/Seed/Extensions/SeedFilterListsDbContext.cs @@ -30,29 +30,19 @@ public static void SeedOrUpdate(this FilterListsDbContext dbContext, string data dbContext.SeedOrUpdate(dataPath); } - private static void SeedOrUpdate(this DbContext dbContext, string dataPath) - where TEntity : IBaseEntity + private static void SeedOrUpdate(this DbContext dbContext, string dataPath) where TEntity : IBaseEntity { var entityType = dbContext.Model.FindEntityType(typeof(TEntity)); var properties = GetPropertiesLessValueGeneratedTimestamps(entityType); - var values = CreateValues(properties, dataPath); - if (values == "") - return; - InsertOnDuplicateKeyUpdate(dbContext, properties, entityType, values); + var seedRows = GetSeedRows(dataPath); + InsertOnDuplicateKeyUpdate(dbContext, properties, entityType, seedRows); } - //TODO: get seed properties dynamically from JSON private static List GetPropertiesLessValueGeneratedTimestamps(IEntityType entityType) => entityType.GetProperties() .Where(x => !new List {"CreatedDateUtc", "ModifiedDateUtc"}.Contains(x.Name)) .ToList(); - private static string CreateValues(IReadOnlyCollection properties, string dataPath) - where TEntity : IBaseEntity => - GetSeedRows(dataPath) - .Select(row => CreateRowValues(properties, row)) - .Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues); - private static List GetSeedRows(string dataPath) where TEntity : IBaseEntity { try @@ -67,6 +57,25 @@ private static List GetSeedRows(string dataPath) where TEntity } } + private static void InsertOnDuplicateKeyUpdate(DbContext dbContext, + IReadOnlyCollection properties, IEntityType entityType, IEnumerable seedRows) + where TEntity : IBaseEntity + { + var values = CreateValues(seedRows, properties); + if (values == "") + return; + var columns = string.Join(", ", properties.Select(x => x.Name)); + var updates = CreateUpdates(properties); + var sql = "INSERT INTO " + entityType.Relational().TableName + " (" + columns + ") VALUES " + values + + " ON DUPLICATE KEY UPDATE " + updates; + dbContext.Database.ExecuteSqlCommand(sql); + } + + private static string CreateValues(IEnumerable seedRows, + IReadOnlyCollection properties) where TEntity : IBaseEntity => + seedRows.Select(row => CreateRowValues(properties, row)) + .Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues); + private static string CreateRowValues(IEnumerable properties, TEntity row) where TEntity : IBaseEntity => (from property in properties @@ -76,7 +85,8 @@ select FormatDataForMySql(property, value)).Aggregate("", private static object FormatDataForMySql(IProperty property, object value) { - if (value == null) return "NULL"; + if (value == null) + return "NULL"; if (property.ClrType == typeof(string)) return "'" + value.ToString().Replace("'", "''") + "'"; if (property.ClrType == typeof(bool)) @@ -86,16 +96,6 @@ private static object FormatDataForMySql(IProperty property, object value) return value; } - private static void InsertOnDuplicateKeyUpdate(DbContext dbContext, IReadOnlyCollection properties, - IEntityType entityType, string values) - { - var columns = string.Join(", ", properties.Select(x => x.Name)); - var updates = CreateUpdates(properties); - var rawSqlString = "INSERT INTO " + entityType.Relational().TableName + " (" + columns + ") VALUES " + - values + " ON DUPLICATE KEY UPDATE " + updates; - dbContext.Database.ExecuteSqlCommand(rawSqlString); - } - private static string CreateUpdates(IReadOnlyCollection properties) { var update =