Browse Source

Update core

pull/12/head
sunkaixuna 3 years ago
parent
commit
dbd014a561
  1. 81
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/FastestProvider.cs
  2. 34
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/FastestProvider/Private.cs
  3. 14
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs
  4. 8
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Abstract/SqlBuilderProvider/QueryBuilder.cs
  5. 14
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Entities/JoinMapper.cs
  6. 106
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs
  7. 6
      Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Interface/IFastest.cs

81
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<int> BulkCopyAsync(string tableName, DataTable dt)
{
if (Size > 0)
{
int resul = 0;
await this.context.Utilities.PageEachAsync(dt.Rows.Cast<DataRow>().ToList(), Size, async item =>
{
resul += await _BulkCopy(tableName,item.CopyToDataTable());
});
return resul;
}
else
{
return await _BulkCopy(tableName,dt);
}
}
public int BulkCopy(List<T> 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<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns)
{
if (Size > 0)
{
int resul = 0;
await this.context.Utilities.PageEachAsync(dataTable.Rows.Cast<DataRow>().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<int> _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<object>(dt);
await buider.ExecuteBulkCopyAsync(dt);
//var queryTemp = this.context.Queryable<T>().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<int> _BulkCopy(List<T> datas)
{
Begin(datas,true);
@ -107,10 +171,23 @@ namespace SqlSugar
End(datas,true);
return result;
}
private async Task<int> _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<T> datas,bool isAdd)
private void End<Type>(List<Type> datas,bool isAdd)
{
var title = isAdd ? "BulkCopy" : "BulkUpdate";
this.context.Ado.IsEnableLogEvent = isLog;
@ -120,7 +197,7 @@ namespace SqlSugar
}
}
private void Begin(List<T> datas,bool isAdd)
private void Begin<Type>(List<Type> datas,bool isAdd)
{
var title = isAdd ? "BulkCopy" : "BulkUpdate";
isLog = this.context.Ado.IsEnableLogEvent;

34
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<DataTable>.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<DataColumn>().Select(it => it.ColumnName.ToLower()).ToList();
var columns = dt.Columns.Cast<DataColumn>().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;
}
}
}

14
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<object>("-- No table ").Select<T>();
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<T> Distinct()
{

8
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<KeyValuePair<string, object>> items)
public virtual string GetSelectByItems(List<KeyValuePair<string, JoinMapper>> items)
{
var array = items.Select(it => {
dynamic dynamicObj = this.Context.Utilities.DeserializeObject<dynamic>(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);

14
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; }
}
}

106
Src/Asp.NetCore2/SqlSeverTest/SqlSugar/Infrastructure/Mapper.cs

@ -19,86 +19,46 @@ namespace SqlSugar
string result = string.Empty;
var veiwModel = _context.EntityMaintenance.GetEntityInfo<TResult>();
var exp = (queryBuilder.JoinExpression as LambdaExpression);
List<KeyValuePair<string, object>> selectItems = new List<KeyValuePair<string, object>>();
foreach (var viewColumn in veiwModel.Columns)
List<KeyValuePair<string, JoinMapper>> selectItems = new List<KeyValuePair<string, JoinMapper>>();
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<string, object>(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<string, object>(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<string, object>(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<string, object>(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<string, object>(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<string, object>(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<string, object>(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<string, object>(expPars.shortName, value));
break;
JoinMapper joinMapper = new JoinMapper()
{
AsName=viewColumns.PropertyName,
DbName=columnInfo.it.DbColumnName
};
selectItems.Add(new KeyValuePair<string, JoinMapper>(expPars.shortName,joinMapper));
isbreak = true;
}
}
}

6
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<T> PageSize(int Size);
int BulkCopy(List<T> datas);
Task<int> BulkCopyAsync(List<T> datas);
int BulkCopy(string tableName,DataTable dataTable);
Task<int> BulkCopyAsync(string tableName, DataTable dataTable);
int BulkUpdate(List<T> datas);
Task<int> BulkUpdateAsync(List<T> datas);
int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns);
Task<int> BulkUpdateAsync(List<T> datas, string[] whereColumns, string[] updateColumns);
int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns);
Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns);
SplitFastest<T> SplitTable();
}
}

Loading…
Cancel
Save