From 9390343b42620cf77c5dafe4606adedead711202 Mon Sep 17 00:00:00 2001 From: Michael Camara Pendon Date: Wed, 14 Sep 2022 23:33:18 +0200 Subject: [PATCH] #941 Fixes to ensure the silent cancellation of the operations. --- .../Interfaces/ITraceForDbConnectionTest.cs | 25 ++ .../Extensions/DbConnectionExtension.cs | 80 ++++- .../RepoDb/Operations/DbConnection/Insert.cs | 12 + .../Operations/DbConnection/InsertAll.cs | 24 ++ .../RepoDb/Operations/DbConnection/Merge.cs | 12 + .../Operations/DbConnection/MergeAll.cs | 24 ++ .../Operations/DbConnection/QueryMultiple.cs | 288 +++++++++++++----- .../RepoDb/Operations/DbConnection/Update.cs | 12 + .../Operations/DbConnection/UpdateAll.cs | 24 ++ RepoDb.Core/RepoDb/Trace/Tracer.cs | 4 +- 10 files changed, 410 insertions(+), 95 deletions(-) diff --git a/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Interfaces/ITraceForDbConnectionTest.cs b/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Interfaces/ITraceForDbConnectionTest.cs index 3614ac20..4a09823b 100644 --- a/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Interfaces/ITraceForDbConnectionTest.cs +++ b/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Interfaces/ITraceForDbConnectionTest.cs @@ -31,6 +31,31 @@ private class TraceEntity public string Name { get; set; } } + private class RejectorTrace : ITrace + { + public void AfterExecution(ResultTraceLog log) + { + } + + public Task AfterExecutionAsync(ResultTraceLog log, + CancellationToken cancellationToken = default) + { + return Task.CompletedTask; + } + + public void BeforeExecution(CancellableTraceLog log) + { + log.Cancel(true); + } + + public Task BeforeExecutionAsync(CancellableTraceLog log, + CancellationToken cancellationToken = default) + { + log.Cancel(true); + return Task.CompletedTask; + } + } + #endregion #region Average diff --git a/RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs b/RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs index 40e1c445..c80c5509 100644 --- a/RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs +++ b/RepoDb.Core/RepoDb/Extensions/DbConnectionExtension.cs @@ -204,6 +204,12 @@ internal static IEnumerable ExecuteQueryInternal(this IDbConnection con var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Execute using (var reader = command.ExecuteReader()) { @@ -345,12 +351,18 @@ internal static async Task> ExecuteQueryAsyncInternal(this skipCommandArrayParametersCheck: skipCommandArrayParametersCheck); // Variables - IEnumerable result; + IEnumerable result = null; // Before Execution var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Execute using (var reader = await command.ExecuteReaderAsync(cancellationToken)) { @@ -639,12 +651,18 @@ private static IEnumerable ExecuteQueryInternalForType(this ID skipCommandArrayParametersCheck: skipCommandArrayParametersCheck); // Variables - var result = (IEnumerable)null; + IEnumerable result = null; // Before Execution var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execute using (var reader = command.ExecuteReader()) { @@ -944,12 +962,18 @@ private static async Task> ExecuteQueryAsyncInternalForType dbFields: dbFields, skipCommandArrayParametersCheck: skipCommandArrayParametersCheck); - IEnumerable result; + IEnumerable result = null; // Before Execution var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Execute using (var reader = await command.ExecuteReaderAsync(cancellationToken)) { @@ -1264,7 +1288,7 @@ internal static IDataReader ExecuteReaderInternal(this IDbConnection connection, Type entityType, IEnumerable dbFields, bool skipCommandArrayParametersCheck, - Action beforeExecutionCallback = null) + Func beforeExecutionCallback = null) { // Variables var setting = DbSettingMapper.Get(connection); @@ -1283,12 +1307,18 @@ internal static IDataReader ExecuteReaderInternal(this IDbConnection connection, try { // A hacky solution for other operations (i.e.: QueryMultiple) - beforeExecutionCallback?.Invoke(command); + var traceResult = beforeExecutionCallback?.Invoke(command); // Before Execution - var traceResult = Tracer + traceResult ??= Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execute var reader = command.ExecuteReader(); @@ -1390,7 +1420,7 @@ internal static async Task ExecuteReaderAsyncInternal(this IDbConne Type entityType, IEnumerable dbFields, bool skipCommandArrayParametersCheck, - Func beforeExecutionCallbackAsync = null) + Func> beforeExecutionCallbackAsync = null) { // Variables var setting = connection.GetDbSetting(); @@ -1409,16 +1439,24 @@ internal static async Task ExecuteReaderAsyncInternal(this IDbConne // Ensure the DbCommand disposal try { + TraceResult traceResult = null; + // A hacky solution for other operations (i.e.: QueryMultipleAsync) if (beforeExecutionCallbackAsync != null) { - await beforeExecutionCallbackAsync(command, cancellationToken); + traceResult = await beforeExecutionCallbackAsync(command, cancellationToken); } // Before Execution - var traceResult = await Tracer + traceResult ??= await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execute var reader = await command.ExecuteReaderAsync(cancellationToken); @@ -1529,6 +1567,12 @@ internal static int ExecuteNonQueryInternal(this IDbConnection connection, var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execution var result = command.ExecuteNonQuery(); @@ -1632,6 +1676,12 @@ internal static async Task ExecuteNonQueryAsyncInternal(this IDbConnection var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execution var result = await command.ExecuteNonQueryAsync(cancellationToken); @@ -1870,6 +1920,12 @@ internal static TResult ExecuteScalarInternal(this IDbConnection connec var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execute var result = Converter.ToType(command.ExecuteScalar()); @@ -2009,6 +2065,12 @@ internal static async Task ExecuteScalarAsyncInternal(this IDb var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + // Execution var result = Converter.ToType(await command.ExecuteScalarAsync(cancellationToken)); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/Insert.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/Insert.cs index a654d5fd..eff6162b 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/Insert.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/Insert.cs @@ -678,6 +678,12 @@ internal static TResult InsertInternalBase(this IDbConnection var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = Converter.ToType(command.ExecuteScalar()); @@ -756,6 +762,12 @@ internal async static Task InsertAsyncInternalBase(th var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = Converter.ToType(await command.ExecuteScalarAsync(cancellationToken)); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/InsertAll.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/InsertAll.cs index 6d97eb7a..eb2a8592 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/InsertAll.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/InsertAll.cs @@ -497,6 +497,12 @@ internal static int InsertAllInternalBase(this IDbConnection connection var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution var returnValue = Converter.DbNullToNull(command.ExecuteScalar()); @@ -567,6 +573,12 @@ internal static int InsertAllInternalBase(this IDbConnection connection var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // No identity setters result += command.ExecuteNonQuery(); @@ -728,6 +740,12 @@ internal static async Task InsertAllAsyncInternalBase(this IDbConn var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution var returnValue = Converter.DbNullToNull(await command.ExecuteScalarAsync(cancellationToken)); @@ -799,6 +817,12 @@ await Tracer var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // No identity setters result += await command.ExecuteNonQueryAsync(cancellationToken); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/Merge.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/Merge.cs index e9e77a2c..512da0e5 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/Merge.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/Merge.cs @@ -2155,6 +2155,12 @@ internal static TResult MergeInternalBase(this IDbConnection c var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = Converter.ToType(command.ExecuteScalar()); @@ -2400,6 +2406,12 @@ internal static async Task MergeAsyncInternalBase(thi var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = Converter.ToType(await command.ExecuteScalarAsync(cancellationToken)); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/MergeAll.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/MergeAll.cs index e59ee63c..7bb3fa85 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/MergeAll.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/MergeAll.cs @@ -1336,6 +1336,12 @@ internal static int MergeAllInternalBase(this IDbConnection connection, var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution var returnValue = Converter.DbNullToNull(command.ExecuteScalar()); @@ -1409,6 +1415,12 @@ internal static int MergeAllInternalBase(this IDbConnection connection, var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // No identity setters result += command.ExecuteNonQuery(); @@ -1710,6 +1722,12 @@ internal static async Task MergeAllAsyncInternalBase(this IDbConne var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution var returnValue = Converter.DbNullToNull(await command.ExecuteScalarAsync(cancellationToken)); @@ -1784,6 +1802,12 @@ await Tracer var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // No identity setters result += await command.ExecuteNonQueryAsync(cancellationToken); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/QueryMultiple.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/QueryMultiple.cs index 3abd692f..624c0c97 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/QueryMultiple.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/QueryMultiple.cs @@ -9501,11 +9501,9 @@ internal static Tuple, IEnumerable> QueryMultipleInternal(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -9521,6 +9519,13 @@ internal static Tuple, IEnumerable> QueryMultipleInternal, IEnumerable, IEnumerable> QueryMu TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -10233,6 +10236,13 @@ internal static Tuple, IEnumerable, IEnumerable> QueryMu skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -11096,11 +11106,9 @@ public static Tuple, IEnumerable, IEnumerable, IEnumerab TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -11116,6 +11124,13 @@ public static Tuple, IEnumerable, IEnumerable, IEnumerab skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -12143,11 +12158,9 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -12163,6 +12176,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -13354,11 +13374,9 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -13374,6 +13392,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -14729,11 +14754,9 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -14749,6 +14772,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumer skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -15251,11 +15281,9 @@ internal static Tuple, IEnumerable> QueryMultipleI TraceResult traceResult = null; // Before Execution - var beforeExecutionCallback = new Action(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -15271,6 +15299,13 @@ internal static Tuple, IEnumerable> QueryMultipleI skipCommandArrayParametersCheck: true, beforeExecutionCallback: beforeExecutionCallback)) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -15833,11 +15868,9 @@ internal static Tuple, IEnumerable, IEnumerable(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -15853,6 +15886,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumerable, IEnumerable(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -16572,6 +16610,13 @@ public static Tuple, IEnumerable, IEnumerable, IEnumerable, IEnumerable(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -17421,6 +17464,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumerable, IEnumerable(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -18402,6 +18450,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumerable, IEnumerable(command => - { + var beforeExecutionCallback = new Func(command => traceResult = Tracer - .InvokeBeforeExecution(traceKey, trace, command); - }); + .InvokeBeforeExecution(traceKey, trace, command)); // Actual Execution using (var reader = (DbDataReader)ExecuteReaderInternal(connection: connection, @@ -19514,6 +19567,13 @@ internal static Tuple, IEnumerable, IEnumerable, IEnumerable>> QueryMultipl TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -20157,6 +20215,13 @@ internal static async Task, IEnumerable>> QueryMultipl skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -20871,11 +20936,9 @@ internal static async Task, IEnumerable, IEnumerable beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -20892,6 +20955,13 @@ internal static async Task, IEnumerable, IEnumerable, IEnumerable, IEnumerable beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -21799,6 +21867,13 @@ internal static async Task, IEnumerable, IEnumerable, IEnumerable, IEnumerable beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -22872,6 +22945,13 @@ internal static async Task, IEnumerable, IEnumerable, IEnumerable, IEnumerable beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -24111,6 +24189,13 @@ internal static async Task, IEnumerable, IEnumerable, IEnumerable, IEnumerable beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -25516,6 +25599,13 @@ internal static async Task, IEnumerable, IEnumerable, IEnumerable>> Qu TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -26061,6 +26149,13 @@ internal static async Task, IEnumerable>> Qu skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -26642,11 +26737,9 @@ internal static async Task, IEnumerable, IEn TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -26663,6 +26756,13 @@ internal static async Task, IEnumerable, IEn skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -27382,11 +27482,9 @@ internal static async Task, IEnumerable, IEn TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -27403,6 +27501,13 @@ internal static async Task, IEnumerable, IEn skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -28255,11 +28360,9 @@ internal static async Task, IEnumerable, IEn TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -28276,6 +28379,13 @@ internal static async Task, IEnumerable, IEn skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -29261,11 +29371,9 @@ internal static async Task, IEnumerable, IEn TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -29282,6 +29390,13 @@ internal static async Task, IEnumerable, IEn skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 @@ -30400,11 +30515,9 @@ internal static async Task, IEnumerable, IEn TraceResult traceResult = null; // Before Execution - Func beforeExecutionCallbackAsync = async (command, cancellationToken) => - { + async Task beforeExecutionCallbackAsync(DbCommand command, CancellationToken cancellationToken) => traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); - }; // Actual Execution using (var reader = (DbDataReader)(await ExecuteReaderAsyncInternal(connection: connection, @@ -30421,6 +30534,13 @@ internal static async Task, IEnumerable, IEn skipCommandArrayParametersCheck: true, beforeExecutionCallbackAsync: beforeExecutionCallbackAsync))) { + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return default; + } + + // DB setting var dbSetting = connection.GetDbSetting(); // T1 diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/Update.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/Update.cs index 8f55f0c9..f19a1876 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/Update.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/Update.cs @@ -1821,6 +1821,12 @@ internal static int UpdateInternalBase(this IDbConnection connection, var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = command.ExecuteNonQuery(); @@ -1899,6 +1905,12 @@ internal static async Task UpdateAsyncInternalBase(this IDbConnect var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result = await command.ExecuteNonQueryAsync(cancellationToken); diff --git a/RepoDb.Core/RepoDb/Operations/DbConnection/UpdateAll.cs b/RepoDb.Core/RepoDb/Operations/DbConnection/UpdateAll.cs index 0feaeb2f..57821e45 100644 --- a/RepoDb.Core/RepoDb/Operations/DbConnection/UpdateAll.cs +++ b/RepoDb.Core/RepoDb/Operations/DbConnection/UpdateAll.cs @@ -1253,6 +1253,12 @@ internal static int UpdateAllInternalBase(this IDbConnection connection var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result += command.ExecuteNonQuery(); @@ -1312,6 +1318,12 @@ internal static int UpdateAllInternalBase(this IDbConnection connection var traceResult = Tracer .InvokeBeforeExecution(traceKey, trace, command); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result += command.ExecuteNonQuery(); @@ -1449,6 +1461,12 @@ internal static async Task UpdateAllAsyncInternalBase(this IDbConn var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result += await command.ExecuteNonQueryAsync(cancellationToken); @@ -1509,6 +1527,12 @@ await Tracer var traceResult = await Tracer .InvokeBeforeExecutionAsync(traceKey, trace, command, cancellationToken); + // Silent cancellation + if (traceResult?.CancellableTraceLog?.IsCancelled == true) + { + return result; + } + // Actual Execution result += await command.ExecuteNonQueryAsync(cancellationToken); diff --git a/RepoDb.Core/RepoDb/Trace/Tracer.cs b/RepoDb.Core/RepoDb/Trace/Tracer.cs index 5eb9db91..4dfd7a83 100644 --- a/RepoDb.Core/RepoDb/Trace/Tracer.cs +++ b/RepoDb.Core/RepoDb/Trace/Tracer.cs @@ -85,7 +85,7 @@ public static void InvokeAfterExecution(TraceResult result, ITrace trace, TResult value) { - if (result == null) + if (trace == null || result == null) { return; } @@ -118,7 +118,7 @@ public static async Task InvokeAfterExecutionAsync(TraceResult result, TResult value, CancellationToken cancellationToken = default) { - if (result == null) + if (trace == null || result == null) { return; }