SimpleExecutor.java

  1. /*
  2.  *    Copyright 2009-2021 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;

  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. import java.sql.Statement;
  20. import java.util.Collections;
  21. import java.util.List;

  22. import org.apache.ibatis.cursor.Cursor;
  23. import org.apache.ibatis.executor.statement.StatementHandler;
  24. import org.apache.ibatis.logging.Log;
  25. import org.apache.ibatis.mapping.BoundSql;
  26. import org.apache.ibatis.mapping.MappedStatement;
  27. import org.apache.ibatis.session.Configuration;
  28. import org.apache.ibatis.session.ResultHandler;
  29. import org.apache.ibatis.session.RowBounds;
  30. import org.apache.ibatis.transaction.Transaction;

  31. /**
  32.  * @author Clinton Begin
  33.  */
  34. public class SimpleExecutor extends BaseExecutor {

  35.   public SimpleExecutor(Configuration configuration, Transaction transaction) {
  36.     super(configuration, transaction);
  37.   }

  38.   @Override
  39.   public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
  40.     Statement stmt = null;
  41.     try {
  42.       Configuration configuration = ms.getConfiguration();
  43.       StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
  44.       stmt = prepareStatement(handler, ms.getStatementLog());
  45.       return handler.update(stmt);
  46.     } finally {
  47.       closeStatement(stmt);
  48.     }
  49.   }

  50.   @Override
  51.   public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
  52.     Statement stmt = null;
  53.     try {
  54.       Configuration configuration = ms.getConfiguration();
  55.       StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
  56.       stmt = prepareStatement(handler, ms.getStatementLog());
  57.       return handler.query(stmt, resultHandler);
  58.     } finally {
  59.       closeStatement(stmt);
  60.     }
  61.   }

  62.   @Override
  63.   protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
  64.     Configuration configuration = ms.getConfiguration();
  65.     StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
  66.     Statement stmt = prepareStatement(handler, ms.getStatementLog());
  67.     Cursor<E> cursor = handler.queryCursor(stmt);
  68.     stmt.closeOnCompletion();
  69.     return cursor;
  70.   }

  71.   @Override
  72.   public List<BatchResult> doFlushStatements(boolean isRollback) {
  73.     return Collections.emptyList();
  74.   }

  75.   private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
  76.     Statement stmt;
  77.     Connection connection = getConnection(statementLog);
  78.     stmt = handler.prepare(connection, transaction.getTimeout());
  79.     handler.parameterize(stmt);
  80.     return stmt;
  81.   }

  82. }