- 增加 Async CancellationToken IInsert/IUdate/IInsertOrUpdate;

This commit is contained in:
2881099
2020-11-12 06:35:29 +08:00
parent 2222fa269e
commit 8bbb7329f9
45 changed files with 492 additions and 433 deletions

View File

@ -7,6 +7,7 @@ using System.Data.Common;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FreeSql.Internal.CommonProvider
@ -16,7 +17,7 @@ namespace FreeSql.Internal.CommonProvider
{
#if net40
#else
async protected Task<int> SplitExecuteAffrowsAsync(int valuesLimit, int parameterLimit)
async protected Task<int> SplitExecuteAffrowsAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default)
{
var ss = SplitSource(valuesLimit, parameterLimit);
var ret = 0;
@ -28,7 +29,7 @@ namespace FreeSql.Internal.CommonProvider
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = await this.RawExecuteAffrowsAsync();
ret = await this.RawExecuteAffrowsAsync(cancellationToken);
ClearData();
return ret;
}
@ -49,7 +50,7 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += await this.RawExecuteAffrowsAsync();
ret += await this.RawExecuteAffrowsAsync(cancellationToken);
}
}
else
@ -66,7 +67,7 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret += await this.RawExecuteAffrowsAsync();
ret += await this.RawExecuteAffrowsAsync(cancellationToken);
}
_transaction.Commit();
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null));
@ -95,7 +96,7 @@ namespace FreeSql.Internal.CommonProvider
return ret;
}
async protected Task<long> SplitExecuteIdentityAsync(int valuesLimit, int parameterLimit)
async protected Task<long> SplitExecuteIdentityAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default)
{
var ss = SplitSource(valuesLimit, parameterLimit);
long ret = 0;
@ -107,7 +108,7 @@ namespace FreeSql.Internal.CommonProvider
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = await this.RawExecuteIdentityAsync();
ret = await this.RawExecuteIdentityAsync(cancellationToken);
ClearData();
return ret;
}
@ -128,8 +129,8 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
if (a < ss.Length - 1) await this.RawExecuteAffrowsAsync();
else ret = await this.RawExecuteIdentityAsync();
if (a < ss.Length - 1) await this.RawExecuteAffrowsAsync(cancellationToken);
else ret = await this.RawExecuteIdentityAsync(cancellationToken);
}
}
else
@ -146,8 +147,8 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
if (a < ss.Length - 1) await this.RawExecuteAffrowsAsync();
else ret = await this.RawExecuteIdentityAsync();
if (a < ss.Length - 1) await this.RawExecuteAffrowsAsync(cancellationToken);
else ret = await this.RawExecuteIdentityAsync(cancellationToken);
}
_transaction.Commit();
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null));
@ -176,7 +177,7 @@ namespace FreeSql.Internal.CommonProvider
return ret;
}
async protected Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit, int parameterLimit)
async protected Task<List<T1>> SplitExecuteInsertedAsync(int valuesLimit, int parameterLimit, CancellationToken cancellationToken = default)
{
var ss = SplitSource(valuesLimit, parameterLimit);
var ret = new List<T1>();
@ -188,7 +189,7 @@ namespace FreeSql.Internal.CommonProvider
if (ss.Length == 1)
{
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, 1, 1));
ret = await this.RawExecuteInsertedAsync();
ret = await this.RawExecuteInsertedAsync(cancellationToken);
ClearData();
return ret;
}
@ -209,7 +210,7 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(await this.RawExecuteInsertedAsync());
ret.AddRange(await this.RawExecuteInsertedAsync(cancellationToken));
}
}
else
@ -226,7 +227,7 @@ namespace FreeSql.Internal.CommonProvider
{
_source = ss[a];
_batchProgress?.Invoke(new BatchProgressStatus<T1>(_source, a + 1, ss.Length));
ret.AddRange(await this.RawExecuteInsertedAsync());
ret.AddRange(await this.RawExecuteInsertedAsync(cancellationToken));
}
_transaction.Commit();
_orm.Aop.TraceAfterHandler?.Invoke(this, new Aop.TraceAfterEventArgs(transBefore, "提交", null));
@ -255,7 +256,7 @@ namespace FreeSql.Internal.CommonProvider
return ret;
}
async protected virtual Task<int> RawExecuteAffrowsAsync()
async protected virtual Task<int> RawExecuteAffrowsAsync(CancellationToken cancellationToken = default)
{
var sql = ToSql();
var before = new Aop.CurdBeforeEventArgs(_table.Type, _table, Aop.CurdType.Insert, sql, _params);
@ -264,7 +265,7 @@ namespace FreeSql.Internal.CommonProvider
Exception exception = null;
try
{
affrows = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params);
affrows = await _orm.Ado.ExecuteNonQueryAsync(_connection, _transaction, CommandType.Text, sql, _commandTimeout, _params, cancellationToken);
}
catch (Exception ex)
{
@ -279,12 +280,12 @@ namespace FreeSql.Internal.CommonProvider
return affrows;
}
protected abstract Task<long> RawExecuteIdentityAsync();
protected abstract Task<List<T1>> RawExecuteInsertedAsync();
protected abstract Task<long> RawExecuteIdentityAsync(CancellationToken cancellationToken = default);
protected abstract Task<List<T1>> RawExecuteInsertedAsync(CancellationToken cancellationToken = default);
public abstract Task<int> ExecuteAffrowsAsync();
public abstract Task<long> ExecuteIdentityAsync();
public abstract Task<List<T1>> ExecuteInsertedAsync();
public abstract Task<int> ExecuteAffrowsAsync(CancellationToken cancellationToken = default);
public abstract Task<long> ExecuteIdentityAsync(CancellationToken cancellationToken = default);
public abstract Task<List<T1>> ExecuteInsertedAsync(CancellationToken cancellationToken = default);
#endif
}
}