SimpleStatementHandler.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.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.List;

  22. import org.apache.ibatis.cursor.Cursor;
  23. import org.apache.ibatis.executor.Executor;
  24. import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
  25. import org.apache.ibatis.executor.keygen.KeyGenerator;
  26. import org.apache.ibatis.executor.keygen.SelectKeyGenerator;
  27. import org.apache.ibatis.mapping.BoundSql;
  28. import org.apache.ibatis.mapping.MappedStatement;
  29. import org.apache.ibatis.mapping.ResultSetType;
  30. import org.apache.ibatis.session.ResultHandler;
  31. import org.apache.ibatis.session.RowBounds;

  32. /**
  33.  * @author Clinton Begin
  34.  */
  35. public class SimpleStatementHandler extends BaseStatementHandler {

  36.   public SimpleStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter,
  37.       RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
  38.     super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
  39.   }

  40.   @Override
  41.   public int update(Statement statement) throws SQLException {
  42.     String sql = boundSql.getSql();
  43.     Object parameterObject = boundSql.getParameterObject();
  44.     KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
  45.     int rows;
  46.     if (keyGenerator instanceof Jdbc3KeyGenerator) {
  47.       statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
  48.       rows = statement.getUpdateCount();
  49.       keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
  50.     } else if (keyGenerator instanceof SelectKeyGenerator) {
  51.       statement.execute(sql);
  52.       rows = statement.getUpdateCount();
  53.       keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
  54.     } else {
  55.       statement.execute(sql);
  56.       rows = statement.getUpdateCount();
  57.     }
  58.     return rows;
  59.   }

  60.   @Override
  61.   public void batch(Statement statement) throws SQLException {
  62.     String sql = boundSql.getSql();
  63.     statement.addBatch(sql);
  64.   }

  65.   @Override
  66.   public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  67.     String sql = boundSql.getSql();
  68.     statement.execute(sql);
  69.     return resultSetHandler.handleResultSets(statement);
  70.   }

  71.   @Override
  72.   public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
  73.     String sql = boundSql.getSql();
  74.     statement.execute(sql);
  75.     return resultSetHandler.handleCursorResultSets(statement);
  76.   }

  77.   @Override
  78.   protected Statement instantiateStatement(Connection connection) throws SQLException {
  79.     if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
  80.       return connection.createStatement();
  81.     }
  82.     return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
  83.   }

  84.   @Override
  85.   public void parameterize(Statement statement) {
  86.     // N/A
  87.   }

  88. }