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");
            }
        }
Exemplo n.º 2
0
        private static string FormatSql(string sql)
        {
            Console.WriteLine($"[Formatting] resulting SQL code.");
            var options = new TSqlStandardFormatterOptions
            {
                KeywordStandardization   = true,
                IndentString             = "\t",
                SpacesPerTab             = 4,
                MaxLineWidth             = 999,
                NewStatementLineBreaks   = 2,
                NewClauseLineBreaks      = 1,
                TrailingCommas           = false,
                SpaceAfterExpandedComma  = false,
                ExpandBetweenConditions  = true,
                ExpandBooleanExpressions = true,
                ExpandCaseStatements     = true,
                ExpandCommaLists         = true,
                BreakJoinOnSections      = true,
                UppercaseKeywords        = true,
                ExpandInLists            = true
            };

            var parsingError      = false;
            var formatter         = new TSqlStandardFormatter(options);
            var formattingManager = new PoorMansTSqlFormatterLib.SqlFormattingManager(formatter);
            var formattedOutput   = formattingManager.Format(sql, ref parsingError);

            return(formattedOutput);
        }
 public TSqlObfuscatingFormatterTests()
 {
     _tokenizer            = new TSqlStandardTokenizer();
     _parser               = new TSqlStandardParser();
     _standardFormatter    = new TSqlStandardFormatter("\t", 4, 999, true, true, false, true, true, true, false, true, false, true);
     _obfuscatingFormatter = new TSqlObfuscatingFormatter();
 }
Exemplo n.º 4
0
        /// <summary>
        ///     Formats the SQL file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="formatOptions">The format options.</param>
        public static void FormatSqlFile(string fileName, TSqlStandardFormatterOptions formatOptions)
        {
            if (!File.Exists(fileName))
            {
                _log.Error($"Die Datei '{fileName}' existiert nicht.");
            }
            else
            {
                var standardFormatter = new TSqlStandardFormatter(formatOptions);
                var sqlFormatManager  = new SqlFormattingManager
                {
                    Formatter = standardFormatter
                };

                _log.Info($"Formatiere SQL der Datei '{fileName}' und speichere als {_destEncoding.GetInfoString()}.");
                var sql = File.ReadAllText(fileName);
                var errorsEncountered = false;
                sql = sqlFormatManager.Format(sql, ref errorsEncountered);

                if (errorsEncountered)
                {
                    _log.Warn("Es sind Fehler während der Formatierung aufgetreten!");
                    if (sql.StartsWith(FormatterWarningMsg))
                    {
                        sql = sql.Replace(FormatterWarningMsg, "");
                    }
                }

                File.WriteAllText(fileName, sql, _destEncoding);
            }
        }
 public TSqlStandardFormatterTests()
 {
     _tokenizer     = new TSqlStandardTokenizer();
     _parser        = new TSqlStandardParser();
     _treeFormatter = new TSqlStandardFormatter();
     _treeFormatter.HTMLColoring = false;
 }
 public PrettyPrintService()
 {
     _formatter = new TSqlStandardFormatter
     {
         TrailingCommas = true
     };
 }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
0
        public string FormatSql(SqlTiming timing)
        {
            var sqlFormatter = new SqlServerFormatter();
            var sqlFormat    = sqlFormatter.FormatSql(timing);

            var poorMansFormatter = new TSqlStandardFormatter();
            var fullFormatter     = new SqlFormattingManager(poorMansFormatter);

            return(fullFormatter.Format(sqlFormat));
        }
Exemplo n.º 9
0
 public TSqlObfuscatingFormatterTests()
 {
     _tokenizer         = new TSqlStandardTokenizer();
     _parser            = new TSqlStandardParser();
     _standardFormatter = new TSqlStandardFormatter(new TSqlStandardFormatterOptions
     {
         TrailingCommas         = true,
         KeywordStandardization = true
     });
     _obfuscatingFormatter = new TSqlObfuscatingFormatter();
 }
Exemplo n.º 10
0
        private TSqlStandardFormatter GetFormatter(string configString)
        {
            TSqlStandardFormatter outFormatter;

            if (!_formatters.TryGetValue(configString, out outFormatter))
            {
                var options = new TSqlStandardFormatterOptions(configString);
                outFormatter = new TSqlStandardFormatter(options);
                _formatters.Add(configString, outFormatter);
            }
            return(outFormatter);
        }
        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.º 12
0
        private TSqlStandardFormatter GetFormatter(string configString)
        {
            TSqlStandardFormatter outFormatter;

            if (!_formatters.TryGetValue(configString, out outFormatter))
            {
                //defaults are as per the object, except disabling colorized/htmlified output
                outFormatter = new TSqlStandardFormatter();
                outFormatter.HTMLColoring = false;
                Utils.SetObjectPropertiesFromConfigString(configString, outFormatter);
            }
            return(outFormatter);
        }
        private TSqlStandardFormatter GetFormatter(string configString)
        {
            TSqlStandardFormatter outFormatter;

            if (!_formatters.TryGetValue(configString, out outFormatter))
            {
                //defaults are as per the object, except disabling colorized/htmlified output
                var options = new TSqlStandardFormatterOptions(configString);
                options.HTMLColoring = false;
                outFormatter         = new TSqlStandardFormatter(options);
            }
            return(outFormatter);
        }
        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);
        }
        public string FormatSql(string commandText, List <SqlTimingParameter> parameters)
        {
            var    sqlFormatter = new SqlServerFormatter();
            string sqlFormat;

            try
            {
                sqlFormat = sqlFormatter.GetFormattedSql(commandText, parameters);
            }
            catch (IndexOutOfRangeException)
            {
                return(string.Format("Could not format SQL: {0} params {1}", commandText, parameters));
            }
            var poorMansFormatter = new TSqlStandardFormatter();
            var fullFormatter     = new SqlFormattingManager(poorMansFormatter);

            return(fullFormatter.Format(sqlFormat));
        }
        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");
            }
        }
        private TSqlStandardFormatter GetFormatter(string configString)
        {
            TSqlStandardFormatter outFormatter;

            if (!_formatters.TryGetValue(configString, out outFormatter))
            {
                //var options = new TSqlStandardFormatterOptions
                //{
                //  KeywordStandardization = true,
                //  IndentString = "\t",
                //  SpacesPerTab = 4,
                //  MaxLineWidth = 999,
                //  NewStatementLineBreaks = 2,
                //  NewClauseLineBreaks = 1,
                //  TrailingCommas = false,
                //  SpaceAfterExpandedComma = false,
                //  ExpandBetweenConditions = true,
                //  ExpandBooleanExpressions = true,
                //  ExpandCaseStatements = true,
                //  ExpandCommaLists = true,
                //  BreakJoinOnSections = false,
                //  UppercaseKeywords = true,
                //  ExpandInLists = true
                //};

                var options = new TSqlStandardFormatterOptions(configString);
                options.IndentString   = new String(' ', 2);
                options.TrailingCommas = true;
                options.ExpandInLists  = true;
                //options.NewStatementLineBreaks = 2;
                outFormatter = new TSqlStandardFormatter(options);
                _formatters.Add(configString, outFormatter);
            }

            return(outFormatter);
        }