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

cn.sylinx.hbatis.ext.xmapper.repository.DaoServiceImpl Maven / Gradle / Ivy

There is a newer version: 1.3.3-rc.2
Show newest version
package cn.sylinx.hbatis.ext.xmapper.repository;

import java.util.List;
import java.util.Map;

import cn.sylinx.hbatis.db.common.Record;
import cn.sylinx.hbatis.db.mapper.QueryMapper;
import cn.sylinx.hbatis.ext.common.repository.CommonDaoServiceImpl;
import cn.sylinx.hbatis.ext.common.spi.HbatisServiceManager;
import cn.sylinx.hbatis.ext.parse.SqlParser;
import cn.sylinx.hbatis.ext.res.StatementHandler;
import cn.sylinx.hbatis.ext.xmapper.xml.QueryMapping;
import cn.sylinx.hbatis.ext.xmapper.xml.Sql;
import cn.sylinx.hbatis.ext.xmapper.xml.SqlForUse;
import cn.sylinx.hbatis.ext.xmapper.xml.XmlSqlMapper;
import cn.sylinx.hbatis.kit.StrKit;
import cn.sylinx.hbatis.kit.Tuple;

public class DaoServiceImpl extends CommonDaoServiceImpl implements DaoService {

	public DaoServiceImpl() {

	}

	public DaoServiceImpl(Repository repository) {
		super(repository);
	}

	@Override
	public  List query(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];
		return query(namespaceId, id, params);
	}

	@Override
	public Map queryFirstForMap(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryFirstMap(st, pms);
	}

	@Override
	public Map queryFirstForMap(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryFirstForMap(namespaceId, id, params);
	}

	@Override
	public List> queryForMapList(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryForMapList(namespaceId, id, params);
	}

	@Override
	public List queryForRecords(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryRecords(st, pms);
	}

	@Override
	public List queryForRecords(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryForRecords(namespaceId, id, params);
	}

	@Override
	public Record queryFirstRecord(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryFirstRecord(st, pms);
	}

	@Override
	public Record queryFirstRecord(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryFirstRecord(namespaceId, id, params);
	}

	@Override
	public List queryObjectArrayList(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).query(st, pms);
	}

	@Override
	public List queryObjectArrayList(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryObjectArrayList(namespaceId, id, params);
	}

	/**
	 * 带有StatementHandler
	 * 
	 * @param statement
	 * @param params
	 * @return
	 */
	private Tuple getQueryStatement(String statement, Map params) {

		StatementHandler sqlHandler = null;
		if (params != null) {
			Object obj = params.get(StatementHandler.class.getName());
			if (obj != null && (obj instanceof StatementHandler)) {
				sqlHandler = (StatementHandler) obj;
			}
		}
		return SqlParser.parseSql(statement, params, sqlHandler);
	}

	@Override
	public Object[] queryFirstObjectArray(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryFirst(st, pms);
	}

	@Override
	public Object[] queryFirstObjectArray(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return queryFirstObjectArray(namespaceId, id, params);
	}

	@Override
	public List> queryForMapList(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryMap(st, pms);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List query(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		final QueryMapping qm = sql.getQueryMapping();
		QueryMapper qmapper = null;

		if (qm != null) {
			qmapper = new QueryMapper() {

				@Override
				public Map getJdbcToJavaMapper() {
					return qm.getMap();
				}

				@Override
				public Class getValueObjectClass() {

					try {
						String clzStr = qm.getReturnClass();
						if (StrKit.notBlank(clzStr)) {
							return (Class) Class.forName(clzStr);
						}
						return null;
					} catch (Exception e) {
						return null;
					}

				}
			};
		}

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).query(st, qmapper, pms);
	}

	@Override
	public  T queryFirst(String sqlId, Map params) {

		List list = query(sqlId, params);

		return list != null && !list.isEmpty() ? list.get(0) : null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T queryFirst(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.QUERY.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not query");
		}

		final QueryMapping qm = sql.getQueryMapping();
		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String st = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		QueryMapper qmapper = null;

		if (qm != null) {
			qmapper = new QueryMapper() {

				@Override
				public Map getJdbcToJavaMapper() {
					return qm.getMap();
				}

				@Override
				public Class getValueObjectClass() {

					try {
						String clzStr = qm.getReturnClass();
						if (StrKit.notBlank(clzStr)) {
							return (Class) Class.forName(clzStr);
						}
						return null;
					} catch (Exception e) {
						return null;
					}

				}
			};
		}

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).queryFirst(st, qmapper, pms);
	}

	@Override
	public int update(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return update(namespaceId, id, params);
	}

	@Override
	public int update(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.UPDATE.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not update for use");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String updateSql = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).update(updateSql, pms);
	}

	@Override
	public int delete(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return delete(namespaceId, id, params);
	}

	@Override
	public int delete(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.DELETE.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not delete for use");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String deleteSql = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).update(deleteSql, pms);
	}

	@Override
	public Object save(String sqlId, Map params) {

		String[] ids = sqlId.split("\\.");
		String namespaceId = ids[0];
		String id = ids[1];

		return save(namespaceId, id, params);
	}

	@Override
	public Object save(String namespaceId, String id, Map params) {

		if (!XmlSqlMapper.get().isInited()) {
			throw new RuntimeException("xmlSqlmapper plugin not started~");
		}

		Sql sql = XmlSqlMapper.get().getSqlBySqlId(namespaceId, id);

		String forUse = sql.getForUse();
		if (!SqlForUse.INSERT.getCode().equals(forUse)) {
			throw new IllegalArgumentException("sql is not insert for use");
		}

		Tuple tp = getQueryStatement(sql.getStatement(), params);
		String insertSql = (String) tp.get(0);
		Object[] pms = (Object[]) tp.get(1);

		return HbatisServiceManager.getHbatisService().use(getDatasourceName()).save(insertSql, pms);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy