mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-18 17:48:11 +08:00
Update SqlSugar
This commit is contained in:
@@ -9,6 +9,10 @@ namespace SqlSugar
|
||||
{
|
||||
public class ConnectionConfig
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string ConfigId { get; set; }
|
||||
/// <summary>
|
||||
///DbType.SqlServer Or Other
|
||||
/// </summary>
|
||||
|
13
Src/Asp.Net/SqlSugar/Entities/SugarTerant.cs
Normal file
13
Src/Asp.Net/SqlSugar/Entities/SugarTerant.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SqlSugar
|
||||
{
|
||||
public class SugarTerant
|
||||
{
|
||||
public ISqlSugarClient Context { get; set; }
|
||||
public ConnectionConfig ConnectionConfig { get; set; }
|
||||
}
|
||||
}
|
@@ -77,6 +77,7 @@
|
||||
<Compile Include="CacheScheme\CacheKeyBuider.cs" />
|
||||
<Compile Include="CacheScheme\CacheSchemeMain.cs" />
|
||||
<Compile Include="Entities\CacheKey.cs" />
|
||||
<Compile Include="Entities\SugarTerant.cs" />
|
||||
<Compile Include="Entities\ConditionalModel.cs" />
|
||||
<Compile Include="Entities\ConnMoreSettings.cs" />
|
||||
<Compile Include="Entities\DiffLogModel.cs" />
|
||||
|
@@ -4,6 +4,7 @@ using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SqlSugar
|
||||
@@ -11,20 +12,40 @@ namespace SqlSugar
|
||||
public class SqlSugarClient : ISqlSugarClient
|
||||
{
|
||||
private ISqlSugarClient _Context = null;
|
||||
private string ThreadId;
|
||||
private ConnectionConfig _CurrentConnectionConfig;
|
||||
private List<SugarTerant> _allClients;
|
||||
|
||||
public ISqlSugarClient Context { get => _Context; set => _Context = value; }
|
||||
public ISqlSugarClient Context { get => GetContext(); set => _Context = value; }
|
||||
|
||||
public SqlSugarClient(ConnectionConfig config)
|
||||
{
|
||||
_Context = new SqlSugarContext(config);
|
||||
Init();
|
||||
Check.Exception(config == null, "ConnectionConfig config is null");
|
||||
InitContext(config);
|
||||
}
|
||||
|
||||
//public SqlSugarClient(List<ConnectionConfig> config)
|
||||
//{
|
||||
public SqlSugarClient(List<ConnectionConfig> configs)
|
||||
{
|
||||
Check.Exception(configs.IsNullOrEmpty(), "List<ConnectionConfig> 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));
|
||||
}
|
||||
public void ChangeDatabase(Func<ConnectionConfig, bool> changeExpression)
|
||||
{
|
||||
var allConfigs = _allClients.Select(it => it.ConnectionConfig);
|
||||
Check.Exception(!allConfigs.Any(changeExpression), "changeExpression was not found {0}", changeExpression.ToString());
|
||||
InitContext(allConfigs.First(changeExpression));
|
||||
}
|
||||
|
||||
public IAdo Ado => this.Context.Ado;
|
||||
|
||||
@@ -33,7 +54,7 @@ namespace SqlSugar
|
||||
public ICodeFirst CodeFirst => this.Context.CodeFirst;
|
||||
|
||||
public Guid ContextID { get => this.Context.ContextID; set => this.Context.ContextID = value; }
|
||||
public ConnectionConfig CurrentConnectionConfig { get => this.Context.CurrentConnectionConfig; set => this.Context.CurrentConnectionConfig=value; }
|
||||
public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; }
|
||||
|
||||
public IDbFirst DbFirst => this.Context.DbFirst;
|
||||
|
||||
@@ -522,8 +543,46 @@ namespace SqlSugar
|
||||
{
|
||||
return this.Context.Updateable<T>(UpdateObjs);
|
||||
}
|
||||
private void Init()
|
||||
|
||||
|
||||
private ISqlSugarClient GetContext()
|
||||
{
|
||||
if (CurrentConnectionConfig.IsShardSameThread)
|
||||
{
|
||||
var result = _Context;
|
||||
if (CallContext.ContextList.Value.IsNullOrEmpty())
|
||||
{
|
||||
CallContext.ContextList.Value = new List<ISqlSugarClient>();
|
||||
CallContext.ContextList.Value.Add(_Context);
|
||||
}
|
||||
else
|
||||
{
|
||||
var cacheContext = CallContext.ContextList.Value.FirstOrDefault(it =>
|
||||
it.CurrentConnectionConfig.ConnectionString == _Context.CurrentConnectionConfig.ConnectionString &&
|
||||
it.CurrentConnectionConfig.DbType == _Context.CurrentConnectionConfig.DbType &&
|
||||
it.CurrentConnectionConfig.IsAutoCloseConnection == _Context.CurrentConnectionConfig.IsAutoCloseConnection &&
|
||||
it.CurrentConnectionConfig.IsShardSameThread == _Context.CurrentConnectionConfig.IsShardSameThread);
|
||||
if (cacheContext != null)
|
||||
{
|
||||
return cacheContext;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else if (ThreadId == Thread.CurrentThread.ManagedThreadId.ToString())
|
||||
{
|
||||
return _Context;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new SqlSugarClient(this.CurrentConnectionConfig);
|
||||
}
|
||||
}
|
||||
private void InitContext(ConnectionConfig config)
|
||||
{
|
||||
_Context = new SqlSugarContext(config);
|
||||
this.CurrentConnectionConfig = config;
|
||||
ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
||||
if (this.MappingTables == null)
|
||||
{
|
||||
this.MappingTables = new MappingTableList();
|
||||
@@ -545,5 +604,24 @@ namespace SqlSugar
|
||||
this.Queues = new QueueList();
|
||||
}
|
||||
}
|
||||
private void InitConfigs(List<ConnectionConfig> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -10,6 +10,6 @@ namespace SqlSugar
|
||||
{
|
||||
internal class CallContext
|
||||
{
|
||||
public static ThreadLocal<List<SqlSugarContext>> ContextList = new ThreadLocal<List<SqlSugarContext>>();
|
||||
public static ThreadLocal<List<ISqlSugarClient>> ContextList = new ThreadLocal<List<ISqlSugarClient>>();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user