Esempio n. 1
0
        /// <summary>
        /// Tracks when 'command' is started.
        /// </summary>
        public void ExecuteStartImpl(ProfiledDbCommand command, SqlExecuteType type)
        {
            var id        = Tuple.Create((object)command, type);
            var sqlTiming = new SqlTiming(command, type, Profiler);

            _inProgress[id] = sqlTiming;
        }
Esempio n. 2
0
 /// <summary>
 /// Called when 'reader' finishes its iterations and is closed.
 /// </summary>
 public static void Exception(this SqlProfiler sqlProfiler, ProfiledDbCommand command, SqlExecuteType type, Exception exception)
 {
     if (sqlProfiler == null)
     {
         return;
     }
     sqlProfiler.ExceptionImpl(command, type, exception);
 }
Esempio n. 3
0
 /// <summary>
 /// Finishes profiling for 'command', recording durations.
 /// </summary>
 public static void ExecuteFinish(this SqlProfiler sqlProfiler, ProfiledDbCommand command, SqlExecuteType type, DbDataReader reader = null)
 {
     if (sqlProfiler == null)
     {
         return;
     }
     sqlProfiler.ExecuteFinishImpl(command, type, reader);
 }
Esempio n. 4
0
 /// <summary>
 /// Tracks when 'command' is started.
 /// </summary>
 public static void ExecuteStart(this SqlProfiler sqlProfiler, ProfiledDbCommand command, SqlExecuteType type)
 {
     if (sqlProfiler == null)
     {
         return;
     }
     sqlProfiler.ExecuteStartImpl(command, type);
 }
Esempio n. 5
0
        /// <summary>
        /// Finishes profiling for 'command', recording durations.
        /// </summary>
        public void ExceptionImpl(ProfiledDbCommand command, SqlExecuteType type, Exception exception)
        {
            var id      = Tuple.Create((object)command, type);
            var current = _inProgress[id];

            current.Exception(exception);
            SqlTiming ignore;

            _inProgress.TryRemove(id, out ignore);
        }
        /// <summary>
        /// Unwraps the given command and returns the inner command.
        /// </summary>
        /// <param name="command">The outer command.</param>
        /// <returns>The inner command.</returns>
        public override IDbCommand GetInnerCommand(IDbCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            ProfiledDbCommand profiledCommand = (ProfiledDbCommand)command;

            return(profiledCommand.InternalCommand);
        }
Esempio n. 7
0
        private static IDbCommand GetCommand(ISession session)
        {
            var command = session.Connection.CreateCommand();

            if (Alma.Common.Config.Settings.EnableProfiling && command != null && command.GetType() != typeof(ProfiledDbCommand) && MiniProfiler.Current != null)
            {
                command = new ProfiledDbCommand(command, command.Connection, MiniProfiler.Current);
            }

            return(command);
        }
