mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-01-23 21:32:04 +08:00
Update BulkMerge
This commit is contained in:
@@ -170,7 +170,7 @@ namespace SqlSugar
|
||||
public Task<int> BulkMergeAsync(List<T> datas)
|
||||
{
|
||||
var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
|
||||
var whereColumns = entityInfo.Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); Begin(datas, false);
|
||||
var whereColumns = entityInfo.Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); ;
|
||||
return BulkMergeAsync(datas, whereColumns, updateColumns);
|
||||
}
|
||||
public int BulkMerge(List<T> datas)
|
||||
@@ -192,7 +192,7 @@ namespace SqlSugar
|
||||
{
|
||||
int resul = 0;
|
||||
await this.context.Utilities.PageEachAsync(datas, Size, async item =>
|
||||
{
|
||||
{
|
||||
resul += await _BulkMerge(item, updateColumns, whereColumns);
|
||||
});
|
||||
return resul;
|
||||
@@ -208,7 +208,8 @@ namespace SqlSugar
|
||||
}
|
||||
|
||||
private async Task<int> _BulkMerge(List<T> 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;
|
||||
@@ -216,18 +217,21 @@ namespace SqlSugar
|
||||
DataTable dt = ToDdateTable(datas);
|
||||
IFastBuilder buider = GetBuider();
|
||||
buider.Context = context;
|
||||
await buider.CreateTempAsync<T>(dt);
|
||||
await buider.ExecuteBulkCopyAsync(dt);
|
||||
if (buider?.DbFastestProperties?.IsMerge == true)
|
||||
{
|
||||
await buider.CreateTempAsync<T>(dt);
|
||||
await buider.ExecuteBulkCopyAsync(dt);
|
||||
}
|
||||
var result = await buider.Merge(dt, this.entityInfo,whereColumns,updateColumns, datas);
|
||||
//var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
|
||||
//var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
|
||||
if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
|
||||
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);
|
||||
End(datas, false,true);
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
@@ -346,9 +350,13 @@ namespace SqlSugar
|
||||
#endregion
|
||||
|
||||
#region AOP
|
||||
private void End<Type>(List<Type> datas,bool isAdd)
|
||||
private void End<Type>(List<Type> datas,bool isAdd,bool isMerge=false)
|
||||
{
|
||||
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
||||
if (isMerge)
|
||||
{
|
||||
title = "BulkMerge";
|
||||
}
|
||||
this.context.Ado.IsEnableLogEvent = isLog;
|
||||
if (this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuted != null)
|
||||
{
|
||||
@@ -356,9 +364,13 @@ namespace SqlSugar
|
||||
}
|
||||
RemoveCache();
|
||||
}
|
||||
private void Begin<Type>(List<Type> datas,bool isAdd)
|
||||
private void Begin<Type>(List<Type> datas,bool isAdd, bool isMerge = false)
|
||||
{
|
||||
var title = isAdd ? "BulkCopy" : "BulkUpdate";
|
||||
if (isMerge)
|
||||
{
|
||||
title = "BulkMerge";
|
||||
}
|
||||
isLog = this.context.Ado.IsEnableLogEvent;
|
||||
this.context.Ado.IsEnableLogEvent = false;
|
||||
if (this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuting != null)
|
||||
|
||||
@@ -11,5 +11,6 @@ namespace SqlSugar
|
||||
public bool HasOffsetTime { get; set; }
|
||||
public string[] WhereColumns { get; set; }
|
||||
public bool IsOffIdentity { get; set; }
|
||||
public bool IsMerge { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,10 @@ namespace SqlSugar
|
||||
{
|
||||
public class OracleFastBuilder : FastBuilder, IFastBuilder
|
||||
{
|
||||
public override DbFastestProperties DbFastestProperties { get; set; } = new DbFastestProperties()
|
||||
{
|
||||
IsMerge = true
|
||||
};
|
||||
private EntityInfo entityInfo;
|
||||
|
||||
public OracleFastBuilder(EntityInfo entityInfo)
|
||||
|
||||
@@ -14,7 +14,8 @@ namespace SqlSugar
|
||||
{
|
||||
public override bool IsActionUpdateColumns { get; set; } = true;
|
||||
public override DbFastestProperties DbFastestProperties { get; set; } = new DbFastestProperties() {
|
||||
HasOffsetTime=true
|
||||
HasOffsetTime=true,
|
||||
IsMerge=true
|
||||
};
|
||||
public async Task<int> ExecuteBulkCopyAsync(DataTable dt)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user