From 063d027673d1d69555735e43b7cc6bdfd34bcef7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Fri, 4 Oct 2024 20:30:08 +0800 Subject: [PATCH] =?UTF-8?q?GetDirtyColumns=E8=BF=94=E5=9B=9E=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BAIDataColumn=E9=9B=86=E5=90=88=EF=BC=8C?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- XCode/Entity/EntityExtension.cs | 42 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/XCode/Entity/EntityExtension.cs b/XCode/Entity/EntityExtension.cs index 8cde3f493..076c4d0fd 100644 --- a/XCode/Entity/EntityExtension.cs +++ b/XCode/Entity/EntityExtension.cs @@ -490,9 +490,8 @@ public static Int32 BatchInsert(this IEnumerable list, BatchOption? option // columns = columns.Where(e => dirtys.Contains(e.Name)).ToArray(); if (!option.FullInsert && !fact.FullInsert) { - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); - //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()); + columns = columns.Where(e => dirtys.Contains(e)).ToArray(); } option.Columns = columns; @@ -580,8 +579,8 @@ public static Int32 BatchInsertIgnore(this IEnumerable list, BatchOption? // 每个列要么有脏数据,要么允许空。不允许空又没有脏数据的字段插入没有意义 if (!option.FullInsert && !fact.FullInsert) { - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); - columns = columns.Where(e => dirtys.Contains(dal.Db.FormatName(e))).ToArray(); + var dirtys = GetDirtyColumns(fact, list.Cast()); + columns = columns.Where(e => dirtys.Contains(e)).ToArray(); } option.Columns = columns; @@ -669,8 +668,8 @@ public static Int32 BatchReplace(this IEnumerable list, BatchOption? optio // 每个列要么有脏数据,要么允许空。不允许空又没有脏数据的字段插入没有意义 if (!option.FullInsert && !fact.FullInsert) { - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); - columns = columns.Where(e => dirtys.Contains(dal.Db.FormatName(e))).ToArray(); + var dirtys = GetDirtyColumns(fact, list.Cast()); + columns = columns.Where(e => dirtys.Contains(e)).ToArray(); } option.Columns = columns; @@ -754,11 +753,11 @@ public static Int32 BatchUpdate(this IEnumerable list, BatchOption? option if (option.UpdateColumns == null) { // 所有实体对象的脏字段作为更新字段 - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); + var dirtys = GetDirtyColumns(fact, list.Cast()); // 创建时间等字段不参与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; @@ -849,8 +848,8 @@ public static Int32 BatchUpsert(this IEnumerable list, BatchOption? option if (!option.FullInsert && !fact.FullInsert) { - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); - columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(dal.Db.FormatName(e))).ToArray(); + var dirtys = GetDirtyColumns(fact, list.Cast()); + columns = columns.Where(e => e.PrimaryKey || dirtys.Contains(e)).ToArray(); } // 遇到自增字段,需要谨慎处理,部分insert部分update则无法执行upsert @@ -898,11 +897,11 @@ public static Int32 BatchUpsert(this IEnumerable list, BatchOption? option if (option.UpdateColumns == null) { // 所有实体对象的脏字段作为更新字段 - var dirtys = GetDirtyColumns(dal, fact, list.Cast()); + var dirtys = GetDirtyColumns(fact, list.Cast()); // 创建时间等字段不参与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; @@ -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; } @@ -1097,14 +1096,13 @@ public static IList Merge(this IList source, IEnumerable news, } /// 获取可用于插入的数据列 - /// /// /// /// - private static IList GetDirtyColumns(DAL dal, IEntityFactory fact, IEnumerable list) + public static IList GetDirtyColumns(this IEntityFactory fact, IEnumerable 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(); @@ -1141,11 +1139,11 @@ private static IList GetDirtyColumns(DAL dal, IEntityFactory fact, IEnum } } - var rs = new List(); + var rs = new List(); 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;