Ejemplo n.º 1
0
 public void CheckNoOps()
 {
     using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
     {
         string sqlScript = "GO";
         var    batches   = parserWrapper.GetBatches(sqlScript);
         Assert.Equal(0, batches.Count);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructor for a query
        /// </summary>
        /// <param name="queryText">The text of the query to execute</param>
        /// <param name="connection">The information of the connection to use to execute the query</param>
        /// <param name="settings">Settings for how to execute the query, from the user</param>
        /// <param name="outputFactory">Factory for creating output files</param>
        public Query(
            string queryText,
            ConnectionInfo connection,
            QueryExecutionSettings settings,
            IFileStreamFactory outputFactory,
            bool getFullColumnSchema    = false,
            bool applyExecutionSettings = false)
        {
            // Sanity check for input
            Validate.IsNotNull(nameof(queryText), queryText);
            Validate.IsNotNull(nameof(connection), connection);
            Validate.IsNotNull(nameof(settings), settings);
            Validate.IsNotNull(nameof(outputFactory), outputFactory);

            // Initialize the internal state
            QueryText          = queryText;
            editorConnection   = connection;
            cancellationSource = new CancellationTokenSource();

            // Process the query into batches
            BatchParserWrapper        parser     = new BatchParserWrapper();
            ExecutionEngineConditions conditions = null;

            if (settings.IsSqlCmdMode)
            {
                conditions = new ExecutionEngineConditions()
                {
                    IsSqlCmd = settings.IsSqlCmdMode
                };
            }
            List <BatchDefinition> parserResult = parser.GetBatches(queryText, conditions);

            var batchSelection = parserResult
                                 .Select((batchDefinition, index) =>
                                         new Batch(batchDefinition.BatchText,
                                                   new SelectionData(
                                                       batchDefinition.StartLine - 1,
                                                       batchDefinition.StartColumn - 1,
                                                       batchDefinition.EndLine - 1,
                                                       batchDefinition.EndColumn - 1),
                                                   index, outputFactory,
                                                   batchDefinition.SqlCmdCommand,
                                                   batchDefinition.BatchExecutionCount,
                                                   getFullColumnSchema));

            Batches = batchSelection.ToArray();

            // Create our batch lists
            BeforeBatches = new List <Batch>();
            AfterBatches  = new List <Batch>();

            if (applyExecutionSettings)
            {
                ApplyExecutionSettings(connection, settings, outputFactory);
            }
        }
Ejemplo n.º 3
0
 public void CheckSQLBatchStatementWithRepeatExecution()
 {
     using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
     {
         string sqlScript = "select * from sys.object" + Environment.NewLine + "GO 2";
         var    batches   = parserWrapper.GetBatches(sqlScript);
         Assert.Equal(1, batches.Count);
         BatchDefinition batch = batches[0];
         Assert.Equal(2, batch.BatchExecutionCount);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Constructor for a query
        /// </summary>
        /// <param name="queryText">The text of the query to execute</param>
        /// <param name="connection">The information of the connection to use to execute the query</param>
        /// <param name="settings">Settings for how to execute the query, from the user</param>
        /// <param name="outputFactory">Factory for creating output files</param>
        public Query(string queryText, ConnectionInfo connection, QueryExecutionSettings settings, IFileStreamFactory outputFactory, bool getFullColumnSchema = false)
        {
            // Sanity check for input
            Validate.IsNotNull(nameof(queryText), queryText);
            Validate.IsNotNull(nameof(connection), connection);
            Validate.IsNotNull(nameof(settings), settings);
            Validate.IsNotNull(nameof(outputFactory), outputFactory);

            // Initialize the internal state
            QueryText          = queryText;
            editorConnection   = connection;
            cancellationSource = new CancellationTokenSource();

            // Process the query into batches
            BatchParserWrapper     parser       = new BatchParserWrapper();
            List <BatchDefinition> parserResult = parser.GetBatches(queryText);

            var batchSelection = parserResult
                                 .Select((batchDefinition, index) =>
                                         new Batch(batchDefinition.BatchText,
                                                   new SelectionData(
                                                       batchDefinition.StartLine - 1,
                                                       batchDefinition.StartColumn - 1,
                                                       batchDefinition.EndLine - 1,
                                                       batchDefinition.EndColumn - 1),
                                                   index, outputFactory,
                                                   batchDefinition.BatchExecutionCount,
                                                   getFullColumnSchema));

            Batches = batchSelection.ToArray();

            // Create our batch lists
            BeforeBatches = new List <Batch>();
            AfterBatches  = new List <Batch>();

            if (DoesSupportExecutionPlan(connection))
            {
                // Checking settings for execution plan options
                if (settings.ExecutionPlanOptions.IncludeEstimatedExecutionPlanXml)
                {
                    // Enable set showplan xml
                    AddBatch(string.Format(SetShowPlanXml, On), BeforeBatches, outputFactory);
                    AddBatch(string.Format(SetShowPlanXml, Off), AfterBatches, outputFactory);
                }
                else if (settings.ExecutionPlanOptions.IncludeActualExecutionPlanXml)
                {
                    AddBatch(string.Format(SetStatisticsXml, On), BeforeBatches, outputFactory);
                    AddBatch(string.Format(SetStatisticsXml, Off), AfterBatches, outputFactory);
                }
            }
        }
Ejemplo n.º 5
0
 public void CheckComment()
 {
     using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
     {
         string sqlScript = "-- this is a comment --";
         var    batches   = parserWrapper.GetBatches(sqlScript);
         Assert.Equal(1, batches.Count);
         BatchDefinition batch = batches[0];
         Assert.Equal(sqlScript, batch.BatchText);
         Assert.Equal(1, batch.StartLine);
         Assert.Equal(1, batch.StartColumn);
         Assert.Equal(2, batch.EndLine);
         Assert.Equal(sqlScript.Length + 1, batch.EndColumn);
     }
 }
 public void CheckSimpleSingleSQLBatchStatement()
 {
     using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
     {
         string sqlScript = "select * from sys.objects";
         var    batches   = parserWrapper.GetBatches(sqlScript);
         Assert.Equal(1, batches.Count);
         BatchDefinition batch = batches[0];
         Assert.Equal(sqlScript, batch.BatchText);
         Assert.Equal(1, batch.StartLine);
         Assert.Equal(1, batch.StartColumn);
         Assert.Equal(2, batch.EndLine);
         Assert.Equal(sqlScript.Length, batch.EndColumn);
     }
 }
Ejemplo n.º 7
0
 public void CheckSimpleMultipleQLBatchStatement()
 {
     using (BatchParserWrapper parserWrapper = new BatchParserWrapper())
     {
         string sqlScript = @"SELECT 'FirstLine';
             GO
             SELECT 'MiddleLine_1';
             GO
             SELECT 'MiddleLine_1'
             GO
             SELECT 'LastLine'";
         var    batches   = parserWrapper.GetBatches(sqlScript);
         // Each select statement is one batch , so we are expecting 4 batches.
         Assert.Equal(4, batches.Count);
     }
 }
Ejemplo n.º 8
0
 public BatchParserWrapperTests()
 {
     parserWrapper = new BatchParserWrapper();
 }