Beispiel #1
0
        public void ReliableConnectionHelperTest()
        {
            using (SelfCleaningTempFile queryTempFile = new SelfCleaningTempFile())
            {
                var            result     = LiveConnectionHelper.InitLiveConnectionInfo(null, queryTempFile.FilePath);
                ConnectionInfo connInfo   = result.ConnectionInfo;
                DbConnection   connection = connInfo.ConnectionTypeToConnectionMap[ConnectionType.Default];
                connection.Open();

                Assert.True(connection.State == ConnectionState.Open, "Connection should be open.");
                Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(connection));

                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(builder, null));
                ReliableConnectionHelper.TryAddAlwaysOnConnectionProperties(builder, new SqlConnectionStringBuilder());

                Assert.NotNull(ReliableConnectionHelper.GetServerName(connection));
                Assert.NotNull(ReliableConnectionHelper.ReadServerVersion(connection));

                Assert.NotNull(ReliableConnectionHelper.GetAsSqlConnection(connection));

                ReliableConnectionHelper.ServerInfo info = ReliableConnectionHelper.GetServerVersion(connection);
                Assert.NotNull(ReliableConnectionHelper.IsVersionGreaterThan2012RTM(info));
            }
        }
        public void ReliableConnectionHelperTest()
        {
            ScriptFile     scriptFile;
            ConnectionInfo connInfo = TestObjects.InitLiveConnectionInfo(out scriptFile);

            Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(connInfo.SqlConnection));

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

            Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(builder));
            ReliableConnectionHelper.TryAddAlwaysOnConnectionProperties(builder, new SqlConnectionStringBuilder());

            Assert.NotNull(ReliableConnectionHelper.GetServerName(connInfo.SqlConnection));
            Assert.NotNull(ReliableConnectionHelper.ReadServerVersion(connInfo.SqlConnection));

            Assert.NotNull(ReliableConnectionHelper.GetAsSqlConnection(connInfo.SqlConnection));

            ServerInfo info = ReliableConnectionHelper.GetServerVersion(connInfo.SqlConnection);

            Assert.NotNull(ReliableConnectionHelper.IsVersionGreaterThan2012RTM(info));
        }
        public void ReliableConnectionHelperTest()
        {
            var            result     = LiveConnectionHelper.InitLiveConnectionInfo();
            ConnectionInfo connInfo   = result.ConnectionInfo;
            DbConnection   connection = connInfo.ConnectionTypeToConnectionMap[ConnectionType.Default];


            Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(connection));

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

            Assert.True(ReliableConnectionHelper.IsAuthenticatingDatabaseMaster(builder));
            ReliableConnectionHelper.TryAddAlwaysOnConnectionProperties(builder, new SqlConnectionStringBuilder());

            Assert.NotNull(ReliableConnectionHelper.GetServerName(connection));
            Assert.NotNull(ReliableConnectionHelper.ReadServerVersion(connection));

            Assert.NotNull(ReliableConnectionHelper.GetAsSqlConnection(connection));

            ReliableConnectionHelper.ServerInfo info = ReliableConnectionHelper.GetServerVersion(connection);
            Assert.NotNull(ReliableConnectionHelper.IsVersionGreaterThan2012RTM(info));
        }
Beispiel #4
0
        /// <summary>
        /// Create a set of batches to be executed before and after the script is executed
        /// </summary>
        /// <remarks>
        /// This is the way some server side settings can be set. Additionally, it supports
        /// a way to wrap the script execution within a transaction block
        /// </remarks>
        private void CreatePrePostConditionBatches()
        {
            StringBuilder scriptPreBatches  = new StringBuilder();
            StringBuilder scriptPostBatches = new StringBuilder();
            int           serverVersion     = 8;

            if (connection != null && connection.State == ConnectionState.Open)
            {
                serverVersion = new Version(ReliableConnectionHelper.ReadServerVersion(connection)).Major;
            }

            ConfigurePrePostConditionBatches(preConditionBatches);
            ConfigurePrePostConditionBatches(postConditionBatches);

            if (conditions.IsNoExec)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.NoExecStatement(false));
            }

            if (conditions.IsStatisticsIO)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsIOStatement(true));
                scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsIOStatement(false));
            }

            if (conditions.IsStatisticsTime)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsTimeStatement(true));
                scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsTimeStatement(false));
            }

            if (conditions.IsEstimatedShowPlan)
            {
                if (serverVersion >= 9)
                {
                    scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ShowPlanXmlStatement(true));
                    scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ShowPlanXmlStatement(false));
                    expectedShowPlan = ShowPlanType.EstimatedXmlShowPlan;
                }
                else
                {
                    scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ShowPlanAllStatement(true));
                    scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ShowPlanAllStatement(false));
                    expectedShowPlan = ShowPlanType.EstimatedExecutionShowPlan;
                }
            }
            else if (conditions.IsActualShowPlan)
            {
                if (serverVersion >= 9)
                {
                    scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsXmlStatement(true));
                    scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsXmlStatement(false));
                    expectedShowPlan = ShowPlanType.ActualXmlShowPlan;
                }
                else
                {
                    scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsProfileStatement(true));
                    scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.StatisticsProfileStatement(false));
                    expectedShowPlan = ShowPlanType.ActualExecutionShowPlan;
                }
            }

            if (conditions.IsTransactionWrapped)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.BeginTransactionStatement);
                // issuing a Rollback or a Commit will depend on the script execution result
            }

            if (conditions.IsParseOnly)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ParseOnlyStatement(true));
                scriptPostBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.ParseOnlyStatement(false));
            }

            if (conditions.IsNoExec)
            {
                scriptPreBatches.AppendFormat(CultureInfo.InvariantCulture, "{0} ", ExecutionEngineConditions.NoExecStatement(true));
            }

            if (conditions.IsShowPlanText &&
                !conditions.IsEstimatedShowPlan &&
                !conditions.IsActualShowPlan)
            {
                // SET SHOWPLAN_TEXT cannot be used with other statements in the batch
                preConditionBatches.Insert(0,
                                           new Batch(
                                               string.Format(CultureInfo.CurrentCulture, "{0} ", ExecutionEngineConditions.ShowPlanTextStatement(true)),
                                               false,
                                               executionTimeout));

                postConditionBatches.Insert(0,
                                            new Batch(
                                                string.Format(CultureInfo.CurrentCulture, "{0} ", ExecutionEngineConditions.ShowPlanTextStatement(false)),
                                                false,
                                                executionTimeout));
            }

            string preBatches  = scriptPreBatches.ToString().Trim();
            string postBatches = scriptPostBatches.ToString().Trim();

            if (scriptPreBatches.Length > 0)
            {
                preConditionBatches.Add(new Batch(preBatches, false, executionTimeout));
            }

            if (scriptPostBatches.Length > 0)
            {
                postConditionBatches.Add(new Batch(postBatches, false, executionTimeout));
            }
        }