mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IInsertOrUpdate BatchOptions 选项;
This commit is contained in:
@ -27,6 +27,8 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public TableInfo _table;
|
||||
public ColumnInfo[] _tempPrimarys;
|
||||
public Func<string, string> _tableRule;
|
||||
public int _batchValuesLimit;
|
||||
public bool _batchAutoTransaction = true;
|
||||
public DbParameter[] _params;
|
||||
public DbTransaction _transaction;
|
||||
public DbConnection _connection;
|
||||
@ -56,7 +58,13 @@ namespace FreeSql.Internal.CommonProvider
|
||||
{
|
||||
_source.Clear();
|
||||
_sourceSql = null;
|
||||
_doNothing = false;
|
||||
_updateIgnore.Clear();
|
||||
_auditValueChangedDict.Clear();
|
||||
_updateSetDict.Clear();
|
||||
_batchValuesLimit = 0;
|
||||
_batchAutoTransaction = false;
|
||||
_params = null;
|
||||
}
|
||||
|
||||
public IInsertOrUpdate<T1> WithTransaction(DbTransaction transaction)
|
||||
@ -204,6 +212,13 @@ namespace FreeSql.Internal.CommonProvider
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual IInsertOrUpdate<T1> BatchOptions(int valuesLimit, bool autoTransaction = true)
|
||||
{
|
||||
_batchValuesLimit = valuesLimit;
|
||||
_batchAutoTransaction = autoTransaction;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected string TableRuleInvoke()
|
||||
{
|
||||
var tbname = _table?.DbName ?? "";
|
||||
@ -353,19 +368,31 @@ namespace FreeSql.Internal.CommonProvider
|
||||
if (threadTransaction != null) this.WithTransaction(threadTransaction);
|
||||
}
|
||||
|
||||
if (_transaction != null || _orm.Ado.MasterPool == null)
|
||||
if (_transaction != null || _orm.Ado.MasterPool == null || _batchAutoTransaction == false)
|
||||
{
|
||||
_SplitSourceByIdentityValueIsNullFlag = 1;
|
||||
foreach (var tmpsource in ss.Item1)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += this.RawExecuteAffrows();
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += this.RawExecuteAffrows();
|
||||
}
|
||||
}
|
||||
_SplitSourceByIdentityValueIsNullFlag = 2;
|
||||
foreach (var tmpsource in ss.Item2)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += this.RawExecuteAffrows();
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += this.RawExecuteAffrows();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -380,14 +407,26 @@ namespace FreeSql.Internal.CommonProvider
|
||||
_SplitSourceByIdentityValueIsNullFlag = 1;
|
||||
foreach (var tmpsource in ss.Item1)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += this.RawExecuteAffrows();
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += this.RawExecuteAffrows();
|
||||
}
|
||||
}
|
||||
_SplitSourceByIdentityValueIsNullFlag = 2;
|
||||
foreach (var tmpsource in ss.Item2)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += this.RawExecuteAffrows();
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += this.RawExecuteAffrows();
|
||||
}
|
||||
}
|
||||
_transaction.Commit();
|
||||
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.Commit, null));
|
||||
@ -461,7 +500,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
}
|
||||
async public Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (_sourceSql != null) return this.RawExecuteAffrows();
|
||||
if (_sourceSql != null) return await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
var affrows = 0;
|
||||
var ss = SplitSourceByIdentityValueIsNull(_source);
|
||||
try
|
||||
@ -472,19 +511,31 @@ namespace FreeSql.Internal.CommonProvider
|
||||
if (threadTransaction != null) this.WithTransaction(threadTransaction);
|
||||
}
|
||||
|
||||
if (_transaction != null || _orm.Ado.MasterPool == null)
|
||||
if (_transaction != null || _orm.Ado.MasterPool == null || _batchAutoTransaction == false)
|
||||
{
|
||||
_SplitSourceByIdentityValueIsNullFlag = 1;
|
||||
foreach (var tmpsource in ss.Item1)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
_SplitSourceByIdentityValueIsNullFlag = 2;
|
||||
foreach (var tmpsource in ss.Item2)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -499,14 +550,26 @@ namespace FreeSql.Internal.CommonProvider
|
||||
_SplitSourceByIdentityValueIsNullFlag = 1;
|
||||
foreach (var tmpsource in ss.Item1)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
_SplitSourceByIdentityValueIsNullFlag = 2;
|
||||
foreach (var tmpsource in ss.Item2)
|
||||
{
|
||||
_source = tmpsource;
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
var pageTotal = Math.Ceiling(tmpsource.Count * 1.0 / _batchValuesLimit);
|
||||
for (var pageNumber = 1; pageNumber <= pageTotal; pageNumber++)
|
||||
{
|
||||
_source = pageNumber > 1 ?
|
||||
tmpsource.Skip((pageNumber - 1) * _batchValuesLimit).Take(_batchValuesLimit).ToList() :
|
||||
tmpsource.Take(_batchValuesLimit).ToList();
|
||||
affrows += await this.RawExecuteAffrowsAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
_transaction.Commit();
|
||||
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, CoreStrings.Commit, null));
|
||||
|
Reference in New Issue
Block a user