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.cursor.Cursor;
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.executor.keygen.SelectKeyGenerator;
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
37
38 public class SimpleStatementHandler extends BaseStatementHandler {
39
40 public SimpleStatementHandler(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 String sql = boundSql.getSql();
47 Object parameterObject = boundSql.getParameterObject();
48 KeyGenerator keyGenerator = mappedStatement.getKeyGenerator();
49 int rows;
50 if (keyGenerator instanceof Jdbc3KeyGenerator) {
51 statement.execute(sql, Statement.RETURN_GENERATED_KEYS);
52 rows = statement.getUpdateCount();
53 keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
54 } else if (keyGenerator instanceof SelectKeyGenerator) {
55 statement.execute(sql);
56 rows = statement.getUpdateCount();
57 keyGenerator.processAfter(executor, mappedStatement, statement, parameterObject);
58 } else {
59 statement.execute(sql);
60 rows = statement.getUpdateCount();
61 }
62 return rows;
63 }
64
65 @Override
66 public void batch(Statement statement) throws SQLException {
67 String sql = boundSql.getSql();
68 statement.addBatch(sql);
69 }
70
71 @Override
72 public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
73 String sql = boundSql.getSql();
74 statement.execute(sql);
75 return resultSetHandler.handleResultSets(statement);
76 }
77
78 @Override
79 public <E> Cursor<E> queryCursor(Statement statement) throws SQLException {
80 String sql = boundSql.getSql();
81 statement.execute(sql);
82 return resultSetHandler.handleCursorResultSets(statement);
83 }
84
85 @Override
86 protected Statement instantiateStatement(Connection connection) throws SQLException {
87 if (mappedStatement.getResultSetType() == ResultSetType.DEFAULT) {
88 return connection.createStatement();
89 } else {
90 return connection.createStatement(mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
91 }
92 }
93
94 @Override
95 public void parameterize(Statement statement) {
96
97 }
98
99 }