package com.pictc.jdbc;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;


public class JdbcContextUtils {
	
	private static JdbcTemplate jdbc;
	
	private static DataSource source;
	
	private static DataSource logSource;
	
	@Autowired
	public void setJdbc(JdbcTemplate jdbc) {
		JdbcContextUtils.jdbc = jdbc;
	}
	
	@Autowired
	public void setSource(DataSource source) {
		JdbcContextUtils.source = source;
	}
	
	public void setLogSource(DataSource source) {
		JdbcContextUtils.logSource = source;
	}
	
	public static JdbcTemplate getJdbc() {
		return JdbcContextUtils.jdbc;
	}

	public static DataSource getSource() {
		return source;
	}
	
	public static DataSource getLogSource() {
		return logSource;
	}
	
	/**
	 * 将驼峰命名的字符串转为下划线
	 * */
	public static String fieldToColumn(String str) {
		StringBuilder result=new StringBuilder();
		char[] chars=str.toCharArray();
		for (int i = 0; i < chars.length; i++) {
			if(Character.isUpperCase(chars[i])) {
				if(i>0) {
					result.append('_');
				}
				result.append(Character.toLowerCase(chars[i]));
				continue;
			}
			result.append(chars[i]);
		}
		return result.toString();
	}

}
