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

  23. import org.apache.ibatis.cursor.Cursor;
  24. import org.apache.ibatis.executor.Executor;
  25. import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
  26. import org.apache.ibatis.executor.keygen.KeyGenerator;
  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 PreparedStatementHandler extends BaseStatementHandler {

  36.   public PreparedStatementHandler(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.     PreparedStatement ps = (PreparedStatement) statement;
  42.     ps.execute();
  43.     int rows = ps.getUpdateCount();
  44.     Object parameterObject = boundSql.getParameterObject();
  45.     KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
  46.     keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
  47.     return rows;
  48.   }

  49.   @Override
  50.   public void batch(Statement statement) throws SQLException {
  51.     PreparedStatement ps = (PreparedStatement) statement;
  52.     ps.addBatch();
  53.   }

  54.   @Override
  55.   public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  56.     PreparedStatement ps = (PreparedStatement) statement;
  57.     ps.execute();
  58.     return resultSetHandler.handleResultSets(ps);
  59.   }

  60.   @Override
  61.   public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
  62.     PreparedStatement ps = (PreparedStatement) statement;
  63.     ps.execute();
  64.     return resultSetHandler.handleCursorResultSets(ps);
  65.   }

  66.   @Override
  67.   protected Statement instantiateStatement(Connection connection) throws SQLException {
  68.     String sql = boundSql.getSql();
  69.     if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
  70.       String[] keyColumnNames = mappedStatement.getKeyColumns();
  71.       if (keyColumnNames == null) {
  72.         return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
  73.       } else {
  74.         return connection.prepareStatement(sql, keyColumnNames);
  75.       }
  76.     } else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
  77.       return connection.prepareStatement(sql);
  78.     } else {
  79.       return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
  80.     }
  81.   }

  82.   @Override
  83.   public void parameterize(Statement statement) throws SQLException {
  84.     parameterHandler.setParameters((PreparedStatement) statement);
  85.   }

  86. }