SimpleStatementHandler.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.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, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
  37.     super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
  38.   }

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

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

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

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

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

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

  88. }