This commit is contained in:
sunkaixuan
2019-05-05 14:05:58 +08:00
parent 914f6cae5f
commit 5978e9424e
16 changed files with 64 additions and 78 deletions

View File

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

View File

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

View File

@@ -50,7 +50,7 @@ namespace OrmTest.UnitTest
{
var join7 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id*1) }).ToSql();
}).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id * 1) }).ToSql();
string sql = @"SELECT [st].[Name] AS [Name] , MIN(( [sc].[Id] * @Id0 )) AS [SchoolId] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) ";
base.Check(sql, new List<SugarParameter>() {
@@ -64,8 +64,8 @@ namespace OrmTest.UnitTest
using (var db = GetInstance())
{
db.MappingTables.Add("School", "SchoolTable");
var join5= db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st => st)
.GroupBy(st=> new{ st.Id,st.Name })
var join5 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st => st)
.GroupBy(st => new { st.Id, st.Name })
.ToSql();
string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] )GROUP BY [st].[ID],[st].[Name] ";
base.Check(sql, null, join5.Key, null, "join 5 Error");
@@ -77,7 +77,7 @@ namespace OrmTest.UnitTest
using (var db = GetInstance())
{
db.MappingTables.Add("School", "SchoolTable");
var join4 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st=>st).ToSql();
var join4 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st => st).ToSql();
string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] ) ";
base.Check(sql, null, join4.Key, null, "join 4 Error");
}
@@ -94,7 +94,7 @@ namespace OrmTest.UnitTest
.AddParameters(new { id = 1 })
.Select("st.*").ToSql();
string sql = @"SELECT st.* FROM [Student] st Left JOIN [School] sh ON sh.id=st.schoolid WHERE st.id>@id ";
base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
base.Check(sql, new List<SugarParameter>() { new SugarParameter("@id", 1) }, join3.Key, join3.Value, "join 3 Error");
}
}
@@ -128,12 +128,19 @@ namespace OrmTest.UnitTest
public new SqlSugarClient GetInstance()
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
db.Ado.IsEnableLogEvent = true;
db.LogEventStarting = (sql, pars) =>
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
Console.WriteLine(sql + " " + pars);
};
ConnectionString = Config.ConnectionString,
DbType = DbType.SqlServer,
AopEvents = new AopEvents()
{
OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + " " + pars);
}
}
});
return db;
}
}

View File

@@ -30,7 +30,7 @@ namespace OrmTest.UnitTest
using (var db = GetInstance())
{
//db.Database.IsEnableLogEvent = true;
db.LogEventStarting = (sql, pars) =>
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + " " + pars);
};
@@ -81,8 +81,7 @@ namespace OrmTest.UnitTest
}, t3.Key, t3.Value, "select t3 Error");
db.Ado.IsEnableLogEvent = true;
db.LogEventStarting = (sql, pars) =>
db.Aop.OnLogExecuting = (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

@@ -57,14 +57,14 @@ namespace SqlSugar
}
public virtual int CommandTimeOut { get; set; }
public virtual CommandType CommandType { get; set; }
public virtual bool IsEnableLogEvent { get => this.Context.IsEnableLogEvent; set => this.Context.IsEnableLogEvent = value; }
public virtual bool IsEnableLogEvent {get;set;}
public virtual bool IsClearParameters { 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;
public virtual Action<string, SugarParameter[]> LogEventStarting=> this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting;
public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted;
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql;
protected virtual Func<string,string> FormatSql { get; set; }
public virtual Action<SqlSugarException> ErrorEvent => this.Context.ErrorEvent;
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.DiffLogEvent;
public virtual Action<SqlSugarException> ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents.OnError;
public virtual Action<DiffLogModel> DiffLogEvent => this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent;
public virtual List<IDbConnection> SlaveConnections { get; set; }
public virtual IDbConnection MasterConnection { get; set; }
#endregion

View File

@@ -11,13 +11,13 @@ namespace SqlSugar
public AopProvider(SqlSugarContext context)
{
this.Context = context;
this.Context.IsEnableLogEvent = true;
this.Context.Ado.IsEnableLogEvent = true;
}
private SqlSugarContext Context { get; set; }
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; } }
public Action<DiffLogModel> OnDiffLogEvent { set { this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent = value; } }
public Action<SqlSugarException> OnError { set { this.Context.CurrentConnectionConfig.AopEvents.OnError = value; } }
public Action<string, SugarParameter[]> OnLogExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuting= value; } }
public Action<string, SugarParameter[]> OnLogExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.OnLogExecuted = value; } }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { set { this.Context.CurrentConnectionConfig.AopEvents.OnExecutingChangeSql = 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.DiffLogEvent != null)
this.Context.DiffLogEvent(diffModel);
if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null)
this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(diffModel);
this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.RemoveCacheFunc != null) {

View File

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

View File

@@ -278,13 +278,13 @@ namespace SqlSugar
public IUpdateable<T> SetColumnsIF(bool isUpdateColumns, Expression<Func<T, bool>> columns)
{
if (isUpdateColumns)
UpdateColumns(columns);
SetColumns(columns);
return this;
}
public IUpdateable<T> SetColumnsIF(bool isUpdateColumns, Expression<Func<T, T>> columns)
{
if (isUpdateColumns)
UpdateColumns(columns);
SetColumns(columns);
return this;
}
@@ -690,8 +690,8 @@ namespace SqlSugar
parameters = new List<SugarParameter>();
diffModel.AfterData = GetDiffTable(sql, parameters);
diffModel.Time = this.Context.Ado.SqlExecutionTime;
if (this.Context.DiffLogEvent != null)
this.Context.DiffLogEvent(diffModel);
if (this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent != null)
this.Context.CurrentConnectionConfig.AopEvents.OnDiffLogEvent(diffModel);
this.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
}
if (this.RemoveCacheFunc != null)

