using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading; using System.Threading.Tasks; namespace SqlSugar { public class SqlSugarClient : ISqlSugarClient { #region Gobal Property private ISqlSugarClient _Context = null; private string _ThreadId; private ConnectionConfig _CurrentConnectionConfig; private List _AllClients; private bool _IsAllTran = false; private MappingTableList _MappingTables; private MappingColumnList _MappingColumns; private IgnoreColumnList _IgnoreColumns; private IgnoreColumnList _IgnoreInsertColumns; #endregion #region Api public ISqlSugarClient Context { get => GetContext(); set => _Context = value; } public SqlSugarClient(ConnectionConfig config) { Check.Exception(config == null, "ConnectionConfig config is null"); InitContext(config); } public SqlSugarClient(List configs) { Check.Exception(configs.IsNullOrEmpty(), "List configs is null"); InitConfigs(configs); var config = configs.First(); InitContext(config); _AllClients = configs.Select(it => new SugarTerant() { ConnectionConfig = it }).ToList(); ; _AllClients.First(it => it.ConnectionConfig.ConfigId == config.ConfigId).Context = this.Context; } public void ChangeDatabase(string configId) { Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId); InitTerant(_AllClients.First(it => it.ConnectionConfig.ConfigId == configId)); if (this._IsAllTran) this.Ado.BeginTran(); } public void ChangeDatabase(Func changeExpression) { var allConfigs = _AllClients.Select(it => it.ConnectionConfig); Check.Exception(!allConfigs.Any(changeExpression), "changeExpression was not found {0}", changeExpression.ToString()); InitTerant(_AllClients.First(it=>it.ConnectionConfig==allConfigs.First(changeExpression))); if (this._IsAllTran) this.Ado.BeginTran(); } public MappingTableList MappingTables { get => _MappingTables; set => _MappingTables = value; } public MappingColumnList MappingColumns { get => _MappingColumns; set => _MappingColumns = value; } public IgnoreColumnList IgnoreColumns { get => _IgnoreColumns; set => _IgnoreColumns = value; } public IgnoreColumnList IgnoreInsertColumns { get => _IgnoreInsertColumns; set => _IgnoreInsertColumns = value; } public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; } public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; } public Dictionary TempItems { get => this.Context.TempItems; set => this.Context.TempItems = value; } public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; } public IAdo Ado => this.Context.Ado; public AopProvider Aop => this.Context.Aop; public ICodeFirst CodeFirst => this.Context.CodeFirst; public Guid ContextID { get => this.Context.ContextID; set => this.Context.ContextID = value; } public IDbFirst DbFirst => this.Context.DbFirst; public IDbMaintenance DbMaintenance => this.Context.DbMaintenance; public EntityMaintenance EntityMaintenance { get => this.Context.EntityMaintenance; set => this.Context.EntityMaintenance = value; } public bool IsSystemTablesConfig => this.Context.IsSystemTablesConfig; public QueryFilterProvider QueryFilter { get => this.Context.QueryFilter; set => this.Context.QueryFilter = value; } public void AddQueue(string sql, object parsmeters = null) { this.Context.AddQueue(sql, parsmeters); } public void AddQueue(string sql, List parsmeters) { this.Context.AddQueue(sql, parsmeters); } public void AddQueue(string sql, SugarParameter parsmeter) { this.Context.AddQueue(sql, parsmeter); } public void Close() { this.Context.Close(); } public IDeleteable Deleteable() where T : class, new() { return this.Context.Deleteable(); } public IDeleteable Deleteable(dynamic primaryKeyValue) where T : class, new() { return this.Context.Deleteable(primaryKeyValue); } public IDeleteable Deleteable(dynamic[] primaryKeyValues) where T : class, new() { return this.Context.Deleteable(primaryKeyValues); } public IDeleteable Deleteable(Expression> expression) where T : class, new() { return this.Context.Deleteable(expression); } public IDeleteable Deleteable(List pkValue) where T : class, new() { return this.Context.Deleteable(pkValue); } public IDeleteable Deleteable(List deleteObjs) where T : class, new() { return this.Context.Deleteable(deleteObjs); } public IDeleteable Deleteable(T deleteObj) where T : class, new() { return this.Context.Deleteable(deleteObj); } public void Dispose() { this.Context.Dispose(); } public DateTime GetDate() { return this.Context.GetDate(); } public SimpleClient GetSimpleClient() { return this.Context.GetSimpleClient(); } public SimpleClient GetSimpleClient() where T : class, new() { return this.Context.GetSimpleClient(); } public void InitMppingInfo(Type type) { this.Context.InitMppingInfo(type); } public void InitMppingInfo() { this.Context.InitMppingInfo(typeof(T)); } public IInsertable Insertable(Dictionary columnDictionary) where T : class, new() { return this.Context.Insertable(columnDictionary); } public IInsertable Insertable(dynamic insertDynamicObject) where T : class, new() { return this.Context.Insertable(insertDynamicObject); } public IInsertable Insertable(List insertObjs) where T : class, new() { return this.Context.Insertable(insertObjs); } public IInsertable Insertable(T insertObj) where T : class, new() { return this.Context.Insertable(insertObj); } public IInsertable Insertable(T[] insertObjs) where T : class, new() { return this.Context.Insertable(insertObjs); } public void Open() { this.Context.Open(); } public ISugarQueryable Queryable(string tableName, string shortName) { return this.Context.Queryable(tableName, shortName); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) where T : class, new() { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(Expression> joinExpression) { return this.Context.Queryable(joinExpression); } public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, Expression> joinExpression) where T : class, new() where T2 : class, new() { return this.Context.Queryable(joinQueryable1, joinQueryable2, joinExpression); } public ISugarQueryable Queryable(ISugarQueryable joinQueryable1, ISugarQueryable joinQueryable2, JoinType joinType, Expression> joinExpression) where T : class, new() where T2 : class, new() { return this.Context.Queryable(joinQueryable1, joinQueryable2, joinType, joinExpression); } public ISugarQueryable Queryable() { return this.Context.Queryable(); } public ISugarQueryable Queryable(ISugarQueryable queryable) where T : class, new() { return this.Context.Queryable(queryable); } public ISugarQueryable Queryable(string shortName) { return this.Context.Queryable(shortName); } public ISaveable Saveable(List saveObjects) where T : class, new() { return this.Context.Saveable(saveObjects); } public ISaveable Saveable(T saveObject) where T : class, new() { return this.Context.Saveable(saveObject); } public int SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List, List, List, List, List, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List, List, List, List, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List, List, List, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List, List, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Tuple, List> SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public List SaveQueues(bool isTran = true) { return this.Context.SaveQueues(isTran); } public Task SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List, List, List, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List, List, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task, List>> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public Task> SaveQueuesAsync(bool isTran = true) { return this.Context.SaveQueuesAsync(isTran); } public ISugarQueryable SqlQueryable(string sql) where T : class, new() { return this.Context.SqlQueryable(sql); } public ISugarQueryable Union(List> queryables) where T : class, new() { return this.Context.Union(queryables); } public ISugarQueryable Union(params ISugarQueryable[] queryables) where T : class, new() { return this.Context.Union(queryables); } public ISugarQueryable UnionAll(List> queryables) where T : class, new() { return this.Context.UnionAll(queryables); } public ISugarQueryable UnionAll(params ISugarQueryable[] queryables) where T : class, new() { return this.Context.UnionAll(queryables); } public IUpdateable Updateable() where T : class, new() { return this.Context.Updateable(); } public IUpdateable Updateable(Dictionary columnDictionary) where T : class, new() { return this.Context.Updateable(columnDictionary); } public IUpdateable Updateable(dynamic updateDynamicObject) where T : class, new() { return this.Context.Updateable(updateDynamicObject); } public IUpdateable Updateable(Expression> columns) where T : class, new() { return this.Context.Updateable(columns); } public IUpdateable Updateable(Expression> columns) where T : class, new() { return this.Context.Updateable(columns); } public IUpdateable Updateable(List UpdateObjs) where T : class, new() { return this.Context.Updateable(UpdateObjs); } public IUpdateable Updateable(T UpdateObj) where T : class, new() { return this.Context.Updateable(UpdateObj); } public IUpdateable Updateable(T[] UpdateObjs) where T : class, new() { return this.Context.Updateable(UpdateObjs); } public void BeginAllTran() { _IsAllTran = true; this.Context.Ado.BeginTran(); } public void CommitAllTran() { if (_AllClients.HasValue()) { foreach (var item in _AllClients.Where(it => it.Context.HasValue())) { item.Context.Ado.CommitTran(); } } _IsAllTran = false; } public void RollbackAllTran() { if (_AllClients.HasValue()) { foreach (var item in _AllClients.Where(it => it.Context.HasValue())) { item.Context.Ado.RollbackTran(); } } _IsAllTran = false; } #endregion #region Helper private ISqlSugarClient GetContext() { if (CurrentConnectionConfig.IsShardSameThread) { ISqlSugarClient result = _Context; if (CallContext.ContextList.Value.IsNullOrEmpty()) { CallContext.ContextList.Value = new List(); CallContext.ContextList.Value.Add(_Context); } else { ISqlSugarClient cacheContext = GetCallContext(); if (cacheContext != null) { result = cacheContext; } else { result = CopyClient(); CallContext.ContextList.Value.Add(result); } } return result; } else if (_ThreadId == Thread.CurrentThread.ManagedThreadId.ToString()) { _Context.MappingColumns = _MappingColumns; _Context.MappingTables = _MappingTables; _Context.IgnoreColumns = _IgnoreColumns; _Context.IgnoreInsertColumns = _IgnoreInsertColumns; return _Context; } else { if (CallContext.ContextList.Value == null) { CallContext.ContextList.Value = new List(); } if (CallContext.ContextList.Value.IsNullOrEmpty() || GetCallContext() == null) { var context = CopyClient(); CallContext.ContextList.Value.Add(context); return context; } else { return GetCallContext(); } } } private SqlSugarClient CopyClient() { var result = new SqlSugarClient(this.CurrentConnectionConfig); result.MappingColumns = _MappingColumns; result.MappingTables = _MappingTables; result.IgnoreColumns = _IgnoreColumns; result.IgnoreInsertColumns = _IgnoreInsertColumns; return result; } private ISqlSugarClient GetCallContext() { return CallContext.ContextList.Value.FirstOrDefault(it => it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType&& it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString&& it.CurrentConnectionConfig.InitKeyType==_Context.CurrentConnectionConfig.InitKeyType ); } private void InitContext(ConnectionConfig config) { var aopIsNull = config.AopEvents == null; if (aopIsNull) { config.AopEvents = new AopEvents(); } _Context = new SqlSugarContext(config); if (!aopIsNull) _Context.Ado.IsEnableLogEvent = true; this.CurrentConnectionConfig = config; _ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(); if (_MappingColumns == null) this.MappingTables = new MappingTableList(); if (this.MappingColumns == null) this.MappingColumns = new MappingColumnList(); if (this.IgnoreColumns == null) this.IgnoreColumns = new IgnoreColumnList(); if (this.IgnoreInsertColumns == null) this.IgnoreInsertColumns = new IgnoreColumnList(); } private void InitConfigs(List configs) { foreach (var item in configs) { if (item.ConfigId == null) { item.ConfigId = Guid.NewGuid().ToString(); } } } private void InitTerant(SugarTerant terant) { if (terant.Context == null) { terant.Context = new SqlSugarClient(terant.ConnectionConfig); } _Context = terant.Context; this.CurrentConnectionConfig = terant.ConnectionConfig; } #endregion #region Obsolete [Obsolete] public EntityMaintenance EntityProvider { get => this.Context.EntityProvider; set => this.Context.EntityProvider = value; } [Obsolete] public IContextMethods RewritableMethods { get => this.Context.RewritableMethods; set => this.Context.RewritableMethods = value; } [Obsolete] public SimpleClient SimpleClient => this.Context.SimpleClient; #endregion } }