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

org.onetwo.dbm.query.DbmNamedFileQueryFactory Maven / Gradle / Ivy

The newest version!
package org.onetwo.dbm.query;

import java.util.List;

import org.onetwo.common.db.dquery.NamedQueryInvokeContext;
import org.onetwo.common.db.filequery.AbstractFileNamedQueryFactory;
import org.onetwo.common.db.filequery.DbmNamedSqlFileManager;
import org.onetwo.common.db.spi.QueryWrapper;
import org.onetwo.common.utils.Assert;
import org.onetwo.common.utils.Page;
import org.springframework.jdbc.core.RowMapper;

import com.google.common.collect.Lists;


public class DbmNamedFileQueryFactory extends AbstractFileNamedQueryFactory {


	public DbmNamedFileQueryFactory(DbmNamedSqlFileManager sqlFileManager) {
		super(sqlFileManager);
	}


	@Override
	public QueryWrapper createQuery(NamedQueryInvokeContext invokeContext){
		return createDataQuery(false, invokeContext);
	}
	
	public QueryWrapper createCountQuery(NamedQueryInvokeContext invokeContext){
		return createDataQuery(true, invokeContext);
	}

	public QueryWrapper createDataQuery(boolean count, NamedQueryInvokeContext invokeContext){
//		public JFishDataQuery createDataQuery(boolean count, String queryName, PlaceHolder type, Object... args){
		Assert.notNull(invokeContext);
		QueryWrapper jq = newQueryWrapperInstance(count, invokeContext);
		jq.setQueryConfig(invokeContext.getParsedParams());
//		jq.setRowMapper(rowMapper); DbmFileQueryWrapperImpl
//		return jq.getRawQuery(QueryWrapper.class);
		return jq;
	}
	
	protected QueryWrapper newQueryWrapperInstance(boolean count, NamedQueryInvokeContext invokeContext) {
		return new DbmFileQueryWrapperImpl(invokeContext, count);
	}
	
	@Override
	public  List findList(NamedQueryInvokeContext invokeContext) {
		QueryWrapper jq = this.createQuery(invokeContext);
		return jq.getResultList();
	}


	@Override
	public  T findUnique(NamedQueryInvokeContext invokeContext) {
		QueryWrapper jq = this.createQuery(invokeContext);
		return jq.getSingleResult();
	}


	@Override
	public  Page findPage(Page page, NamedQueryInvokeContext invokeContext) {
		if(page.isAutoCount()){
			QueryWrapper jq = this.createCountQuery(invokeContext);
			Long total = jq.getSingleResult();
			total = (total==null?0:total);
			page.setTotalCount(total);
			if(total>0){
				if (page.getPageSize()<=0) {
					// 若设置了页数为0,则直接返回
					page.setResult(Lists.newArrayList());
					return page;
				}
				
				// DbmQueryWrapperImpl
				jq = this.createQuery(invokeContext);
				/*jq.setFirstResult(page.getFirst()-1);
				jq.setMaxResults(page.getPageSize());*/
				jq.setPageParameter(page);
				List datalist = jq.getResultList();
				page.setResult(datalist);
			}
		}else{
			if (page.getPageSize()<=0) {
				// 若设置了页数为0,则直接返回
				page.setResult(Lists.newArrayList());
				return page;
			}
			
			// QueryWrapper -> DbmFileQueryWrapperImpl
			QueryWrapper jq = this.createQuery(invokeContext);
			jq.setPageParameter(page);
			List datalist = jq.getResultList();
			page.setResult(datalist);
//			page.setTotalCount(datalist.size());
		}
		return page;
	}
	
	
//	@Override
	public  Page findPage(Page page, NamedQueryInvokeContext invokeContext, RowMapper rowMapper) {
		if(page.isAutoCount()){
			QueryWrapper jq = this.createCountQuery(invokeContext);
			Long total = jq.getSingleResult();
			page.setTotalCount(total);
			if(total!=null && total>0){
				if (page.getPageSize()<=0) {
					// 若设置了页数为0,则直接返回
					page.setResult(Lists.newArrayList());
					return page;
				}
				
				jq = this.createQuery(invokeContext);
				/*jq.setFirstResult(page.getFirst()-1);
				jq.setMaxResults(page.getPageSize());*/
				jq.setPageParameter(page);
				jq.setRowMapper(rowMapper);
				List datalist = jq.getResultList();
				page.setResult(datalist);
			}
		}else{
			if (page.getPageSize()<=0) {
				// 若设置了页数为0,则直接返回
				page.setResult(Lists.newArrayList());
				return page;
			}
			
			QueryWrapper jq = this.createQuery(invokeContext);
			jq.setPageParameter(page);
			jq.setRowMapper(rowMapper);
			List datalist = jq.getResultList();
			page.setResult(datalist);
		}
		return page;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy