Exemplo n.º 1
0
        public void TestMultipleDispose()
        {
            var connectionString = new SqlServerDatabaseConnectionString(
                Instance.DatabasePath,
                TestDatabaseName,
                true
                );

            using (var itemSaver = new SqlBatchItemSaverFactory(
                new EmptyLoggerAdapter(), 
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new BatchBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {
                    ebSaver.Dispose();
                }
            }

        }
Exemplo n.º 2
0
        public void CreateDatabase(
            string databasePath,
            string databaseName
            )
        {
            if (databasePath == null)
            {
                throw new ArgumentNullException("databasePath");
            }
            if (databaseName == null)
            {
                throw new ArgumentNullException("databaseName");
            }

            //формируем строку подключения
            var defaultConnectionString = new SqlServerDatabaseConnectionString(
                databasePath,
                string.Empty
                );

            //создаем базу
            using (var connection = new SqlConnection(defaultConnectionString.ConnectionString))
            {
                connection.Open();
                try
                {
                    using (var q = new SqlCommand(
                        string.Format(CreateDatabaseQueryText, databaseName),
                        connection
                        ))
                    {
                        q.ExecuteNonQuery();
                    }

                    using (var q = new SqlCommand(
                        string.Format(ModifyDatabaseQueryText, databaseName),
                        connection))
                    {
                        q.ExecuteNonQuery();
                    }
                }
                finally
                {
                    connection.Close();
                }
            }
        }
Exemplo n.º 3
0
        public void TestSaveItem2()
        {
            var connectionString = new SqlServerDatabaseConnectionString(
                Instance.DatabasePath,
                TestDatabaseName,
                true
                );


            var child0 = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName:Inner0"),
                StringGenerator.GetString("MethodName:Inner0"),
                DateTime.Now.AddSeconds(1),
                10,
                StringGenerator.GetString("CreationStack:Inner0")
                );

            var child1 = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName:Inner1"),
                StringGenerator.GetString("MethodName:Inner1"),
                DateTime.Now.AddSeconds(2),
                50,
                StringGenerator.GetString("CreationStack:Inner1")
                );

            var record = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName"),
                StringGenerator.GetString("MethodName"),
                DateTime.Now,
                100,
                StringGenerator.GetString("CreationStack"),
                new List<IPerformanceRecordData>
                {
                    child0,
                    child1
                }
                );

            var dataExists0 = false;
            var dataExists1 = false;
            var dataExists2 = false;

            using (var itemSaver = new SqlBatchItemSaverFactory(
                new EmptyLoggerAdapter(), 
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new BatchBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {
                    ebSaver.Save(record);
                }
            }

            using (var connection = new SqlConnection(connectionString.ConnectionString))
            {
                connection.Open();

                var commandText = string.Format(@"
select
    *
from [{0}].dbo.[{1}] l
join [{0}].dbo.[{1}Stack] s on l.id_stack = s.id
order by
    l.id asc", TrnHelper.MainDatabaseName, TableName);

                using (var command = new SqlCommand(commandText, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        var index = 0;

                        while (reader.Read())
                        {
                            switch (index)
                            {
                                case 0:
                                    Assert.IsTrue(record.CheckEqualityFor(0, reader));
                                    dataExists0 = true;
                                    break;
                                case 1:
                                    Assert.IsTrue(child0.CheckEqualityFor(1, reader));
                                    dataExists1 = true;
                                    break;
                                case 2:
                                    Assert.IsTrue(child1.CheckEqualityFor(1, reader));
                                    dataExists2 = true;
                                    break;
                                default:
                                    Assert.Fail();
                                    break;
                            }

                            index++;
                        }
                    }
                }
            }

            Assert.IsTrue(dataExists0);
            Assert.IsTrue(dataExists1);
            Assert.IsTrue(dataExists2);
        }
Exemplo n.º 4
0
        public void TestSaveItem1()
        {
            var connectionString = new SqlServerDatabaseConnectionString(
                Instance.DatabasePath,
                TestDatabaseName,
                true
                );

            var record = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName"),
                StringGenerator.GetString("MethodName"),
                DateTime.Now,
                100,
                StringGenerator.GetString("CreationStack"),
                new Exception(StringGenerator.GetString("ExceptionMessage"))
                );

            using (var itemSaver = new SqlBatchItemSaverFactory(
                new EmptyLoggerAdapter(), 
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new BatchBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {
                    ebSaver.Save(record);
                }
            }

            var dataExists = false;

            using (var connection = new SqlConnection(connectionString.ConnectionString))
            {
                connection.Open();

                var commandText = string.Format(@"
select top(1)
    *
from [{0}].dbo.[{1}] l
join [{0}].dbo.[{1}Stack] s on l.id_stack = s.id
order by
    l.id desc", TrnHelper.MainDatabaseName, TableName);

                using (var command = new SqlCommand(commandText, connection))
                {
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Assert.IsTrue(record.CheckEqualityFor(0, reader));

                            dataExists = true;
                        }
                    }
                }
            }

            Assert.IsTrue(dataExists);
        }
