-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
273 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Masuit.Tools.AspNetCore/Extensions/QueryWithNoLockDbCommandInterceptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.Data.Common; | ||
using System.Text.RegularExpressions; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
|
||
namespace Masuit.Tools.Core; | ||
|
||
public class QueryWithNoLockDbCommandInterceptor : DbCommandInterceptor | ||
{ | ||
private static readonly Regex TableAliasRegex = new Regex(@"(?<tableAlias>AS \[[a-zA-Z]\w*\](?! WITH \(NOLOCK\)))", RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase); | ||
|
||
public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return base.ScalarExecuting(command, eventData, result); | ||
} | ||
|
||
#if NETCOREAPP3_1 | ||
|
||
public override Task<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return base.ScalarExecutingAsync(command, eventData, result, cancellationToken); | ||
} | ||
|
||
#else | ||
public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return base.ScalarExecutingAsync(command, eventData, result, cancellationToken); | ||
} | ||
#endif | ||
|
||
public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return result; | ||
} | ||
|
||
#if NETCOREAPP3_1 | ||
|
||
public override Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken); | ||
} | ||
|
||
#else | ||
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result, | ||
CancellationToken cancellationToken = new CancellationToken()) | ||
{ | ||
command.CommandText = TableAliasRegex.Replace( | ||
command.CommandText, | ||
"${tableAlias} WITH (NOLOCK)" | ||
); | ||
return base.ReaderExecutingAsync(command, eventData, result, cancellationToken); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Masuit.Tools.Core; | ||
|
||
public class ChangeEntry | ||
{ | ||
/// <summary> | ||
/// 所属实体 | ||
/// </summary> | ||
public object Entity { get; set; } | ||
|
||
/// <summary> | ||
/// 实体类型 | ||
/// </summary> | ||
public Type EntityType { get; set; } | ||
|
||
/// <summary> | ||
/// 变更类型 | ||
/// </summary> | ||
public EntityState EntityState { get; set; } | ||
|
||
/// <summary> | ||
/// 字段变更信息 | ||
/// </summary> | ||
public List<ChangePropertyInfo> ChangeProperties { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System.Reflection; | ||
|
||
namespace Masuit.Tools.Core; | ||
|
||
public class ChangePropertyInfo | ||
{ | ||
/// <summary> | ||
/// 属性 | ||
/// </summary> | ||
public PropertyInfo PropertyInfo { get; set; } | ||
|
||
/// <summary> | ||
/// 原始值 | ||
/// </summary> | ||
public object OriginalValue { get; set; } | ||
|
||
/// <summary> | ||
/// 新值 | ||
/// </summary> | ||
public object CurrentValue { get; set; } | ||
|
||
/// <summary> | ||
/// 是否是主键 | ||
/// </summary> | ||
public bool IsPrimaryKey { get; set; } | ||
|
||
/// <summary> | ||
/// 是否是外键 | ||
/// </summary> | ||
public bool IsForeignKey { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.