RoutingStatementHandler.java

  1. /*
  2.  *    Copyright 2009-2023 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.  *       https://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. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20. import java.util.List;

  21. import org.apache.ibatis.cursor.Cursor;
  22. import org.apache.ibatis.executor.Executor;
  23. import org.apache.ibatis.executor.ExecutorException;
  24. import org.apache.ibatis.executor.parameter.ParameterHandler;
  25. import org.apache.ibatis.mapping.BoundSql;
  26. import org.apache.ibatis.mapping.MappedStatement;
  27. import org.apache.ibatis.session.ResultHandler;
  28. import org.apache.ibatis.session.RowBounds;

  29. /**
  30.  * @author Clinton Begin
  31.  */
  32. public class RoutingStatementHandler implements StatementHandler {

  33.   private final StatementHandler delegate;

  34.   public RoutingStatementHandler(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds,
  35.       ResultHandler resultHandler, BoundSql boundSql) {

  36.     switch (ms.getStatementType()) {
  37.       case STATEMENT:
  38.         delegate = new SimpleStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
  39.         break;
  40.       case PREPARED:
  41.         delegate = new PreparedStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
  42.         break;
  43.       case CALLABLE:
  44.         delegate = new CallableStatementHandler(executor, ms, parameter, rowBounds, resultHandler, boundSql);
  45.         break;
  46.       default:
  47.         throw new ExecutorException("Unknown statement type: " + ms.getStatementType());
  48.     }

  49.   }

  50.   @Override
  51.   public Statement prepare(Connection connection, Integer transactionTimeout) throws SQLException {
  52.     return delegate.prepare(connection, transactionTimeout);
  53.   }

  54.   @Override
  55.   public void parameterize(Statement statement) throws SQLException {
  56.     delegate.parameterize(statement);
  57.   }

  58.   @Override
  59.   public void batch(Statement statement) throws SQLException {
  60.     delegate.batch(statement);
  61.   }

  62.   @Override
  63.   public int update(Statement statement) throws SQLException {
  64.     return delegate.update(statement);
  65.   }

  66.   @Override
  67.   public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  68.     return delegate.query(statement, resultHandler);
  69.   }

  70.   @Override
  71.   public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
  72.     return delegate.queryCursor(statement);
  73.   }

  74.   @Override
  75.   public BoundSql getBoundSql() {
  76.     return delegate.getBoundSql();
  77.   }

  78.   @Override
  79.   public ParameterHandler getParameterHandler() {
  80.     return delegate.getParameterHandler();
  81.   }
  82. }