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

cn.sylinx.hbatis.ext.xmapper.DefaultXmapperCacheQuery Maven / Gradle / Ivy

The newest version!
package cn.sylinx.hbatis.ext.xmapper;

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

import cn.sylinx.hbatis.db.cache.CacheQuery;
import cn.sylinx.hbatis.db.common.Record;
import cn.sylinx.hbatis.db.mapper.QueryMapper;
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 DefaultXmapperCacheQuery implements XmapperCacheQuery {

	private CacheQuery cacheQuery;

	public DefaultXmapperCacheQuery(CacheQuery cacheQuery) {
		this.cacheQuery = cacheQuery;
	}

	public  List query(String sqlId, Map params) {

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

	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 for use");
		}

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

		return cacheQuery.queryFirstMap(st, pms);
	}

	public Map queryFirstForMap(String sqlId, Map params) {

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

		return queryFirstForMap(namespaceId, id, params);
	}

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

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

		return queryForMapList(namespaceId, id, params);
	}

	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 for use");
		}

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

		return cacheQuery.queryRecords(st, pms);
	}

	public List queryForRecords(String sqlId, Map params) {

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

		return queryForRecords(namespaceId, id, params);
	}

	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 for use");
		}

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

		return cacheQuery.queryFirstRecord(st, pms);
	}

	public Record queryFirstRecord(String sqlId, Map params) {

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

		return queryFirstRecord(namespaceId, id, params);
	}

	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 for use");
		}

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

		return cacheQuery.query(st, pms);
	}

	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);
	}

	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 for use");
		}

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

		return cacheQuery.queryFirst(st, pms);
	}

	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 for use");
		}

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

		return cacheQuery.queryMap(st, pms);
	}

	@SuppressWarnings("unchecked")
	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 for use");
		}

		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.isNotBlank(clzStr)) {
							return (Class) Class.forName(clzStr);
						}
						return null;
					} catch (Exception e) {
						return null;
					}

				}
			};
		}

		return cacheQuery.query(st, qmapper, pms);
	}

	public  T queryFirst(String sqlId, Map params) {

		List list = query(sqlId, params);

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

	@SuppressWarnings("unchecked")
	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 for use");
		}

		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.isNotBlank(clzStr)) {
							return (Class) Class.forName(clzStr);
						}
						return null;
					} catch (Exception e) {
						return null;
					}

				}
			};
		}

		return cacheQuery.queryFirst(st, qmapper, pms);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy