Пример #1
0
        static void Try(string input, int server_version = 0)
        {
            //var str = new AntlrInputStream(input);
            var str   = new AntlrInputStream(input);
            var lexer = new MySQLLexer(str);

            lexer.serverVersion = server_version;
            var tokens = new CommonTokenStream(lexer);
            var parser = new MySQLParser(tokens);

            parser.serverVersion = server_version;
            var listener = new ErrorListener <IToken>(parser, lexer, tokens);

            parser.AddErrorListener(listener);
            var tree = parser.queries();

            if (listener.had_error)
            {
                System.Console.WriteLine("error in parse.");
            }
            else
            {
                System.Console.WriteLine("parse completed.");
            }
            System.Console.WriteLine(tokens.OutputTokens());
            System.Console.WriteLine(tree.OutputTree(tokens));
        }
Пример #2
0
        public static TokenStreamRemovable GetTokenStream(string sql, Version version)
        {
            Dictionary <Version, TokenStreamRemovable> lines;

            if (_parserCache.TryGetValue(sql, out lines))
            {
                TokenStreamRemovable tsr;
                if (lines.TryGetValue(version, out tsr))
                {
                    return(tsr);
                }
            }
            // no cache entry, then parse
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sql));//ASCIIEncoding.ASCII.GetBytes(sql));
            CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms);
            MySQLLexer lexer = new MySQLLexer(input);

            lexer.MySqlVersion = version;
            TokenStreamRemovable tokens = new TokenStreamRemovable(lexer);

            if (lines == null)
            {
                lines = new Dictionary <Version, TokenStreamRemovable>();
                _parserCache.Add(sql, lines);
            }
            lines.Add(version, tokens);
            return(tokens);
        }
Пример #3
0
        public override ITokenStream GetAntlrTokenStream(TextReader reader)
        {
            MySQLLexer        lexer  = new MySQLLexer(new ANTLRReaderStream(reader));
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            return(tokens);
        }
Пример #4
0
 public override string ReformatSpecificObject(string objtype, string createsql)
 {
     if (objtype == "view")
     {
         try
         {
             MySQLLexer        lexer  = new MySQLLexer(new ANTLRReaderStream(new StringReader(createsql)));
             CommonTokenStream tokens = new CommonTokenStream(lexer);
             MySQLParser       parser = new MySQLParser(tokens);
             var res = parser.create_view();
             if (parser.Errors != null)
             {
                 UsageStats.Usage("warning:badview", "viewsql", createsql);
                 return(createsql);
             }
             var tree = (Antlr.Runtime.Tree.ITree)res.Tree;
             var sw   = new StringWriter();
             var dmp  = this.CreateDumper(sw);
             var admp = new AntlrTreeDumper(tree, createsql, dmp);
             admp.Run();
             return(sw.ToString());
         }
         catch { }
     }
     return(base.ReformatSpecificObject(objtype, createsql));
 }
Пример #5
0
        protected void LoadShowTable(TableStructure table)
        {
            string sql = CachedGetShowTable(table.FullName.Name);

            MySQLLexer        lexer  = new MySQLLexer(new ANTLRReaderStream(new StringReader(sql)));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            MySQLParser       parser = new MySQLParser(tokens);

            parser.create_table(table, false);
            if (parser.Errors != null)
            {
                throw new MySqlParseCreateTableError(parser.Errors, sql);
            }
            // remove indexes, which only reflects foreign keys
            table._Constraints.RemoveIf(IndexShouldBeRemoved);
            // fix primary key table name
            foreach (var cnt in table.Constraints)
            {
                var fk = cnt as ForeignKey;
                if (fk == null)
                {
                    continue;
                }
                fk.PrimaryKeyTable = NewNameWithSchema(table.FullName.Schema, fk.PrimaryKeyTable.Name);
            }
        }
Пример #6
0
        internal static MySQL51Parser.program_return ParseSql(
            string sql, bool expectErrors, out StringBuilder sb, out CommonTokenStream tokensOutput, Version version)
        {
            sql = sql.TrimStart();
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sql));//ASCIIEncoding.ASCII.GetBytes(sql));
            CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms);
            //ANTLRInputStream input = new ANTLRInputStream(ms);
            MySQLLexer lexer = new MySQLLexer(input);

            lexer.MySqlVersion = version;
            CommonTokenStream tokens = new CommonTokenStream(lexer);

            tokensOutput = tokens;
            return(DoParse(tokens, expectErrors, out sb, lexer.MySqlVersion));
        }
