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.proxy.backend.connector.jdbc.executor;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
22  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
23  import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
24  import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutionUnit;
25  import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
27  import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
28  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
29  import org.apache.shardingsphere.infra.session.query.QueryContext;
30  import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
31  import org.apache.shardingsphere.proxy.backend.connector.DatabaseConnector;
32  import org.apache.shardingsphere.proxy.backend.connector.jdbc.executor.callback.ProxyJDBCExecutorCallbackFactory;
33  import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
34  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
35  
36  import java.sql.SQLException;
37  import java.util.List;
38  
39  /**
40   * Proxy JDBC executor.
41   */
42  @RequiredArgsConstructor
43  public final class ProxyJDBCExecutor {
44      
45      private final String type;
46      
47      private final ConnectionSession connectionSession;
48      
49      private final DatabaseConnector databaseConnector;
50      
51      private final JDBCExecutor jdbcExecutor;
52      
53      private final ProcessEngine processEngine = new ProcessEngine();
54      
55      /**
56       * Execute.
57       * 
58       * @param queryContext query context
59       * @param executionGroupContext execution group context
60       * @param isReturnGeneratedKeys is return generated keys
61       * @param isExceptionThrown is exception thrown
62       * @return execute results
63       * @throws SQLException SQL exception
64       */
65      public List<ExecuteResult> execute(final QueryContext queryContext, final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext,
66                                         final boolean isReturnGeneratedKeys, final boolean isExceptionThrown) throws SQLException {
67          try {
68              MetaDataContexts metaDataContexts = ProxyContext.getInstance().getContextManager().getMetaDataContexts();
69              ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(connectionSession.getDatabaseName());
70              DatabaseType protocolType = database.getProtocolType();
71              processEngine.executeSQL(executionGroupContext, queryContext);
72              SQLStatementContext context = queryContext.getSqlStatementContext();
73              return jdbcExecutor.execute(executionGroupContext,
74                      ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, database.getResourceMetaData(), context.getSqlStatement(), databaseConnector, isReturnGeneratedKeys,
75                              isExceptionThrown,
76                              true),
77                      ProxyJDBCExecutorCallbackFactory.newInstance(type, protocolType, database.getResourceMetaData(), context.getSqlStatement(), databaseConnector, isReturnGeneratedKeys,
78                              isExceptionThrown,
79                              false));
80          } finally {
81              processEngine.completeSQLExecution(executionGroupContext.getReportContext().getProcessId());
82          }
83      }
84  }