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