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.executor.sql.execute.engine.driver.jdbc;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
22  import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
23  import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
24  import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
25  
26  import java.sql.SQLException;
27  import java.util.Collections;
28  import java.util.List;
29  
30  /**
31   * JDBC executor.
32   */
33  @RequiredArgsConstructor
34  public final class JDBCExecutor {
35      
36      private final ExecutorEngine executorEngine;
37      
38      private final ConnectionContext connectionContext;
39      
40      /**
41       * Execute.
42       *
43       * @param executionGroupContext execution group context
44       * @param callback JDBC execute callback
45       * @param <T> class type of return value
46       * @return execute result
47       * @throws SQLException SQL exception
48       */
49      public <T> List<T> execute(final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext, final JDBCExecutorCallback<T> callback) throws SQLException {
50          return execute(executionGroupContext, null, callback);
51      }
52      
53      /**
54       * Execute.
55       *
56       * @param executionGroupContext execution group context
57       * @param firstCallback first JDBC execute callback
58       * @param callback JDBC execute callback
59       * @param <T> class type of return value
60       * @return execute result
61       * @throws SQLException SQL exception
62       */
63      public <T> List<T> execute(final ExecutionGroupContext<JDBCExecutionUnit> executionGroupContext,
64                                 final JDBCExecutorCallback<T> firstCallback, final JDBCExecutorCallback<T> callback) throws SQLException {
65          try {
66              return executorEngine.execute(executionGroupContext, firstCallback, callback, connectionContext.getTransactionContext().isInDistributedTransaction());
67          } catch (final SQLException ex) {
68              SQLExecutorExceptionHandler.handleException(ex);
69              return Collections.emptyList();
70          }
71      }
72  }