View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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,
58        BoundSql boundSql) throws SQLException {
59      Statement stmt = null;
60      try {
61        Configuration configuration = ms.getConfiguration();
62        StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler,
63            boundSql);
64        stmt = prepareStatement(handler, ms.getStatementLog());
65        return handler.query(stmt, resultHandler);
66      } finally {
67        closeStatement(stmt);
68      }
69    }
70  
71    @Override
72    protected <E> Cursor<E> doQueryCursor(MappedStatement ms, Object parameter, RowBounds rowBounds, BoundSql boundSql)
73        throws SQLException {
74      Configuration configuration = ms.getConfiguration();
75      StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, null, boundSql);
76      Statement stmt = prepareStatement(handler, ms.getStatementLog());
77      Cursor<E> cursor = handler.queryCursor(stmt);
78      stmt.closeOnCompletion();
79      return cursor;
80    }
81  
82    @Override
83    public List<BatchResult> doFlushStatements(boolean isRollback) {
84      return Collections.emptyList();
85    }
86  
87    private Statement prepareStatement(StatementHandler handler, Log statementLog) throws SQLException {
88      Statement stmt;
89      Connection connection = getConnection(statementLog);
90      stmt = handler.prepare(connection, transaction.getTimeout());
91      handler.parameterize(stmt);
92      return stmt;
93    }
94  
95  }