Пример #1
0
        public void WrapStringsInQuotes()
        {
            var sut = new CommandSanitizer();

            var parameter = new CommandParameterMetadata
            {
                Name  = "@StringParameter",
                Type  = typeof(string).Name,
                Value = "This is the parameter value."
            };

            var command = string.Format("SELECT * FROM Table WHERE StringColumn = {0}", parameter.Name);

            var result = sut.Process(command, new[] { parameter });

            var expected = string.Format(CommandParameterParser.QuotedFormat, parameter.Value, parameter.Name);

            Assert.Contains(expected, result);
        }
Пример #2
0
        public void ReplaceParameterPlaceholders()
        {
            var sut = new CommandSanitizer();

            var parameter = new CommandParameterMetadata
            {
                Name  = "@SomeParameter",
                Type  = typeof(int).Name,
                Value = 1234
            };

            var command = string.Format("SELECT * FROM Table WHERE Id = {0}", parameter.Name);

            var result = sut.Process(command, new[] { parameter });

            var expected = string.Format(CommandParameterParser.UnquotedFormat, parameter.Value, parameter.Name);

            Assert.Contains(expected, result);
        }
Пример #3
0
        public void NotReplacePartialMatches()
        {
            var sut = new CommandSanitizer();

            var parameters = Enumerable.Range(1, 30).Select(x => new CommandParameterMetadata
            {
                Name  = "@Id" + x,
                Type  = typeof(int).Name,
                Value = x
            }).ToList();

            var parameterList = string.Join(", ", parameters.Select(x => x.Name));

            var command = string.Format("SELECT * FROM Table WHERE Id IN ({0})", parameterList);

            var result = sut.Process(command, parameters);

            foreach (var parameter in parameters)
            {
                var expected = string.Format(CommandParameterParser.UnquotedFormat, parameter.Value, parameter.Name);

                Assert.Contains(expected, result);
            }
        }
Пример #4
0
        public override object GetData(ITabContext context)
        {
            var sanitizer     = new CommandSanitizer();
            var messages      = context.GetMessages <AdoMessage>().ToList();
            var aggregator    = new MessageAggregator(messages);
            var queryMetadata = aggregator.Aggregate();

            if (queryMetadata == null)
            {
                return(null);
            }

            var connections = new List <object[]> {
                new object[] { "Commands per Connection", "Duration" }
            };

            foreach (var connection in queryMetadata.Connections.Values)
            {
                if (connection.Commands.Count == 0 && connection.Transactions.Count == 0)
                {
                    continue;
                }

                var commands = new List <object[]> {
                    new object[] { "Transaction Start", "Ordinal", "Command", "Parameters", "Records", "Duration", "Offset", "Async", "Transaction End", "Errors" }
                };
                var commandCount = 1;
                foreach (var command in connection.Commands.Values)
                {
                    // Transaction Start
                    List <object[]> headTransaction = null;
                    if (command.HeadTransaction != null)
                    {
                        headTransaction = new List <object[]> {
                            new object[] { "\t▼ Transaction - Started", "Isolation Level - " + command.HeadTransaction.IsolationLevel }
                        };
                        if (!command.HeadTransaction.Committed.HasValue)
                        {
                            headTransaction.Add(new object[] { string.Empty, "Transaction was never completed", "error" });
                        }
                    }

                    // Transaction Finish
                    List <object[]> tailTransaction = null;
                    if (command.TailTransaction != null)
                    {
                        tailTransaction = new List <object[]> {
                            new object[] { "\t▲ Transaction - Finished", "Status - " + (command.TailTransaction.Committed.GetValueOrDefault() ? "Committed" : "Rollbacked") }
                        };
                    }

                    // Parameters
                    List <object[]> parameters = null;
                    if (command.Parameters.Count > 0)
                    {
                        parameters = new List <object[]> {
                            new object[] { "Name", "Value", "Type", "Size" }
                        };
                        foreach (var parameter in command.Parameters)
                        {
                            parameters.Add(new[] { parameter.Name, parameter.Value, parameter.Type, parameter.Size });
                        }
                    }

                    // Exception
                    List <object[]> errors = null;
                    if (command.Exception != null)
                    {
                        var exception     = command.Exception.GetBaseException();
                        var exceptionName = command.Exception != exception ? command.Exception.Message + ": " + exception.Message : exception.Message;

                        errors = new List <object[]> {
                            new object[] { "Error", "Stack" }, new object[] { exceptionName, exception.StackTrace }
                        };
                    }

                    // Commands
                    var records = command.RecordsAffected == null || command.RecordsAffected < 0 ? command.TotalRecords : command.RecordsAffected;

                    var status = errors != null ? "error" : (command.IsDuplicate ? "warn" : string.Empty);
                    commands.Add(new object[] { headTransaction, string.Format("{0}{1}", command.HasTransaction ? "\t\t\t" : string.Empty, commandCount++), sanitizer.Process(command.Command, command.Parameters), parameters, records, command.Duration, command.Offset, command.IsAsync, tailTransaction, errors, status });
                }

                connections.Add(new[] { commands, connection.Duration.HasValue ? (object)connection.Duration.Value : null });
            }

            if (connections.Count > 1)
            {
                SqlStatistics sqlStatistics = SqlStatisticsCalculator.Caluculate(queryMetadata);

                return(new Dictionary <string, object>
                {
                    { "SQL Statistics", new object[] { new { sqlStatistics.ConnectionCount, sqlStatistics.QueryCount, sqlStatistics.TransactionCount, sqlStatistics.QueryExecutionTime, sqlStatistics.ConnectionOpenTime } } },
                    { "Queries", connections }
                });
            }

            return(null);
        }
Пример #5
0
 public EF(IGlimpseFactory factory)
 {
     Logger = factory.CreateLogger();
     Sanitizer = new CommandSanitizer();
 }
Пример #6
0
 public SQL(IGlimpseFactory factory)
 {
     Logger    = factory.CreateLogger();
     Sanitizer = new CommandSanitizer();
 }