View Javadoc
1   /**
2    *    Copyright 2009-2019 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.apache.ibatis.executor.statement;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  import java.util.List;
22  
23  import org.apache.ibatis.cursor.Cursor;
24  import org.apache.ibatis.executor.Executor;
25  import org.apache.ibatis.executor.ExecutorException;
26  import org.apache.ibatis.executor.parameter.ParameterHandler;
27  import org.apache.ibatis.mapping.BoundSql;
28  import org.apache.ibatis.mapping.MappedStatement;
29  import org.apache.ibatis.session.ResultHandler;
30  import org.apache.ibatis.session.RowBounds;
31  
32  /**
33   * @author Clinton Begin
34   */
35  public class RoutingStatementHandler implements StatementHandler {
36  
37    private final StatementHandler delegate;
38  
39    public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
40  
41      switch (ms.getStatementType()) {
42        case STATEMENT:
43          delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
44          break;
45        case PREPARED:
46          delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
47          break;
48        case CALLABLE:
49          delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
50          break;
51        default:
52          throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
53      }
54  
55    }
56  
57    @Override
58    public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
59      return delegate.prepare(connection, transactionTimeout);
60    }
61  
62    @Override
63    public void parameterize(Statement statement) throws SQLException {
64      delegate.parameterize(statement);
65    }
66  
67    @Override
68    public void batch(Statement statement) throws SQLException {
69      delegate.batch(statement);
70    }
71  
72    @Override
73    public int update(Statement statement) throws SQLException {
74      return delegate.update(statement);
75    }
76  
77    @Override
78    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
79      return delegate.query(statement, resultHandler);
80    }
81  
82    @Override
83    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
84      return delegate.queryCursor(statement);
85    }
86  
87    @Override
88    public BoundSql getBoundSql() {
89      return delegate.getBoundSql();
90    }
91  
92    @Override
93    public ParameterHandler getParameterHandler() {
94      return delegate.getParameterHandler();
95    }
96  }