Exemplo n.º 1
0
        public void StandardFormatReparsingReformatting(string FileName)
        {
            string inputSQL = Utils.GetTestFileContent(FileName, Utils.INPUTSQLFOLDER);
            TSqlStandardFormatter _treeFormatter = GetFormatter("");
            ITokenList            tokenized      = _tokenizer.TokenizeSQL(inputSQL);
            Node   parsed    = _parser.ParseSQL(tokenized);
            string outputSQL = _treeFormatter.FormatSQLTree(parsed);

            var inputToSecondPass = outputSQL;

            if (inputToSecondPass.StartsWith(Utils.ERROR_FOUND_WARNING))
            {
                inputToSecondPass = inputToSecondPass.Replace(Utils.ERROR_FOUND_WARNING, "");
            }

            ITokenList tokenizedAgain = _tokenizer.TokenizeSQL(inputToSecondPass);
            Node       parsedAgain    = _parser.ParseSQL(tokenizedAgain);
            string     formattedAgain = _treeFormatter.FormatSQLTree(parsedAgain);

            if (!inputSQL.Contains(Utils.REFORMATTING_INCONSISTENCY_WARNING))
            {
                Assert.AreEqual(outputSQL, formattedAgain, "first-pass formatted vs reformatted");
                Utils.StripWhiteSpaceFromSqlTree(parsed);
                Utils.StripWhiteSpaceFromSqlTree(parsedAgain);
                Assert.AreEqual(parsed.ToXmlDoc().OuterXml.ToUpper(), parsedAgain.ToXmlDoc().OuterXml.ToUpper(), "first parse xml vs reparse xml");
            }
        }
 public void CheckThatReformattingOutputSqlYieldsSameSql()
 {
     foreach (string inputSQL in Utils.FolderTextFileIterator(InputDataFolder))
     {
         ITokenList  tokenized      = _tokenizer.TokenizeSQL(inputSQL);
         XmlDocument parsed         = _parser.ParseSQL(tokenized);
         string      outputSQL      = _treeFormatter.FormatSQLTree(parsed);
         ITokenList  tokenizedAgain = _tokenizer.TokenizeSQL(outputSQL);
         XmlDocument parsedAgain    = _parser.ParseSQL(tokenizedAgain);
         string      formattedAgain = _treeFormatter.FormatSQLTree(parsedAgain);
         if (!inputSQL.Contains("KNOWN SQL REFORMATTING INCONSISTENCY") && !inputSQL.Contains("THIS TEST FILE IS NOT VALID SQL"))
         {
             Assert.AreEqual <string>(outputSQL, formattedAgain, "reformatted SQL should be the same as first pass of formatting");
         }
     }
 }
