From dbd014a5615d9cb8573c51972aeeba2430968a11 Mon Sep 17 00:00:00 2001 From: sunkaixuna <610262374@qq.com> Date: Wed, 5 Jan 2022 20:48:55 +0800 Subject: [PATCH] Update core --- .../FastestProvider/FastestProvider.cs | 81 ++++++++++++- .../Abstract/FastestProvider/Private.cs | 34 ++++++ .../QueryableProvider/QueryableProvider.cs | 14 ++- .../SqlBuilderProvider/QueryBuilder.cs | 8 +- .../SqlSugar/Entities/JoinMapper.cs | 14 +++ .../SqlSugar/Infrastructure/Mapper.cs | 106 ++++++------------ .../SqlSugar/Interface/IFastest.cs | 6 +- 7 files changed, 180 insertions(+), 83 deletions(-) create mode 100644 Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/JoinMapper.cs diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/FastestProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/FastestProvider.cs index be9e61b2b..5ae276559 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/FastestProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/FastestProvider.cs @@ -20,6 +20,26 @@ namespace SqlSugar } #region BulkCopy + public int BulkCopy(string tableName,DataTable dt) + { + return BulkCopyAsync(tableName,dt).ConfigureAwait(true).GetAwaiter().GetResult(); + } + public async Task BulkCopyAsync(string tableName, DataTable dt) + { + if (Size > 0) + { + int resul = 0; + await this.context.Utilities.PageEachAsync(dt.Rows.Cast().ToList(), Size, async item => + { + resul += await _BulkCopy(tableName,item.CopyToDataTable()); + }); + return resul; + } + else + { + return await _BulkCopy(tableName,dt); + } + } public int BulkCopy(List datas) { return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult(); @@ -74,6 +94,28 @@ namespace SqlSugar return await _BulkUpdate(datas, whereColumns, updateColumns); } } + + public int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns) + { + return BulkUpdateAsync(tableName,dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult(); + } + public async Task BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns) + { + + if (Size > 0) + { + int resul = 0; + await this.context.Utilities.PageEachAsync(dataTable.Rows.Cast().ToList(), Size, async item => + { + resul += await _BulkUpdate(tableName,item.CopyToDataTable(), whereColumns, updateColumns); + }); + return resul; + } + else + { + return await _BulkUpdate(tableName,dataTable, whereColumns, updateColumns); + } + } #endregion #region Core @@ -97,6 +139,28 @@ namespace SqlSugar End(datas, false); return result; } + private async Task _BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns) + { + var datas = new string[dataTable.Rows.Count].ToList(); + 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.TableName = this.queryable.SqlBuilder.GetTranslationTableName(tableName); + DataTable dt = GetCopyWriteDataTable(dataTable); + IFastBuilder buider = GetBuider(); + 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); + this.context.DbMaintenance.DropTable(dt.TableName); + this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto; + buider.CloseDb(); + End(datas, false); + return result; + } private async Task _BulkCopy(List datas) { Begin(datas,true); @@ -107,10 +171,23 @@ namespace SqlSugar End(datas,true); return result; } + private async Task _BulkCopy(string tableName,DataTable dataTable) + { + var datas =new string[dataTable.Rows.Count].ToList(); + Begin(datas, true); + DataTable dt = dataTable; + dt.TableName =this.queryable.SqlBuilder.GetTranslationTableName(tableName); + dt = GetCopyWriteDataTable(dt); + IFastBuilder buider = GetBuider(); + buider.Context = context; + var result = await buider.ExecuteBulkCopyAsync(dt); + End(datas, true); + return result; + } #endregion #region AOP - private void End(List datas,bool isAdd) + private void End(List datas,bool isAdd) { var title = isAdd ? "BulkCopy" : "BulkUpdate"; this.context.Ado.IsEnableLogEvent = isLog; @@ -120,7 +197,7 @@ namespace SqlSugar } } - private void Begin(List datas,bool isAdd) + private void Begin(List datas,bool isAdd) { var title = isAdd ? "BulkCopy" : "BulkUpdate"; isLog = this.context.Ado.IsEnableLogEvent; diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/Private.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/Private.cs index fd6048980..13a38e467 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/Private.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/Private.cs @@ -127,5 +127,39 @@ namespace SqlSugar } return value; } + private DataTable GetCopyWriteDataTable(DataTable dt) + { + DataTable tempDataTable = ReflectionInoCore.GetInstance().GetOrCreate("BulkCopyAsync_dt" + dt.TableName, + () => + { + if (AsName == null) + { + return queryable.Where(it => false).Select("*").ToDataTable(); + } + else + { + return queryable.AS(AsName).Where(it => false).Select("*").ToDataTable(); + } + } + ); + var temColumnsList = tempDataTable.Columns.Cast().Select(it => it.ColumnName.ToLower()).ToList(); + var columns = dt.Columns.Cast().Where(it => temColumnsList.Contains(it.ColumnName.ToLower())).ToList(); + foreach (DataRow item in dt.Rows) + { + DataRow dr = tempDataTable.NewRow(); + foreach (DataColumn column in columns) + { + + dr[column.ColumnName] = item[column.ColumnName]; + if (dr[column.ColumnName] == null) + { + dr[column.ColumnName] = DBNull.Value; + } + } + tempDataTable.Rows.Add(dr); + } + tempDataTable.TableName = dt.TableName; + return tempDataTable; + } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 0b5f1eb85..1122deed8 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -931,11 +931,19 @@ namespace SqlSugar { tableQueryables.Add(this.Clone().AS(item.TableName)); } - Check.Exception(tableQueryables.Count == 0, ErrorMessage.GetThrowMessage("SplitTable error . There are no tables after filtering", "SplitTable没有筛选出分表,请检查条件和数据库中的表")); - var unionall = this.Context._UnionAll(tableQueryables.ToArray()); + if (tableQueryables.Count == 0) + { + var result= this.Context.SqlQueryable("-- No table ").Select(); + result.QueryBuilder.SelectValue = null; + return result; + } + else + { + var unionall = this.Context._UnionAll(tableQueryables.ToArray()); + return unionall; + } //var values= unionall.QueryBuilder.GetSelectValue; //unionall.QueryBuilder.SelectValue = values; - return unionall; } public ISugarQueryable Distinct() { diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs index faaf92d60..6f8e64eb5 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs @@ -441,12 +441,12 @@ namespace SqlSugar return string.Format(temp, sql.ToString(), (pageIndex - 1) * pageSize + 1, pageIndex * pageSize); } - public virtual string GetSelectByItems(List> items) + public virtual string GetSelectByItems(List> items) { var array = items.Select(it => { - dynamic dynamicObj = this.Context.Utilities.DeserializeObject(this.Context.Utilities.SerializeObject(it.Value)); - var dbName = Builder.GetTranslationColumnName((string)(dynamicObj.dbName)); - var asName = Builder.GetTranslationColumnName((string)(dynamicObj.asName)); + JoinMapper dynamicObj = it.Value; + var dbName = Builder.GetTranslationColumnName(dynamicObj.DbName); + var asName = Builder.GetTranslationColumnName(dynamicObj.AsName); return string.Format("{0}.{1} AS {2}", it.Key, dbName, asName); }); return string.Join(",", array); diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/JoinMapper.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/JoinMapper.cs new file mode 100644 index 000000000..efe4f965a --- /dev/null +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/JoinMapper.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class JoinMapper + { + public string AsName { get; set; } + public string DbName { get; set; } + } +} diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs index c8bb72b8c..ef6cc495b 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs @@ -19,86 +19,46 @@ namespace SqlSugar string result = string.Empty; var veiwModel = _context.EntityMaintenance.GetEntityInfo(); var exp = (queryBuilder.JoinExpression as LambdaExpression); - List> selectItems = new List>(); - foreach (var viewColumn in veiwModel.Columns) + List> selectItems = new List>(); + var exParsmeters = exp.Parameters.Select(it => new { shortName = it.Name, type = it.Type }).ToList(); + foreach (var viewColumns in veiwModel.Columns) { - var exParsmeters = exp.Parameters.Select(it => new { shortName = it.Name, type = it.Type }).ToList(); + var isbreak = false; foreach (var expPars in exParsmeters) - { - var columns = _context.EntityMaintenance.GetEntityInfo(expPars.type).Columns.Where(it => it.IsIgnore == false); - var joinModelDbColumns = columns.Select(it => - new { asName = it.PropertyName, dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelProperties = columns.Select(it => - new { asName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelDbColumnsWithType = columns.Select(it => - new { asName = (expPars.type.Name + it.PropertyName), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelPropertiesWithTye = columns.Select(it => - new { asName = (expPars.type.Name + _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type)), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelDbColumns_WithType = columns.Select(it => - new { asName = (expPars.type.Name +"_"+ it.PropertyName), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelProperties_WithType = columns.Select(it => - new { asName = (expPars.type.Name +"_"+ _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type)), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelDbColumnsWithSN = columns.Select(it => - new { asName = (expPars.shortName + it.PropertyName), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - var joinModelPropertiesWithSN = columns.Select(it => - new { asName = (expPars.shortName + _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type)), dbName = _context.EntityMaintenance.GetDbColumnName(it.PropertyName, expPars.type) }).ToList(); - - if (joinModelDbColumns.Any(it => it.asName.Equals(viewColumn.PropertyName,StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelDbColumns.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName,value)); - break; - } - if (joinModelProperties.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) + { + if (isbreak) { - var value = joinModelProperties.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; - } - if (joinModelDbColumnsWithType.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelDbColumnsWithType.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; - } - if (joinModelPropertiesWithTye.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelPropertiesWithTye.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); break; } + var entityInfo = _context.EntityMaintenance.GetEntityInfo(expPars.type); + var columns = entityInfo.Columns.Where(it => it.IsIgnore == false); + var list = columns.Select(it => { + var array = new string[] + { + it.PropertyName, + it.DbColumnName, - if (joinModelDbColumns_WithType.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelDbColumns_WithType.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; - } - if (joinModelProperties_WithType.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelProperties_WithType.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; - } - if (joinModelDbColumnsWithSN.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) - { - var value = joinModelDbColumnsWithSN.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; - } - if (joinModelPropertiesWithSN.Any(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase))) + expPars.type.Name+it.PropertyName, + expPars.type.Name+it.DbColumnName, + + expPars.type.Name+"_"+it.PropertyName, + expPars.type.Name+"_"+it.DbColumnName, + + expPars.shortName+it.PropertyName, + expPars.shortName+it.DbColumnName, + }; + return new { it, array }; + }).ToList(); + var columnInfo= list.FirstOrDefault(y => y.array.Select(z=>z.ToLower()).Contains(viewColumns.PropertyName.ToLower())); + if (columnInfo != null) { - var value = joinModelPropertiesWithSN.First(it => it.asName.Equals(viewColumn.PropertyName, StringComparison.CurrentCultureIgnoreCase)); - if (viewColumn.PropertyName.Equals(value.asName, StringComparison.CurrentCultureIgnoreCase)) - selectItems.Add(new KeyValuePair(expPars.shortName, value)); - break; + JoinMapper joinMapper = new JoinMapper() + { + AsName=viewColumns.PropertyName, + DbName=columnInfo.it.DbColumnName + }; + selectItems.Add(new KeyValuePair(expPars.shortName,joinMapper)); + isbreak = true; } } } diff --git a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs index 9d8fb37a6..e51ad16b2 100644 --- a/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs +++ b/Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Text; using System.Threading.Tasks; @@ -11,12 +12,15 @@ namespace SqlSugar IFastest PageSize(int Size); int BulkCopy(List datas); Task BulkCopyAsync(List datas); + int BulkCopy(string tableName,DataTable dataTable); + Task BulkCopyAsync(string tableName, DataTable dataTable); int BulkUpdate(List datas); Task BulkUpdateAsync(List datas); int BulkUpdate(List datas, string[] whereColumns, string[] updateColumns); Task BulkUpdateAsync(List datas, string[] whereColumns, string[] updateColumns); - + int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns); + Task BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns); SplitFastest SplitTable(); } }