/*
 * Copyright 2016-2018 shardingsphere.io.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

package io.shardingsphere.core.parsing;

import com.google.common.base.Optional;
import io.shardingsphere.core.constant.DatabaseType;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.antlr.AntlrParsingEngine;
import io.shardingsphere.core.parsing.cache.ParsingResultCache;
import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.LexerEngineFactory;
import io.shardingsphere.core.parsing.lexer.token.Token;
import io.shardingsphere.core.parsing.parser.sql.SQLParser;
import io.shardingsphere.core.parsing.parser.sql.SQLParserFactory;
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
import io.shardingsphere.core.parsing.parser.sql.ddl.DDLStatement;
import io.shardingsphere.core.parsing.parser.sql.tcl.TCLStatement;
import io.shardingsphere.core.rule.ShardingRule;
import lombok.RequiredArgsConstructor;

/**
 * SQL parsing engine.
 *
 * @author zhangliang
 */
@RequiredArgsConstructor
public final class SQLParsingEngine {
    
    private final DatabaseType dbType;
    
    private final String sql;
    
    private final ShardingRule shardingRule;
    
    private final ShardingTableMetaData shardingTableMetaData;
    
    /**
     * Parse SQL.
     * 
     * @param useCache use cache or not
     * @return parsed SQL statement
     */
    public SQLStatement parse(final boolean useCache) {
        Optional<SQLStatement> cachedSQLStatement = getSQLStatementFromCache(useCache);
        if (cachedSQLStatement.isPresent()) {
            return cachedSQLStatement.get();
        }
        LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql);
        lexerEngine.nextToken();
        Token firstToken = lexerEngine.getCurrentToken();
        SQLStatement result;
        SQLParser sqlParser = SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData);
        Token currentToken = lexerEngine.getCurrentToken();
        if (firstToken != currentToken) {
            if (DDLStatement.isDDL(firstToken.getType(), currentToken.getType())) {
                result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);
            } else {
                result = sqlParser.parse();
            }
        } else if (TCLStatement.isTCL(firstToken.getType())) {
            result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);
        } else {
            result = sqlParser.parse();
        }
        if (useCache) {
            ParsingResultCache.getInstance().put(sql, result);
        }
        return result;
    }
    
    private Optional<SQLStatement> getSQLStatementFromCache(final boolean useCache) {
        return useCache ? Optional.fromNullable(ParsingResultCache.getInstance().getSQLStatement(sql)) : Optional.<SQLStatement>absent();
    }
}

@@ -1370,32 +1370,99 @@
 l.DDLStatement;%0A
+import io.shardingsphere.core.parsing.parser.sql.dql.DQLStatement;%0A
 import io.shardi
@@ -3178,32 +3178,248 @@
        %7D else %7B%0A
+            if(DQLStatement.isDQL(firstToken.getType()) && DatabaseType.MySQL == dbType) %7B%0A                result = AntlrParsingEngine.parse(dbType, sql, shardingRule, shardingTableMetaData);%0A            %7Delse %7B%0A    
             resu
@@ -3434,32 +3434,46 @@
 Parser.parse();%0A
+            %7D%0A
         %7D%0A      

use antlr parse MySQL DQL