DruidParser

WBOY
WBOY原创
2016-06-07 14:50:521876浏览

最近用阿里的Druid的SQL parser来解析SQL语句。在此记录下研究: 调用它来解析出AST语意树一般这么写(针对MySQL): MySqlStatementParser parser = new MySqlStatementParser(sql);ListSQLStatement statementList = parser.parseStatementList(); for (SQ

最近用阿里的Druid的SQL parser来解析SQL语句。在此记录下研究:
调用它来解析出AST语意树一般这么写(针对MySQL):

<code class="language-java hljs ">MySqlStatementParser parser = <span class="hljs-keyword">new</span> MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
<span class="hljs-keyword">for</span>(SQLStatement statement:statementList){
    MySqlSchemaStatVisitor visitor = <span class="hljs-keyword">new</span> MySqlSchemaStatVisitor();
    statemen.accept(visitor);
}        </code>

对于每一个SQL请求(可能包含多语句),需要先新建一个MySqlStatementParser。注意,MySqlStatementParser 不是线程安全的,所以一种做法是针对每个session的请求,需要新建一个MySqlStatementParser。
那么这个初始化过程究竟是怎样的呢?涉及到哪些类?

涉及到的类如下所示:
这里写图片描述
SQL解析可以分为三层:语句解析->表达式解析->词法解析。对应的主要类分别是MySqlStatementParser,MySqlExprParser,MySqlLexer。可以说,MySqlLexer是解析出每个词的词义,表达式由词组成,MySqlExprParser用来解析出不同表达式的含义。多个表达式和词组成完整的语句,这个由MySqlStatementParser解析。
首先看MySqlStatementParser的结构:
这里写图片描述

  • SQLParser.java
    • errorEndPos :解析出错记录位置
    • lexer:词法解析
    • dbType:数据库类型
  • SQLStatement.java
    • exprParser:表达式解析类
    • SQLCreateTableParser:建表语句解析类,因为建表语句比较复杂,所以单拿出来。其他DDL语句都在本类SQLStatement中解析
    • parseValuesSize:记录解析结果集大小
    • keepComments:是否保留注释
    • parseCompleteValues:是否全部解析完成
  • MySqlStatementParser.java:
    • 静态关键词:比如auto increment,collate等,对于DDL语句或者DCL语句
    • exprParser:针对MySQL语句的parser

接着是MySqlStatementParser的MySqlExprParser的结构:
这里写图片描述

  • SQLExprParser:
    • AGGREGATE_FUNCTIONS:一些统计函数的关键词
    • aggregateFunctions:保存统计函数的关键词
  • MySqlSQLExprParser:
    • AGGREGATE_FUNCTIONS:针对MySql统计函数的关键词

最后是MySqlLexer:
这里写图片描述

  • Lexer:
    • text:保存目前的整个SQL语句
    • pos:当前处理位置
    • mark:当前处理词的开始位置
    • ch:当前处理字符
    • buf:当前缓存的处理词
    • bufPos:用于取出词的标记,当前从text中取出的词应该为从mark位置开始,mark+bufPos结束的词
    • token:当前位于的关键词
    • stringVal:当前处理词
    • comments:注释
    • skipComment:是否跳过注释
    • savePoint:保存点
    • varIndex:针对?表达式
    • lines:总行数
    • digits:数字ASCII码
    • EOF:是否结尾
    • keepComments:是否保留注释
    • endOfComment:是否注释结尾
    • line:当前处理行数
    • commentHandler:注释处理器
    • allowComment:是否允许注释
    • KeyWords:所有关键词集合

新建MySqlStatementParser

初始化MySqlStatementParser:
MySqlStateMentParser.java:

<code class=" hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title">MySqlStatementParser</span>(String sql){
    <span class="hljs-keyword">super</span>(<span class="hljs-keyword">new</span> MySqlExprParser(sql));
}</code>

会新建MySqlExprParser:
MySqlExprParser.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-title">MySqlExprParser</span>(String sql){
    <span class="hljs-keyword">this</span>(<span class="hljs-keyword">new</span> MySqlLexer(sql));
    <span class="hljs-comment">//读取第一个有效词</span>
    <span class="hljs-keyword">this</span>.lexer.nextToken();
}</code>

会新建MySqlLexer:
MySqlLexer.java

<code class=" hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title">MySqlLexer</span>(String input){
    <span class="hljs-keyword">super</span>(input);
    <span class="hljs-comment">//初始化MySQL关键词</span>
    <span class="hljs-keyword">super</span>.keywods = DEFAULT_MYSQL_KEYWORDS;
}</code>