Exemplo n.º 5
0
        public void TwoTestWithSameDatabase()
        {
            var connectionString = new SqlServerDatabaseConnectionString(
                Instance.DatabasePath,
                TestDatabaseName,
                true
                );

            var record0 = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName0"),
                StringGenerator.GetString("MethodName0"),
                DateTime.Now,
                100,
                StringGenerator.GetString("CreationStack0")
                );

            var record1 = new TestPerformanceRecordData(
                StringGenerator.GetString("ClassName1"),
                StringGenerator.GetString("MethodName1"),
                DateTime.Now,
                200,
                StringGenerator.GetString("CreationStack1")
                );

            using (var itemSaver = new SqlBatchItemSaverFactory(
                new EmptyLoggerAdapter(),
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new BatchBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {
                }

            }

            using (var itemSaver = new SqlBatchItemSaverFactory(
                new EmptyLoggerAdapter(),
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new BatchBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {
                    ebSaver.Save(record1);
                }

            }
        }
Exemplo n.º 6
0
        public void TestPreparationLogic()
        {
            var connectionString = new SqlServerDatabaseConnectionString(
                Instance.DatabasePath,
                TestDatabaseName,
                true
                );

            using (var itemSaver = new SqlCommandItemSaverFactory(
                new EmptyLoggerAdapter(), 
                connectionString.ConnectionString,
                TrnHelper.MainDatabaseName,
                TableName,
                1000000L
                ))
            {
                using (var ebSaver = new EventBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {

                }

                using (var ebSaver = new EventBasedSaver(
                    itemSaver,
                    new EmptyLoggerAdapter(),
                    false
                    ))
                {

                }
            }
        }
Exemplo n.º 7
0
        public void RestoreDatabase(
            string databasePath,
            string databaseName,
            string pathToTrnFile,
            string defaultDatabaseName
            )
        {
            if (databasePath == null)
            {
                throw new ArgumentNullException("databasePath");
            }
            if (databaseName == null)
            {
                throw new ArgumentNullException("databaseName");
            }
            if (pathToTrnFile == null)
            {
                throw new ArgumentNullException("pathToTrnFile");
            }
            if (defaultDatabaseName == null)
            {
                throw new ArgumentNullException("defaultDatabaseName");
            }

            var systemTemp = Environment.GetEnvironmentVariable("TEMP", EnvironmentVariableTarget.Machine);

            var pathToMdfFile =
                Path.Combine(
                    systemTemp,
                    Path.GetRandomFileName() + ".mdf"
                    );
            var pathToLdfFile =
                Path.Combine(
                    systemTemp,
                    Path.GetRandomFileName() + ".ldf"
                    );

            //формируем строку подключения
            var defaultConnectionString = new SqlServerDatabaseConnectionString(
                databasePath,
                string.Empty
                );

            //создаем базу
            using (var connection = new SqlConnection(defaultConnectionString.ConnectionString))
            {
                connection.Open();
                try
                {
                    using (var q = new SqlCommand(
                        string.Format(
                            RestoreDatabaseQueryText,
                            databaseName,
                            pathToTrnFile,
                            defaultDatabaseName,
                            pathToMdfFile,
                            pathToLdfFile
                            ),
                        connection
                        ))
                    {
                        q.ExecuteNonQuery();
                    }
                }
                finally
                {
                    connection.Close();
                }
            }
        }