exsvelte/Infrastructure/Database/DbCommandInterceptor.cs
2020-11-10 21:44:20 +11:00

166 lines
5.3 KiB
C#

using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace Infrastructure.Data.OliVerse
{
public class OutputCommandInterceptor : IDbCommandInterceptor
{
public List<string> NonQueries = new List<string>();
public InterceptionResult<DbCommand> CommandCreating(CommandCorrelatedEventData eventData, InterceptionResult<DbCommand> result)
{
return result;
}
public DbCommand CommandCreated(CommandEndEventData eventData, DbCommand result)
{
return result;
}
public InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result
)
{
var cmd = command.CommandText;
var paramRegex = new Regex("@\\w+");
var matches = paramRegex.Matches(cmd);
//cmd = matches.Cast<Match>().Aggregate(cmd, (current, match) => current.Replace(match.Value, command.Parameters.Cast<SqParameter>().First(p => p.ParameterName == match.Value).Value.ToString())); ;
if (matches.Count > 0)
{
}
NonQueries.Add(command.CommandText);
return result;
}
public InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result)
{
return result;
}
public InterceptionResult<int> NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<int> result)
{
var cmd = command.CommandText;
foreach (DbParameter parameter in command.Parameters)
{
cmd = parameter.DbType == DbType.String
? cmd.Replace(parameter.ParameterName, "'" + parameter.Value + "'")
: cmd.Replace(parameter.ParameterName, parameter.Value.ToString());
}
NonQueries.Add(cmd);
//(command as En SqlCeMultiCommand)
//command.Cancel();
return result;
}
public Task<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public Task<InterceptionResult<object>> ScalarExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<object> result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public Task<InterceptionResult<int>> NonQueryExecutingAsync(
DbCommand command,
CommandEventData eventData,
InterceptionResult<int> result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public DbDataReader ReaderExecuted(DbCommand command, CommandExecutedEventData eventData, DbDataReader result)
{
return result;
}
public object ScalarExecuted(DbCommand command, CommandExecutedEventData eventData, object result)
{
return result;
}
public int NonQueryExecuted(DbCommand command, CommandExecutedEventData eventData, int result)
{
NonQueries.Add(command.CommandText);
return result;
}
public Task<DbDataReader> ReaderExecutedAsync(
DbCommand command,
CommandExecutedEventData eventData,
DbDataReader result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public Task<object> ScalarExecutedAsync(
DbCommand command,
CommandExecutedEventData eventData,
object result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public Task<int> NonQueryExecutedAsync(
DbCommand command,
CommandExecutedEventData eventData,
int result,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.FromResult(result);
}
public void CommandFailed(DbCommand command, CommandErrorEventData eventData)
{
NonQueries.Add(command.CommandText);
}
public Task CommandFailedAsync(
DbCommand command,
CommandErrorEventData eventData,
CancellationToken cancellationToken = new CancellationToken()
)
{
return Task.CompletedTask;
}
public InterceptionResult DataReaderDisposing(DbCommand command, DataReaderDisposingEventData eventData, InterceptionResult result)
{
return result;
}
}
}