Пример #7
0
        public override List <DependencyItem> DetectDependencies(ISpecificObjectStructure spec)
        {
            if (spec.CreateSql == null)
            {
                return(new List <DependencyItem>());
            }
            var               dc     = new DepsCollector();
            MySQLLexer        lexer  = new MySQLLexer(new ANTLRReaderStream(new StringReader(spec.CreateSql)));
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            MySQLParser       parser = new MySQLParser(tokens);

            parser.find_deps(dc);
            if (parser.Errors != null)
            {
                var err = new InternalError("DAE-00057 Error parsing dependencies:" + parser.Errors);
                err.Data["sql"] = spec.CreateSql;
                throw err;
            }
            return(spec.BuildDependencyList(dc));
        }
Пример #8
0
        public void TestTokenRemove()
        {
            string       sql = "select *, a, c, d from table1 where a is null";
            MemoryStream ms  = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql));
            CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms);
            MySQLLexer lexer = new MySQLLexer(input);

            Parser.TokenStreamRemovable tsr = new Parser.TokenStreamRemovable(lexer);
            tsr.Fill();
            List <IToken> tokens  = tsr.GetTokens();
            IToken        removed = null;

            foreach (IToken t in tokens)
            {
                if (t.Text == "d")
                {
                    removed = t;
                    break;
                }
            }

            tsr.Remove(removed);
            tokens = tsr.GetTokens();
        }
Пример #9
0
        private int parseQuery(string query)
        {
            var t = string.Empty;
            IMySQLRecognizerCommon mySQLRecognizerCommon = new MySQLRecognizerCommon(serverVersion, sqlMode);
            var lastErrors    = new StringBuilder();
            var errorListener = new TestErrorListener(lastErrors);
            var errorStrategy = new BailErrorStrategy();
            var input         = new AntlrInputStream(query);
            var lexer         = new MySQLLexer(input, mySQLRecognizerCommon);

            lexer.RemoveErrorListeners();
            lexer.AddErrorListener(errorListener);

            lexer.serverVersion = serverVersion;
            lexer.sqlMode       = sqlMode;
            lexer.charsets      = charsets;
            var tokens = new CommonTokenStream(lexer);

            var parser = new MySQLParser(tokens, mySQLRecognizerCommon);

            parser.serverVersion  = serverVersion;
            parser.sqlMode        = sqlMode;
            parser.BuildParseTree = buildParserTree;

            parser.ErrorHandler = errorStrategy;
            parser.Interpreter.PredictionMode = PredictionMode.SLL;
            parser.RemoveParseListeners();

            tokens.Fill();

            ParserRuleContext tree = default;

            try
            {
                tree = parser.query();
            }
            catch (Antlr4.Runtime.Misc.ParseCanceledException)
            {
                // If parsing was cancelled we either really have a syntax error or we need to do a second step,
                // now with the default strategy and LL parsing.
                tokens.Reset();
                parser.Reset();
                parser.ErrorHandler = new DefaultErrorStrategy();
                parser.Interpreter.PredictionMode = PredictionMode.LL;
                parser.AddErrorListener(errorListener);

                tree = parser.query();
            }

            var toks = tokens.GetTokens();

            t = input.GetText(new Antlr4.Runtime.Misc.Interval(toks[0].StartIndex, int.MaxValue));

            if (dump && buildParserTree)
            {
                if (tree == null)
                {
                    Trace.TraceInformation(@"No parse tree available");
                }
                else
                {
                    t             = tree.GetText();
                    parseTreeView = tree.ToStringTree(parser);
                    var text = $"Token count: {tokens.Size}{Environment.NewLine}{MySQLRecognizerCommon.dumpTree(tree, parser.Vocabulary)}";
                    Trace.TraceInformation(text.Trim());
                }
            }

            return(parser.NumberOfSyntaxErrors);
        }