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 { internal SqlSugarProvider conn; internal string initThreadMainId; internal string initkey = null; StackFrame[] frames; public SqlSugarScopeProvider(SqlSugarProvider conn) { this.conn = conn; this.initThreadMainId = GetCurrentThreadId(); var key = GetKey(); this.initkey = key; this.GetContext(true); } private static string GetCurrentThreadId() { return System.Threading.Thread.CurrentThread.ManagedThreadId + ""; } public SqlSugarProvider ScopedContext { get { return GetContext(); } } private SqlSugarProvider GetAsyncContext(bool isInit=false) { if (isInit) { CallContextAsync.SetData(GetKey(), this.conn); isInit = false; return conn; } else { SqlSugarProvider result = CallContextAsync.GetData(GetKey()); if (result == null) { SqlSugarProvider db=new SqlSugarProvider(this.conn.CurrentConnectionConfig); CallContextAsync.SetData(GetKey(), db); return db; } else { return result; } } } private SqlSugarProvider GetThreadContext(bool isInit = false) { if (isInit) { CallContextThread.SetData(GetKey(), this.conn); isInit = false; return conn; } else { SqlSugarProvider result = CallContextThread.GetData(GetKey()); if (result == null) { SqlSugarProvider db = new SqlSugarProvider(this.conn.CurrentConnectionConfig); CallContextThread.SetData(GetKey(), db); return db; } else { return result; } } } protected virtual SqlSugarProvider GetContext(bool isInit = false) { SqlSugarProvider result = null; var key = GetKey(); ; StackTrace st = new StackTrace(true); var methods = st.GetFrames(); var isAsync = UtilMethods.IsAnyAsyncMethod(methods); if (isAsync) { result= GetAsyncContext(isInit); } else { result= GetThreadContext(isInit); } return result; } private string GetKey() { if (!string.IsNullOrEmpty(this.initkey) &&this.initThreadMainId == GetCurrentThreadId()) { return this.initkey; } var key= "SqlSugarProviderScope_" + conn.CurrentConnectionConfig.ConfigId; if (frames == null) { frames = new StackTrace(true).GetFrames(); } if (frames.Length >= 0) { foreach (var method in frames.Take(35)) { var refType = method.GetMethod()?.ReflectedType; if (refType != null) { var getInterfaces = refType.Name.StartsWith("<") ? refType?.ReflectedType?.GetInterfaces() : refType?.GetInterfaces(); if (getInterfaces != null && getInterfaces.Any(it => it.Name.IsIn("IJob"))) { key = $"{key}IJob"; break; } } } } return key; } #region API public Task AsyncLock(int timeOutSeconds = 30) { return ScopedContext.AsyncLock(timeOutSeconds); } 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 DeleteMethodInfo DeleteableByObject(object singleEntityObjectOrListObject) { return ScopedContext.DeleteableByObject(singleEntityObjectOrListObject); } 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 T CreateContext(bool isTran) where T : SugarUnitOfWork, new() { Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); return null; } public SugarUnitOfWork CreateContext(bool isTran = true) { Check.ExceptionEasy(" var childDb=Db.GetConnection(configId); use Db.CreateContext ", " 例如 var childDb=Db.GetConnection(configId);其中Db才能使用CreateContext,childDb不能使用"); return null; } public SimpleClient GetSimpleClient() where T : class, new() { return ScopedContext.GetSimpleClient(); } public RepositoryType GetRepository() where RepositoryType : ISugarRepository, new() { return ScopedContext.GetRepository(); } public void InitMappingInfo(Type type) { ScopedContext.InitMappingInfo(type); } public void InitMappingInfo() { ScopedContext.InitMappingInfo(); } public IInsertable> InsertableByDynamic(object insertDynamicObject) { return ScopedContext.InsertableByDynamic(insertDynamicObject); } public InsertMethodInfo InsertableByObject(object singleEntityObjectOrListObject) { return ScopedContext.InsertableByObject(singleEntityObjectOrListObject); } 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 SlaveQueryable() { return ScopedContext.SlaveQueryable(); } public ISugarQueryable MasterQueryable() { return ScopedContext.MasterQueryable(); } 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) { return ScopedContext.Queryable(queryable); } public ISugarQueryable Queryable(ISugarQueryable queryable, string shortName) { return ScopedContext.Queryable(queryable, shortName); } 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(T[] dataList) where T : class, new() { return ScopedContext.Storageable(dataList); } public StorageableDataTable Storageable(List> dictionaryList, string tableName) { return ScopedContext.Storageable(dictionaryList, tableName); } public StorageableDataTable Storageable(Dictionary dictionary, string tableName) { return ScopedContext.Storageable(dictionary, tableName); } public IStorageable Storageable(List dataList) where T : class, new() { return ScopedContext.Storageable(dataList); } public IStorageable Storageable(IList dataList) where T : class, new() { return ScopedContext.Storageable(dataList?.ToList()); } public IStorageable Storageable(T data) where T : class, new() { return ScopedContext.Storageable(data); } public StorageableDataTable Storageable(DataTable data) { return ScopedContext.Storageable(data); } public StorageableMethodInfo StorageableByObject(object singleEntityObjectOrListObject) { return ScopedContext.StorageableByObject(singleEntityObjectOrListObject); } public ISugarQueryable Union(List> queryables) where T : class { return ScopedContext.Union(queryables); } public ISugarQueryable Union(params ISugarQueryable[] queryables) where T : class { return ScopedContext.Union(queryables); } public ISugarQueryable UnionAll(List> queryables) where T : class { return ScopedContext.UnionAll(queryables); } public ISugarQueryable UnionAll(params ISugarQueryable[] queryables) where T : class { return ScopedContext.UnionAll(queryables); } public UpdateExpressionMethodInfo UpdateableByObject(Type entityType) { return ScopedContext.UpdateableByObject(entityType); } public UpdateMethodInfo UpdateableByObject(object singleEntityObjectOrListObject) { return ScopedContext.UpdateableByObject(singleEntityObjectOrListObject); } public IUpdateable> UpdateableByDynamic(object updateDynamicObject) { return ScopedContext.UpdateableByDynamic(updateDynamicObject); } 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 SplitTableContext SplitHelper(Type entityType) { return ScopedContext.SplitHelper(entityType); } 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); } public InsertNavTaskInit InsertNav(T data) where T : class, new() { return ScopedContext.InsertNav(data); } public InsertNavTaskInit InsertNav(List datas) where T : class, new() { return ScopedContext.InsertNav(datas); } public InsertNavTaskInit InsertNav(T data,InsertNavRootOptions rootOptions) where T : class, new() { return ScopedContext.InsertNav(data,rootOptions); } public InsertNavTaskInit InsertNav(List datas, InsertNavRootOptions rootOptions) where T : class, new() { return ScopedContext.InsertNav(datas,rootOptions); } public DeleteNavTaskInit DeleteNav(T data) where T : class, new() { return ScopedContext.DeleteNav(data); } public DeleteNavTaskInit DeleteNav(List datas) where T : class, new() { return ScopedContext.DeleteNav(datas); } public DeleteNavTaskInit DeleteNav(Expression> whereExpression) where T : class, new() { return ScopedContext.DeleteNav(whereExpression); } public DeleteNavTaskInit DeleteNav(T data, DeleteNavRootOptions options) where T : class, new() { return ScopedContext.DeleteNav(data, options); } public DeleteNavTaskInit DeleteNav(List datas, DeleteNavRootOptions options) where T : class, new() { return ScopedContext.DeleteNav(datas, options); } public DeleteNavTaskInit DeleteNav(Expression> whereExpression, DeleteNavRootOptions options) where T : class, new() { return ScopedContext.DeleteNav(whereExpression, options); } public UpdateNavTaskInit UpdateNav(T data) where T : class, new() { return ScopedContext.UpdateNav(data); } public UpdateNavTaskInit UpdateNav(List datas) where T : class, new() { return ScopedContext.UpdateNav(datas); } public UpdateNavTaskInit UpdateNav(List datas, UpdateNavRootOptions rootOptions) where T : class, new() { return this.ScopedContext.UpdateNav(datas, rootOptions); } public UpdateNavTaskInit UpdateNav(T data, UpdateNavRootOptions rootOptions) where T : class, new() { return this.ScopedContext.UpdateNav(data, rootOptions); } public SqlSugarClient CopyNew() { var result= new SqlSugarClient(UtilMethods.CopyConfig(this.Ado.Context.CurrentConnectionConfig)); result.QueryFilter = this.QueryFilter; return result; } public DynamicBuilder DynamicBuilder() { return ScopedContext.DynamicBuilder(); } public void Tracking(T data) where T : class, new() { ScopedContext.Tracking(data); } public void Tracking(List datas) where T : class, new() { ScopedContext.Tracking(datas); } public QueryMethodInfo QueryableByObject(Type entityType) { return ScopedContext.QueryableByObject(entityType); } public QueryMethodInfo QueryableByObject(Type entityType, string shortName) { return ScopedContext.QueryableByObject(entityType, shortName); } public GridSaveProvider GridSave(List oldList, List saveList) where T : class, new() { return ScopedContext.GridSave(oldList, saveList); } public GridSaveProvider GridSave(List saveList) where T : class, new() { return ScopedContext.GridSave(saveList); } public void ClearTracking() { ScopedContext.ClearTracking(); } #endregion } }