コード例 #1
0
        } // End Sub WalkerTest

        static void VisitorTest(string text)
        {
            try
            {
                System.IO.StringReader          reader = new System.IO.StringReader(text);
                Antlr4.Runtime.AntlrInputStream input  = new Antlr4.Runtime.AntlrInputStream(reader);
                TSqlLexer lexer = new TSqlLexer(input);
                Antlr4.Runtime.CommonTokenStream tokens = new Antlr4.Runtime.CommonTokenStream(lexer);
                TSqlParser parser = new TSqlParser(tokens);
                //Specify our entry point

                // TSqlParser.Query_specificationContext
                TSqlParser.Tsql_fileContext fileContext = parser.tsql_file();

                System.Console.WriteLine("fileContext.ChildCount = " + fileContext.ChildCount.ToString());

                SqlVisitor vis = new SqlVisitor();
                string     s   = vis.Visit(fileContext);
                System.Console.WriteLine(s);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        } // End Sub VisitorTest
コード例 #2
0
        static void Main(string[] args)
        {
            string lQuery = "SELECT Jmeno, Prijmeni FROM Osoba, Film  WHERE Stat LIKE 'Kanada' OR Rok_natoceni >1995 UNION " +
                            "(SELECT Jmeno, Prijmeni FROM Uzivatel WHERE Jmeno = 'ads' AND Prijmeni = 'asd');";
            string            lQuery2 = "SElect jmeno, nazev_cz FROM Osoba os JOIN Osoba_Film osf ON os.idO = osf.Osoba_idO JOIN Film f ON f.idF = osf.Film_idF Where f.Rok_natoceni > 1995;";
            string            lQuery1 = "select * from table1 t1, t2 where t1.neco = 1";
            string            text    = ReadFile($@"C:\Users\Lukáš\Desktop\doc\Workload_bp.txt");
            StringReader      reader  = new StringReader(text);
            AntlrInputStream  input   = new AntlrInputStream(reader);
            TSqlLexer         lexer   = new TSqlLexer(new CaseChangingCharStream(input, true));
            CommonTokenStream tokens  = new CommonTokenStream(lexer);
            TSqlParser        parser  = new TSqlParser(tokens);

            TSqlParser.Tsql_fileContext Tsql_fileContext1 = parser.tsql_file();
            //Console.WriteLine("Tsql_fileContext1.ChildCount = " + Tsql_fileContext1.ChildCount.ToString());

            Antlr4.Runtime.Tree.ParseTreeWalker walker = new Antlr4.Runtime.Tree.ParseTreeWalker();
            AntlrTsqlListener listener = new AntlrTsqlListener();

            walker.Walk(listener, Tsql_fileContext1);

            foreach (var nTable in listener.AnalyzedWorkload)
            {
                Console.WriteLine("Tabulka " + nTable.Name);
                foreach (var nColumn in nTable.Columns)
                {
                    foreach (var nCondition in nColumn.Conditions)
                    {
                        Console.WriteLine($" col { nCondition.ColumnName} operator { nCondition.Operator} val {nCondition.Value}");
                    }
                }
            }
            Console.ReadKey();
        }
コード例 #3
0
        } // End Sub LexerTest

        static void WalkerTest(string text)
        {
            try
            {
                System.IO.StringReader          reader = new System.IO.StringReader(text);
                Antlr4.Runtime.AntlrInputStream input  = new Antlr4.Runtime.AntlrInputStream(reader);
                TSqlLexer lexer = new TSqlLexer(input);
                Antlr4.Runtime.CommonTokenStream tokens = new Antlr4.Runtime.CommonTokenStream(lexer);
                TSqlParser parser = new TSqlParser(tokens);
                // Specify our entry point

                // TSqlParser.Query_specificationContext
                TSqlParser.Tsql_fileContext fileContext = parser.tsql_file();
                // Antlr4.Runtime.Tree.IParseTree root = (Antlr4.Runtime.Tree.IParseTree)fileContext;


                // TSqlParser.Query_specificationContext tsqlParser.Tsql_fileContext fileContext = parser.tsql_file();
                System.Console.WriteLine("fileContext.ChildCount = " + fileContext.ChildCount.ToString());
                // Walk it and attach our listener
                Antlr4.Runtime.Tree.ParseTreeWalker walker = new Antlr4.Runtime.Tree.ParseTreeWalker();
                // AntlrTsqListener listener = new AntlrTsqListener();
                EverythingListener listener = new EverythingListener();

                walker.Walk(listener, fileContext);
            }
            catch (System.Exception e)
            {
                System.Console.WriteLine(e.Message);
            }
        } // End Sub WalkerTest
コード例 #4
0
ファイル: Parser.cs プロジェクト: ruo2012/SQLParser
        /// <summary>
        /// Parses the TSQL.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="listener">The listener.</param>
        private static void ParseTSQL(string input, IParseTreeListener listener)
        {
            ICharStream  Stream = CharStreams.fromstring(input);
            ITokenSource Lexer  = new TSqlLexer(Stream, TextWriter.Null, TextWriter.Null);
            ITokenStream Tokens = new CommonTokenStream(Lexer);
            TSqlParser   Parser = new TSqlParser(Tokens, TextWriter.Null, TextWriter.Null)
            {
                BuildParseTree = true
            };
            IParseTree tree = Parser.tsql_file();

            ParseTreeWalker.Default.Walk(listener, tree);
        }
コード例 #5
0
ファイル: Parser.cs プロジェクト: flipworldit/SqlFormatter
        public static IParseTree ParseTree(string input)
        {
            ICharStream  Stream = CharStreams.fromstring(input);
            ITokenSource Lexer  = new TSqlLexer(Stream, TextWriter.Null, TextWriter.Null);
            ITokenStream Tokens = new CommonTokenStream(Lexer);
            TSqlParser   Parser = new TSqlParser(Tokens, TextWriter.Null, TextWriter.Null)
            {
                BuildParseTree = true
            };
            IParseTree tree = Parser.tsql_file();

            return(tree);
        }
コード例 #6
0
        private TableInfo GetTableInfo(string script)
        {
            var input = new AntlrInputStream(script);
            var caseChangingStream = new CaseChangingCharStream(input, true);
            var lexer    = new TSqlLexer(caseChangingStream);
            var tokens   = new CommonTokenStream(lexer);
            var parser   = new TSqlParser(tokens);
            var tree     = parser.tsql_file();
            var walker   = new ParseTreeWalker();
            var listener = new SqlListener();

            walker.Walk(listener, tree);

            return(listener.TableInfo);
        }
コード例 #7
0
        public void ProcessWorkload(string aScriptPath, EventHandler <string> aOnAnalyzingError)
        {
            string            lText   = ReadFile(aScriptPath);
            StringReader      lReader = new StringReader(lText);
            AntlrInputStream  lInput  = new AntlrInputStream(lReader);
            TSqlLexer         lLexer  = new TSqlLexer(new CaseChangingCharStream(lInput, true));
            CommonTokenStream lTokens = new CommonTokenStream(lLexer);
            TSqlParser        lParser = new TSqlParser(lTokens);

            TSqlParser.Tsql_fileContext Tsql_fileContext1 = lParser.tsql_file();

            Antlr4.Runtime.Tree.ParseTreeWalker lWalker = new Antlr4.Runtime.Tree.ParseTreeWalker();
            AntlrTsqlListener lListener = new AntlrTsqlListener();

            lListener.OnAnalyzingError += aOnAnalyzingError;
            lWalker.Walk(lListener, Tsql_fileContext1);
            Tables = lListener.AnalyzedWorkload;
        }