BatchParserAction ICommandHandler.Go(TextBlock batch, int repeatCount, SqlCmdCommand tokenType)
 {
     batch.GetText(true, out string batchText, out LineInfo lineInfo);
     _scriptBuilder.AppendLine(batchText);
     _scriptBuilder.AppendLine(BATCH_SEPARATOR);
     return(BatchParserAction.Continue);
 }
Example #2
0
        /// <summary>
        /// Executes a given batch given the number of times
        /// </summary>
        /// <param name="batchScript"></param>
        /// <param name="num"></param>
        /// <param name="lineNumber"></param>
        /// <returns>True if we should continue processing, false otherwise</returns>
        private bool ExecuteBatchInternal(
            string batchScript,
            int num,
            int lineNumber,
            SqlCmdCommand sqlCmdCommand)
        {
            if (lineNumber == -1)
            {
                //it means that there was not a single sqlcmd command,
                //including Batch Delimiter (i.e.) "GO" at the end of the batch.
                //it should be adjusted it to be the very first line in this case
                lineNumber = 0;
            }

            TextSpan localTextSpan = new TextSpan();

            localTextSpan.iStartLine = lineNumber;

            if (!String.IsNullOrEmpty(batchScript))
            {
                bool continueProcessing = true;
                numBatchExecutionTimes = num;
                ExecuteBatchTextSpanInternal(batchScript, localTextSpan, out continueProcessing, sqlCmdCommand);
                return(continueProcessing);
            }
            else
            {
                return(true);
            }
        }
 /// <summary>
 /// Constructor method for a BatchDefinition
 /// </summary>
 public BatchDefinition(string batchText, int startLine, int endLine, int startColumn, int endColumn, int executionCount, SqlCmdCommand command)
 {
     BatchText   = batchText;
     StartLine   = startLine;
     EndLine     = endLine;
     StartColumn = startColumn;
     EndColumn   = endColumn;
     // set the batch execution count, with min value of 1
     BatchExecutionCount = executionCount > 0 ? executionCount : 1;
     SqlCmdCommand       = command;
 }
        /// <summary>
        /// Take approptiate action on the parsed batches
        /// </summary>
        public BatchParserAction Go(TextBlock batch, int repeatCount, SqlCmdCommand command)
        {
            string   str;
            LineInfo lineInfo;

            batch.GetText(!variableSubstitutionDisabled, out str, out lineInfo);

            bool executeResult = false;

            if (executeDelegate != null)
            {
                executeResult = executeDelegate(str, repeatCount, lineInfo.GetStreamPositionForOffset(0).Line + startingLine - 1, command);
            }
            return(executeResult ? BatchParserAction.Continue : BatchParserAction.Abort);
        }
Example #5
0
        public BatchParserAction Go(TextBlock batch, int repeatCount, SqlCmdCommand command)
        {
            string   textWithVariablesResolved;
            string   textWithVariablesUnresolved;
            LineInfo lineInfoVarsResolved;
            LineInfo lineInfoVarsUnresolved;

            batch.GetText(true, out textWithVariablesResolved, out lineInfoVarsResolved);
            batch.GetText(false, out textWithVariablesUnresolved, out lineInfoVarsUnresolved);
            outputString.AppendFormat(CultureInfo.InvariantCulture, "*** Execute batch ({0})\n", repeatCount);

            if (string.Compare(textWithVariablesUnresolved, textWithVariablesResolved, StringComparison.Ordinal) != 0)
            {
                outputString.AppendLine("Text with variables not resolved:");
                outputString.AppendLine(textWithVariablesResolved);
                outputString.AppendLine("Text with variables not resolved:");
                outputString.AppendLine(textWithVariablesUnresolved);
                int i = 0;
                outputString.AppendLine("Mapping from resolved string to unresolved:");
                while (i <= textWithVariablesResolved.Length)
                {
                    PositionStruct pos       = lineInfoVarsResolved.GetStreamPositionForOffset(i);
                    string         character = i < textWithVariablesResolved.Length ? ("" + textWithVariablesResolved[i]).Replace("\n", @"\n").Replace("\r", @"\r") : "EOF";
                    outputString.AppendFormat(CultureInfo.InvariantCulture, "Pos [{0}] {1}:{2} \"{3}\"",
                                              i,
                                              BatchParserTests.GetFilenameOnly(pos.Filename),
                                              pos.Offset,
                                              character);
                    outputString.AppendLine();
                    i++;
                }
            }
            else
            {
                outputString.AppendLine("Batch text:");
                outputString.AppendLine(textWithVariablesUnresolved);
            }
            outputString.AppendLine();
            return(BatchParserAction.Continue);
        }
