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 NonQueries = new List(); public InterceptionResult CommandCreating(CommandCorrelatedEventData eventData, InterceptionResult result) { return result; } public DbCommand CommandCreated(CommandEndEventData eventData, DbCommand result) { return result; } public InterceptionResult ReaderExecuting( DbCommand command, CommandEventData eventData, InterceptionResult result ) { var cmd = command.CommandText; var paramRegex = new Regex("@\\w+"); var matches = paramRegex.Matches(cmd); //cmd = matches.Cast().Aggregate(cmd, (current, match) => current.Replace(match.Value, command.Parameters.Cast().First(p => p.ParameterName == match.Value).Value.ToString())); ; if (matches.Count > 0) { } NonQueries.Add(command.CommandText); return result; } public InterceptionResult ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult result) { return result; } public InterceptionResult NonQueryExecuting(DbCommand command, CommandEventData eventData, InterceptionResult 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> ReaderExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = new CancellationToken() ) { return Task.FromResult(result); } public Task> ScalarExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = new CancellationToken() ) { return Task.FromResult(result); } public Task> NonQueryExecutingAsync( DbCommand command, CommandEventData eventData, InterceptionResult 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 ReaderExecutedAsync( DbCommand command, CommandExecutedEventData eventData, DbDataReader result, CancellationToken cancellationToken = new CancellationToken() ) { return Task.FromResult(result); } public Task ScalarExecutedAsync( DbCommand command, CommandExecutedEventData eventData, object result, CancellationToken cancellationToken = new CancellationToken() ) { return Task.FromResult(result); } public Task 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; } } }