View File

@@ -37,8 +37,7 @@ namespace SqlSugar
/// Configure External Services replace default services,For example, Redis storage
/// </summary>
[JsonIgnore]
public ConfigureExternalServices ConfigureExternalServices = _DefaultServices;
private static ConfigureExternalServices _DefaultServices = new ConfigureExternalServices();
public ConfigureExternalServices ConfigureExternalServices = new ConfigureExternalServices();
/// <summary>
/// If SlaveConnectionStrings has value,ConnectionString is write operation, SlaveConnectionStrings is read operation.
/// All operations within a transaction is ConnectionString
@@ -52,8 +51,18 @@ namespace SqlSugar
/// Used for debugging errors or BUG,Used for debugging, which has an impact on Performance
/// </summary>
public SugarDebugger Debugger { get; set; }
}
[JsonIgnore]
public AopEvents AopEvents = new AopEvents();
}
public class AopEvents
{
public Action<DiffLogModel> OnDiffLogEvent { get; set; }
public Action<SqlSugarException> OnError { get; set; }
public Action<string, SugarParameter[]> OnLogExecuting { get; set; }
public Action<string, SugarParameter[]> OnLogExecuted { get; set; }
public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnExecutingChangeSql { get; set; }
}
public class ConfigureExternalServices
{

View File

@@ -277,10 +277,6 @@ namespace SqlSugar
newClient.IgnoreColumns = this.TranslateCopy(Context.IgnoreColumns);
if (isCopyEvents)
{
newClient.Ado.IsEnableLogEvent = Context.Ado.IsEnableLogEvent;
newClient.LogEventStarting = Context.LogEventStarting;
newClient.LogEventCompleted = Context.LogEventCompleted;
newClient.ProcessingEventStartingSQL = Context.ProcessingEventStartingSQL;
newClient.QueryFilter = Context.QueryFilter;
}
return newClient;

View File

@@ -23,7 +23,6 @@ namespace SqlSugar
}
}
public bool IsEnableLogEvent { get; set; }
public ConnectionConfig CurrentConnectionConfig { get; set; }
public Dictionary<string, object> TempItems { get; set; }
public bool IsSystemTablesConfig { get { return this.CurrentConnectionConfig.InitKeyType == InitKeyType.SystemTable; } }
@@ -33,15 +32,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
public QueueList _Queues;
protected ISqlBuilder _SqlBuilder;
protected ISqlSugarClient _Context { get; set; }
protected EntityMaintenance _EntityProvider;

View File

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

View File

@@ -19,6 +19,7 @@ namespace SqlSugar
SqlSugarContext Context { get; set; }
void ExecuteBefore(string sql, SugarParameter[] pars);
void ExecuteAfter(string sql, SugarParameter[] pars);
bool IsEnableLogEvent{get;set;}
IDataParameterCollection DataReaderParameters { get; set; }
CommandType CommandType { get; set; }

View File

@@ -12,11 +12,7 @@ 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; }

View File

@@ -21,11 +21,7 @@ namespace SqlSugar
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
@@ -67,11 +63,7 @@ namespace SqlSugar
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; }
@@ -637,11 +629,6 @@ namespace SqlSugar
_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
@@ -670,11 +657,7 @@ namespace SqlSugar
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;
}