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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy