SimpleExecutor.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;

  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,
  52.       BoundSql boundSql) throws SQLException {
  53.     Statement stmt = null;
  54.     try {
  55.       Configuration configuration = ms.getConfiguration();
  56.       StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler,
  57.           boundSql);
  58.       stmt = prepareStatement(handler, ms.getStatementLog());
  59.       return handler.query(stmt, resultHandler);
  60.     } finally {
  61.       closeStatement(stmt);
  62.     }
  63.   }

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

  74.   @Override
  75.   public List<BatchResult> doFlushStatements(boolean isRollback) {
  76.     return Collections.emptyList();
  77.   }

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

  85. }