Esempio n. 8
0
 void IDbProfiler.ExecuteFinish(ProfiledDbCommand profiledDbCommand, SqlExecuteType executeType, DbDataReader reader)
 {
     if (reader != null)
     {
         SqlProfiler.ExecuteFinish(profiledDbCommand, executeType, reader);
     }
     else
     {
         SqlProfiler.ExecuteFinish(profiledDbCommand, executeType);
     }
 }
        public void DataReaderViaProfiledDbCommandWithNullConnection()
        {
            // https://github.com/MiniProfiler/dotnet/issues/313

            using (var conn = GetConnection())
            {
                var command        = new SqliteCommand("select 1");
                var wrappedCommand = new ProfiledDbCommand(command, conn, conn.Profiler);

                var reader = wrappedCommand.ExecuteReader(CommandBehavior.SequentialAccess);
                Assert.True(reader.Read());
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Finishes profiling for 'command', recording durations.
        /// </summary>
        public void ExecuteFinishImpl(ProfiledDbCommand command, SqlExecuteType type, DbDataReader reader = null)
        {
            var id      = Tuple.Create((object)command, type);
            var current = _inProgress[id];

            current.ExecutionComplete(reader != null, reader);
            SqlTiming ignore;

            _inProgress.TryRemove(id, out ignore);
            if (reader != null)
            {
                _inProgressReaders[reader] = current;
            }
        }
        public override IDbCommand CreateCommand()
        {
            var command = base.CreateCommand();

            if (StackExchange.Profiling.MiniProfiler.Current != null)
            {
                command = new ProfiledDbCommand(
                    (DbCommand)command,
                    (ProfiledDbConnection)command.Connection,
                    StackExchange.Profiling.MiniProfiler.Current);
            }

            return(command);
        }
Esempio n. 12
0
        /// <summary>
        /// Initialises a new instance of the <see cref="SqlTiming"/> class.
        /// Creates a new <c>SqlTiming</c> to profile 'command'.
        /// </summary>
        public SqlTiming(ProfiledDbCommand command, SqlExecuteType type, MiniProfiler profiler)
        {
            if (profiler == null)
            {
                throw new ArgumentNullException("profiler");
            }
            _profiler = profiler;

            var commandText = AddSpacesToParameters(command.CommandText);
            var parameters  = GetCommandParameters(command);

            Command       = command;
            commandText   = Settings.SqlFormatter.FormatSql(commandText, parameters);
            _customTiming = MiniProfiler.CustomTiming("sql", commandText, type.ToString());
            if (_customTiming == null)
            {
                throw new InvalidOperationException();
            }
        }
Esempio n. 13
0
 public ActionResult Index()
 { 
     Database db = DatabaseFactory.CreateDatabase();
     DbCommand dbCommand = db.GetStoredProcCommand("spGetSomething");
     DbCommand cmd = new ProfiledDbCommand(dbCommand, dbCommand.Connection, MiniProfiler.Current);
     DataSet ds = db.ExecuteDataSet(cmd);
Esempio n. 14
0
 void IDbProfiler.OnError(ProfiledDbCommand profiledDbCommand, SqlExecuteType executeType, Exception exception)
 {
     SqlProfiler.Exception(profiledDbCommand, executeType, exception);
 }
Esempio n. 15
0
        public void TestProfiledDbCommandWithIDbCommand()
        {
            var mockCommand    = new Mock <IDbCommand>();
            var mockDbProfiler = new Mock <IDbProfiler>();

            var tags   = new TagCollection(new[] { "test" });
            var target = new ProfiledDbCommand(mockCommand.Object, mockDbProfiler.Object, tags) as IDbCommand;

            // test Cancel()
            var cancelCalled = false;

            mockCommand.Setup(cmd => cmd.Cancel()).Callback(() => cancelCalled = true);
            target.Cancel();
            Assert.IsTrue(cancelCalled);

            // test CommandText
            var sql            = "test sql;";
            var sql2           = "test sql 2";
            var commandTextSet = false;

            mockCommand.Setup(cmd => cmd.CommandText).Returns(sql);
            mockCommand.SetupSet(cmd => cmd.CommandText = It.IsAny <string>()).Callback <string>(a =>
            {
                Assert.AreEqual(sql2, a);
                commandTextSet = true;
            });
            Assert.AreEqual(sql, target.CommandText);
            target.CommandText = sql2;
            Assert.IsTrue(commandTextSet);

            // test CommandTimeout
            var timeout1          = 1;
            var timeout2          = 2;
            var commandTimeoutSet = false;

            mockCommand.Setup(cmd => cmd.CommandTimeout).Returns(timeout1);
            mockCommand.SetupSet(cmd => cmd.CommandTimeout = It.IsAny <int>()).Callback <int>(a =>
            {
                Assert.AreEqual(timeout2, a);
                commandTimeoutSet = true;
            });
            Assert.AreEqual(timeout1, target.CommandTimeout);
            target.CommandTimeout = timeout2;
            Assert.IsTrue(commandTimeoutSet);

            // test CommandType
            var cmdType1       = CommandType.StoredProcedure;
            var cmdType2       = CommandType.Text;
            var commandTypeSet = false;

            mockCommand.Setup(cmd => cmd.CommandType).Returns(cmdType1);
            mockCommand.SetupSet(cmd => cmd.CommandType = It.IsAny <CommandType>()).Callback <CommandType>(a =>
            {
                Assert.AreEqual(cmdType2, a);
                commandTypeSet = true;
            });
            Assert.AreEqual(cmdType1, target.CommandType);
            target.CommandType = cmdType2;
            Assert.IsTrue(commandTypeSet);

            // test CreateDbParameter()
            var mockParameter = new Mock <DbParameter>();
            var parameterName = "p1";

            mockParameter.Setup(p => p.ParameterName).Returns(parameterName);
            mockCommand.Setup(cmd => cmd.CreateParameter()).Returns(mockParameter.Object);
            var parameter = target.CreateParameter();

            Assert.AreNotEqual(mockParameter.Object, parameter);
            Assert.AreEqual(parameterName, parameter.ParameterName);

            // test DbConnection
            var mockConnection = new Mock <IDbConnection>();
            var connStr        = "test conn str";

            mockConnection.Setup(c => c.ConnectionString).Returns(connStr);
            mockCommand.Setup(cmd => cmd.Connection).Returns(mockConnection.Object);
            var connection = target.Connection;

            Assert.AreEqual(mockConnection.Object, connection);
            Assert.AreEqual(connection, target.Connection);
            Assert.AreEqual(connStr, connection.ConnectionString);

            // test DbParameterCollection
            Assert.IsNull(target.Parameters);
            // ReSharper disable HeuristicUnreachableCode
            var mockParameterCollection = new Mock <IDataParameterCollection>();

            mockParameterCollection.Setup(c => c.Count).Returns(1);
            mockCommand.Setup(cmd => cmd.Parameters).Returns(mockParameterCollection.Object);
            var parameterCollection = target.Parameters;

            Assert.IsFalse(ReferenceEquals(mockParameterCollection.Object, parameterCollection));
            Assert.AreEqual(parameterCollection, target.Parameters);
            Assert.AreEqual(mockParameterCollection.Object.Count, parameterCollection.Count);

            // test DbTransaction
            var mockTransaction = new Mock <IDbTransaction>();
            var isoLevel        = IsolationLevel.Chaos;

            mockTransaction.Setup(t => t.IsolationLevel).Returns(isoLevel);
            mockCommand.Setup(cmd => cmd.Transaction).Returns(mockTransaction.Object);
            var transaction = target.Transaction;

            Assert.AreEqual(mockTransaction.Object, transaction);
            Assert.AreEqual(transaction, target.Transaction);
            Assert.AreEqual(isoLevel, transaction.IsolationLevel);

            //test ExecuteDbDataReader()
            var mockReader = new Mock <IDataReader>();

            mockReader.Setup(r => r.Depth).Returns(1);
            var executeDbCommandCalled = false;

            mockDbProfiler.Setup(p => p.ExecuteDbCommand(It.IsAny <DbExecuteType>(), It.IsAny <IDbCommand>(), It.IsAny <Func <IDataReader> >(), It.IsAny <TagCollection>()))
            .Callback <DbExecuteType, IDbCommand, Func <IDataReader>, IEnumerable <string> >((a, b, c, d) =>
            {
                Assert.AreEqual(mockCommand.Object, b);
                Assert.AreEqual(tags.First(), d.First());
                c();
                executeDbCommandCalled = true;
            });
            var cmdBehavior = CommandBehavior.CloseConnection;

            mockCommand.Setup(c => c.ExecuteReader(cmdBehavior)).Returns(mockReader.Object);
            var reader = target.ExecuteReader(cmdBehavior);

            Assert.IsTrue(executeDbCommandCalled);
            Assert.AreNotEqual(mockReader.Object, reader);
            Assert.AreEqual(1, reader.Depth);

            // test ExecuteNonQuery()
            executeDbCommandCalled = false;
            var executeNonQueryCalled = false;
            var executeNonQueryResult = 1;

            mockCommand.Setup(c => c.ExecuteNonQuery()).Callback(() => executeNonQueryCalled = true).Returns(executeNonQueryResult);
            Assert.AreEqual(executeNonQueryResult, target.ExecuteNonQuery());
            Assert.IsTrue(executeDbCommandCalled);
            Assert.IsTrue(executeNonQueryCalled);

            // test ExecuteScalar()
            executeDbCommandCalled = false;
            var executeScalarCalled = false;
            var executeScalarResult = new object();

            mockCommand.Setup(c => c.ExecuteScalar()).Callback(() => executeScalarCalled = true).Returns(executeScalarResult);
            Assert.AreEqual(executeScalarResult, target.ExecuteScalar());
            Assert.IsTrue(executeDbCommandCalled);
            Assert.IsTrue(executeScalarCalled);

            // test Prepare()
            var prepareCalled = false;

            mockCommand.Setup(c => c.Prepare()).Callback(() => prepareCalled = true);
            target.Prepare();
            Assert.IsTrue(prepareCalled);

            // test UpdatedRowSource
            var updateRowSource1      = UpdateRowSource.Both;
            var updateRowSource2      = UpdateRowSource.FirstReturnedRecord;
            var updateRowSourceCalled = false;

            mockCommand.Setup(cmd => cmd.UpdatedRowSource).Returns(updateRowSource1);
            mockCommand.SetupSet(cmd => cmd.UpdatedRowSource = It.IsAny <UpdateRowSource>()).Callback <UpdateRowSource>(a =>
            {
                Assert.AreEqual(updateRowSource2, a);
                updateRowSourceCalled = true;
            });
            Assert.AreEqual(updateRowSource1, target.UpdatedRowSource);
            target.UpdatedRowSource = updateRowSource2;
            Assert.IsTrue(updateRowSourceCalled);

            // ReSharper restore HeuristicUnreachableCode
        }
Esempio n. 16
0
 void IDbProfiler.ExecuteStart(ProfiledDbCommand profiledDbCommand, SqlExecuteType executeType)
 {
     SqlProfiler.ExecuteStart(profiledDbCommand, executeType);
 }
Esempio n. 17
0
        public void TestProfiledDbCommandWithProfiledDbCommand()
        {
            var mockCommand    = new Mock <DbCommand>();
            var mockDbProfiler = new Mock <IDbProfiler>();

            var target = new ProfiledDbCommand(mockCommand.Object, mockDbProfiler.Object);

            // test CreateDbParameter()
            var mockParameter = new Mock <DbParameter>();

            mockCommand.Protected().Setup <DbParameter>("CreateDbParameter").Returns(mockParameter.Object);
            Assert.AreEqual(mockParameter.Object, target.CreateParameter());

            // test DbConnection
            var mockConnection     = new Mock <DbConnection>();
            var profiledConnection = new ProfiledDbConnection(mockConnection.Object, mockDbProfiler.Object);

            mockCommand.Protected().Setup <DbConnection>("DbConnection").Returns(profiledConnection);
            var connection = target.Connection;

            Assert.AreEqual(profiledConnection, connection);

            // test DbParameterCollection
            var mockParameterCollection = new Mock <DbParameterCollection>();

            mockCommand.Protected().Setup <DbParameterCollection>("DbParameterCollection").Returns(mockParameterCollection.Object);
            var parameterCollection = target.Parameters;

            Assert.AreEqual(mockParameterCollection.Object, parameterCollection);

            // test DbTransaction
            var mockTransaction     = new Mock <DbTransaction>();
            var profiledTransaction = new ProfiledDbTransaction(mockTransaction.Object, profiledConnection);

            mockCommand.Protected().Setup <DbTransaction>("DbTransaction").Returns(profiledTransaction);
            var transaction = target.Transaction;

            Assert.AreEqual(profiledTransaction, transaction);

            // test DesignTimeVisible
            var designTimeVisible1   = true;
            var designTimeVisible2   = false;
            var designTimeVisibleSet = false;

            mockCommand.Setup(cmd => cmd.DesignTimeVisible).Returns(designTimeVisible1);
            mockCommand.SetupSet(cmd => cmd.DesignTimeVisible = It.IsAny <bool>()).Callback <bool>(a =>
            {
                Assert.AreEqual(designTimeVisible2, a);
                designTimeVisibleSet = true;
            });
            Assert.AreEqual(designTimeVisible1, target.DesignTimeVisible);
            target.DesignTimeVisible = designTimeVisible2;
            Assert.IsTrue(designTimeVisibleSet);

            // test ExecuteDbDataReader()
            var mockReader     = new Mock <DbDataReader>();
            var profiledReader = new ProfiledDbDataReader(mockReader.Object, mockDbProfiler.Object);
            var cmdBehavior    = CommandBehavior.CloseConnection;

            mockCommand.Protected().Setup <DbDataReader>("ExecuteDbDataReader", cmdBehavior).Returns(profiledReader);
            var executeDbCommandCalled = false;

            mockDbProfiler.Setup(p => p.ExecuteDbCommand(It.IsAny <DbExecuteType>(), It.IsAny <IDbCommand>(), It.IsAny <Func <IDataReader> >(), It.IsAny <TagCollection>()))
            .Callback <DbExecuteType, IDbCommand, Func <IDataReader>, IEnumerable <string> >((a, b, c, d) =>
            {
                Assert.AreEqual(mockCommand.Object, b);
                c();
                executeDbCommandCalled = true;
            });
            var reader = target.ExecuteReader(cmdBehavior);

            Assert.IsTrue(executeDbCommandCalled);
            Assert.AreEqual(profiledReader, reader);

            // test Dispose()
            var disposeCalled = false;

            mockCommand.Protected().Setup("Dispose", true).Callback <bool>(a => disposeCalled = true);
            target.Dispose();
            Assert.IsTrue(disposeCalled);
        }