Update SqlSugar

This commit is contained in:
sunkaixuan
2019-05-05 12:44:43 +08:00
parent 707c611e78
commit 579d5e5766
17 changed files with 193 additions and 74 deletions

View File

@@ -69,7 +69,7 @@ namespace OrmTest.Demo
});
//Processing prior to execution of SQL
db.Ado.ProcessingEventStartingSQL = (sql, par) =>
db.ProcessingEventStartingSQL = (sql, par) =>
{
if (sql.Contains("{0}"))
{

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OrmTest.Demos
{
class MultiClient
{
}
}

View File

@@ -66,6 +66,7 @@
<Compile Include="Demos\H_Queue.cs" />
<Compile Include="Demos\I_InsertOrUpdate.cs" />
<Compile Include="Demos\J_Debugger.cs" />
<Compile Include="Demos\K_MultiClient.cs" />
<Compile Include="Models\Brand.cs" />
<Compile Include="BugTest\Bug1.cs" />
<Compile Include="Models\VendorAndBrand.cs" />

View File

@@ -58,7 +58,7 @@ namespace OrmTest.UnitTest
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
db.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars));
Console.WriteLine();

View File

@@ -130,7 +130,7 @@ namespace OrmTest.UnitTest
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
db.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + " " + pars);
};

View File

@@ -30,7 +30,7 @@ namespace OrmTest.UnitTest
using (var db = GetInstance())
{
//db.Database.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
db.LogEventStarting = (sql, pars) =>
{
Console.WriteLine(sql + " " + pars);
};
@@ -82,7 +82,7 @@ namespace OrmTest.UnitTest
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
db.LogEventStarting = (sql, pars) =>
{
base.Check(" SELECT COUNT(1) FROM (SELECT [st].[ID] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) GROUP BY [st].[ID] ) CountTable ",
null, sql, null, "select t4 Error");

View File

@@ -59,12 +59,12 @@ namespace SqlSugar
public virtual CommandType CommandType { get; set; }
public virtual bool IsEnableLogEvent { get; set; }
public virtual bool IsClearParameters { get; set; }
public virtual Action<string, SugarParameter[]> LogEventStarting { get; set; }
public virtual Action<string, SugarParameter[]> LogEventCompleted { get; set; }
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
public virtual Action<string, SugarParameter[]> LogEventStarting=> this.Context.LogEventStarting;
public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.LogEventCompleted;
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.ProcessingEventStartingSQL;
protected virtual Func<string,string> FormatSql { get; set; }
public virtual Action<SqlSugarException> ErrorEvent { get; set; }
public virtual Action<DiffLogModel> DiffLogEvent { get; set; }
public virtual Action<SqlSugarException> ErrorEvent => this.Context.ErrorEvent;
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.DiffLogEvent;
public virtual List<IDbConnection> SlaveConnections { get; set; }
public virtual IDbConnection MasterConnection { get; set; }
#endregion

View File

@@ -14,10 +14,10 @@ namespace SqlSugar
this.Context.Ado.IsEnableLogEvent = true;
}
private SqlSugarContext Context { get; set; }
public Action<DiffLogModel> OnDiffLogEvent { set { this.Context.Ado.DiffLogEvent = value; } }
public Action<SqlSugarException> OnError { set { this.Context.Ado.ErrorEvent = value; } }
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.Ado.LogEventStarting = value; } }
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.Ado.LogEventCompleted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.Ado.ProcessingEventStartingSQL = value; } }
public Action<DiffLogModel> OnDiffLogEvent { set { this.Context.DiffLogEvent = value; } }
public Action<SqlSugarException> OnError { set { this.Context.ErrorEvent = value; } }
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.LogEventStarting = value; } }
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.LogEventCompleted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.ProcessingEventStartingSQL = value; } }
}
}

View File

