<commit_before>package org.dc.jdbc.core;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dc.jdbc.core.operate.DataBaseDaoImp;
import org.dc.jdbc.core.operate.IDataBaseDao;
import org.dc.jdbc.core.proxy.DataBaseOperateProxy;
import org.dc.jdbc.exceptions.TooManyResultsException;

/**
 * 数据持久化操作类
 * sql执行步骤：1，sql解析，2，获得数据库连接，3，执行核心jdbc操作。
 * @author dc
 * @time 2015-8-17
 */
public class DBHelper {
	private volatile DataSource dataSource;

	private static final Log LOG = LogFactory.getLog(DBHelper.class);
	private static final IDataBaseDao dataBaseDaoProxy = (IDataBaseDao) new DataBaseOperateProxy(DataBaseDaoImp.getInstance()).getProxy();
	public DBHelper(DataSource dataSource){
		this.dataSource = dataSource;
	}
	public <T> T selectOneEntity(Object entity) throws Exception{
		return this.selectOneEntity(entity,null,new Object[]{});
	}
	public <T> T selectOneEntity(Object entity,String whereSql) throws Exception{
		return this.selectOneEntity(entity,whereSql,new Object[]{});
	}
	public <T> T selectOneEntity(Object entity,String whereSql,Object...params) throws Exception{
		List<T> list = this.selectEntity(entity, whereSql,params);
		if(list==null){
			return null;
		}else if(list.size()>1){
			throw new TooManyResultsException(list.size());
		}else{
			return list.get(0);
		}
	}
	public <T> List<T> selectEntity(Object entity) throws Exception{
		return this.selectEntity(entity, null,new Object[]{});
	}
	public <T> List<T> selectEntity(Object entity,String whereSql) throws Exception{
		return this.selectEntity(entity, whereSql,new Object[]{});
	}
	public <T> List<T> selectEntity(Object entity,String whereSql,Object...params) throws Exception{
		SqlContext.getContext().setCurrentDataSource(dataSource);
		return dataBaseDaoProxy.selectList(entity, whereSql, params);
	}
	public long selectCount(String sqlOrID) throws Exception{
		return this.selectCount(sqlOrID,new Object[]{});
	}
	public long selectCount(String sqlOrID,Object...params) throws Exception{
		String dosql = sqlOrID.startsWith("$")?CacheCenter.SQL_SOURCE_MAP.get(sqlOrID):sqlOrID;
		return this.selectOne("SELECT COUNT(*) FROM ("+dosql+") t", Long.class, params);
	}
	public <T> T selectOne(String sqlOrID,Class<? extends T> returnClass,Object...params) throws Exception{
		SqlContext.getContext().setCurrentDataSource(dataSource);<commit_msg>添加nullArgs静态变量<commit_after>package org.dc.jdbc.core;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dc.jdbc.core.operate.DataBaseDaoImp;
import org.dc.jdbc.core.operate.IDataBaseDao;
import org.dc.jdbc.core.proxy.DataBaseOperateProxy;
import org.dc.jdbc.exceptions.TooManyResultsException;

/**
 * 数据持久化操作类
 * sql执行步骤：1，sql解析，2，获得数据库连接，3，执行核心jdbc操作。
 * @author dc
 * @time 2015-8-17
 */
public class DBHelper {
	private final static Object[] nullArgs = new Object[0];

	private volatile DataSource dataSource;

	private static final Log LOG = LogFactory.getLog(DBHelper.class);
	private static final IDataBaseDao dataBaseDaoProxy = (IDataBaseDao) new DataBaseOperateProxy(DataBaseDaoImp.getInstance()).getProxy();
	public DBHelper(DataSource dataSource){
		this.dataSource = dataSource;
	}
	public <T> T selectOneEntity(Object entity) throws Exception{
		return this.selectOneEntity(entity,null,nullArgs);
	}
	public <T> T selectOneEntity(Object entity,String whereSql) throws Exception{
		return this.selectOneEntity(entity,whereSql,nullArgs);
	}
	public <T> T selectOneEntity(Object entity,String whereSql,Object...params) throws Exception{
		List<T> list = this.selectEntity(entity, whereSql,params);
		if(list==null){
			return null;
		}else if(list.size()>1){
			throw new TooManyResultsException(list.size());
		}else{
			return list.get(0);
		}
	}
	public <T> List<T> selectEntity(Object entity) throws Exception{
		return this.selectEntity(entity, null,nullArgs);
	}
	public <T> List<T> selectEntity(Object entity,String whereSql) throws Exception{
		return this.selectEntity(entity, whereSql,nullArgs);
	}
	public <T> List<T> selectEntity(Object entity,String whereSql,Object...params) throws Exception{
		SqlContext.getContext().setCurrentDataSource(dataSource);
		return dataBaseDaoProxy.selectList(entity, whereSql, params);
	}
	public long selectCount(String sqlOrID) throws Exception{
		return this.selectCount(sqlOrID,nullArgs);
	}
	public long selectCount(String sqlOrID,Object...params) throws Exception{
		String dosql = sqlOrID.startsWith("$")?CacheCenter.SQL_SOURCE_MAP.get(sqlOrID):sqlOrID;
		return this.selectOne("SELECT COUNT(*) FROM ("+dosql+") t", Long.class, params);
	}
	public <T> T selectOne(String sqlOrID,Class<? extends T> returnClass,Object...params) throws Exception{
		SqlContext.getContext().setCurrentDataSource(dataSource);
{'fraction_non_alphanumeric': 0.1231753649270146, 'fraction_numerical': 0.004999000199960008, 'mean_word_length': 6.270348837209302, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2868620', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1647, 'n_words': 343}