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.raw;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.executor.kernel.ExecutorEngine;
22  import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
23  import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutorExceptionHandler;
24  import org.apache.shardingsphere.infra.executor.sql.execute.engine.raw.callback.RawSQLExecutorCallback;
25  import org.apache.shardingsphere.infra.executor.sql.execute.result.ExecuteResult;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
27  import org.apache.shardingsphere.infra.executor.sql.process.ProcessEngine;
28  import org.apache.shardingsphere.infra.session.connection.ConnectionContext;
29  import org.apache.shardingsphere.infra.session.query.QueryContext;
30  
31  import java.sql.SQLException;
32  import java.util.Collections;
33  import java.util.List;
34  
35  /**
36   * Raw executor.
37   */
38  @RequiredArgsConstructor
39  public final class RawExecutor {
40      
41      private final ExecutorEngine executorEngine;
42      
43      private final ConnectionContext connectionContext;
44      
45      private final ProcessEngine processEngine = new ProcessEngine();
46      
47      /**
48       * Execute.
49       *
50       * @param executionGroupContext execution group context
51       * @param queryContext query context
52       * @param callback raw SQL executor callback
53       * @return execute results
54       * @throws SQLException SQL exception
55       */
56      public List<ExecuteResult> execute(final ExecutionGroupContext<RawSQLExecutionUnit> executionGroupContext,
57                                         final QueryContext queryContext, final RawSQLExecutorCallback callback) throws SQLException {
58          try {
59              processEngine.executeSQL(executionGroupContext, queryContext);
60              // TODO Load query header for first query
61              List<ExecuteResult> results = execute(executionGroupContext, (RawSQLExecutorCallback) null, callback);
62              return results.isEmpty() || null == results.get(0) ? Collections.singletonList(new UpdateResult(0, 0L)) : results;
63          } finally {
64              processEngine.completeSQLExecution(executionGroupContext.getReportContext().getProcessId());
65          }
66      }
67      
68      @SuppressWarnings("unchecked")
69      private <T> List<T> execute(final ExecutionGroupContext<RawSQLExecutionUnit> executionGroupContext,
70                                  final RawSQLExecutorCallback firstCallback, final RawSQLExecutorCallback callback) throws SQLException {
71          try {
72              return (List<T>) executorEngine.execute(executionGroupContext, firstCallback, callback, connectionContext.getTransactionContext().isInTransaction());
73          } catch (final SQLException ex) {
74              SQLExecutorExceptionHandler.handleException(ex);
75              return Collections.emptyList();
76          }
77      }
78  }