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.driver.executor;
19  
20  import lombok.Getter;
21  import org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
22  import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
23  import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
24  import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
25  import org.apache.shardingsphere.infra.executor.sql.execute.engine.raw.RawExecutor;
26  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
27  import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
28  import org.apache.shardingsphere.sqlfederation.engine.SQLFederationEngine;
29  import org.apache.shardingsphere.traffic.executor.TrafficExecutor;
30  
31  import java.sql.SQLException;
32  
33  /**
34   * Driver executor.
35   */
36  @Getter
37  public final class DriverExecutor implements AutoCloseable {
38      
39      private final DriverJDBCExecutor regularExecutor;
40      
41      private final RawExecutor rawExecutor;
42      
43      private final SQLFederationEngine sqlFederationEngine;
44      
45      private final TrafficExecutor trafficExecutor;
46      
47      public DriverExecutor(final ShardingSphereConnection connection) {
48          MetaDataContexts metaDataContexts = connection.getContextManager().getMetaDataContexts();
49          ExecutorEngine executorEngine = connection.getContextManager().getExecutorEngine();
50          JDBCExecutor jdbcExecutor = new JDBCExecutor(executorEngine, connection.getDatabaseConnectionManager().getConnectionContext());
51          regularExecutor = new DriverJDBCExecutor(connection.getDatabaseName(), connection.getContextManager(), jdbcExecutor);
52          rawExecutor = new RawExecutor(executorEngine, connection.getDatabaseConnectionManager().getConnectionContext());
53          ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(connection.getDatabaseName());
54          String schemaName = new DatabaseTypeRegistry(database.getProtocolType()).getDefaultSchemaName(connection.getDatabaseName());
55          sqlFederationEngine = new SQLFederationEngine(connection.getDatabaseName(), schemaName, metaDataContexts.getMetaData(), metaDataContexts.getStatistics(), jdbcExecutor);
56          trafficExecutor = new TrafficExecutor();
57      }
58      
59      /**
60       * Close.
61       *
62       * @throws SQLException SQL exception
63       */
64      @Override
65      public void close() throws SQLException {
66          sqlFederationEngine.close();
67          trafficExecutor.close();
68      }
69  }