@@ -410,8 +410,8 @@ namespace SqlSugar
parameters = new List<SugarParameter>();
diffModel.AfterData = null;
diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.Ado.DiffLogEvent != null)
this.Context.Ado.DiffLogEvent(diffModel);
if (this.Context.DiffLogEvent != null)
this.Context.DiffLogEvent(diffModel);
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.RemoveCacheFunc != null) {

View File

@@ -10,7 +10,7 @@ namespace SqlSugar
{
public class InsertableProvider<T> : IInsertable<T> where T : class, new()
{
public ISqlSugarClient Context { get; set; }
public SqlSugarContext Context { get; set; }
public IAdo Ado { get { return Context.Ado; } }
public ISqlBuilder SqlBuilder { get; set; }
public InsertBuilder InsertBuilder { get; set; }
@@ -477,8 +477,8 @@ namespace SqlSugar
parameters = new List<SugarParameter>();
diffModel.AfterData = GetDiffTable(sql, result);
diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.Ado.DiffLogEvent != null)
this.Context.Ado.DiffLogEvent(diffModel);
if (this.Context.DiffLogEvent != null)
this.Context.DiffLogEvent(diffModel);
this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.RemoveCacheFunc != null)

View File

@@ -690,8 +690,8 @@ namespace SqlSugar
parameters = new List<SugarParameter>();
diffModel.AfterData = GetDiffTable(sql, parameters);
diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.Ado.DiffLogEvent != null)
this.Context.Ado.DiffLogEvent(diffModel);
if (this.Context.DiffLogEvent != null)
this.Context.DiffLogEvent(diffModel);
this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.RemoveCacheFunc != null)

View File

@@ -278,9 +278,9 @@ namespace SqlSugar
if (isCopyEvents)
{
newClient.Ado.IsEnableLogEvent = Context.Ado.IsEnableLogEvent;
newClient.Ado.LogEventStarting = Context.Ado.LogEventStarting;
newClient.Ado.LogEventCompleted = Context.Ado.LogEventCompleted;
newClient.Ado.ProcessingEventStartingSQL = Context.Ado.ProcessingEventStartingSQL;
newClient.LogEventStarting = Context.LogEventStarting;
newClient.LogEventCompleted = Context.LogEventCompleted;
newClient.ProcessingEventStartingSQL = Context.ProcessingEventStartingSQL;
newClient.QueryFilter = Context.QueryFilter;
}
return newClient;

View File

@@ -31,6 +31,12 @@ namespace SqlSugar
public MappingColumnList MappingColumns { get; set; }
public IgnoreColumnList IgnoreColumns { get; set; }
public IgnoreColumnList IgnoreInsertColumns { get; set; }
public Action<string, SugarParameter[]> LogEventStarting { get; set; }
public Action<string, SugarParameter[]> LogEventCompleted { get; set; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
public Action<SqlSugarException> ErrorEvent { get; set; }
public Action<DiffLogModel> DiffLogEvent { get; set; }
public QueueList _Queues = new QueueList();
#endregion
#region Fields
@@ -127,7 +133,7 @@ namespace SqlSugar
}
#endregion
internal void InitMppingInfo<T>()
public void InitMppingInfo<T>()
{
InitMppingInfo(typeof(T));
}

View File

