View Javadoc
1   /**
2    *    Copyright 2009-2015 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.executor.Executor;
24  import org.apache.ibatis.executor.ExecutorException;
25  import org.apache.ibatis.executor.parameter.ParameterHandler;
26  import org.apache.ibatis.mapping.BoundSql;
27  import org.apache.ibatis.mapping.MappedStatement;
28  import org.apache.ibatis.session.ResultHandler;
29  import org.apache.ibatis.session.RowBounds;
30  
31  /**
32   * @author Clinton Begin
33   */
34  public class RoutingStatementHandler implements StatementHandler {
35  
36    private final StatementHandler delegate;
37  
38    public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
39  
40      switch (ms.getStatementType()) {
41        case STATEMENT:
42          delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
43          break;
44        case PREPARED:
45          delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
46          break;
47        case CALLABLE:
48          delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
49          break;
50        default:
51          throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
52      }
53  
54    }
55  
56    @Override
57    public Statement prepare(Connection connection) throws SQLException {
58      return delegate.prepare(connection);
59    }
60  
61    @Override
62    public void parameterize(Statement statement) throws SQLException {
63      delegate.parameterize(statement);
64    }
65  
66    @Override
67    public void batch(Statement statement) throws SQLException {
68      delegate.batch(statement);
69    }
70  
71    @Override
72    public int update(Statement statement) throws SQLException {
73      return delegate.update(statement);
74    }
75  
76    @Override
77    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
78      return delegate.<E>query(statement, resultHandler);
79    }
80  
81    @Override
82    public BoundSql getBoundSql() {
83      return delegate.getBoundSql();
84    }
85  
86    @Override
87    public ParameterHandler getParameterHandler() {
88      return delegate.getParameterHandler();
89    }
90  }