From 9daae7160ed37e1779d61041dfdcb6f681ddf3e3 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 11 Apr 2025 09:22:36 +0800 Subject: [PATCH] Add BulkMerge (userInfo, new string[] { "UserId" }, new string[] { "UserName" }, true) --- .../FastestProvider/FastestProvider.cs | 46 +++++++++++++++++++ .../SqlSugar/Interface/IFastest.cs | 2 + 2 files changed, 48 insertions(+) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs index 2babbbe3a..4ed1956f3 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs @@ -198,6 +198,18 @@ namespace SqlSugar var result = (Task)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue }); return result; } + public int BulkMerge(DataTable dataTable, string[] whereColumns, string[] updateColumns, bool isIdentity) + { + return BulkMergeAsync(dataTable, whereColumns, updateColumns, isIdentity).GetAwaiter().GetResult(); + } + public Task BulkMergeAsync(DataTable dataTable, string[] whereColumns, string[] updateColumns, bool isIdentity) + { + object newValue, fastestMethod; + MethodInfo bulkCopyMethod; + _BulkMerge(dataTable, whereColumns,updateColumns, out newValue, out fastestMethod, out bulkCopyMethod, true, isIdentity); + var result = (Task)bulkCopyMethod.Invoke(fastestMethod, new object[] { newValue,whereColumns,updateColumns }); + return result; + } public Task BulkMergeAsync(List datas, string[] whereColumns) { var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); @@ -303,6 +315,40 @@ namespace SqlSugar .Invoke(this.context, null); bulkCopyMethod = fastestMethod.GetType().GetMyMethod(isAsync? "BulkMergeAsync" : "BulkMerge", 1); } + private void _BulkMerge(DataTable dataTable, string[] whereColumns,string [] updateColumns, out object newValue, out object fastestMethod, out MethodInfo bulkCopyMethod, bool isAsync, bool isIdentity) + { + Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名"); + var className = "BulkMerge_" + isIdentity + this.AsName.GetNonNegativeHashCodeString(); + var builder = this.context.DynamicBuilder().CreateClass(className, new SugarTable() + { + TableName = this.AsName + }); + foreach (DataColumn item in dataTable.Columns) + { + var isPrimaryKey = whereColumns.Any(it => it.EqualCase(item.ColumnName)); + var propertyType = item.DataType; + if (!propertyType.IsClass() && propertyType != typeof(string) && propertyType != typeof(byte[])) + { + propertyType = typeof(Nullable<>).MakeGenericType(UtilMethods.GetUnderType(item.DataType)); + } + builder.CreateProperty(item.ColumnName, propertyType, new SugarColumn() + { + IsPrimaryKey = isPrimaryKey, + IsIdentity = isIdentity && isPrimaryKey, + IsNullable = true, + + }); + } + var dicList = this.context.Utilities.DataTableToDictionaryList(dataTable); + var type = builder.WithCache().BuilderType(); + var value = this.context.DynamicBuilder().CreateObjectByType(type, dicList); + newValue = UtilMethods.ConvertToObjectList(type, value); + fastestMethod = this.context.GetType() + .GetMethod("Fastest") + .MakeGenericMethod(type) + .Invoke(this.context, null); + bulkCopyMethod = fastestMethod.GetType().GetMyMethod(isAsync ? "BulkMergeAsync" : "BulkMerge", 3, newValue.GetType(), typeof(string[]), typeof(string[])); + } private async Task _BulkUpdate(List datas, string[] whereColumns, string[] updateColumns) { diff --git a/Src/Asp.NetCore2/SqlSugar/Interface/IFastest.cs b/Src/Asp.NetCore2/SqlSugar/Interface/IFastest.cs index 7e969996d..3c7d2ce6c 100644 --- a/Src/Asp.NetCore2/SqlSugar/Interface/IFastest.cs +++ b/Src/Asp.NetCore2/SqlSugar/Interface/IFastest.cs @@ -38,6 +38,8 @@ namespace SqlSugar int BulkMerge(List datas); int BulkMerge(DataTable dataTable, string[] whereColumns,bool isIdentity); Task BulkMergeAsync(DataTable dataTable, string[] whereColumns, bool isIdentity); + int BulkMerge(DataTable dataTable, string[] whereColumns,string[] updateColumns, bool isIdentity); + Task BulkMergeAsync(DataTable dataTable, string[] whereColumns, string[] updateColumns, bool isIdentity); Task BulkMergeAsync(List datas, string[] whereColumns); int BulkMerge(List datas, string[] whereColumns); Task BulkMergeAsync(List datas, string[] whereColumns, string[] updateColumns);