package li.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import li.util.Log;

/**
 * 用于构建PreparedStatement,执行SQL查询
 * 
 * @author li (limw@w.cn)
 * @version 0.1.6 (2012-05-08)
 */
public class QueryRunner {
	private static final Log log = Log.init();

	/**
	 * 当前QueryRunner实例的connection
	 */
	private Connection connection;

	/**
	 * 当前QueryRunner实例的preparedStatement
	 */
	private PreparedStatement preparedStatement;

	/**
	 * 初始化一个QueryRunner
	 */
	public QueryRunner(Connection connection) {
		this.connection = connection;
	}

	/**
	 * 实例变量,保存最后一条被插入记录被设置的自增ID
	 */
	public Integer LAST_INSERT_ID;

	/**
	 * 执行查询类SQL,返回ResultSet结果集
	 */
	public ResultSet executeQuery(String sql) {
		ResultSet resultSet = null;
		if (null == Trans.CONNECTION_MAP.get() || null == Trans.EXCEPTION.get()) {
			try { // 如果未进入事务或事务中未出现异常,则执行后面的语句
				log.info(String.format("%s -> %s@%s", sql, connection.getClass().getName(), Integer.toHexString(connection.hashCode())));
				preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				resultSet = preparedStatement.executeQuery();
			} catch (Exception e) {
				Trans.EXCEPTION.set(e);// 出现异常,记录起来
				log.error(e);
			}
		}
		// 查询类SQL,在ModelBuilder中关闭
		return resultSet;
	}

	/**
	 * 执行更新类SQL,返回Integer类型的,受影响的行数
	 */
	public Integer executeUpdate(String sql) {
		Integer count = -1;
		if (null == Trans.CONNECTION_MAP.get() || null == Trans.EXCEPTION.get()) {
			try { // 如果未进入事务或事务中未出现异常,则执行后面的语句
				log.info(String.format("%s -> %s@%s", sql, connection.getClass().getName(), Integer.toHexString(connection.hashCode())));

				preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);// 构建要返回GeneratedKeys的Statement
				count = preparedStatement.executeUpdate();

				ResultSet generatedKeys = preparedStatement.getGeneratedKeys();// 获得主键结果集
				String lastInsertId = new ModelBuilder(null, generatedKeys).value("GENERATED_KEY", true, false);// 获得最后更新的主键的值
				generatedKeys.close();// 关闭主键结果集,方法返回前关闭链接
				this.LAST_INSERT_ID = Integer.valueOf(lastInsertId);
			} catch (Exception e) {
				Trans.EXCEPTION.set(e); // 出现异常,记录起来
				log.error(e);
			}
		}
		this.close();// 更新类SQL,在这里关闭
		return count;
	}

	/**
	 * 关闭QueryRunner：关闭PreparedStatement;关闭Connection,如果未进入事务的话
	 */
	public void close() {
		try {
			if (null != preparedStatement) {
				preparedStatement.close();
			}
			if (null != connection && null == Trans.CONNECTION_MAP.get()) {
				connection.close();// Trans.CONNECTION_MAP.get()为空表示未进入事务,若已进入事务,则由事务关闭连接
				log.debug(String.format("Closing %s@%s", connection.getClass().getName(), Integer.toHexString(connection.hashCode())));
			}
		} catch (Exception e) {
			throw new RuntimeException("Exception at li.dao.QueryRunner.close()", e);
		}
	}
}
@@ -689,23 +689,22 @@
 %09public 
-Integer
+String
  LAST_IN
@@ -1996,27 +1996,27 @@
 %09%09%09%09
-String lastInsertId
+this.LAST_INSERT_ID
  = n
@@ -2156,66 +2156,8 @@
 %E9%93%BE%E6%8E%A5%0D%0A
-%09%09%09%09this.LAST_INSERT_ID = Integer.valueOf(lastInsertId);%0D%0A
 %09%09%09%7D

修改QueryRunner.LAST_INSERT_ID为String类型,写值的时候不转类型