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

com.tsc9526.monalisa.orm.dao.Select Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
/*******************************************************************************************
 *	Copyright (c) 2016, zzg.zhou([email protected])
 * 
 *  Monalisa is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.

 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.

 *	You should have received a copy of the GNU Lesser General Public License
 *	along with this program.  If not, see .
 *******************************************************************************************/
package com.tsc9526.monalisa.orm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.tsc9526.monalisa.orm.Query;
import com.tsc9526.monalisa.orm.criteria.Example;
import com.tsc9526.monalisa.orm.criteria.QEH;
import com.tsc9526.monalisa.orm.datasource.DBConfig;
import com.tsc9526.monalisa.orm.executor.ResultHandler;
import com.tsc9526.monalisa.orm.model.Model;
import com.tsc9526.monalisa.tools.datatable.DataTable;
import com.tsc9526.monalisa.tools.datatable.Page;

/**
 * 
 * @author zzg.zhou([email protected])
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class Select {
	protected T model;
	
	protected DBConfig db;
	
	public Select(T model){
		this.model=model;		 
	}
	
	public T getModel(){
		return this.model;
	}
			
	public Select set(String name,Object value){		
		this.model.set(name,value);
		return this;
	}	
	
	/**
	 * 只提取某些字段
	 * 
	 * @param fields  需要的字段名称
	 * @return this
	 */
	public S include(String... fields){
		model.include(fields);
		return (S)this;
	}
	
	/**
	 * 排除表的某些字段。 用于在查询表时, 过滤掉某些不必要的字段
	 * 
	 * @param fields 要排除的字段名
	 * 
	 * @return  this
	 */
	public S exclue(String... fields){
		model.exclude(fields);
		return (S)this;
	}
	
	/**
	 * 排除大字段(字段长度 大于等于 #Short.MAX_VALUE)
	 * 
	 * @return this
	 */
	public S excludeBlobs(){
		model.excludeBlobs();
		return (S)this;
	}
	
	/**
	 *  排除超过指定长度的字段
	 * 
	 * @param maxLength  字段长度
	 * 
	 * @return this
	 */
	public S excludeBlobs(int maxLength ){
		model.excludeBlobs(maxLength);
		return (S)this;
	}
	 
	public Select use(DBConfig db){
		this.db=db;
		return this;
	}		

	public DBConfig db(){
		return this.db==null?model.db():this.db;
	}
	 
	/**
	 * 
	 * @param whereStatement where cause
	 * @param args args
	 * 
	 * @return the first record
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String,Object...)
	 */
	public T selectOne(String whereStatement,Object ... args){
		Query query=model.dialect().selectOne(model,whereStatement, args);
		setup(query);
		
		T r=(T)query.getResult(getResultCreator(query));
		return r;
	}
	
	public long count(){
		Query query=model.dialect().count(model,null);		 
		setup(query);
		
		return query.getResult(Long.class);
	}	

	/**
	 * 
	 * @param example Example
	 * @return count of records
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public long count(Example example){
		Query w=QEH.getQuery(example);
		
		return count(w.getSql(), w.getParameters());	 
	}
	
	/**
	 * 
	 * @param whereStatement where cause
	 * @param args args
	 * @return count of records
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public long count(String whereStatement,Object ... args){
		Query query=model.dialect().count(model,whereStatement, args);		 
		setup(query);
		
		return query.getResult(Long.class);
	}
	
	/**
	 * 
	 * @param example Example
	 * @return the first record
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public T selectOneByExample(Example example){
		Query w=QEH.getQuery(example);
		
		Query query=model.dialect().selectOne(model,w.getSql(), w.getParameters());
		setup(query);
		
		T r= (T)query.getResult(getResultCreator(query));
		return r;
	}
	
	/**
	 * 
	 * @param example Example
	 * @return the first record
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public T selectByKeyExample(Example example){
		Query w=QEH.getQuery(example);
		
		Query query=model.dialect().select(model,w.getSql(), w.getParameters());
		setup(query);
		
		T r= (T)query.getResult(getResultCreator(query));
		return r;
	}
 	
	/**
	 * 
	 * @param whereStatement where cause
	 * @param args args
	 * @return DataTable
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public DataTable select(String whereStatement,Object ... args){
		Query query=model.dialect().select(model,whereStatement, args);
		setup(query);
		
		return (DataTable)query.getList(getResultCreator(query));
	}
	
	/**
	 * 
	 * @param example Example
	 * @return DataTable
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public DataTable selectByExample(Example example){
		Query w=QEH.getQuery(example);
		
		Query query=model.dialect().select(model,w.getSql(), w.getParameters());
		setup(query);
		
		DataTable r= (DataTable)query.getList(getResultCreator(query));
		return r;
	}
	
	/**
	 * 
	 * @param limit limit
	 * @param offset offset 
	 * @param whereStatement where cause
	 * @param args args 
	 * @return DataTable
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public DataTable select(int limit,int offset,String whereStatement,Object ... args){
		Query query=model.dialect().select(model,whereStatement, args);
		setup(query);
		
		DataTable r=(DataTable)query.getList(getResultCreator(query),limit, offset);
		return r;
	}
	
	/**
	 * 
	 * @param limit limit 
	 * @param offset offset
	 * @param example Example
	 * @return DataTable
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public DataTable selectByExample(int limit,int offset,Example example){
		Query w=QEH.getQuery(example);
		
		Query query=model.dialect().select(model,w.getSql(), w.getParameters());
		setup(query);
		
		DataTable r=(DataTable)query.getList(getResultCreator(query),limit, offset);
		return r;
	}
	
	/**
	 * @param limit limit
	 * @param offset offset
	 * @param whereStatement where cause
	 * @param args args
	 * @return Page
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public Page selectPage(int limit,int offset,String whereStatement,Object ... args){
		Query query=model.dialect().select(model,whereStatement, args);
		setup(query);
		
		Page r=(Page)query.getPage(getResultCreator(query),limit, offset);
		return r;
	}
	
	/**
	 * 
	 * @param limit limit 
	 * @param offset offset
	 * @param example Example 
	 * @return Page
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public Page selectPageByExample(int limit,int offset,Example example){
		Query w=QEH.getQuery(example);
		
		Query query=model.dialect().select(model,w.getSql(), w.getParameters());
		setup(query);
		
		Page r=(Page)query.getPage(getResultCreator(query),limit, offset);
		return r;
	}
  
	
	public DataTable select(){
		return select(null);
	}
	
	/**
	 * 
	 * @param limit limit 
	 * @param offset offset
	 * @return DataTable
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public DataTable select(int limit,int offset){
		return select(limit, offset, null);
	}	
	
	/**
	 * 
	 * @param limit limit
	 * @param offset offset
	 * @return Page
	 * 
	 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
	 */
	public Page selectPage(int limit,int offset){
		return selectPage(limit, offset, null);
	}	
	
	public $SelectForExample selectForExample(Example example){
		return new $SelectForExample(example);
	} 
	
	
	protected ResultHandler getResultCreator(Query query) {
		return new ResultHandler(query,model.getClass()){
			public  T createResult(ResultSet rs)throws SQLException{
				Model result=model.shallow();
				loadModel(rs, result);				
				return (T)result;
			}
		};
		
	}
	
	protected void setup(Query query) {
		DBConfig db=db();
		
		query.use(db);
		
		query.setTag("@"+db.getKey()+"#"+model.table().name());
		 
		
		query.setCache(db.getCfg().getCache(model));	
	}
	
	public class $SelectForExample{
		protected Example example;
		
		public $SelectForExample(Example example){
			this.example=example;
		}
		
		public $SelectForExample set(String name,Object value){		
			set(name,value);
			return this;
		}	
		
		/**
		 * 只提取某些字段
		 * 
		 * @param fields  需要的字段名称
		 * @return SelectForExample
		 */
		public $SelectForExample include(String... fields){
			model.include(fields);
			return this;
		}
		
		/**
		 * 排除表的某些字段。 用于在查询表时, 过滤掉某些不必要的字段
		 * 
		 * @param fields 要排除的字段名
		 * 
		 * @return  SelectForExample
		 */
		public $SelectForExample exclue(String... fields){
			model.exclude(fields);
			return this;
		}
		
		/**
		 * 排除大字段(字段长度 大于等于 #Short.MAX_VALUE)
		 * 
		 * @return SelectForExample
		 */
		public $SelectForExample excludeBlobs(){
			model.excludeBlobs();
			return this;
		}
		
		/**
		 *  排除超过指定长度的字段
		 * 
		 * @param maxLength  字段长度
		 * 
		 * @return SelectForExample
		 */
		public $SelectForExample excludeBlobs(int maxLength ){
			Select.this.excludeBlobs(maxLength);
			return this;
		}
		
		public long count(){
			return Select.this.count(example);	 
		}
			 
		public T selectOne(){
			return Select.this.selectOneByExample(example);
		}	 	 
		
		public DataTable select(){
			return Select.this.selectByExample(example);
		}	 
		
		/**
		 * 
		 * @param limit limit 
		 * @param offset offset 
		 * @return DataTable
		 * 
		 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
		 */
		public DataTable select(int limit,int offset){
			return Select.this.selectByExample(limit, offset, example);
		}		 
		
		/**
		 * 
		 * @param limit limit
		 * @param offset offset
		 * @return Page
		 * 
		 * @see com.tsc9526.monalisa.orm.resources.HelpDoc#helpQuery(int,int,Example,String, Object...)
		 */
		public Page selectPage(int limit,int offset){
			return Select.this.selectPageByExample(limit, offset, example);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy