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