Exemple #1
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);
     }
 }
Exemple #2
0
        public void CheckComment()
        {
            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, batch.EndColumn);
        }
Exemple #3
0
        public void CheckSimpleSingleSQLBatchStatement()
        {
            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);
        }
Exemple #4
0
 public Batch(string fname, Config cfg)
 {
     chnd = cfg;
     if (File.Exists(fname))
     {
         FileStream input = File.OpenRead(fname);
         Console.WriteLine("+ processing batch file: {0}", fname);
         DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings {
             UseSimpleDictionaryFormat = true
         };
         var serializer = new DataContractJsonSerializer(typeof(BatchDefinition), settings);
         bdef = serializer.ReadObject(input) as BatchDefinition;
         foreach (KeyValuePair <string, string> item in bdef.options)
         {
             Console.WriteLine("~ batch option: {0} {1}", item.Key, item.Value);
         }
     }
 }
        /// <summary>
        /// Helper method used to Convert line/column information in a file to offset
        /// </summary>
        private static List <BatchDefinition> ConvertToBatchDefinitionList(string content, List <BatchInfo> batchInfos)
        {
            List <BatchDefinition> batchDefinitionList = new List <BatchDefinition>();

            if (batchInfos.Count == 0)
            {
                return(batchDefinitionList);
            }
            List <int> offsets = GetOffsets(content, batchInfos);

            if (!string.IsNullOrEmpty(content) && (batchInfos.Count > 0))
            {
                // Instantiate a string reader for the whole sql content
                using (StringReader reader = new StringReader(content))
                {
                    // Generate the first batch definition list
                    int    startLine      = batchInfos[0].startLine + 1; //positions is 0 index based
                    int    endLine        = startLine;
                    int    lineDifference = 0;
                    int    endColumn;
                    int    offset      = offsets[0];
                    int    startColumn = batchInfos[0].startColumn;
                    int    count       = batchInfos.Count;
                    string batchText   = batchInfos[0].batchText;

                    // if there's only one batch then the line difference is just startLine
                    if (count > 1)
                    {
                        lineDifference = batchInfos[1].startLine - batchInfos[0].startLine;
                    }

                    // get endLine, endColumn for the current batch and the lineStartOffset for the next batch
                    var position = ReadLines(reader, lineDifference, endLine);
                    endLine   = position.Item1;
                    endColumn = position.Item2;

                    // create a new BatchDefinition and add it to the list
                    BatchDefinition batchDef = new BatchDefinition(
                        batchText,
                        startLine,
                        endLine,
                        startColumn + 1,
                        endColumn + 1,
                        batchInfos[0].executionCount,
                        batchInfos[0].sqlCmdCommand
                        );

                    batchDefinitionList.Add(batchDef);

                    if (count > 1)
                    {
                        offset = offsets[1] + batchInfos[0].startColumn;
                    }

                    // Generate the rest batch definitions
                    for (int index = 1; index < count - 1; index++)
                    {
                        lineDifference = batchInfos[index + 1].startLine - batchInfos[index].startLine;
                        position       = ReadLines(reader, lineDifference, endLine);
                        endLine        = position.Item1;
                        endColumn      = position.Item2;
                        offset         = offsets[index];
                        batchText      = batchInfos[index].batchText;
                        startLine      = batchInfos[index].startLine + 1; //positions is 0 index based
                        startColumn    = batchInfos[index].startColumn;

                        // make a new batch definition for each batch
                        BatchDefinition batch = new BatchDefinition(
                            batchText,
                            startLine,
                            endLine,
                            startColumn + 1,
                            endColumn + 1,
                            batchInfos[index].executionCount,
                            batchInfos[index].sqlCmdCommand
                            );
                        batchDefinitionList.Add(batch);
                    }

                    // if there is only one batch then that was the last one anyway
                    if (count > 1)
                    {
                        batchText = batchInfos[count - 1].batchText;
                        BatchDefinition lastBatchDef = GetLastBatchDefinition(reader, batchInfos[count - 1], batchText);
                        batchDefinitionList.Add(lastBatchDef);
                    }
                }
            }
            return(batchDefinitionList);
        }
Exemple #6
0
 public BatchDefinition GetBatchDefinition(BatchDefinition batchDefinition)
 {
     throw new System.NotImplementedException();
 }
Exemple #7
0
        /// <summary>
        /// Helper method used to Convert line/column information in a file to offset
        /// </summary>
        private static List <BatchDefinition> ConvertToBatchDefinitionList(string content,
                                                                           IList <Tuple <int, int> > positions, List <int> lengths)
        {
            List <BatchDefinition> batchDefinitionList = new List <BatchDefinition>();

            if (positions.Count == 0 && lengths.Count == 0)
            {
                return(batchDefinitionList);
            }
            List <int> offsets = GetOffsets(content, positions);

            if (!string.IsNullOrEmpty(content) && (positions.Count > 0))
            {
                // Instantiate a string reader for the whole sql content
                using (StringReader reader = new StringReader(content))
                {
                    // Generate the first batch definition list
                    int    startLine      = positions[0].Item1 + 1; //positions is 0 index based
                    int    endLine        = startLine;
                    int    lineDifference = startLine - 1;
                    int    endColumn;
                    int    offset      = offsets[0];
                    int    startColumn = positions[0].Item2;
                    int    count       = positions.Count;
                    string batchText   = content.Substring(offset, lengths[0]);

                    // if there's only one batch then the line difference is just startLine
                    if (count > 1)
                    {
                        lineDifference = positions[1].Item1 - positions[0].Item1;
                    }

                    // get endLine, endColumn for the current batch and the lineStartOffset for the next batch
                    var batchInfo = ReadLines(reader, lineDifference, endLine);
                    endLine   = batchInfo.Item1;
                    endColumn = batchInfo.Item2;

                    // create a new BatchDefinition and add it to the list
                    BatchDefinition batchDef = new BatchDefinition(
                        batchText,
                        startLine,
                        endLine,
                        startColumn + 1,
                        endColumn
                        );

                    batchDefinitionList.Add(batchDef);

                    if (count > 1)
                    {
                        offset = offsets[1] + positions[0].Item2;
                    }

                    // Generate the rest batch definitions
                    for (int index = 1; index < count - 1; index++)
                    {
                        lineDifference = positions[index + 1].Item1 - positions[index].Item1;
                        batchInfo      = ReadLines(reader, lineDifference, endLine);
                        endLine        = batchInfo.Item1;
                        endColumn      = batchInfo.Item2;
                        offset         = offsets[index];
                        batchText      = content.Substring(offset, lengths[index]);
                        startLine      = positions[index].Item1;
                        startColumn    = positions[index].Item2;

                        // make a new batch definition for each batch
                        BatchDefinition batch = new BatchDefinition(
                            batchText,
                            startLine,
                            endLine,
                            startColumn + 1,
                            endColumn
                            );
                        batchDefinitionList.Add(batch);
                    }

                    // if there is only one batch then that was the last one anyway
                    if (count > 1)
                    {
                        batchText = content.Substring(offsets[count - 1], lengths[count - 1]);
                        BatchDefinition lastBatchDef = GetLastBatchDefinition(reader, positions[count - 1], batchText);
                        batchDefinitionList.Add(lastBatchDef);
                    }
                }
            }
            return(batchDefinitionList);
        }