Example #6
0
        /// <summary>
        /// Executes the batch text given the text span
        /// </summary>
        /// <param name="batchScript"></param>
        /// <param name="textSpan"></param>
        /// <param name="continueProcessing"></param>
        private void ExecuteBatchTextSpanInternal(string batchScript, TextSpan textSpan, out bool continueProcessing, SqlCmdCommand sqlCmdCommand)
        {
            Debug.Assert(!String.IsNullOrEmpty(batchScript));
            continueProcessing = true;

            if (batchScript.Trim().Length <= 0)
            {
                result |= ScriptExecutionResult.Success;
                return;
            }

            Debug.Assert(currentBatch != null);

            if (executionState == ExecutionState.Cancelling)
            {
                result = ScriptExecutionResult.Cancel;
            }
            else
            {
                currentBatch.Reset();
                currentBatch.Text                   = batchScript;
                currentBatch.TextSpan               = textSpan;
                currentBatch.BatchIndex             = currentBatchIndex;
                currentBatch.ExpectedExecutionCount = numBatchExecutionTimes;

                currentBatchIndex++;

                if (conditions != null)
                {
                    currentBatch.IsSuppressProviderMessageHeaders = conditions.IsSuppressProviderMessageHeaders;

                    // TODO this is associated with Dacfx specific situations, so uncomment if need be
                    //currentBatch.IsScriptExecutionTracked = conditions.IsScriptExecutionTracked;
                    if (conditions.IsScriptExecutionTracked)
                    {
                        currentBatch.ScriptTrackingId = scriptTrackingId++;
                    }
                }

                //ExecutingBatch state means currentBatch is valid to use from another thread to Cancel
                executionState = ExecutionState.ExecutingBatch;
            }

            ScriptExecutionResult batchResult = ScriptExecutionResult.Failure;

            if (result != ScriptExecutionResult.Cancel)
            {
                bool isExecutionDiscarded = false;
                try
                {
                    RaiseBatchParserExecutionStarted(currentBatch, textSpan);

                    if (!isLocalParse)
                    {
                        batchResult = DoBatchExecution(currentBatch);
                    }
                    else
                    {
                        batchResult = ScriptExecutionResult.Success;
                    }
                }
                finally
                {
                    isExecutionDiscarded = (executionState == ExecutionState.Discarded);
                    if (executionState == ExecutionState.Cancelling || isExecutionDiscarded)
                    {
                        batchResult = ScriptExecutionResult.Cancel;
                    }
                    else
                    {
                        executionState = ExecutionState.Executing;
                    }
                }

                if (!isExecutionDiscarded)
                {
                    RaiseBatchParserExecutionFinished(currentBatch, batchResult, sqlCmdCommand);
                }
            }
            else
            {
                batchResult = ScriptExecutionResult.Cancel;
            }

            //if we're in Cancel or Halt state, do some special actions
            if (batchResult == ScriptExecutionResult.Cancel || batchResult == ScriptExecutionResult.Halted)
            {
                result             = batchResult;
                continueProcessing = false;
                return;
            }
            else
            {
                result |= batchResult;
            }
        }
Example #7
0
        /// <summary>
        /// Called just after batch has been executed
        /// </summary>
        /// <param name="batch"></param>
        /// <param name="batchResult"></param>
        private void RaiseBatchParserExecutionFinished(Batch batch, ScriptExecutionResult batchResult, SqlCmdCommand sqlCmdCommand)
        {
            Debug.Assert(batch != null);

            EventHandler <BatchParserExecutionFinishedEventArgs> cache = BatchParserExecutionFinished;

            if (cache != null)
            {
                BatchParserExecutionFinishedEventArgs args = new BatchParserExecutionFinishedEventArgs(batchResult, batch, sqlCmdCommand);
                cache(this, args);
            }
        }
Example #8
0
 internal Batch(string batchText, SelectionData selection, int ordinalId,
                IFileStreamFactory outputFileFactory, SqlCmdCommand sqlCmdCommand, int executionCount = 1, bool getFullColumnSchema = false) : this(batchText, selection, ordinalId,
                                                                                                                                                    outputFileFactory, executionCount, getFullColumnSchema)
 {
     this.SqlCmdCommand = sqlCmdCommand;
 }
 /// <summary>
 /// Constructor method for the class
 /// </summary>
 public BatchParserExecutionFinishedEventArgs(ScriptExecutionResult batchResult, Batch batch, SqlCmdCommand sqlCmdCommand)
 {
     this.batch         = batch;
     result             = batchResult;
     this.sqlCmdCommand = sqlCmdCommand;
 }