From 436855a8d96fd3dee40771d285c51842cf177d03 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 27 Apr 2022 17:27:29 +0800 Subject: [PATCH] Add Db.GetConnectionScope --- .../SugarProvider/SqlSugarScopeProvider.cs | 640 ++++++++++++++++++ Src/Asp.Net/SqlSugar/Interface/ITenant.cs | 2 + Src/Asp.Net/SqlSugar/SqlSugar.csproj | 1 + Src/Asp.Net/SqlSugar/SqlSugarClient.cs | 14 + Src/Asp.Net/SqlSugar/SqlSugarScope.cs | 9 +- 5 files changed, 665 insertions(+), 1 deletion(-) create mode 100644 Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs new file mode 100644 index 000000000..2d9c3a482 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarScopeProvider.cs @@ -0,0 +1,640 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Diagnostics; +using System.Dynamic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class SqlSugarScopeProvider:ISqlSugarClient + { + private SqlSugarProvider conn; + + public SqlSugarScopeProvider(SqlSugarProvider conn) + { + this.conn = conn; + var key = GetKey(); + CallContextAsync.SetData(key, conn); + } + public SqlSugarProvider ScopedContext { get { return GetContext(); } } + private SqlSugarProvider GetAsyncContext(string key) + { + SqlSugarProvider result = CallContextAsync.GetData(key); + if (result == null) + { + CallContextAsync.SetData(key, new SqlSugarProvider(conn.CurrentConnectionConfig)); + result = CallContextAsync.GetData(key); + } + + return result; + } + private SqlSugarProvider GetThreadContext(string key) + { + SqlSugarProvider result = CallContextThread.GetData(key); + if (result == null) + { + CallContextThread.SetData(key, new SqlSugarProvider (this.CurrentConnectionConfig)); + result = CallContextThread.GetData(key); + } + return result; + } + private SqlSugarProvider GetContext() + { + SqlSugarProvider result = null; + var key = GetKey(); ; + StackTrace st = new StackTrace(true); + var methods = st.GetFrames(); + var isAsync = UtilMethods.IsAnyAsyncMethod(methods); + if (isAsync) + { + result= GetAsyncContext(key); + } + else + { + result= GetThreadContext(key); + } + return result; + } + private dynamic GetKey() + { + return "SqlSugarProviderScope_" + conn.CurrentConnectionConfig.ConfigId + conn.GetHashCode(); + } + + #region API + + public SugarActionType SugarActionType { get => ScopedContext.SugarActionType; set => ScopedContext.SugarActionType = value; } + public MappingTableList MappingTables { get => ScopedContext.MappingTables; set => ScopedContext.MappingTables = value; } + public MappingColumnList MappingColumns { get => ScopedContext.MappingColumns; set => ScopedContext.MappingColumns = value; } + public IgnoreColumnList IgnoreColumns { get => ScopedContext.IgnoreColumns; set => ScopedContext.IgnoreColumns = value; } + public IgnoreColumnList IgnoreInsertColumns { get => ScopedContext.IgnoreInsertColumns; set => ScopedContext.IgnoreInsertColumns = value; } + public Dictionary TempItems { get => ScopedContext.TempItems; set => ScopedContext.TempItems = value; } + public ConfigQuery ConfigQuery { get => ScopedContext.ConfigQuery; set => ScopedContext.ConfigQuery = value; } + + public bool IsSystemTablesConfig => ScopedContext.IsSystemTablesConfig; + + public Guid ContextID { get => ScopedContext.ContextID; set => ScopedContext.ContextID = value; } + public ConnectionConfig CurrentConnectionConfig { get => ScopedContext.CurrentConnectionConfig; set => ScopedContext.CurrentConnectionConfig = value; } + + public IAdo Ado => ScopedContext.Ado; + + public AopProvider Aop => ScopedContext.Aop; + + public ICodeFirst CodeFirst => ScopedContext.CodeFirst; + + public IDbFirst DbFirst => ScopedContext.DbFirst; + + public IDbMaintenance DbMaintenance => ScopedContext.DbMaintenance; + + public EntityMaintenance EntityMaintenance { get => ScopedContext.EntityMaintenance; set => ScopedContext.EntityMaintenance = value; } + public QueryFilterProvider QueryFilter { get => ScopedContext.QueryFilter; set => ScopedContext.QueryFilter = value; } + public IContextMethods Utilities { get => ScopedContext.Utilities; set => ScopedContext.Utilities = value; } + public QueueList Queues { get => ScopedContext.Queues; set => ScopedContext.Queues = value; } + + public SugarCacheProvider DataCache => ScopedContext.DataCache; + + + public void AddQueue(string sql, object parsmeters = null) + { + ScopedContext.AddQueue(sql, parsmeters); + } + + public void AddQueue(string sql, List parsmeters) + { + ScopedContext.AddQueue(sql, parsmeters); + } + + public void AddQueue(string sql, SugarParameter parsmeter) + { + ScopedContext.AddQueue(sql, parsmeter); + } + + + public void Close() + { + ScopedContext.Close(); + } + public IDeleteable Deleteable() where T : class, new() + { + return ScopedContext.Deleteable(); + } + + public IDeleteable Deleteable(dynamic primaryKeyValue) where T : class, new() + { + return ScopedContext.Deleteable(primaryKeyValue); + } + + public IDeleteable Deleteable(dynamic[] primaryKeyValues) where T : class, new() + { + return ScopedContext.Deleteable(primaryKeyValues); + } + + public IDeleteable Deleteable(Expression> expression) where T : class, new() + { + return ScopedContext.Deleteable(expression); + } + + public IDeleteable Deleteable(List pkValue) where T : class, new() + { + return ScopedContext.Deleteable(pkValue); + } + + public IDeleteable Deleteable(List deleteObjs) where T : class, new() + { + return ScopedContext.Deleteable(deleteObjs); + } + + public IDeleteable Deleteable(T deleteObj) where T : class, new() + { + return ScopedContext.Deleteable(deleteObj); + } + + public void Dispose() + { + ScopedContext.Dispose(); + } + + public DateTime GetDate() + { + return ScopedContext.GetDate(); + } + + public SimpleClient GetSimpleClient() where T : class, new() + { + return ScopedContext.GetSimpleClient(); + } + + public void InitMappingInfo(Type type) + { + ScopedContext.InitMappingInfo(type); + } + + public void InitMappingInfo() + { + ScopedContext.InitMappingInfo(); + } + + public IInsertable Insertable(Dictionary columnDictionary) where T : class, new() + { + return ScopedContext.Insertable(columnDictionary); + } + + public IInsertable Insertable(dynamic insertDynamicObject) where T : class, new() + { + return ScopedContext.Insertable((object)insertDynamicObject); + } + + public IInsertable Insertable(List insertObjs) where T : class, new() + { + return ScopedContext.Insertable(insertObjs); + } + + public IInsertable Insertable(T insertObj) where T : class, new() + { + return ScopedContext.Insertable(insertObj); + } + + public IInsertable Insertable(T[] insertObjs) where T : class, new() + { + return ScopedContext.Insertable(insertObjs); + } + + public void Open() + { + ScopedContext.Open(); + } + + public ISugarQueryable Queryable(string tableName, string shortName) + { + return ScopedContext.Queryable(tableName, shortName); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(Expression> joinExpression) + { + return ScopedContext.Queryable(joinExpression); + } + + public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, Expression> joinExpression) + where T : class, new() + where T2 : class, new() + { + return ScopedContext.Queryable(joinQueryable1, joinQueryable2, joinExpression); + } + + public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, JoinType joinType, Expression> joinExpression) + where T : class, new() + where T2 : class, new() + { + return ScopedContext.Queryable(joinQueryable1, joinQueryable2, joinType, joinExpression); + } + + public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, ISugarQueryable joinQueryable3, JoinType joinType1, Expression> joinExpression1, JoinType joinType2, Expression> joinExpression2) + where T : class, new() + where T2 : class, new() + where T3 : class, new() + { + return ScopedContext.Queryable(joinQueryable1, joinQueryable2, joinQueryable3, joinType1, joinExpression1, joinType2, joinExpression2); + } + public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, ISugarQueryable joinQueryable3, ISugarQueryable joinQueryable4, JoinType joinType1, Expression> joinExpression1, JoinType joinType2, Expression> joinExpression2, JoinType joinType3, Expression> joinExpression3) + where T : class, new() + where T2 : class, new() + where T3 : class, new() + where T4 : class, new() + { + return ScopedContext.Queryable(joinQueryable1, joinQueryable2, joinQueryable3, joinQueryable4, joinType1, joinExpression1, joinType2, joinExpression2, joinType3, joinExpression3); + } + public ISugarQueryable Queryable() + { + return ScopedContext.Queryable(); + } + + public ISugarQueryable Queryable(ISugarQueryable queryable) where T : class, new() + { + return ScopedContext.Queryable(queryable); + } + + public ISugarQueryable Queryable(string shortName) + { + return ScopedContext.Queryable(shortName); + } + + public IReportable Reportable(T data) + { + return ScopedContext.Reportable(data); + } + + public IReportable Reportable(List list) + { + return ScopedContext.Reportable(list); + } + + public IReportable Reportable(T[] array) + { + return ScopedContext.Reportable(array); + } + + public int SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List, List, List, List, List, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List, List, List, List, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List, List, List, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List, List, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Tuple, List> SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public List SaveQueues(bool isTran = true) + { + return ScopedContext.SaveQueues(isTran); + } + + public Task SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List, List, List, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List, List, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task, List>> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public Task> SaveQueuesAsync(bool isTran = true) + { + return ScopedContext.SaveQueuesAsync(isTran); + } + + public ISugarQueryable SqlQueryable(string sql) where T : class, new() + { + return ScopedContext.SqlQueryable(sql); + } + + public IStorageable Storageable(List dataList) where T : class, new() + { + return ScopedContext.Storageable(dataList); + } + + public IStorageable Storageable(T data) where T : class, new() + { + return ScopedContext.Storageable(data); + } + public StorageableDataTable Storageable(DataTable data) + { + return ScopedContext.Storageable(data); + } + + public ISugarQueryable Union(List> queryables) where T : class, new() + { + return ScopedContext.Union(queryables); + } + + public ISugarQueryable Union(params ISugarQueryable[] queryables) where T : class, new() + { + return ScopedContext.Union(queryables); + } + + public ISugarQueryable UnionAll(List> queryables) where T : class, new() + { + return ScopedContext.UnionAll(queryables); + } + + public ISugarQueryable UnionAll(params ISugarQueryable[] queryables) where T : class, new() + { + return ScopedContext.UnionAll(queryables); + } + + public IUpdateable Updateable() where T : class, new() + { + return ScopedContext.Updateable(); + } + + public IUpdateable Updateable(Dictionary columnDictionary) where T : class, new() + { + return ScopedContext.Updateable(columnDictionary); + } + + public IUpdateable Updateable(dynamic updateDynamicObject) where T : class, new() + { + return ScopedContext.Updateable((object)updateDynamicObject); + } + + public IUpdateable Updateable(Expression> columns) where T : class, new() + { + return ScopedContext.Updateable(columns); + } + + public IUpdateable Updateable(Expression> columns) where T : class, new() + { + return ScopedContext.Updateable(columns); + } + + public IUpdateable Updateable(List UpdateObjs) where T : class, new() + { + return ScopedContext.Updateable(UpdateObjs); + } + + public IUpdateable Updateable(T UpdateObj) where T : class, new() + { + return ScopedContext.Updateable(UpdateObj); + } + + public IUpdateable Updateable(T[] UpdateObjs) where T : class, new() + { + return ScopedContext.Updateable(UpdateObjs); + } + public SplitTableContext SplitHelper() where T : class, new() + { + return ScopedContext.SplitHelper(); + } + public SplitTableContextResult SplitHelper(T data) where T : class, new() + { + return ScopedContext.SplitHelper(data); + } + public SplitTableContextResult SplitHelper(List dataList) where T : class, new() + { + return ScopedContext.SplitHelper(dataList); + } + public IFastest Fastest() where T : class, new() + { + return ScopedContext.Fastest(); + } + + public void ThenMapper(IEnumerable list, Action action) + { + ScopedContext.ThenMapper(list, action); + } + + public Task ThenMapperAsync(IEnumerable list, Func action) + { + return ScopedContext.ThenMapperAsync(list, action); + } + + public ITenant AsTenant() + { + return ScopedContext.AsTenant(); + } + + public ISaveable Saveable(List saveObjects) where T : class, new() + { + return ScopedContext.Saveable(saveObjects); + } + + public ISaveable Saveable(T saveObject) where T : class, new() + { + return ScopedContext.Saveable(saveObject); + } + #endregion + } +} \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar/Interface/ITenant.cs b/Src/Asp.Net/SqlSugar/Interface/ITenant.cs index 33c13967b..84ed87555 100644 --- a/Src/Asp.Net/SqlSugar/Interface/ITenant.cs +++ b/Src/Asp.Net/SqlSugar/Interface/ITenant.cs @@ -21,7 +21,9 @@ namespace SqlSugar Task> UseTranAsync(Func> action, Action errorCallBack = null); void AddConnection(ConnectionConfig connection); SqlSugarProvider GetConnection(dynamic configId); + SqlSugarScopeProvider GetConnectionScope(dynamic configId); SqlSugarProvider GetConnectionWithAttr(); + SqlSugarScopeProvider GetConnectionScopeWithAttr(); bool IsAnyConnection(dynamic configId); void Close(); diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index 0344e44da..f6bb27396 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -254,6 +254,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs index b0e3b1371..400f69612 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarClient.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarClient.cs @@ -656,6 +656,14 @@ namespace SqlSugar var configId = attr.configId; return this.GetConnection(configId); } + public SqlSugarScopeProvider GetConnectionScopeWithAttr() + { + var attr = typeof(T).GetCustomAttribute(); + if (attr == null) + return this.GetConnection(this.CurrentConnectionConfig.ConfigId); + var configId = attr.configId; + return this.GetConnectionScope(configId); + } public SqlSugarProvider GetConnection(dynamic configId) { InitTenant(); @@ -679,6 +687,12 @@ namespace SqlSugar } return db.Context; } + + public SqlSugarScopeProvider GetConnectionScope(dynamic configId) + { + var conn = GetConnection(configId); + return new SqlSugarScopeProvider(conn); + } public bool IsAnyConnection(dynamic configId) { InitTenant(); diff --git a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs index 0da46c31d..679e6b37e 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugarScope.cs +++ b/Src/Asp.Net/SqlSugar/SqlSugarScope.cs @@ -158,11 +158,18 @@ namespace SqlSugar { return ScopedContext.GetConnection(configId); } + public SqlSugarScopeProvider GetConnectionScope(dynamic configId) + { + return ScopedContext.GetConnectionScope(configId); + } public SqlSugarProvider GetConnectionWithAttr() { return ScopedContext.GetConnectionWithAttr(); } - + public SqlSugarScopeProvider GetConnectionScopeWithAttr() + { + return ScopedContext.GetConnectionScopeWithAttr(); + } public DateTime GetDate() { return ScopedContext.GetDate();