mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
parent
1ad6d9a5f9
commit
1bec3d6974
1 changed files with 24 additions and 24 deletions
|
|
@ -30,29 +30,19 @@ public static void SeedOrUpdate(this FilterListsDbContext dbContext, string data
|
|||
dbContext.SeedOrUpdate<SoftwareSyntax>(dataPath);
|
||||
}
|
||||
|
||||
private static void SeedOrUpdate<TEntity>(this DbContext dbContext, string dataPath)
|
||||
where TEntity : IBaseEntity
|
||||
private static void SeedOrUpdate<TEntity>(this DbContext dbContext, string dataPath) where TEntity : IBaseEntity
|
||||
{
|
||||
var entityType = dbContext.Model.FindEntityType(typeof(TEntity));
|
||||
var properties = GetPropertiesLessValueGeneratedTimestamps(entityType);
|
||||
var values = CreateValues<TEntity>(properties, dataPath);
|
||||
if (values == "")
|
||||
return;
|
||||
InsertOnDuplicateKeyUpdate(dbContext, properties, entityType, values);
|
||||
var seedRows = GetSeedRows<TEntity>(dataPath);
|
||||
InsertOnDuplicateKeyUpdate(dbContext, properties, entityType, seedRows);
|
||||
}
|
||||
|
||||
//TODO: get seed properties dynamically from JSON
|
||||
private static List<IProperty> GetPropertiesLessValueGeneratedTimestamps(IEntityType entityType) =>
|
||||
entityType.GetProperties()
|
||||
.Where(x => !new List<string> {"CreatedDateUtc", "ModifiedDateUtc"}.Contains(x.Name))
|
||||
.ToList();
|
||||
|
||||
private static string CreateValues<TEntity>(IReadOnlyCollection<IProperty> properties, string dataPath)
|
||||
where TEntity : IBaseEntity =>
|
||||
GetSeedRows<TEntity>(dataPath)
|
||||
.Select(row => CreateRowValues(properties, row))
|
||||
.Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues);
|
||||
|
||||
private static List<TEntity> GetSeedRows<TEntity>(string dataPath) where TEntity : IBaseEntity
|
||||
{
|
||||
try
|
||||
|
|
@ -67,6 +57,25 @@ private static List<TEntity> GetSeedRows<TEntity>(string dataPath) where TEntity
|
|||
}
|
||||
}
|
||||
|
||||
private static void InsertOnDuplicateKeyUpdate<TEntity>(DbContext dbContext,
|
||||
IReadOnlyCollection<IProperty> properties, IEntityType entityType, IEnumerable<TEntity> 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<TEntity>(IEnumerable<TEntity> seedRows,
|
||||
IReadOnlyCollection<IProperty> properties) where TEntity : IBaseEntity =>
|
||||
seedRows.Select(row => CreateRowValues(properties, row))
|
||||
.Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues);
|
||||
|
||||
private static string CreateRowValues<TEntity>(IEnumerable<IProperty> 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<IProperty> 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<IProperty> properties)
|
||||
{
|
||||
var update =
|
||||
|
|
|
|||
Loading…
Reference in a new issue