Update tenant

This commit is contained in:
skx
2021-01-07 19:53:50 +08:00
parent 1c4838167b
commit d3a41104b7
3 changed files with 56 additions and 5 deletions

View File

@@ -11,13 +11,15 @@ namespace SqlSugar
void BeginTran(); void BeginTran();
void CommitTran(); void CommitTran();
void RollbackTran(); void RollbackTran();
void ChangeDatabase(string configId); void ChangeDatabase(dynamic configId);
void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression); void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression);
DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null); DbResult<bool> UseTran(Action action, Action<Exception> errorCallBack = null);
Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null); Task<DbResult<bool>> UseTranAsync(Action action, Action<Exception> errorCallBack = null);
DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null); DbResult<T> UseTran<T>(Func<T> action, Action<Exception> errorCallBack = null);
Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null); Task<DbResult<T>> UseTranAsync<T>(Func<T> action, Action<Exception> errorCallBack = null);
void AddConnection(ConnectionConfig connection);
SqlSugarProvider GetConnection(dynamic configId);
void Close(); void Close();
void Open(); void Open();

View File

@@ -189,7 +189,7 @@ namespace SqlSugar
{ {
get get
{ {
return "CREATE {3} INDEX Index_{0}_{2} ON {0}({1})"; return "alter table {0} rename to {1}";
} }
} }
@@ -197,7 +197,7 @@ namespace SqlSugar
{ {
get get
{ {
return "SELECT count(*) FROM sqlite_master WHERE name = '{0}'"; return "CREATE {3} INDEX Index_{0}_{2} ON {0}({1})";
} }
} }
protected override string AddDefaultValueSql protected override string AddDefaultValueSql

View File

@@ -543,7 +543,44 @@ namespace SqlSugar
#endregion #endregion
#region TenantManager #region TenantManager
public void ChangeDatabase(string configId) public void AddConnection(ConnectionConfig connection)
{
Check.ArgumentNullException(connection, "AddConnection.connection can't be null");
InitTenant();
var db = this._AllClients.FirstOrDefault(it => it.ConnectionConfig.ConfigId == connection.ConfigId);
if (db == null)
{
if (this._AllClients == null)
{
this._AllClients = new List<SugarTenant>();
}
var provider = new SqlSugarProvider(connection);
if (connection.AopEvents != null)
{
provider.Ado.IsEnableLogEvent = true;
}
this._AllClients.Add(new SugarTenant()
{
ConnectionConfig = connection,
Context = provider
});
}
}
public SqlSugarProvider GetConnection(dynamic configId)
{
InitTenant();
var db = this._AllClients.FirstOrDefault(it => it.ConnectionConfig.ConfigId == configId);
if (db == null)
{
Check.Exception(true, "ConfigId was not found {0}", configId);
}
if (db.Context == null)
{
db.Context = new SqlSugarProvider(db.ConnectionConfig);
}
return db.Context;
}
public void ChangeDatabase(dynamic configId)
{ {
var isLog = _Context.Ado.IsEnableLogEvent; var isLog = _Context.Ado.IsEnableLogEvent;
Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId); Check.Exception(!_AllClients.Any(it => it.ConnectionConfig.ConfigId == configId), "ConfigId was not found {0}", configId);
@@ -573,7 +610,7 @@ namespace SqlSugar
public void BeginTran() public void BeginTran()
{ {
_IsAllTran = true; _IsAllTran = true;
this.Context.Ado.BeginTran(); AllClientEach(it => it.Ado.BeginTran());
} }
public void CommitTran() public void CommitTran()
{ {
@@ -749,6 +786,18 @@ namespace SqlSugar
} }
} }
} }
private void InitTenant()
{
if (this._AllClients == null)
{
this._AllClients = new List<SugarTenant>();
this._AllClients.Add(new SugarTenant()
{
ConnectionConfig = this.CurrentConnectionConfig,
Context = this.Context
});
}
}
private SqlSugarProvider Synchronization() private SqlSugarProvider Synchronization()
{ {