/*
 * 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.antler.parser.factory.StatementFactory;
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.TokenType;
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.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();
        TokenType tokenType = lexerEngine.getCurrentToken().getType();
        SQLStatement result = null;
        if (DDLStatement.isDDL(tokenType, tokenType)) {
            result = StatementFactory.getStatement(dbType, tokenType, shardingRule, sql, shardingTableMetaData);
        }else {
            result = SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData).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();
    }
}

@@ -694,16 +694,17 @@
 tional;%0A
+%0A
 import i
@@ -1134,20 +1134,76 @@
 en.Token
-Type
+;%0Aimport io.shardingsphere.core.parsing.parser.sql.SQLParser
 ;%0Aimport
@@ -2295,22 +2295,19 @@
 oken
-Type tokenType
+ firstToken
  = l
@@ -2334,26 +2334,16 @@
 tToken()
-.getType()
 ;%0A      
@@ -2384,56 +2384,345 @@
     
-if (DDLStatement.isDDL(tokenType, token
+SQLParser parser = SQLParserFactory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData);%0A        Token currentToken = lexerEngine.getCurrentToken();%0A        if (firstToken != currentToken) %7B%0A            if(DDLStatement.isDDL(firstToken.getType(), currentToken.get
 Type
+()
 )) %7B%0A
+    
     
@@ -2780,17 +2780,28 @@
 pe, 
-token
+firstToken.get
 Type
+()
 , sh
@@ -2853,16 +2853,20 @@
     
+    
 %7Delse %7B%0A
     
@@ -2857,24 +2857,28 @@
     %7Delse %7B%0A
+    
             
@@ -2890,135 +2890,81 @@
 t = 
-SQLP
+p
 arser
-Factory.newInstance(dbType, lexerEngine.getCurrentToken().getType(), shardingRule, lexerEngine, shardingTableMetaData)
+.parse();%0A            %7D%0A        %7Delse %7B%0A            result = parser
 .par

Fix bug for dcl test