package org.pinae.timon.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.pinae.timon.mapper.MapMapper;
import org.pinae.timon.session.SQLMetaData;
import org.pinae.timon.session.SQLSession;

/**
 * 样本数据载入
 * 
 * @author Huiyugeng
 * 
 */
public class SampleDataLoader {
	
	private SQLSession session = null;
	private SQLMetaData metadata = null;
	
	public SampleDataLoader(SQLSession session, SQLMetaData metadata) {
		this.session = session;
		this.metadata = metadata;
	}
	
	/**
	 * 根据数据表获取样本数据
	 * 
	 * @param table 数据表
	 * @param columns 数据字段
	 * 
	 * @return 样本数据列表
	 */
	public List<Map<String, Object>> loadSampleDataByTable(String table, Iterator<String> columns) {
		if (columns == null || StringUtils.isEmpty(table)) {
			return null;
		}
		String sql = String.format("select %s from %s", StringUtils.join(columns, " ,"), table);
		return loadSampleDataBySql(sql);
	}

	/**
	 * 根据SQL获取样本数据
	 * 
	 * @param datasource 数据源
	 * @param sql SQL语句
	 * 
	 * @return 样本数据列表
	 */
	public List<Map<String, Object>> loadSampleDataBySql(String sql) {

		List<Map<String, Object>> result = null;
		
		if (session != null && metadata != null) {
			String columns[] = metadata.getColumnsBySQL(sql);
			List<Object[]> table = session.select(sql);
			result = new MapMapper().toMapList(table, columns);
		}
		
		session.close();

		return result;
	}

}

@@ -258,17 +258,17 @@
 .SQLMeta
-D
+d
 ata;%0D%0Aim
@@ -445,33 +445,33 @@
 %09private SQLMeta
-D
+d
 ata metadata = n
@@ -531,17 +531,17 @@
  SQLMeta
-D
+d
 ata meta
@@ -1133,32 +1133,65 @@
 n %E6%A0%B7%E6%9C%AC%E6%95%B0%E6%8D%AE%E5%88%97%E8%A1%A8%0D%0A%09 */%0D%0A
+%09@SuppressWarnings(%22unchecked%22)%0D%0A
 %09public List%3CMap
@@ -1400,104 +1400,81 @@
 %0A%09%09%09
-List%3CObject%5B%5D%3E table = session.select(sql);%0D%0A%09%09%09result = new MapMapper().toMapList(table, column
+result = (List%3CMap%3CString, Object%3E%3E)session.select(sql, columns, Map.clas
 s);%0D

Update SampleDataLoader