public ComparedEntity Create(string representation) { if (representation == null) { Logger.Error($"{nameof(representation)} is null"); throw new ArgumentNullException(nameof(representation)); } var parsedFragment = _fragmentFactory.CreateFragment(representation); var result = new ComparedEntity { Tree = parsedFragment.Fragment, ParseErrors = parsedFragment.ParseErrors, Representation = representation }; var symbolVisitor = new ProcedureObjectNameVisitor(result.Identifier); result.Tree.Accept(symbolVisitor); return(result); }
private ComparisonResult Compare(CompareOptions compareOptions, ComparedEntity entity1, ComparedEntity entity2) { var comparisonResult = new ComparisonResult { LeftEntity = entity1, RightEntity = entity2, CompareOptions = compareOptions }; var entity1Tokens = entity1.Tree.ScriptTokenStream.AsEnumerable(); var entity2Tokens = entity2.Tree.ScriptTokenStream.AsEnumerable(); if (compareOptions.IgnoreWhitespace) { entity1Tokens = entity1Tokens.Where(x => x.TokenType != TSqlTokenType.WhiteSpace); entity2Tokens = entity2Tokens.Where(x => x.TokenType != TSqlTokenType.WhiteSpace); } if (compareOptions.IgnoreEndOfFile) { entity1Tokens = entity1Tokens.Where(x => x.TokenType != TSqlTokenType.EndOfFile); entity2Tokens = entity2Tokens.Where(x => x.TokenType != TSqlTokenType.EndOfFile); } // Important: only windows newlines so far. Add others too // Newlines are just whitespace but with the newline characters if (compareOptions.IgnoreNewLines) { entity1Tokens = entity1Tokens.Where(x => x.TokenType != TSqlTokenType.WhiteSpace && x.Text != "\r\n"); entity2Tokens = entity2Tokens.Where(x => x.TokenType != TSqlTokenType.WhiteSpace && x.Text != "\r\n"); } var areEqual = AreEqual( entity1Tokens, entity2Tokens, comparisonResult.LinesDifferingInLeftEntity, comparisonResult.LinesDifferingInRightEntity, compareOptions); comparisonResult.Outcome = areEqual ? ComparisonOutcome.Equal : ComparisonOutcome.Different; // Re-using the original ScriptTokenStream so we don't use the filtered stream based on compareOptions var entity1LineByLine = entity1.Tree.ScriptTokenStream.GroupBy(x => x.Line); foreach (var line in entity1LineByLine) { comparisonResult.LeftSourceTreeByLine.Add(line.Key, string.Join(string.Empty, line.Select(x => x.Text))); } var entity2LineByLine = entity2.Tree.ScriptTokenStream.GroupBy(x => x.Line); foreach (var line in entity2LineByLine) { comparisonResult.RightSourceTreeByLine.Add(line.Key, string.Join(string.Empty, line.Select(x => x.Text))); } // Done after the actual line-by-line check to make sure it doesn't get overwritten if (string.IsNullOrWhiteSpace(entity1.Representation) || string.IsNullOrWhiteSpace(entity2.Representation)) { comparisonResult.Outcome = ComparisonOutcome.Missing; } return(comparisonResult); }