@@ -912,7 +912,7 @@ namespace SqlSugar
}
this.Queues.Add(sql, parsmeters);
}
public QueueList Queues { get; set; }
public QueueList Queues { get =>_Queues; set => _Queues = value; }
private T SaveQueuesProvider<T>(bool isTran, Func<string, List<SugarParameter>, T> func)
{

View File

@@ -24,11 +24,6 @@ namespace SqlSugar
CommandType CommandType { get; set; }
bool IsEnableLogEvent { get; set; }
bool IsDisableMasterSlaveSeparation { get; set; }
Action<string, SugarParameter []> LogEventStarting { get; set; }
Action<string, SugarParameter []> LogEventCompleted { get; set; }
Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
Action<SqlSugarException> ErrorEvent { get; set; }
Action<DiffLogModel> DiffLogEvent { get; set; }
bool IsClearParameters { get; set; }
int CommandTimeOut { get; set; }
TimeSpan SqlExecutionTime { get; }

View File

@@ -12,6 +12,11 @@ namespace SqlSugar
MappingColumnList MappingColumns { get; set; }
IgnoreColumnList IgnoreColumns { get; set; }
IgnoreColumnList IgnoreInsertColumns { get; set; }
Action<string, SugarParameter[]> LogEventStarting { get; set; }
Action<string, SugarParameter[]> LogEventCompleted { get; set; }
Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get; set; }
Action<SqlSugarException> ErrorEvent { get; set; }
Action<DiffLogModel> DiffLogEvent { get; set; }
QueueList Queues { get; set; }
IAdo Ado { get; }
AopProvider Aop { get; }
@@ -47,6 +52,7 @@ namespace SqlSugar
SimpleClient GetSimpleClient();
SimpleClient<T> GetSimpleClient<T>() where T : class, new();
void InitMppingInfo(Type type);
void InitMppingInfo<T>();
IInsertable<T> Insertable<T>(Dictionary<string, object> columnDictionary) where T : class, new();
IInsertable<T> Insertable<T>(dynamic insertDynamicObject) where T : class, new();
IInsertable<T> Insertable<T>(List<T> insertObjs) where T : class, new();

View File

