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

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

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

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

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

  85.   @Override
  86.   public void parameterize(Statement statement) throws SQLException {
  87.     parameterHandler.setParameters((PreparedStatement) statement);
  88.   }

  89. }