mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
resolve some static analysis warnings
This commit is contained in:
parent
925bb166da
commit
420556de19
1 changed files with 16 additions and 10 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
|
@ -16,6 +17,9 @@ public static class SeedFilterListsDbContext
|
|||
{
|
||||
public static async Task SeedOrUpdateAsync(FilterListsDbContext dbContext, string dataPath)
|
||||
{
|
||||
if (dbContext is null)
|
||||
throw new ArgumentNullException(nameof(dbContext));
|
||||
|
||||
await SeedOrUpdate<Language>(dbContext, dataPath);
|
||||
await SeedOrUpdate<License>(dbContext, dataPath);
|
||||
await SeedOrUpdate<Maintainer>(dbContext, dataPath);
|
||||
|
|
@ -94,7 +98,7 @@ private static async Task InsertOnDuplicateKeyUpdate<TEntity>(DbContext dbContex
|
|||
var entityType = dbContext.Model.FindEntityType(typeof(TEntity));
|
||||
var properties = GetPropertiesLessValueGeneratedTimestamps(entityType);
|
||||
var values = CreateValues(seed, properties);
|
||||
if (values == "")
|
||||
if (string.IsNullOrEmpty(values))
|
||||
return;
|
||||
var columns = string.Join(", ", properties.Select(x => x.Name));
|
||||
var updates = CreateUpdates(properties);
|
||||
|
|
@ -114,17 +118,18 @@ private static List<IProperty> GetPropertiesLessValueGeneratedTimestamps(IEntity
|
|||
}.Contains(x.Name))
|
||||
.ToList();
|
||||
|
||||
private static string CreateValues<TEntity>(IEnumerable<TEntity> seed,
|
||||
IReadOnlyCollection<IProperty> properties) where TEntity : IBaseEntity =>
|
||||
seed.Select(row => CreateRowValues(properties, row))
|
||||
.Aggregate("", (current, rowValues) => current == "" ? rowValues : current + ", " + rowValues);
|
||||
private static string
|
||||
CreateValues<TEntity>(IEnumerable<TEntity> seed, IReadOnlyCollection<IProperty> properties)
|
||||
where TEntity : IBaseEntity => seed.Select(row => CreateRowValues(properties, row))
|
||||
.Aggregate("",
|
||||
(current, rowValues) => string.IsNullOrEmpty(current) ? rowValues : current + ", " + rowValues);
|
||||
|
||||
private static string CreateRowValues<TEntity>(IEnumerable<IProperty> properties, TEntity row)
|
||||
where TEntity : IBaseEntity =>
|
||||
(from property in properties
|
||||
let value = row.GetType().GetProperty(property.Name)?.GetValue(row)
|
||||
select FormatDataForMySql(property, value)).Aggregate("",
|
||||
(rowValues, value) => rowValues == "" ? "(" + value : rowValues + ", " + value) + ")";
|
||||
(rowValues, value) => string.IsNullOrEmpty(rowValues) ? "(" + value : rowValues + ", " + value) + ")";
|
||||
|
||||
private static object FormatDataForMySql(IProperty property, object value)
|
||||
{
|
||||
|
|
@ -133,9 +138,9 @@ private static object FormatDataForMySql(IProperty property, object value)
|
|||
if (property.ClrType == typeof(string))
|
||||
return "'" + value.ToString().Replace("'", "''") + "'";
|
||||
if (property.ClrType == typeof(bool))
|
||||
return Convert.ToInt32(value);
|
||||
return Convert.ToInt32(value, CultureInfo.InvariantCulture);
|
||||
if (property.ClrType == typeof(DateTime?))
|
||||
return "'" + ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss") + "'";
|
||||
return "'" + ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) + "'";
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
@ -145,8 +150,9 @@ private static string CreateUpdates(IReadOnlyCollection<IProperty> properties)
|
|||
(from property in properties
|
||||
where !property.IsPrimaryKey()
|
||||
select property.Name + " = VALUES(" + property.Name + ")").Aggregate("",
|
||||
(updates, columnUpdates) => updates == "" ? columnUpdates : updates + ", " + columnUpdates);
|
||||
if (update == "")
|
||||
(updates, columnUpdates) =>
|
||||
string.IsNullOrEmpty(updates) ? columnUpdates : updates + ", " + columnUpdates);
|
||||
if (string.IsNullOrEmpty(update))
|
||||
update = GetUpdateUnchangedColumnHack(properties);
|
||||
return update;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue