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.statement;
19  
20  import org.apache.shardingsphere.db.protocol.parameter.TypeUnspecifiedSQLParameter;
21  import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
22  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
23  import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
24  import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;
25  import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.ExecutorJDBCStatementManager;
26  import org.apache.shardingsphere.infra.executor.sql.prepare.driver.jdbc.StatementOption;
27  
28  import java.sql.Connection;
29  import java.sql.PreparedStatement;
30  import java.sql.SQLException;
31  import java.sql.Statement;
32  import java.sql.Types;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.Optional;
36  
37  /**
38   * JDBC backend statement.
39   */
40  public final class JDBCBackendStatement implements ExecutorJDBCStatementManager {
41      
42      @Override
43      public Statement createStorageResource(final Connection connection, final ConnectionMode connectionMode, final StatementOption option, final DatabaseType databaseType) throws SQLException {
44          Statement result = connection.createStatement();
45          if (ConnectionMode.MEMORY_STRICTLY == connectionMode) {
46              setFetchSize(result, databaseType);
47          }
48          return result;
49      }
50      
51      @Override
52      public Statement createStorageResource(final ExecutionUnit executionUnit, final Connection connection, final ConnectionMode connectionMode, final StatementOption option,
53                                             final DatabaseType databaseType) throws SQLException {
54          String sql = executionUnit.getSqlUnit().getSql();
55          List<Object> params = executionUnit.getSqlUnit().getParameters();
56          PreparedStatement result = option.isReturnGeneratedKeys()
57                  ? connection.prepareStatement(executionUnit.getSqlUnit().getSql(), Statement.RETURN_GENERATED_KEYS)
58                  : connection.prepareStatement(sql);
59          Iterator<Object> paramIterator = params.iterator();
60          int index = 0;
61          while (paramIterator.hasNext()) {
62              Object param = paramIterator.next();
63              if (param instanceof TypeUnspecifiedSQLParameter) {
64                  result.setObject(index + 1, param, Types.OTHER);
65              } else {
66                  result.setObject(index + 1, param);
67              }
68              index++;
69          }
70          if (ConnectionMode.MEMORY_STRICTLY == connectionMode) {
71              setFetchSize(result, databaseType);
72          }
73          return result;
74      }
75      
76      private void setFetchSize(final Statement statement, final DatabaseType databaseType) throws SQLException {
77          Optional<StatementMemoryStrictlyFetchSizeSetter> fetchSizeSetter = DatabaseTypedSPILoader.findService(StatementMemoryStrictlyFetchSizeSetter.class, databaseType);
78          if (fetchSizeSetter.isPresent()) {
79              fetchSizeSetter.get().setFetchSize(statement);
80          }
81      }
82  }