Open Source Repository

Home /ibatis/ibatis-sqlmap-3.0-beta9 | Repository Home



org/apache/ibatis/jdbc/SqlRunner.java
package org.apache.ibatis.jdbc;

import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.io.Resources;

import java.sql.*;
import java.util.*;

public class SqlRunner {

  public static final int NO_GENERATED_KEY = Integer.MIN_VALUE + 1001;

  private Connection connection;
  private TypeHandlerRegistry typeHandlerRegistry;
  private boolean useGeneratedKeySupport;

  public SqlRunner(Connection connection) {
    this.connection = connection;
    this.typeHandlerRegistry = new TypeHandlerRegistry();
  }

  public void setUseGeneratedKeySupport(boolean useGeneratedKeySupport) {
    this.useGeneratedKeySupport = useGeneratedKeySupport;
  }

  /**
   * Executes a SELECT statement that returns one row.
   *
   @param sql  The SQL
   @param args The arguments to be set on the statement.
   @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
   @throws SQLException If more than one row is returned
   */
  public Map<String, Object> selectOne(String sql, Object... argsthrows SQLException {
    List<Map<String, Object>> results = selectAll(sql, args);
    if (results.size() != 1) {
      throw new SQLException("Statement returned " + results.size() " results where exactly one (1) was expected.");
    }
    return results.get(0);
  }

  /**
   * Executes a SELECT statement that returns multiple rows.
   *
   @param sql  The SQL
   @param args The arguments to be set on the statement.
   @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
   @throws SQLException If statement prepration or execution fails
   */
  public List<Map<String, Object>> selectAll(String sql, Object... argsthrows SQLException {
    PreparedStatement ps = connection.prepareStatement(sql);
    try {
      setParameters(ps, args);
      ResultSet rs = ps.executeQuery();
      return getResults(rs);
    finally {
      try {
        ps.close();
      catch (SQLException e) {
        //ignore
      }
    }
  }

  /**
   * Executes an INSERT statement.
   *
   @param sql  The SQL
   @param args The arguments to be set on the statement.
   @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
   @throws SQLException If statement prepration or execution fails
   */
  public int insert(String sql, Object... argsthrows SQLException {
    PreparedStatement ps;
    if (useGeneratedKeySupport) {
      ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    else {
      ps = connection.prepareStatement(sql);
    }

    try {
      setParameters(ps, args);
      ps.executeUpdate();
      if (useGeneratedKeySupport) {
        List<Map<String, Object>> keys = getResults(ps.getGeneratedKeys());
        if (keys.size() == 1) {
          Map<String, Object> key = keys.get(0);
          Iterator i = key.values().iterator();
          if (i.hasNext()) {
            Object genkey = i.next();
            if (genkey != null) {
              try {
                return Integer.parseInt(genkey.toString());
              catch (NumberFormatException e) {
                //ignore, no numeric key suppot
              }
            }
          }
        }
      }
      return NO_GENERATED_KEY;
    finally {
      try {
        ps.close();
      catch (SQLException e) {
        //ignore
      }
    }
  }

  /**
   * Executes an UPDATE statement.
   *
   @param sql  The SQL
   @param args The arguments to be set on the statement.
   @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
   @throws SQLException If statement prepration or execution fails
   */
  public int update(String sql, Object... argsthrows SQLException {
    PreparedStatement ps = connection.prepareStatement(sql);
    try {
      setParameters(ps, args);
      return ps.executeUpdate();
    finally {
      try {
        ps.close();
      catch (SQLException e) {
        //ignore
      }
    }
  }

  /**
   * Executes a DELETE statement.
   *
   @param sql  The SQL
   @param args The arguments to be set on the statement.
   @return The number of rows impacted or BATCHED_RESULTS if the statements are being batched.
   @throws SQLException If statement prepration or execution fails
   */
  public int delete(String sql, Object... argsthrows SQLException {
    return update(sql, args);
  }

  /**
   * Executes any string as a JDBC Statement.
   * Good for DDL
   *
   @param sql The SQL
   @throws SQLException If statement prepration or execution fails
   */
  public void run(String sqlthrows SQLException {
    Statement stmt = connection.createStatement();
    try {
      stmt.execute(sql);
    finally {
      try {
        stmt.close();
      catch (SQLException e) {
        //ignore
      }
    }
  }

  public void closeConnection() {
    try {
      connection.close();
    catch (SQLException e) {
      //ignore
    }
  }

  private void setParameters(PreparedStatement ps, Object... argsthrows SQLException {
    for (int i = 0, n = args.length; i < n; i++) {
      if (args[i== null) {
        throw new SQLException("SqlRunner requires an instance of Null to represent typed null values for JDBC compatibility");
      else if (args[iinstanceof Null) {
        ((Nullargs[i]).getTypeHandler().setParameter(ps, i + 1, null, ((Nullargs[i]).getJdbcType());
      else {
        TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(args[i].getClass());
        if (typeHandler == null) {
          throw new SQLException("SqlRunner could not find a TypeHandler instance for " + args[i].getClass());
        else {
          typeHandler.setParameter(ps, i + 1, args[i]null);
        }
      }
    }
  }

  private List<Map<String, Object>> getResults(ResultSet rsthrows SQLException {
    try {
      List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
      List<String> columns = new ArrayList<String>();
      List<TypeHandler> typeHandlers = new ArrayList<TypeHandler>();
      ResultSetMetaData rsmd = rs.getMetaData();
      for (int i = 0, n = rsmd.getColumnCount(); i < n; i++) {
        columns.add(rsmd.getColumnLabel(i + 1));
        try {
          Class type = Resources.classForName(rsmd.getColumnClassName(i + 1));
          TypeHandler typeHandler = typeHandlerRegistry.getTypeHandler(type);
          if (typeHandler == null) {
            typeHandler = typeHandlerRegistry.getTypeHandler(Object.class);
          }
          typeHandlers.add(typeHandler);
        catch (Exception e) {
          typeHandlers.add(typeHandlerRegistry.getTypeHandler(Object.class));
        }
      }
      while (rs.next()) {
        Map<String, Object> row = new HashMap<String, Object>();
        for (int i = 0, n = columns.size(); i < n; i++) {
          String name = columns.get(i);
          TypeHandler handler = typeHandlers.get(i);
          row.put(name.toUpperCase(), handler.getResult(rs, name));
        }
        list.add(row);
      }
      return list;
    finally {
      try {
        if (rs != nullrs.close();
      catch (Exception e) {
        //ignore
      }
    }
  }

}