Exemplo n.º 3
0
        internal static void FormatSql()
        {
            Gateway.SetCurrentLanguage(LangType.L_SQL);

            var text = GetCurrentText();

            var options = new TSqlStandardFormatterOptions
            {
                BreakJoinOnSections      = _settings.BreakJoinOnSections,
                ExpandBetweenConditions  = _settings.ExpandBetweenConditions,
                ExpandBooleanExpressions = _settings.ExpandBooleanExpressions,
                ExpandCaseStatements     = _settings.ExpandCaseStatements,
                ExpandCommaLists         = _settings.ExpandCommaList,
                ExpandInLists            = false,
                HTMLColoring             = false,
                IndentString             = _settings.IndentString,
                KeywordStandardization   = _settings.KeywordStandardization,
                MaxLineWidth             = _settings.MaxLineWidth,
                NewClauseLineBreaks      = 0,
                NewStatementLineBreaks   = 0,
                SpaceAfterExpandedComma  = _settings.SpaceAfterExpandedComma,
                SpacesPerTab             = _settings.SpacesPerTab,
                TrailingCommas           = _settings.TrailingCommas,
                UppercaseKeywords        = _settings.UppercaseKeywords
            };

            var innerFormatter = new TSqlStandardFormatter(options);
            var tokenizer      = new TSqlStandardTokenizer();
            var parser         = new TSqlStandardParser();


            var parsedSql = parser.ParseSQL(tokenizer.TokenizeSQL(text));

            Editor.SetText(innerFormatter.FormatSQLTree(parsedSql));
        }
        public string PrettyPrint(string sql)
        {
            var tokenizer = new TSqlStandardTokenizer();
            var tokenized = tokenizer.TokenizeSQL(sql);
            var parser    = new TSqlStandardParser();
            var parsedSql = parser.ParseSQL(tokenized);

            return(_formatter.FormatSQLTree(parsedSql)?.TrimEnd());
        }
        public void StandardFormatReparsingReformatting(string FileName)
        {
            string inputSQL = Utils.GetTestFileContent(FileName, Utils.INPUTSQLFOLDER);
            TSqlStandardFormatter _treeFormatter = GetFormatter("");
            ITokenList            tokenized      = _tokenizer.TokenizeSQL(inputSQL);
            XmlDocument           parsed         = _parser.ParseSQL(tokenized);
            string      outputSQL      = _treeFormatter.FormatSQLTree(parsed);
            ITokenList  tokenizedAgain = _tokenizer.TokenizeSQL(outputSQL);
            XmlDocument parsedAgain    = _parser.ParseSQL(tokenizedAgain);
            string      formattedAgain = _treeFormatter.FormatSQLTree(parsedAgain);

            if (!inputSQL.Contains(Utils.REFORMATTING_INCONSISTENCY_WARNING) && !inputSQL.Contains(Utils.INVALID_SQL_WARNING))
            {
                Assert.AreEqual(outputSQL, formattedAgain, "first-pass formatted vs reformatted");
                Utils.StripWhiteSpaceFromSqlTree(parsed);
                Utils.StripWhiteSpaceFromSqlTree(parsedAgain);
                Assert.AreEqual(parsed.OuterXml.ToUpper(), parsedAgain.OuterXml.ToUpper(), "first parse xml vs reparse xml");
            }
        }
        public void ObfuscatingFormatReformatMatch(string FileName)
        {
            string      inputSQL       = Utils.GetTestFileContent(FileName, Utils.INPUTSQLFOLDER);
            ITokenList  tokenized      = _tokenizer.TokenizeSQL(inputSQL);
            XmlDocument parsedOriginal = _parser.ParseSQL(tokenized);

            string      obfuscatedSql   = _obfuscatingFormatter.FormatSQLTree(parsedOriginal);
            ITokenList  tokenizedAgain  = _tokenizer.TokenizeSQL(obfuscatedSql);
            XmlDocument parsedAgain     = _parser.ParseSQL(tokenizedAgain);
            string      unObfuscatedSql = _standardFormatter.FormatSQLTree(parsedAgain);

            Utils.StripCommentsFromSqlTree(parsedOriginal);
            string standardFormattedSql = _standardFormatter.FormatSQLTree(parsedOriginal);

            if (!inputSQL.Contains(Utils.INVALID_SQL_WARNING))
            {
                Assert.AreEqual(standardFormattedSql, unObfuscatedSql, "standard-formatted vs obfuscatd and reformatted");
            }
        }
        public void StandardFormatExpectedOutput(string FileName)
        {
            string expectedSql = Utils.GetTestFileContent(FileName, Utils.STANDARDFORMATSQLFOLDER);
            string inputSql    = Utils.GetTestFileContent(Utils.StripFileConfigString(FileName), Utils.INPUTSQLFOLDER);
            TSqlStandardFormatter _treeFormatter = GetFormatter(Utils.GetFileConfigString(FileName));

            ITokenList  tokenized = _tokenizer.TokenizeSQL(inputSql);
            XmlDocument parsed    = _parser.ParseSQL(tokenized);
            string      formatted = _treeFormatter.FormatSQLTree(parsed);

            Assert.AreEqual(expectedSql, formatted);
        }
Exemplo n.º 8
0
        public void ObfuscatingFormatReformatMatch(string FileName)
        {
            string     inputSQL       = Utils.GetTestFileContent(FileName, Utils.INPUTSQLFOLDER);
            ITokenList tokenized      = _tokenizer.TokenizeSQL(inputSQL);
            Node       parsedOriginal = _parser.ParseSQL(tokenized);
            string     obfuscatedSql  = _obfuscatingFormatter.FormatSQLTree(parsedOriginal);

            var inputToSecondPass = obfuscatedSql;

            if (inputToSecondPass.StartsWith(Utils.ERROR_FOUND_WARNING))
            {
                inputToSecondPass = inputToSecondPass.Replace(Utils.ERROR_FOUND_WARNING, "");
            }

            ITokenList tokenizedAgain  = _tokenizer.TokenizeSQL(inputToSecondPass);
            Node       parsedAgain     = _parser.ParseSQL(tokenizedAgain);
            string     unObfuscatedSql = _standardFormatter.FormatSQLTree(parsedAgain);

            Utils.StripCommentsFromSqlTree(parsedOriginal);
            string standardFormattedSql = _standardFormatter.FormatSQLTree(parsedOriginal);

            Assert.AreEqual(standardFormattedSql, unObfuscatedSql, "standard-formatted vs obfuscatd and reformatted");
        }
        public void StandardFormatExpectedOutput(string fileName)
        {
            //string expectedSql = Utils.GetTestFileContent(fileName, Utils.STANDARDFORMATSQLFOLDER);


            string inputSql = Utils.GetTestFileContent(Utils.StripFileConfigString(fileName), Utils.INPUTSQLFOLDER);
            TSqlStandardFormatter treeFormatter = GetFormatter(Utils.GetFileConfigString(fileName));

            treeFormatter.Debug = false;
            ITokenList tokenized = _tokenizer.TokenizeSQL(inputSql);
            Node       parsed    = _parser.ParseSQL(tokenized);
            string     formatted = treeFormatter.FormatSQLTree(parsed);

            // System.Diagnostics.Debug.WriteLine(formatted);
            Utils.WriteTestFileContent($"formatted-{fileName}", Utils.STANDARDFORMATSQLFOLDER, formatted);
            //Assert.AreEqual(expectedSql, formatted);
        }