/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

package org.apache.shardingsphere.infra.context.kernel;

import org.apache.shardingsphere.infra.audit.SQLAuditEngine;
import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import java.sql.SQLException;
import java.util.Collection;

/**
 * Kernel processor.
 */
public final class KernelProcessor {
    
    /**
     * Generate execution context.
     *
     * @param logicSQL logic SQL
     * @param metaData ShardingSphere meta data
     * @param props configuration properties
     * @return execution context
     * @throws SQLException SQL exception
     */
    public ExecutionContext generateExecutionContext(final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ConfigurationProperties props) throws SQLException {
        Collection<ShardingSphereRule> rules = metaData.getRuleMetaData().getRules();
        new SQLAuditEngine().audit(logicSQL.getSqlStatementContext().getSqlStatement(), logicSQL.getParameters(), metaData.getName(), rules);
        SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(rules, props);
        SQLStatementContext<?> sqlStatementContext = logicSQL.getSqlStatementContext();
        RouteContext routeContext = sqlRouteEngine.route(logicSQL, metaData);
        SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(metaData.getSchema(), props, rules);
        SQLRewriteResult rewriteResult = rewriteEntry.rewrite(logicSQL.getSql(), logicSQL.getParameters(), sqlStatementContext, routeContext);
        Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(metaData, rewriteResult, sqlStatementContext);
        ExecutionContext result = new ExecutionContext(sqlStatementContext, executionUnits, routeContext);
        logSQL(logicSQL, props, result);
        return result;
    }
    
    private void logSQL(final LogicSQL logicSQL, final ConfigurationProperties props, final ExecutionContext executionContext) {
        if (props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
            SQLLogger.logSQL(logicSQL, props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
        }
    }
}

@@ -974,85 +974,8 @@
 QL;%0A
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;%0A
 impo
@@ -1302,83 +1302,8 @@
 er;%0A
-import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;%0A
 impo
@@ -1718,72 +1718,8 @@
 ine;
-%0Aimport org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 %0A%0Aim
@@ -1749,37 +1749,8 @@
 ion;
-%0Aimport java.util.Collection;
 %0A%0A/*
@@ -2265,85 +2265,486 @@
     
-Collection%3CShardingSphereRule%3E rules = metaData.getRuleMetaData().getRules();
+audit(logicSQL, metaData);%0A        RouteContext routeContext = route(logicSQL, metaData, props);%0A        SQLRewriteResult rewriteResult = rewrite(logicSQL, metaData, props, routeContext);%0A        ExecutionContext result = createExecutionContext(logicSQL, metaData, routeContext, rewriteResult);%0A        logSQL(logicSQL, props, result);%0A        return result;%0A    %7D%0A    %0A    private void audit(final LogicSQL logicSQL, final ShardingSphereMetaData metaData) throws SQLException %7B
 %0A   
@@ -2878,88 +2878,272 @@
 (), 
-r
+metaData.getRuleMetaData().getR
 ules
+()
 );%0A    
+%7D%0A
     
-SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(r
+%0A    private RouteContext route(final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ConfigurationProperties props) %7B%0A        return new SQLRouteEngine(metaData.getRuleMetaData().getR
 ules
+()
 , props)
 ;%0A  
@@ -3142,214 +3142,234 @@
 ops)
+.route(logicSQL, metaData)
 ;%0A    
+%7D%0A
     
-SQLStatementContext%3C?%3E sqlStatementContext = logicSQL.getSqlStatementContext();%0A        RouteContext routeContext = sqlRouteEngine.route(logicSQL, metaData);%0A        SQLRewriteEntry rewriteEntry =
+%0A    private SQLRewriteResult rewrite(final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final ConfigurationProperties props, final RouteContext routeContext) %7B%0A        return
  new
@@ -3385,16 +3385,33 @@
 teEntry(
+%0A                
 metaData
@@ -3435,69 +3435,46 @@
 ps, 
-rules);%0A        SQLRewriteResult rewriteResult = rewriteEntry
+metaData.getRuleMetaData().getRules())
 .rew
@@ -3523,17 +3523,29 @@
 ters(), 
-s
+logicSQL.getS
 qlStatem
@@ -3554,16 +3554,18 @@
 tContext
+()
 , routeC
@@ -3581,165 +3581,218 @@
     
+%7D%0A
     
-Collection%3CExecutionUnit%3E executionUnits = ExecutionContextBuilder.build(metaData, rewriteResult, sqlStatementContext);%0A        ExecutionContext result =
+%0A    private ExecutionContext createExecutionContext(final LogicSQL logicSQL, final ShardingSphereMetaData metaData, final RouteContext routeContext, final SQLRewriteResult rewriteResult) %7B%0A        return
  new
@@ -3809,17 +3809,29 @@
 Context(
-s
+logicSQL.getS
 qlStatem
@@ -3840,27 +3840,29 @@
 tContext
+()
 , 
-e
+E
 xecution
 Units, r
@@ -3857,92 +3857,103 @@
 tion
-Units, routeContext);%0A       
+ContextBuilder.build(metaData, rewriteResult,
  log
+ic
 SQL
-(logicSQL
+.getSqlStatementContext())
 , 
-p
 ro
-ps, result);%0A        return result
+uteContext)
 ;%0A  

Refactor KernelProcessor (#8715)