package cn.hmsoft.jdbc.mapper;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import cn.hmsoft.jdbc.exception.RsmdNotReadException;

/***********************
 * 处理SQL的返回结果集，将结果集封装至对应的实体对象中.基类,提供一些基础的方法
 * @author zxq
 * @create 2019-05-06 22:15:50
 * @version 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */

public abstract class AbstractMapper<T> implements RowMapper<T> {

	// 当前的结果集中的列
	protected List<String> columns = null;

	protected void setCurrentColumns(ResultSet rs) {
		try {
			ResultSetMetaData rsmd = rs.getMetaData();
			columns = new ArrayList<String>();

			for (int i = 0; i < rsmd.getColumnCount(); i++) {
				String type = rsmd.getColumnTypeName(i + 1).toUpperCase();
				if (type.equals("BLOB") || type.equals("CLOB")) {

				} else
					columns.add(rsmd.getColumnLabel(i + 1).toLowerCase());
				// System.out.println(columns[i] +":"+rsmd.getColumnTypeName(column));
			}
		} catch (Exception e) {
			throw new RsmdNotReadException();
		}
	}

	protected boolean columnsIsNull() {
		return columns == null ? true : false;
	}

}
