View Javadoc
1   /*
2    *    Copyright 2009-2021 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;
17  
18  import java.sql.Connection;
19  import java.sql.SQLException;
20  import java.sql.Statement;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.apache.ibatis.cursor.Cursor;
25  import org.apache.ibatis.executor.statement.StatementHandler;
26  import org.apache.ibatis.logging.Log;
27  import org.apache.ibatis.mapping.BoundSql;
28  import org.apache.ibatis.mapping.MappedStatement;
29  import org.apache.ibatis.session.Configuration;
30  import org.apache.ibatis.session.ResultHandler;
31  import org.apache.ibatis.session.RowBounds;
32  import org.apache.ibatis.transaction.Transaction;
33  
34  /**
35   * @author Clinton Begin
36   */
37  public class SimpleExecutor extends BaseExecutor {
38  
39    public SimpleExecutor(Configuration configuration, Transaction transaction) {
40      super(configuration, transaction);
41    }
42  
43    @Override
44    public int doUpdate(MappedStatement ms, Object parameter) throws SQLException {
45      Statement stmt = null;
46      try {
47        Configuration configuration = ms.getConfiguration();
48        StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, RowBounds.DEFAULT, null, null);
49        stmt = prepareStatement(handler, ms.getStatementLog());
50        return handler.update(stmt);
51      } finally {
52        closeStatement(stmt);
53      }
54    }
55  
56    @Override
57    public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
58      Statement stmt = null;
59      try {
60        Configuration configuration = ms.getConfiguration();
61        StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
62        stmt = prepareStatement(handler, ms.getStatementLog());
63        return handler.query(stmt, resultHandler);
64      } finally {
65        closeStatement(stmt);
66      }
67    }
68  
69    @Override
70    protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql) throws SQLException {
71      Configuration configuration = ms.getConfiguration();
72      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
73      Statement stmt = prepareStatement(handler, ms.getStatementLog());
74      Cursor<E> cursor = handler.queryCursor(stmt);
75      stmt.closeOnCompletion();
76      return cursor;
77    }
78  
79    @Override
80    public List<BatchResult> doFlushStatements(boolean isRollback) {
81      return Collections.emptyList();
82    }
83  
84    private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
85      Statement stmt;
86      Connection connection = getConnection(statementLog);
87      stmt = handler.prepare(connection, transaction.getTimeout());
88      handler.parameterize(stmt);
89      return stmt;
90    }
91  
92  }