public void ExecuteStatement(ExecScope scope = ExecScope.Block) { if (!CanExecute()) { return; } SaveActiveAndAnchorPoints(); if (!(document.Selection as TextSelection).IsEmpty) { Exec(); } else { var script = GetDocumentText(); var caretPoint = GetCaretPoint(); bool success = ParseSqlFragments(script, out TSqlScript sqlScript); if (success) { TextBlock currentStatement = null; foreach (var batch in sqlScript?.Batches) { currentStatement = FindCurrentStatement(batch.Statements, caretPoint, scope); if (currentStatement != null) { break; } } if (currentStatement != null) { // select the statement to be executed MakeSelection(currentStatement.StartPoint, currentStatement.EndPoint); // execute the statement Exec(); // restore selection RestoreActiveAndAnchorPoints(); } } else { // there are syntax errors // execute anyway to show the errors Exec(); } } }
private TextBlock FindCurrentStatement(IList <TSqlStatement> statements, VirtualPoint caret, ExecScope scope) { if (statements == null || statements.Count == 0) { return(null); } foreach (var statement in statements) { if (scope == ExecScope.Inner) { IList <TSqlStatement> statementList = GetInnerStatements(statement); TextBlock currentStatement = FindCurrentStatement(statementList, caret, scope); if (currentStatement != null) { return(currentStatement); } } if (IsCaretInsideStatement(statement, caret)) { return(GetTextBlockFromStatement(statement)); } } return(null); }