@@ -11,13 +11,25 @@ namespace SqlSugar
{
public class SqlSugarClient : ISqlSugarClient
{
#region Gobal Property
private ISqlSugarClient _Context = null;
private string ThreadId;
private string _ThreadId;
private ConnectionConfig _CurrentConnectionConfig;
private List<SugarTerant> _allClients;
private List<SugarTerant> _AllClients;
private bool _IsAllTran = false;
private MappingTableList _MappingTables;
private MappingColumnList _MappingColumns;
private IgnoreColumnList _IgnoreColumns;
private IgnoreColumnList _IgnoreInsertColumns;
private Action<string, SugarParameter[]> _LogEventStarting;
private Action<string, SugarParameter[]> _LogEventCompleted;
private Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> _ProcessingEventStartingSQL;
private Action<SqlSugarException> _ErrorEvent;
private Action<DiffLogModel> _DiffLogEvent;
#endregion
#region Api
public ISqlSugarClient Context { get => GetContext(); set => _Context = value; }
public SqlSugarClient(ConnectionConfig config)
{
Check.Exception(config == null, "ConnectionConfig config is null");
@@ -30,23 +42,45 @@ namespace SqlSugar
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;
_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));
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<ConnectionConfig, bool> changeExpression)
{
var allConfigs = _allClients.Select(it => it.ConnectionConfig);
var allConfigs = _AllClients.Select(it => it.ConnectionConfig);
Check.Exception(!allConfigs.Any(changeExpression), "changeExpression was not found {0}", changeExpression.ToString());
InitContext(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 Action<string, SugarParameter[]> LogEventStarting { get => _LogEventStarting; set => _LogEventStarting = value; }
public Action<string, SugarParameter[]> LogEventCompleted { get => _LogEventCompleted; set => _LogEventCompleted = value; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL { get => _ProcessingEventStartingSQL; set => _ProcessingEventStartingSQL = value; }
public Action<SqlSugarException> ErrorEvent { get => _ErrorEvent; set => _ErrorEvent = value; }
public Action<DiffLogModel> DiffLogEvent { get => _DiffLogEvent; set => _DiffLogEvent = value; }
public ConnectionConfig CurrentConnectionConfig { get => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; }
public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; }
public Dictionary<string, object> TempItems { get => this.Context.TempItems??new Dictionary<string, object>(); 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;
@@ -54,7 +88,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 => _CurrentConnectionConfig; set => _CurrentConnectionConfig = value; }
public IDbFirst DbFirst => this.Context.DbFirst;
@@ -72,14 +106,6 @@ namespace SqlSugar
[Obsolete]
public SimpleClient SimpleClient => this.Context.SimpleClient;
public Dictionary<string, object> TempItems { get => this.Context.TempItems; set => this.Context.TempItems = value; }
public IContextMethods Utilities { get => this.Context.Utilities; set => this.Context.Utilities = value; }
public MappingTableList MappingTables { get => this.Context.MappingTables; set => this.Context.MappingTables = value; }
public MappingColumnList MappingColumns { get => this.Context.MappingColumns; set => this.Context.MappingColumns = value; }
public IgnoreColumnList IgnoreColumns { get => this.Context.IgnoreColumns; set => this.Context.IgnoreColumns = value; }
public IgnoreColumnList IgnoreInsertColumns { get => this.Context.IgnoreInsertColumns; set => this.Context.IgnoreInsertColumns = value; }
public QueueList Queues { get => this.Context.Queues; set => this.Context.Queues = value; }
public void AddQueue(string sql, object parsmeters = null)
{
this.Context.AddQueue(sql, parsmeters);
@@ -159,6 +185,10 @@ namespace SqlSugar
{
this.Context.InitMppingInfo(type);
}
public void InitMppingInfo<T>()
{
this.Context.InitMppingInfo(typeof(T));
}
public IInsertable<T> Insertable<T>(Dictionary<string, object> columnDictionary) where T : class, new()
{
@@ -544,66 +574,134 @@ namespace SqlSugar
return this.Context.Updateable<T>(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)
{
var result = _Context;
ISqlSugarClient 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);
ISqlSugarClient cacheContext = GetCallContext();
if (cacheContext != null)
{
return cacheContext;
result = cacheContext;
}
else
{
result = CopyClient();
CallContext.ContextList.Value.Add(result);
}
}
return result;
}
else if (ThreadId == Thread.CurrentThread.ManagedThreadId.ToString())
else if (_ThreadId == Thread.CurrentThread.ManagedThreadId.ToString())
{
_Context.MappingColumns = _MappingColumns;
_Context.MappingTables = _MappingTables;
_Context.IgnoreColumns = _IgnoreColumns;
_Context.IgnoreInsertColumns = _IgnoreInsertColumns;
_Context.DiffLogEvent = _DiffLogEvent;
_Context.LogEventCompleted = _LogEventCompleted;
_Context.LogEventStarting = _LogEventStarting;
_Context.ErrorEvent = _ErrorEvent;
_Context.ProcessingEventStartingSQL = _ProcessingEventStartingSQL;
return _Context;
}
else
{
return new SqlSugarClient(this.CurrentConnectionConfig);
if (CallContext.ContextList.Value == null)
{
CallContext.ContextList.Value = new List<ISqlSugarClient>();
}
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;
result.DiffLogEvent = _DiffLogEvent;
result.LogEventCompleted = _LogEventCompleted;
result.LogEventStarting = _LogEventStarting;
result.ErrorEvent = _ErrorEvent;
result.ProcessingEventStartingSQL = _ProcessingEventStartingSQL;
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)
{
_Context = new SqlSugarContext(config);
this.CurrentConnectionConfig = config;
ThreadId = Thread.CurrentThread.ManagedThreadId.ToString();
if (this.MappingTables == null)
{
_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();
}
if (this.Queues == null)
{
this.Queues = new QueueList();
}
}
private void InitConfigs(List<ConnectionConfig> configs)
{
foreach (var item in configs)
@@ -614,6 +712,7 @@ namespace SqlSugar
}
}
}
private void InitTerant(SugarTerant terant)
{
if (terant.Context == null)
@@ -622,6 +721,7 @@ namespace SqlSugar
}
_Context = terant.Context;
this.CurrentConnectionConfig = terant.ConnectionConfig;
}
}
#endregion
}
}