View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.connection.kernel;
19  
20  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
21  import org.apache.shardingsphere.infra.checker.SupportedSQLCheckEngine;
22  import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
23  import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
24  import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
25  import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContextBuilder;
26  import org.apache.shardingsphere.infra.executor.sql.log.SQLLogger;
27  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
28  import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
29  import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
30  import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult;
31  import org.apache.shardingsphere.infra.route.context.RouteContext;
32  import org.apache.shardingsphere.infra.route.engine.SQLRouteEngine;
33  import org.apache.shardingsphere.infra.session.query.QueryContext;
34  
35  /**
36   * Kernel processor.
37   */
38  @HighFrequencyInvocation
39  public final class KernelProcessor {
40      
41      /**
42       * Generate execution context.
43       *
44       * @param queryContext query context
45       * @param globalRuleMetaData global rule meta data
46       * @param props configuration properties
47       * @return execution context
48       */
49      public ExecutionContext generateExecutionContext(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ConfigurationProperties props) {
50          check(queryContext);
51          RouteContext routeContext = route(queryContext, globalRuleMetaData, props);
52          SQLRewriteResult rewriteResult = rewrite(queryContext, globalRuleMetaData, props, routeContext);
53          ExecutionContext result = createExecutionContext(queryContext, routeContext, rewriteResult);
54          logSQL(queryContext, props, result);
55          return result;
56      }
57      
58      private void check(final QueryContext queryContext) {
59          if (queryContext.getHintValueContext().isSkipMetadataValidate()) {
60              return;
61          }
62          ShardingSphereDatabase database = queryContext.getUsedDatabase();
63          new SupportedSQLCheckEngine().checkSQL(database.getRuleMetaData().getRules(), queryContext.getSqlStatementContext(), database);
64      }
65      
66      private RouteContext route(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ConfigurationProperties props) {
67          ShardingSphereDatabase database = queryContext.getUsedDatabase();
68          return new SQLRouteEngine(database.getRuleMetaData().getRules(), props).route(queryContext, globalRuleMetaData, database);
69      }
70      
71      private SQLRewriteResult rewrite(final QueryContext queryContext, final RuleMetaData globalRuleMetaData, final ConfigurationProperties props, final RouteContext routeContext) {
72          SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(queryContext.getUsedDatabase(), globalRuleMetaData, props);
73          return sqlRewriteEntry.rewrite(queryContext, routeContext);
74      }
75      
76      private ExecutionContext createExecutionContext(final QueryContext queryContext, final RouteContext routeContext, final SQLRewriteResult rewriteResult) {
77          return new ExecutionContext(queryContext, ExecutionContextBuilder.build(queryContext.getUsedDatabase(), rewriteResult, queryContext.getSqlStatementContext()), routeContext);
78      }
79      
80      private void logSQL(final QueryContext queryContext, final ConfigurationProperties props, final ExecutionContext executionContext) {
81          if (props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW)) {
82              SQLLogger.logSQL(queryContext, props.<Boolean>getValue(ConfigurationPropertyKey.SQL_SIMPLE), executionContext);
83          }
84      }
85  }