- 增加 IInsert/IUpdate BatchProgress 方法处理批量插入/更新时的进度;

This commit is contained in:
28810
2020-07-21 15:22:33 +08:00
parent a363ab82a7
commit f2f1495efc
11 changed files with 325 additions and 173 deletions

View File

@ -26,6 +26,7 @@ namespace FreeSql.Internal.CommonProvider
public bool _noneParameter, _insertIdentity;
public int _batchValuesLimit, _batchParameterLimit;
public bool _batchAutoTransaction = true;
public Action<BatchProgressStatus<T1>> _batchProgress;
public DbParameter[] _params;
public DbTransaction _transaction;
public DbConnection _connection;
@ -55,6 +56,7 @@ namespace FreeSql.Internal.CommonProvider
{
_batchValuesLimit = _batchParameterLimit = 0;
_batchAutoTransaction = true;
_batchProgress = null;
_insertIdentity = false;
_source.Clear();
_ignore.Clear();
@ -96,6 +98,12 @@ namespace FreeSql.Internal.CommonProvider
return this;
}
public IInsert<T1> BatchProgress(Action<BatchProgressStatus<T1>> callback)
{
_batchProgress = callback;
return this;
}
public IInsert<T1> AppendData(T1 source)
{
if (source != null)
@ -204,6 +212,7 @@ namespace FreeSql.Internal.CommonProvider
}
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = this.RawExecuteAffrows();
ClearData();
return ret;
@ -221,6 +230,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += this.RawExecuteAffrows();
}
}
@ -236,6 +246,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += this.RawExecuteAffrows();
}
_transaction.Commit();
@ -276,6 +287,7 @@ namespace FreeSql.Internal.CommonProvider
}
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = this.RawExecuteIdentity();
ClearData();
return ret;
@ -293,6 +305,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
if (a < ss.Length - 1) this.RawExecuteAffrows();
else ret = this.RawExecuteIdentity();
}
@ -309,6 +322,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
if (a < ss.Length - 1) this.RawExecuteAffrows();
else ret = this.RawExecuteIdentity();
}
@ -350,6 +364,7 @@ namespace FreeSql.Internal.CommonProvider
}
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = this.RawExecuteInserted();
ClearData();
return ret;
@ -367,6 +382,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(this.RawExecuteInserted());
}
}
@ -382,6 +398,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(this.RawExecuteInserted());
}
_transaction.Commit();

View File

@ -32,6 +32,7 @@ namespace FreeSql.Internal.CommonProvider
public bool _noneParameter;
public int _batchRowsLimit, _batchParameterLimit;
public bool _batchAutoTransaction = true;
public Action<BatchProgressStatus<T1>> _batchProgress;
public DbTransaction _transaction;
public DbConnection _connection;
@ -101,6 +102,12 @@ namespace FreeSql.Internal.CommonProvider
return this;
}
public IUpdate<T1> BatchProgress(Action<BatchProgressStatus<T1>> callback)
{
_batchProgress = callback;
return this;
}
protected void ValidateVersionAndThrow(int affrows)
{
if (_table.VersionColumn != null && _source.Count > 0)
@ -144,6 +151,7 @@ namespace FreeSql.Internal.CommonProvider
var ret = 0;
if (ss.Length <= 1)
{
if (_source?.Any() == true) _batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = this.RawExecuteAffrows();
ClearData();
return ret;
@ -161,6 +169,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += this.RawExecuteAffrows();
}
}
@ -176,6 +185,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += this.RawExecuteAffrows();
}
_transaction.Commit();
@ -211,6 +221,7 @@ namespace FreeSql.Internal.CommonProvider
var ret = new List<T1>();
if (ss.Length <= 1)
{
if (_source?.Any() == true) _batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = this.RawExecuteUpdated();
ClearData();
return ret;
@ -228,6 +239,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(this.RawExecuteUpdated());
}
}
@ -243,6 +255,7 @@ namespace FreeSql.Internal.CommonProvider
for (var a = 0; a < ss.Length; a++)
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(this.RawExecuteUpdated());
}
_transaction.Commit();

View File

@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace FreeSql.Internal.Model
{
public class BatchProgressStatus<T1>
{
/// <summary>
/// 当前操作的数据
/// </summary>
public IEnumerable<T1> Data { get; }
/// <summary>
/// 当前批次
/// </summary>
public int Current { get; }
/// <summary>
/// 总批次数量
/// </summary>
public int Total { get; }
public BatchProgressStatus(List<T1> data, int current, int total)
{
this.Data = data;
this.Current = current;
this.Total = total;
}
}
}