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

com.github.yydf.struts.jdbc.SqlSessionBase Maven / Gradle / Ivy

There is a newer version: 1.0.2
Show newest version
package com.github.yydf.struts.jdbc;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.sql.DataSource;

import com.github.yydf.struts.mapper.ResultMapper;
import com.github.yydf.struts.util.FieldUtils;
import com.github.yydf.struts.util.JdbcUtils;
import com.github.yydf.struts.util.MD5;

public abstract class SqlSessionBase {
	private final DataSource ds;
	private final Map> resultMappings = new HashMap<>();
	private final ServletContext ctx;

	public SqlSessionBase(DataSource ds2, ServletContext arg0) {
		this.ds = ds2;
		this.ctx = arg0;
	}

	public SqlTranction beginTranction() throws Exception {
		return new SqlTranction(ds.getConnection());
	}

	public void close(SqlTranction tran) {
		tran.close();
	}

	protected  T execute(ResultMapper mapper) {
		try {
			return execute(mapper, ds.getConnection(), true);
		} catch (SQLException e) {
			if (ctx != null)
				ctx.log("execute faild", e);
			return null;
		}
	}

	protected  T execute(SqlTranction tran, ResultMapper mapper) throws SQLException {
		return execute(mapper, tran.Connection(), false);
	}

	protected  T execute(ResultMapper mapper, Connection con, boolean close) throws SQLException {
		if (ctx != null)
			ctx.log("Run:[" + mapper.getSql() + "]" + JdbcUtils.printArgs(mapper.getArgs()));
		PreparedStatement stmt = null;
		try {
			stmt = con.prepareStatement(mapper.getSql());
			applySettings(stmt);
			T result = mapper.doStatement(stmt);
			handleWarnings(stmt);
			return result;
		} catch (SQLException ex) {
			throw new SQLException("Execute statement callback faild:" + mapper.getSql(), ex);
		} finally {
			mapper.clean();
			JdbcUtils.closeStatement(stmt);
			if (close) {
				JdbcUtils.closeConnection(con);
			}
		}
	}

	private  Map buildFiledMappings(Class target, String sql, ResultSetMetaData metaData)
			throws SQLException {
		String key = MD5.encodeByMD5(target.getName() + "_" + sql);
		if (resultMappings.containsKey(key))
			return resultMappings.get(key);
		Map mapping = JdbcUtils.buildFiledMappings(target, metaData);
		resultMappings.put(key, mapping);
		return mapping;
	}

	private void applySettings(PreparedStatement stmt) throws SQLException {
		stmt.setQueryTimeout(5);
	}

	private void handleWarnings(PreparedStatement stmt) throws SQLException {
		if (ctx != null) {
			SQLWarning warningToLog = stmt.getWarnings();
			while (warningToLog != null) {
				ctx.log("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() + "', error code '"
						+ warningToLog.getErrorCode() + "', message [" + warningToLog.getMessage() + "]");
				warningToLog = warningToLog.getNextWarning();
			}
		}
	}

	protected final class defaultResultMapper implements ResultMapper {

		private String sql;
		private Object[] objs;

		public defaultResultMapper(Selector selector) {
			this.sql = selector.toString();
			this.objs = selector.array();
		}

		public defaultResultMapper(String sql, Object[] array) {
			this.sql = sql;
			this.objs = array;
		}

		@Override
		public Integer doStatement(PreparedStatement stmt) throws SQLException {
			JdbcUtils.bindArgs(stmt, objs);
			return stmt.executeUpdate();
		}

		@Override
		public String getSql() {
			return sql;
		}

		@Override
		public Object[] getArgs() {
			return objs;
		}

		@Override
		public void clean() {
			sql = null;
			objs = null;
		}

	}

	protected final class queryResultMapper implements ResultMapper> {

		private Object[] objs;
		private String sql;
		private Class target;

		public queryResultMapper(Class target, Selector selector) {
			this.sql = selector.toString();
			this.objs = selector.array();
			this.target = target;
		}

		public queryResultMapper(Class target, String sql, Object[] array) {
			this.sql = sql;
			this.objs = array;
			this.target = target;
		}

		@SuppressWarnings("unchecked")
		@Override
		public List doStatement(PreparedStatement stmt) throws SQLException {
			JdbcUtils.bindArgs(stmt, objs);
			List dataList = new ArrayList<>();
			ResultSet rs = stmt.executeQuery();
			Map mappings = null;
			T t;
			while (rs.next()) {
				if (FieldUtils.isPrimitive(target))
					t = (T) FieldUtils.toValue(target.getName(), rs.getObject(1));
				else {
					if (mappings == null)
						mappings = buildFiledMappings(target, sql, rs.getMetaData());
					t = JdbcUtils.toBean(target, mappings, rs);
				}
				dataList.add(t);
			}
			rs.close();
			return dataList;
		}

		@Override
		public String getSql() {
			return sql;
		}

		@Override
		public Object[] getArgs() {
			return objs;
		}

		@Override
		public void clean() {
			sql = null;
			objs = null;
			target = null;
		}

	}

	protected final class singleResultMapper implements ResultMapper {

		private Object[] objs;
		private Class target;
		private String sql;

		public singleResultMapper(Class target, Selector selector) {
			this.objs = selector.array();
			this.target = target;
			this.sql = selector.toString();
		}

		public singleResultMapper(Class target, String sql, Object[] array) {
			this.objs = array;
			this.target = target;
			this.sql = sql;
		}

		@SuppressWarnings("unchecked")
		@Override
		public T doStatement(PreparedStatement stmt) throws SQLException {
			JdbcUtils.bindArgs(stmt, objs);
			ResultSet rs = stmt.executeQuery();
			T t = null;
			while (rs.next()) {
				if (FieldUtils.isPrimitive(target))
					t = (T) FieldUtils.toValue(target.getName(), rs.getObject(1));
				else {
					Map mappings = buildFiledMappings(target, sql, rs.getMetaData());
					t = JdbcUtils.toBean(target, mappings, rs);
				}
				break;
			}
			rs.close();
			return t;
		}

		@Override
		public String getSql() {
			return sql;
		}

		@Override
		public Object[] getArgs() {
			return objs;
		}

		@Override
		public void clean() {
			sql = null;
			objs = null;
			target = null;
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy