From a020ae7bf8c10a7134b200662ca01b604fa1a6d7 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 9 Jan 2024 11:30:43 +0800 Subject: [PATCH] Optimized code --- .../FastestProvider/FastestProvider.cs | 94 +++++++++++-------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs index bf68a0b89..2010cecfe 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/FastestProvider/FastestProvider.cs @@ -209,57 +209,73 @@ namespace SqlSugar private async Task _BulkMerge(List datas, string[] updateColumns, string[] whereColumns) { - Begin(datas, false,true); - Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); - Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); - var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection; - this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; - DataTable dt = ToDdateTable(datas); - IFastBuilder buider = GetBuider(); - buider.Context = context; - if (buider?.DbFastestProperties?.IsMerge == true) + try { - await buider.CreateTempAsync(dt); - await buider.ExecuteBulkCopyAsync(dt); + Begin(datas, false, true); + Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); + Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); + var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection; + this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; + DataTable dt = ToDdateTable(datas); + IFastBuilder buider = GetBuider(); + buider.Context = context; + if (buider?.DbFastestProperties?.IsMerge == true) + { + await buider.CreateTempAsync(dt); + await buider.ExecuteBulkCopyAsync(dt); + } + var result = await buider.Merge(GetTableName(), dt, this.entityInfo, whereColumns, updateColumns, datas); + //var queryTemp = this.context.Queryable().AS(dt.TableName).ToList();//test + //var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns); + if (buider?.DbFastestProperties?.IsMerge == true && this.context.CurrentConnectionConfig.DbType != DbType.Sqlite) + { + this.context.DbMaintenance.DropTable(dt.TableName); + } + this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; + buider.CloseDb(); + End(datas, false, true); + return result; } - var result = await buider.Merge(GetTableName(),dt, this.entityInfo,whereColumns,updateColumns, datas); - //var queryTemp = this.context.Queryable().AS(dt.TableName).ToList();//test - //var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns); - if (buider?.DbFastestProperties?.IsMerge == true&&this.context.CurrentConnectionConfig.DbType != DbType.Sqlite) + catch (Exception) { - this.context.DbMaintenance.DropTable(dt.TableName); + this.context.Close(); + throw; } - this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; - buider.CloseDb(); - End(datas, false,true); - return result; } #endregion #region Core private async Task _BulkUpdate(List datas, string[] whereColumns, string[] updateColumns) { - Begin(datas,false); - Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); - Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0"); - var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection; - this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; - DataTable dt = ToDdateTable(datas); - IFastBuilder buider = GetBuider(); - ActionIgnoreColums(whereColumns, updateColumns, dt, buider.IsActionUpdateColumns); - buider.Context = context; - await buider.CreateTempAsync(dt); - await buider.ExecuteBulkCopyAsync(dt); - //var queryTemp = this.context.Queryable().AS(dt.TableName).ToList();//test - var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns); - if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite) + try { - this.context.DbMaintenance.DropTable(dt.TableName); + Begin(datas, false); + Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key"); + Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0"); + var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection; + this.context.CurrentConnectionConfig.IsAutoCloseConnection = false; + DataTable dt = ToDdateTable(datas); + IFastBuilder buider = GetBuider(); + ActionIgnoreColums(whereColumns, updateColumns, dt, buider.IsActionUpdateColumns); + buider.Context = context; + await buider.CreateTempAsync(dt); + await buider.ExecuteBulkCopyAsync(dt); + //var queryTemp = this.context.Queryable().AS(dt.TableName).ToList();//test + var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns); + if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite) + { + this.context.DbMaintenance.DropTable(dt.TableName); + } + this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; + buider.CloseDb(); + End(datas, false); + return result; + } + catch (Exception) + { + this.context.Close(); + throw; } - this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; - buider.CloseDb(); - End(datas, false); - return result; } private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns)