Skip to content

Commit

Permalink
GetDirtyColumns返回修改为IDataColumn集合,方便使用
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 4, 2024
1 parent ca854f0 commit 063d027
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions XCode/Entity/EntityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,9 +490,8 @@ public static Int32 BatchInsert<T>(this IEnumerable<T> list, BatchOption? option
// columns = columns.Where(e => dirtys.Contains(e.Name)).ToArray();
if (!option.FullInsert && !fact.FullInsert)
{
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
//columns = columns.Where(e => dirtys.Contains(e.Name)).ToArray();
columns.Where(e => dirtys.Contains(dal.Db.FormatName(e))).ToArray();
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
columns = columns.Where(e => dirtys.Contains(e)).ToArray();
}

option.Columns = columns;
Expand Down Expand Up @@ -580,8 +579,8 @@ public static Int32 BatchInsertIgnore<T>(this IEnumerable<T> list, BatchOption?
// 每个列要么有脏数据,要么允许空。不允许空又没有脏数据的字段插入没有意义
if (!option.FullInsert && !fact.FullInsert)
{
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
columns = columns.Where(e => dirtys.Contains(dal.Db.FormatName(e))).ToArray();
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
columns = columns.Where(e => dirtys.Contains(e)).ToArray();
}

option.Columns = columns;
Expand Down Expand Up @@ -669,8 +668,8 @@ public static Int32 BatchReplace<T>(this IEnumerable<T> list, BatchOption? optio
// 每个列要么有脏数据,要么允许空。不允许空又没有脏数据的字段插入没有意义
if (!option.FullInsert && !fact.FullInsert)
{
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
columns = columns.Where(e => dirtys.Contains(dal.Db.FormatName(e))).ToArray();
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
columns = columns.Where(e => dirtys.Contains(e)).ToArray();
}

option.Columns = columns;
Expand Down Expand Up @@ -754,11 +753,11 @@ public static Int32 BatchUpdate<T>(this IEnumerable<T> list, BatchOption? option
if (option.UpdateColumns == null)
{
// 所有实体对象的脏字段作为更新字段
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
// 创建时间等字段不参与Update
dirtys = dirtys.Where(e => !e.StartsWithIgnoreCase("Create")).ToArray();
dirtys = dirtys.Where(e => !e.Name.StartsWithIgnoreCase("Create")).ToArray();

if (dirtys.Count > 0) option.UpdateColumns = dirtys;
if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => dal.Db.FormatName(e)).ToArray();
}
var updateColumns = option.UpdateColumns;
var addColumns = option.AddColumns ??= fact.AdditionalFields;
Expand Down Expand Up @@ -849,8 +848,8 @@ public static Int32 BatchUpsert<T>(this IEnumerable<T> list, BatchOption? option

if (!option.FullInsert && !fact.FullInsert)
{
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(dal.Db.FormatName(e))).ToArray();
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(e)).ToArray();
}

// 遇到自增字段,需要谨慎处理,部分insert部分update则无法执行upsert
Expand Down Expand Up @@ -898,11 +897,11 @@ public static Int32 BatchUpsert<T>(this IEnumerable<T> list, BatchOption? option
if (option.UpdateColumns == null)
{
// 所有实体对象的脏字段作为更新字段
var dirtys = GetDirtyColumns(dal, fact, list.Cast<IEntity>());
var dirtys = GetDirtyColumns(fact, list.Cast<IEntity>());
// 创建时间等字段不参与Update
dirtys = dirtys.Where(e => !e.StartsWithIgnoreCase("Create")).ToArray();
dirtys = dirtys.Where(e => !e.Name.StartsWithIgnoreCase("Create")).ToArray();

if (dirtys.Count > 0) option.UpdateColumns = dirtys;
if (dirtys.Count > 0) option.UpdateColumns = dirtys.Select(e => dal.Db.FormatName(e)).ToArray();
}
var updateColumns = option.UpdateColumns;
var addColumns = option.AddColumns ??= fact.AdditionalFields;
Expand Down Expand Up @@ -989,8 +988,8 @@ public static Int32 Upsert(this IEntity entity, BatchOption? option = null, IEnt
// columns = columns.Where(e => dirtys.Contains(e.Name)).ToArray();
if (!option.FullInsert && !fact.FullInsert)
{
var dirtys = GetDirtyColumns(dal, fact, [entity]);
columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(dal.Db.FormatName(e))).ToArray();
var dirtys = GetDirtyColumns(fact, [entity]);
columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(e)).ToArray();
}
option.Columns = columns;
}
Expand Down Expand Up @@ -1097,14 +1096,13 @@ public static IList<T> Merge<T, T2>(this IList<T> source, IEnumerable<T2> news,
}

/// <summary>获取可用于插入的数据列</summary>
/// <param name="dal"></param>
/// <param name="fact"></param>
/// <param name="list"></param>
/// <returns></returns>
private static IList<String> GetDirtyColumns(DAL dal, IEntityFactory fact, IEnumerable<IEntity> list)
public static IList<IDataColumn> GetDirtyColumns(this IEntityFactory fact, IEnumerable<IEntity> list)
{
// 任意实体来自数据库,则全部都是目标字段。因为有可能是从数据库查询出来的实体,然后批量插入
if (list.Any(e => e.IsFromDatabase)) return fact.Fields.Select(e => e.Name).ToList();
if (list.Any(e => e.IsFromDatabase)) return fact.Fields.Select(e => e.Field).ToList();

// 构建集合,已经标记为脏数据的字段不再搜索,减少循环次数
var fields = fact.Fields.ToList();
Expand Down Expand Up @@ -1141,11 +1139,11 @@ private static IList<String> GetDirtyColumns(DAL dal, IEntityFactory fact, IEnum
}
}

var rs = new List<String>();
var rs = new List<IDataColumn>();
foreach (var item in columns)
{
var dc = item.Field;
if (dc != null) rs.Add(dal.Db.FormatName(dc));
if (dc != null) rs.Add(dc);
}

return rs;
Expand Down

0 comments on commit 063d027

Please sign in to comment.