Add Queryable.TranLock

This commit is contained in:
sunkaixuan 2022-05-23 16:25:21 +08:00
parent df30a3c6e2
commit 32a0057668
8 changed files with 51 additions and 0 deletions

View File

@ -389,6 +389,27 @@ namespace SqlSugar
_WhereClassByPrimaryKey(new List<T>() { data });
return this;
}
public ISugarQueryable<T> TranLock(DbLockType LockType = DbLockType.Wait)
{
Check.ExceptionEasy(this.Context.Ado.Transaction == null, "need BeginTran", "需要事务才能使用TranLock");
Check.ExceptionEasy(this.QueryBuilder.IsSingle()==false, "TranLock, can only be used for single table query", "TranLock只能用在单表查询");
if (this.Context.CurrentConnectionConfig.DbType == DbType.SqlServer)
{
if (LockType == DbLockType.Wait)
{
this.With("UpdLock,RowLock");
}
else
{
this.With("UpdLock,RowLock,NoWait");
}
}
else
{
this.QueryBuilder.TranLock = (LockType == DbLockType.Error? " for update nowait" : " for update");
}
return this;
}
public ISugarQueryable<T> WhereColumns(List<Dictionary<string, object>> list)
{
List<IConditionalModel> conditionalModels = new List<IConditionalModel>();
@ -3323,6 +3344,7 @@ namespace SqlSugar
_Size=it._Size
}).ToList();
}
asyncQueryableBuilder.TranLock = this.QueryBuilder.TranLock;
asyncQueryableBuilder.IsDisableMasterSlaveSeparation = this.QueryBuilder.IsDisableMasterSlaveSeparation;
asyncQueryableBuilder.IsQueryInQuery = this.QueryBuilder.IsQueryInQuery;
asyncQueryableBuilder.Includes = this.QueryBuilder.Includes;

View File

@ -33,6 +33,7 @@ namespace SqlSugar
#endregion
#region Splicing basic
public string TranLock { get; set; }
public bool IsDisableMasterSlaveSeparation { get; set; }
public bool IsQueryInQuery { get; set; }
public List<object> Includes { get; set; }

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public enum DbLockType
{
Wait=0,
Error=1
}
}

View File

@ -54,6 +54,7 @@ namespace SqlSugar
ISugarQueryable<T> WhereClassByPrimaryKey(List<T> list);
ISugarQueryable<T> WhereClassByPrimaryKey(T data) ;
ISugarQueryable<T> WhereColumns(List<Dictionary<string, object>> columns);
ISugarQueryable<T> TranLock(DbLockType LockType = DbLockType.Wait);
ISugarQueryable<T> Where(Expression<Func<T, bool>> expression);
ISugarQueryable<T> Where(string whereString, object parameters = null);
ISugarQueryable<T> Where(List<IConditionalModel> conditionalModels);

View File

@ -66,6 +66,10 @@ namespace SqlSugar
{
return "-- No table";
}
if (TranLock != null)
{
result = result + TranLock;
}
return result;
}
private string ToCountSqlString()

View File

@ -43,6 +43,10 @@ namespace SqlSugar
result = this.Context.SqlQueryable<object>(result).Skip(Skip??0).Take(Take??0).ToSql().Key;
}
if (TranLock != null)
{
result = result + TranLock;
}
return result;
}

View File

@ -66,6 +66,10 @@ namespace SqlSugar
{
return "-- No table";
}
if (TranLock != null)
{
result = result + TranLock;
}
return result;
}

View File

@ -105,6 +105,7 @@
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Entities\SugarAbMapping.cs" />
<Compile Include="Entities\JoinMapper.cs" />
<Compile Include="Enum\DbLockType.cs" />
<Compile Include="Enum\NavigatType.cs" />
<Compile Include="Enum\SugarActionType.cs" />
<Compile Include="Entities\SugarConnection.cs" />