View Javadoc
1   /**
2    *    Copyright 2009-2018 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  
18  import java.sql.Connection;
19  import java.sql.PreparedStatement;
20  import java.sql.ResultSet;
21  import java.sql.SQLException;
22  import java.sql.Statement;
23  import java.util.List;
24  
25  import org.apache.ibatis.cursor.Cursor;
26  import org.apache.ibatis.executor.Executor;
27  import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
28  import org.apache.ibatis.executor.keygen.KeyGenerator;
29  import org.apache.ibatis.mapping.BoundSql;
30  import org.apache.ibatis.mapping.MappedStatement;
31  import org.apache.ibatis.mapping.ResultSetType;
32  import org.apache.ibatis.session.ResultHandler;
33  import org.apache.ibatis.session.RowBounds;
34  
35  /**
36   * @author Clinton Begin
37   */
38  public class PreparedStatementHandler extends BaseStatementHandler {
39  
40    public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
41      super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
42    }
43  
44    @Override
45    public int update(Statement statement) throws SQLException {
46      PreparedStatement ps = (PreparedStatement) statement;
47      ps.execute();
48      int rows = ps.getUpdateCount();
49      Object parameterObject = boundSql.getParameterObject();
50      KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
51      keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
52      return rows;
53    }
54  
55    @Override
56    public void batch(Statement statement) throws SQLException {
57      PreparedStatement ps = (PreparedStatement) statement;
58      ps.addBatch();
59    }
60  
61    @Override
62    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
63      PreparedStatement ps = (PreparedStatement) statement;
64      ps.execute();
65      return resultSetHandler.handleResultSets(ps);
66    }
67  
68    @Override
69    public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
70      PreparedStatement ps = (PreparedStatement) statement;
71      ps.execute();
72      return resultSetHandler.handleCursorResultSets(ps);
73    }
74  
75    @Override
76    protected Statement instantiateStatement(Connection connection) throws SQLException {
77      String sql = boundSql.getSql();
78      if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
79        String[] keyColumnNames = mappedStatement.getKeyColumns();
80        if (keyColumnNames == null) {
81          return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
82        } else {
83          return connection.prepareStatement(sql, keyColumnNames);
84        }
85      } else if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
86        return connection.prepareStatement(sql);
87      } else {
88        return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
89      }
90    }
91  
92    @Override
93    public void parameterize(Statement statement) throws SQLException {
94      parameterHandler.setParameters((PreparedStatement) statement);
95    }
96  
97  }