CallableStatementHandler.java

  1. /*
  2.  *    Copyright 2009-2024 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.CallableStatement;
  18. import java.sql.Connection;
  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.ExecutorException;
  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.ParameterMapping;
  30. import org.apache.ibatis.mapping.ParameterMode;
  31. import org.apache.ibatis.mapping.ResultSetType;
  32. import org.apache.ibatis.session.ResultHandler;
  33. import org.apache.ibatis.session.RowBounds;
  34. import org.apache.ibatis.type.JdbcType;

  35. /**
  36.  * @author Clinton Begin
  37.  */
  38. public class CallableStatementHandler extends BaseStatementHandler {

  39.   public CallableStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter,
  40.       RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
  41.     super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
  42.   }

  43.   @Override
  44.   public int update(Statement statement) throws SQLException {
  45.     CallableStatement cs = (CallableStatement) statement;
  46.     cs.execute();
  47.     int rows = cs.getUpdateCount();
  48.     Object parameterObject = boundSql.getParameterObject();
  49.     KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
  50.     keyGenerator.processAfter(executor, mappedStatement, cs, parameterObject);
  51.     resultSetHandler.handleOutputParameters(cs);
  52.     return rows;
  53.   }

  54.   @Override
  55.   public void batch(Statement statement) throws SQLException {
  56.     CallableStatement cs = (CallableStatement) statement;
  57.     cs.addBatch();
  58.   }

  59.   @Override
  60.   public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
  61.     CallableStatement cs = (CallableStatement) statement;
  62.     cs.execute();
  63.     List<E> resultList = resultSetHandler.handleResultSets(cs);
  64.     resultSetHandler.handleOutputParameters(cs);
  65.     return resultList;
  66.   }

  67.   @Override
  68.   public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
  69.     CallableStatement cs = (CallableStatement) statement;
  70.     cs.execute();
  71.     Cursor<E> resultList = resultSetHandler.handleCursorResultSets(cs);
  72.     resultSetHandler.handleOutputParameters(cs);
  73.     return resultList;
  74.   }

  75.   @Override
  76.   protected Statement instantiateStatement(Connection connection) throws SQLException {
  77.     String sql = boundSql.getSql();
  78.     if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
  79.       return connection.prepareCall(sql);
  80.     }
  81.     return connection.prepareCall(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
  82.   }

  83.   @Override
  84.   public void parameterize(Statement statement) throws SQLException {
  85.     registerOutputParameters((CallableStatement) statement);
  86.     parameterHandler.setParameters((CallableStatement) statement);
  87.   }

  88.   private void registerOutputParameters(CallableStatement cs) throws SQLException {
  89.     List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  90.     for (int i = 0, n = parameterMappings.size(); i < n; i++) {
  91.       ParameterMapping parameterMapping = parameterMappings.get(i);
  92.       if (parameterMapping.getMode() == ParameterMode.OUT || parameterMapping.getMode() == ParameterMode.INOUT) {
  93.         if (null == parameterMapping.getJdbcType()) {
  94.           throw new ExecutorException(
  95.               "The JDBC Type must be specified for output parameter.  Parameter: " + parameterMapping.getProperty());
  96.         }
  97.         if (parameterMapping.getNumericScale() != null && (parameterMapping.getJdbcType() == JdbcType.NUMERIC
  98.             || parameterMapping.getJdbcType() == JdbcType.DECIMAL)) {
  99.           cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE, parameterMapping.getNumericScale());
  100.         } else {
  101.           if (parameterMapping.getJdbcTypeName() == null) {
  102.             cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE);
  103.           } else {
  104.             cs.registerOutParameter(i + 1, parameterMapping.getJdbcType().TYPE_CODE,
  105.                 parameterMapping.getJdbcTypeName());
  106.           }
  107.         }
  108.       }
  109.     }
  110.   }

  111. }