View Javadoc
1   /**
2    *    Copyright 2009-2015 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.executor.Executor;
26  import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
27  import org.apache.ibatis.executor.keygen.KeyGenerator;
28  import org.apache.ibatis.mapping.BoundSql;
29  import org.apache.ibatis.mapping.MappedStatement;
30  import org.apache.ibatis.session.ResultHandler;
31  import org.apache.ibatis.session.RowBounds;
32  
33  /**
34   * @author Clinton Begin
35   */
36  public class PreparedStatementHandler extends BaseStatementHandler {
37  
38    public PreparedStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
39      super(executor, mappedStatement, parameter, rowBounds, resultHandler, boundSql);
40    }
41  
42    @Override
43    public int update(Statement statement) throws SQLException {
44      PreparedStatement ps = (PreparedStatement) statement;
45      ps.execute();
46      int rows = ps.getUpdateCount();
47      Object parameterObject = boundSql.getParameterObject();
48      KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
49      keyGenerator.processAfter(executor, mappedStatement, ps, parameterObject);
50      return rows;
51    }
52  
53    @Override
54    public void batch(Statement statement) throws SQLException {
55      PreparedStatement ps = (PreparedStatement) statement;
56      ps.addBatch();
57    }
58  
59    @Override
60    public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
61      PreparedStatement ps = (PreparedStatement) statement;
62      ps.execute();
63      return resultSetHandler.<E> handleResultSets(ps);
64    }
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() != null) {
77        return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
78      } else {
79        return connection.prepareStatement(sql);
80      }
81    }
82  
83    @Override
84    public void parameterize(Statement statement) throws SQLException {
85      parameterHandler.setParameters((PreparedStatement) statement);
86    }
87  
88  }