Code Analysis of MySQL Using ANTLR4

2023年1月12日 3942点热度 0人点赞 0条评论
内容目录

Environment Setup and Generation

First, install Java version 8 or above.

Open https://www.antlr.org/download.html,
find "ANTLR tool itself", or directly click on https://www.antlr.org/download/antlr-4.11.1-complete.jar to download the package.

file

Then open https://github.com/antlr/grammars-v4
to download the corresponding syntax analysis template.

file

Execute the command to generate Java code.

 java -jar antlr-4.11.1-complete.jar .\MySqlLexer.g4 -Dlanguage=Java

Execute the command to generate C# code:

 java -jar antlr-4.11.1-complete.jar .\MySqlLexer.g4 -Dlanguage=CSharp
 java -jar antlr-4.11.1-complete.jar .\MySqlParser.g4 -Dlanguage=CSharp

file

C# Analysis of SQL

Import the Antlr4.Runtime library.

C# can analyze SQL like this:

    internal class Program
    {
        static void Main()
        {
            String input = "ALTER TABLE `sjzy_user_center`.`t_system`ADD COLUMN `Token` varchar(1000) NULL DEFAULT NULL COMMENT '子系统token' AFTER `AssociatedInternalSubsystem`;";
            ICharStream stream = CharStreams.fromString(input);
            ITokenSource lexer = new MySqlLexer(stream);
            ITokenStream tokens = new CommonTokenStream(lexer);
            MySqlParser parser = new MySqlParser(tokens);
            parser.BuildParseTree = true;
            StartSlaveContext tree = parser.startSlave();
            Console.WriteLine(tree.ToStringTree());
            P(tree.children);
        }
        
        static void P(IList<IParseTree> tree)
        {
            foreach (var item in tree)
            {
                if (item.ChildCount != 0)
                {
                    List<IParseTree> child = new List<IParseTree>(0);
                    for (int i = 0; i < item.ChildCount; i++)
                    {
                        child.Add(item.GetChild(i));
                    }
                    P(child);
                }
                else
                {
                    Console.WriteLine(item.ToStringTree());
                }
            }
        }
    }

file

Each Token corresponds to a Type, which can be found in the MySqlLexer.tokens or MySqlParser.tokens files.

First, use tree.children to get all child nodes, returning a list of IList<IParseTree>, where each IParseTree contains a Payload property that records all the attributes of the Token.

                var token = item.Payload as CommonToken;

For example, STRING_LITERAL indicates that the keyword belongs to a name.

If you want to find database or table names, or field names, you can search like this:

                if(token.Type == MySqlParser.STRING_LITERAL)
                {

                }
                if (token.Type == 1141)
                {

                }

痴者工良

高级程序员劝退师

文章评论