All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.db.generator.DBConnecton Maven / Gradle / Ivy

package org.onetwo.common.db.generator;

import java.io.Closeable;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.onetwo.common.db.generator.utils.DBUtils;
import org.onetwo.common.db.sql.DynamicQuery;
import org.onetwo.common.db.sql.DynamicQueryFactory;
import org.onetwo.common.exception.BaseException;
import org.onetwo.common.exception.ServiceException;
import org.onetwo.common.utils.CUtils;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class DBConnecton implements Closeable {
	
	private Connection connection;
	private PreparedStatement preparedStatement;
//	private DataSource dataSource;
	
	public DBConnecton(DataSource ds){
//		this.connection = DataSourceUtils.getConnection(ds);
		try {
			this.connection = ds.getConnection();
		} catch (SQLException e) {
			throw new BaseException("get connection error form datasource: " + ds, e);
		}
	}
	
	public ResultSet query(String sql, Object...objects){
		return query(sql, CUtils.asMap(objects));
	}
	
	public ResultSet query(String sql, Map params){
		return (ResultSet) execute(sql, params, true, false);
	}

	
	public Object unique(String sql, Map params){
		Map row = one(sql, params);
		Collection rs = row.values();
		if(rs==null || rs.isEmpty())
			return null;
		return rs.iterator().next();
	}

	public Map one(String sql, Object...objects){
		return one(sql, CUtils.asMap(objects));
	}
	
	public Map one(String sql, Map params){
		List> datas = queryForList(sql, params);
		if(datas==null || datas.isEmpty())
			return null;
		Map row = datas.get(0);
		return row;
	}
	
	public List> queryForList(String sql, Map params){
		try {
			ResultSet rs = query(sql, params);

			List> datas = new ArrayList<>();
			datas = DBUtils.toList(rs, true);
			
			return datas;
		}finally{
			closePreparedStatement();
		}
	}
	
	public int update(String sql, Map params){
		try{
			return (Integer) execute(sql, params, false);
		}finally{
			closePreparedStatement();
		}
	}

	
	public Object execute(String sql, Map params, boolean isQuery){
		return execute(sql, params, isQuery, false);
	}
	
	public Object execute(String sql, Map params, boolean isQuery, boolean autoClosePstm){
		DynamicQuery q = DynamicQueryFactory.create(sql);
		if(params!=null && !params.isEmpty())
			q.setParameters(params);
		q.compile();
		this.preparedStatement = createPreparedStatement(q.getTransitionSql());
		int index = 0;
		for(Object value : q.getValues()){
			DBUtils.setPstmParameterWithoutSqlType(preparedStatement, index+1, value);
			index++;
		}
		try {
			if(isQuery)
				return preparedStatement.executeQuery();
			else
				return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			throw new ServiceException("execute sql error : " + sql, e);
		} finally{
			if(autoClosePstm)
				closePreparedStatement();
		}
	}

	protected PreparedStatement createPreparedStatement(String sql){
		try {
			PreparedStatement pstm = this.connection.prepareStatement(sql);
			return pstm;
		} catch (SQLException e) {
			throw new ServiceException("createPreparedStatement error : " + sql, e);
		}
	}
	
	@Override
	public void close(){
		this.closePreparedStatement();
		DBUtils.closeCon(connection);
		this.connection = null;
	}
	
	public void closePreparedStatement(){
		DBUtils.closePreparedStatement(this.preparedStatement);
	}
	
	public DatabaseMetaData getMetaData() {
		try {
			return connection.getMetaData();
		} catch (SQLException e) {
			throw new ServiceException("get DatabaseMetaData error!", e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy