1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.executor.statement;
17
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
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.executor.keygen.SelectKeyGenerator;
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
35
36 public class SimpleStatementHandler extends BaseStatementHandler {
37
38 public SimpleStatementHandler(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 String sql = boundSql.getSql();
45 Object parameterObject = boundSql.getParameterObject();
46 KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
47 int rows;
48 if (keyGenerator instanceof Jdbc3KeyGenerator) {
49 statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
50 rows = statement.getUpdateCount();
51 keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
52 } else if (keyGenerator instanceof SelectKeyGenerator) {
53 statement.execute(sql);
54 rows = statement.getUpdateCount();
55 keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
56 } else {
57 statement.execute(sql);
58 rows = statement.getUpdateCount();
59 }
60 return rows;
61 }
62
63 @Override
64 public void batch(Statement statement) throws SQLException {
65 String sql = boundSql.getSql();
66 statement.addBatch(sql);
67 }
68
69 @Override
70 public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
71 String sql = boundSql.getSql();
72 statement.execute(sql);
73 return resultSetHandler.<E>handleResultSets(statement);
74 }
75
76 @Override
77 protected Statement instantiateStatement(Connection connection) throws SQLException {
78 if (mappedStatement.getResultSetType() != null) {
79 return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
80 } else {
81 return connection.createStatement();
82 }
83 }
84
85 @Override
86 public void parameterize(Statement statement) throws SQLException {
87
88 }
89
90 }