Lexer.java

<code class=" hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title">Lexer</span>(String input){
 <span class="hljs-keyword">this</span>(input, <span class="hljs-keyword">null</span>);
}

<span class="hljs-javadoc">/**
 *<span class="hljs-javadoctag"> @param</span> input 输入SQL语句
 *<span class="hljs-javadoctag"> @param</span> commentHandler 注释处理器
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-title">Lexer</span>(String input, CommentHandler commentHandler){
    <span class="hljs-keyword">this</span>(input, <span class="hljs-keyword">true</span>);
    <span class="hljs-keyword">this</span>.commentHandler = commentHandler;
}
<span class="hljs-javadoc">/**
 *
 *<span class="hljs-javadoctag"> @param</span> input
 *<span class="hljs-javadoctag"> @param</span> skipComment 是否跳过注释
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-title">Lexer</span>(String input, <span class="hljs-keyword">boolean</span> skipComment){
    <span class="hljs-keyword">this</span>.skipComment = skipComment;

    <span class="hljs-keyword">this</span>.text = input;
    <span class="hljs-keyword">this</span>.pos = -<span class="hljs-number">1</span>;
    <span class="hljs-comment">//读取第一个字符</span>
    scanChar();
}
<span class="hljs-keyword">protected</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">void</span> <span class="hljs-title">scanChar</span>() {
    ch = charAt(++pos);
}</code>

初始化Lexer之后,回到MySqlExprParser的构造器,初始化KeyWords集合:

<code class=" hljs avrasm">public final static Keywords DEFAULT_MYSQL_KEYWORDS<span class="hljs-comment">;</span>

    static {
    //MySQL关键词
    Map<String, Token> map = new HashMap<String, Token>()<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.putAll</span>(Keywords<span class="hljs-preprocessor">.DEFAULT</span>_KEYWORDS<span class="hljs-preprocessor">.getKeywords</span>())<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DUAL"</span>, Token<span class="hljs-preprocessor">.DUAL</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FALSE"</span>, Token<span class="hljs-preprocessor">.FALSE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"IDENTIFIED"</span>, Token<span class="hljs-preprocessor">.IDENTIFIED</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"IF"</span>, Token<span class="hljs-preprocessor">.IF</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"KILL"</span>, Token<span class="hljs-preprocessor">.KILL</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LIMIT"</span>, Token<span class="hljs-preprocessor">.LIMIT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TRUE"</span>, Token<span class="hljs-preprocessor">.TRUE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"BINARY"</span>, Token<span class="hljs-preprocessor">.BINARY</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SHOW"</span>, Token<span class="hljs-preprocessor">.SHOW</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CACHE"</span>, Token<span class="hljs-preprocessor">.CACHE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ANALYZE"</span>, Token<span class="hljs-preprocessor">.ANALYZE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OPTIMIZE"</span>, Token<span class="hljs-preprocessor">.OPTIMIZE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ROW"</span>, Token<span class="hljs-preprocessor">.ROW</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"BEGIN"</span>, Token<span class="hljs-preprocessor">.BEGIN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"END"</span>, Token<span class="hljs-preprocessor">.END</span>)<span class="hljs-comment">;</span>

    // for oceanbase & mysql <span class="hljs-number">5.7</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"PARTITION"</span>, Token<span class="hljs-preprocessor">.PARTITION</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CONTINUE"</span>, Token<span class="hljs-preprocessor">.CONTINUE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"UNDO"</span>, Token<span class="hljs-preprocessor">.UNDO</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SQLSTATE"</span>, Token<span class="hljs-preprocessor">.SQLSTATE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CONDITION"</span>, Token<span class="hljs-preprocessor">.CONDITION</span>)<span class="hljs-comment">;</span>

    DEFAULT_MYSQL_KEYWORDS = new Keywords(map)<span class="hljs-comment">;</span>
}</code>

Keywords.java:

<code class=" hljs avrasm">public final static Keywords     DEFAULT_KEYWORDS<span class="hljs-comment">;</span>

static {
    Map<String, Token> map = new HashMap<String, Token>()<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ALL"</span>, Token<span class="hljs-preprocessor">.ALL</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ALTER"</span>, Token<span class="hljs-preprocessor">.ALTER</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"AND"</span>, Token<span class="hljs-preprocessor">.AND</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ANY"</span>, Token<span class="hljs-preprocessor">.ANY</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"AS"</span>, Token<span class="hljs-preprocessor">.AS</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ENABLE"</span>, Token<span class="hljs-preprocessor">.ENABLE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DISABLE"</span>, Token<span class="hljs-preprocessor">.DISABLE</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ASC"</span>, Token<span class="hljs-preprocessor">.ASC</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"BETWEEN"</span>, Token<span class="hljs-preprocessor">.BETWEEN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"BY"</span>, Token<span class="hljs-preprocessor">.BY</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CASE"</span>, Token<span class="hljs-preprocessor">.CASE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CAST"</span>, Token<span class="hljs-preprocessor">.CAST</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CHECK"</span>, Token<span class="hljs-preprocessor">.CHECK</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CONSTRAINT"</span>, Token<span class="hljs-preprocessor">.CONSTRAINT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CREATE"</span>, Token<span class="hljs-preprocessor">.CREATE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DATABASE"</span>, Token<span class="hljs-preprocessor">.DATABASE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DEFAULT"</span>, Token<span class="hljs-preprocessor">.DEFAULT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"COLUMN"</span>, Token<span class="hljs-preprocessor">.COLUMN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TABLESPACE"</span>, Token<span class="hljs-preprocessor">.TABLESPACE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"PROCEDURE"</span>, Token<span class="hljs-preprocessor">.PROCEDURE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FUNCTION"</span>, Token<span class="hljs-preprocessor">.FUNCTION</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DELETE"</span>, Token<span class="hljs-preprocessor">.DELETE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DESC"</span>, Token<span class="hljs-preprocessor">.DESC</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DISTINCT"</span>, Token<span class="hljs-preprocessor">.DISTINCT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DROP"</span>, Token<span class="hljs-preprocessor">.DROP</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ELSE"</span>, Token<span class="hljs-preprocessor">.ELSE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"EXPLAIN"</span>, Token<span class="hljs-preprocessor">.EXPLAIN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"EXCEPT"</span>, Token<span class="hljs-preprocessor">.EXCEPT</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"END"</span>, Token<span class="hljs-preprocessor">.END</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ESCAPE"</span>, Token<span class="hljs-preprocessor">.ESCAPE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"EXISTS"</span>, Token<span class="hljs-preprocessor">.EXISTS</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FOR"</span>, Token<span class="hljs-preprocessor">.FOR</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FOREIGN"</span>, Token<span class="hljs-preprocessor">.FOREIGN</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FROM"</span>, Token<span class="hljs-preprocessor">.FROM</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FULL"</span>, Token<span class="hljs-preprocessor">.FULL</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"GROUP"</span>, Token<span class="hljs-preprocessor">.GROUP</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"HAVING"</span>, Token<span class="hljs-preprocessor">.HAVING</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"IN"</span>, Token<span class="hljs-preprocessor">.IN</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INDEX"</span>, Token<span class="hljs-preprocessor">.INDEX</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INNER"</span>, Token<span class="hljs-preprocessor">.INNER</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INSERT"</span>, Token<span class="hljs-preprocessor">.INSERT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INTERSECT"</span>, Token<span class="hljs-preprocessor">.INTERSECT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INTERVAL"</span>, Token<span class="hljs-preprocessor">.INTERVAL</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INTO"</span>, Token<span class="hljs-preprocessor">.INTO</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"IS"</span>, Token<span class="hljs-preprocessor">.IS</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"JOIN"</span>, Token<span class="hljs-preprocessor">.JOIN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"KEY"</span>, Token<span class="hljs-preprocessor">.KEY</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LEFT"</span>, Token<span class="hljs-preprocessor">.LEFT</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LIKE"</span>, Token<span class="hljs-preprocessor">.LIKE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LOCK"</span>, Token<span class="hljs-preprocessor">.LOCK</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"MINUS"</span>, Token<span class="hljs-preprocessor">.MINUS</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"NOT"</span>, Token<span class="hljs-preprocessor">.NOT</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"NULL"</span>, Token<span class="hljs-preprocessor">.NULL</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ON"</span>, Token<span class="hljs-preprocessor">.ON</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OR"</span>, Token<span class="hljs-preprocessor">.OR</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ORDER"</span>, Token<span class="hljs-preprocessor">.ORDER</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OUTER"</span>, Token<span class="hljs-preprocessor">.OUTER</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"PRIMARY"</span>, Token<span class="hljs-preprocessor">.PRIMARY</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"REFERENCES"</span>, Token<span class="hljs-preprocessor">.REFERENCES</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"RIGHT"</span>, Token<span class="hljs-preprocessor">.RIGHT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SCHEMA"</span>, Token<span class="hljs-preprocessor">.SCHEMA</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SELECT"</span>, Token<span class="hljs-preprocessor">.SELECT</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SET"</span>, Token<span class="hljs-preprocessor">.SET</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SOME"</span>, Token<span class="hljs-preprocessor">.SOME</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TABLE"</span>, Token<span class="hljs-preprocessor">.TABLE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"THEN"</span>, Token<span class="hljs-preprocessor">.THEN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TRUNCATE"</span>, Token<span class="hljs-preprocessor">.TRUNCATE</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"UNION"</span>, Token<span class="hljs-preprocessor">.UNION</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"UNIQUE"</span>, Token<span class="hljs-preprocessor">.UNIQUE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"UPDATE"</span>, Token<span class="hljs-preprocessor">.UPDATE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"VALUES"</span>, Token<span class="hljs-preprocessor">.VALUES</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"VIEW"</span>, Token<span class="hljs-preprocessor">.VIEW</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"SEQUENCE"</span>, Token<span class="hljs-preprocessor">.SEQUENCE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TRIGGER"</span>, Token<span class="hljs-preprocessor">.TRIGGER</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"USER"</span>, Token<span class="hljs-preprocessor">.USER</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"WHEN"</span>, Token<span class="hljs-preprocessor">.WHEN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"WHERE"</span>, Token<span class="hljs-preprocessor">.WHERE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"XOR"</span>, Token<span class="hljs-preprocessor">.XOR</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OVER"</span>, Token<span class="hljs-preprocessor">.OVER</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"TO"</span>, Token<span class="hljs-preprocessor">.TO</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"USE"</span>, Token<span class="hljs-preprocessor">.USE</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"REPLACE"</span>, Token<span class="hljs-preprocessor">.REPLACE</span>)<span class="hljs-comment">;</span>

    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"COMMENT"</span>, Token<span class="hljs-preprocessor">.COMMENT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"COMPUTE"</span>, Token<span class="hljs-preprocessor">.COMPUTE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"WITH"</span>, Token<span class="hljs-preprocessor">.WITH</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"GRANT"</span>, Token<span class="hljs-preprocessor">.GRANT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"REVOKE"</span>, Token<span class="hljs-preprocessor">.REVOKE</span>)<span class="hljs-comment">;</span>

    // MySql procedure: <span class="hljs-keyword">add</span> by zz
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"WHILE"</span>, Token<span class="hljs-preprocessor">.WHILE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DO"</span>, Token<span class="hljs-preprocessor">.DO</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"DECLARE"</span>, Token<span class="hljs-preprocessor">.DECLARE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LOOP"</span>, Token<span class="hljs-preprocessor">.LOOP</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"LEAVE"</span>, Token<span class="hljs-preprocessor">.LEAVE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"ITERATE"</span>, Token<span class="hljs-preprocessor">.ITERATE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"REPEAT"</span>, Token<span class="hljs-preprocessor">.REPEAT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"UNTIL"</span>, Token<span class="hljs-preprocessor">.UNTIL</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OPEN"</span>, Token<span class="hljs-preprocessor">.OPEN</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CLOSE"</span>, Token<span class="hljs-preprocessor">.CLOSE</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"CURSOR"</span>, Token<span class="hljs-preprocessor">.CURSOR</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"FETCH"</span>, Token<span class="hljs-preprocessor">.FETCH</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"OUT"</span>, Token<span class="hljs-preprocessor">.OUT</span>)<span class="hljs-comment">;</span>
    map<span class="hljs-preprocessor">.put</span>(<span class="hljs-string">"INOUT"</span>, Token<span class="hljs-preprocessor">.INOUT</span>)<span class="hljs-comment">;</span>

    DEFAULT_KEYWORDS = new Keywords(map)<span class="hljs-comment">;</span>
}</code>

之后,回到构造MySqlStatementParser:
调用父类方法初始化:
SQLStatementParser.java

<code class=" hljs java"><span class="hljs-keyword">public</span> <span class="hljs-title">SQLStatementParser</span>(SQLExprParser exprParser){
    <span class="hljs-keyword">super</span>(exprParser.getLexer(), exprParser.getDbType());
    <span class="hljs-keyword">this</span>.exprParser = exprParser;
}</code>

SQLParser.java

<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-title">SQLParser</span>(Lexer lexer, String dbType){
    <span class="hljs-keyword">this</span>.lexer = lexer;
    <span class="hljs-keyword">this</span>.dbType = dbType;
}</code>

至此,初始化完成

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn