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

com.tsc9526.monalisa.orm.criteria.Field 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.criteria;

import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.tsc9526.monalisa.orm.Query;
import com.tsc9526.monalisa.orm.datasource.DataSourceManager;
import com.tsc9526.monalisa.tools.clazz.MelpEnum;
import com.tsc9526.monalisa.tools.string.MelpTypes;

/**
 * 
 * @author zzg.zhou([email protected])
 */
@SuppressWarnings({"unchecked"})
public class Field>{
	private DataSourceManager dsm=DataSourceManager.getInstance();
	
	protected Y criteria;
	protected String name;
	protected String columnName;
	
	protected Query q;
	 
	protected String type;
	
	public Field(String name,Y criteria){
		this(name, criteria, Types.INTEGER);
	}
	
	public Field(String name,Y criteria,int jdbcType){
		this.name=name;
		this.criteria=criteria;	
		this.q=criteria.q;
		
		this.type=MelpTypes.getJavaType(jdbcType);		
	}	
		 
	/**
	 * SQL: =
	 * 
	 * @param value the value
	 * @return this
	 */	 
	public Y eq(X value){
		return add(" = ?", value); 
	}
	
	/**
	 * SQL: <>
	 * 
	 * @param value the value
	 * @return this
	 */	
	public Y ne(X value){
		return add(" <> ?", value); 
	}
	
	/**
	 * SQL: >
	 *  
	 * @param value the value
	 * @return this
	 */
	public Y gt(X value){
		return add(" > ?", value); 
	}
	
	/**
	 * SQL: >=
	 * 
	 * @param value the value
	 * @return this
	 */
	public Y ge(X value){
		return add(" >= ?", value); 
	}
	
	/**
	 * SQL: <
	 * 
	 * @param value the value
	 * @return this
	 */
	public Y lt(X value){
		return add(" < ?", value); 
	}
	
	/**
	 * SQL: <=
	 * 
	 * @param value the value
	 * @return this
	 */
	public Y le(X value){
		return add(" <= ?", value); 
	}

	/**
	 * Example: 
* like("%value%"); -> like '%value%'
* like("value%"); -> like 'value%'
* like("%value"); -> like '%value'
* * @param value the value * @return this */ public Y like(X value){ return add(" like ?", value); } /** * SQL: IS NULL * * @return this */ public Y isNull(){ return add(" IS NULL"); } /** * SQL: IS NOT NULL * * @return this */ public Y isNotNull(){ return add(" IS NOT NULL"); } /** * * SQL: BETWEEN ? AND ? * * @param from >= from * @param to <= to * @return this */ public Y between(X from,X to){ return add(" BETWEEN ? AND ?", from,to); } /** * SQL: NOT BETWEEN ? AND ? * * @param from < from * @param to > to * @return this */ public Y notBetween(X from,X to){ return add(" NOT BETWEEN ? AND ?", from,to); } /** * SQL: IN(...) * * @param value the value * @param values other values * @return this */ public Y in(X value,X... values){ if(isIngore(value)){ return criteria; } if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).in(getValues(value,values)); return criteria; } /** * SQL: NOT IN (...) * * @param value the value * @param values other values * @return this */ public Y notin(X value,X... values){ if(isIngore(value)){ return criteria; } if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).notin(getValues(value,values)); return criteria; } /** * SQL: IN(...)= * * @param values list values * @return this */ public Y in(List values){ if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).in(getValues(values)); return criteria; } /** * SQL: NOT IN (...) * * @param values list values * @return this */ public Y notin(List values){ if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).notin(getValues(values)); return criteria; } /** * SQL: [ORDER BY] ASC * * @return this */ public Y asc(){ criteria.addOrderByAsc(getColumnName()); return criteria; } /** * SQL: [ORDER BY] DESC * * @return this */ public Y desc(){ criteria.addOrderByDesc(getColumnName()); return criteria; } private Y add(String op,X value,X... values){ if(isIngore(value)){ return criteria; } if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).add(op, getValues(value,values)); return criteria; } private Y add(String op){ if(q.isEmpty()==false){ q.add(" AND "); } q.add(getColumnName()).add(op); return criteria; } private List getValues(List values){ if(values!=null && values.size()>0 && values.get(0).getClass().isEnum()){ List vs=new ArrayList(); for(int i=0;i)values.get(i))); }else{ vs.add(MelpEnum.getIntValue((Enum)values.get(i))); } } return vs; }else{ return values; } } private List getValues(X value,X... values){ List vs=new ArrayList(); if(value==null){ vs.add(null); }else{ if(value.getClass().isEnum()){ if("String".equals(type)){ vs.add( MelpEnum.getStringValue((Enum)value) ); }else{ vs.add( MelpEnum.getIntValue((Enum)value) ); } }else{ vs.add(value); } } if(values!=null && values.length>0 ){ if(values[0].getClass().isEnum()){ for(int i=0;i)values[i]) ); }else{ vs.add( MelpEnum.getIntValue((Enum)values[i]) ); } } }else{ for(int i=0;i> extends Field{ public FieldInteger(String name, Y criteria) { super(name, criteria); } /** * SQL: = * * @param value the value * @return this */ public Y eq(String value){ if(value==null || value.trim().length()==0){ return super.eq((Integer)null); }else{ return super.eq(Integer.parseInt(value.trim())); } } /** * SQL: <> * * * @param value the value * @return this */ public Y ne(String value){ if(value==null || value.trim().length()==0){ return super.ne((Integer)null); }else{ return super.ne(Integer.parseInt(value.trim())); } } /** * SQL: > * * @param value the value * @return this */ public Y gt(String value){ if(value==null || value.trim().length()==0){ return super.gt((Integer)null); }else{ return super.gt(Integer.parseInt(value.trim())); } } /** * SQL: >= * * @param value the value * @return this */ public Y ge(String value){ if(value==null || value.trim().length()==0){ return super.ge((Integer)null); }else{ return super.ge(Integer.parseInt(value.trim())); } } /** * SQL: < * * @param value the value * @return this */ public Y lt(String value){ if(value==null || value.trim().length()==0){ return super.lt((Integer)null); }else{ return super.lt(Integer.parseInt(value.trim())); } } /** * SQL: <= * * @param value the value * @return this */ public Y le(String value){ if(value==null || value.trim().length()==0){ return super.le((Integer)null); }else{ return super.le(Integer.parseInt(value.trim())); } } /** * * @param valueSplitByComma 逗号分隔的整型列表 * @return this */ public Y in(String valueSplitByComma){ return in(valueSplitByComma.split(",")); } /** * @param valueSplitByComma 逗号分隔的整型列表 * * @return this */ public Y notin(String valueSplitByComma){ return notin(valueSplitByComma.split(",")); } /** * @param values 整型字符串数字 * * @return this */ public Y in(String[] values){ return super.in(toIntegers(values)); } /** * @param values 整型字符串数字 * * @return this */ public Y notin(String[] values){ return super.notin(toIntegers(values)); } public Y in(int[] values){ return super.in(toIntegers(values)); } public Y notin(int[] values){ return super.notin(toIntegers(values)); } private List toIntegers(String[] values){ List xs=new ArrayList(); for(int i=0;i toIntegers(int[] values){ List xs=new ArrayList(); for(int i=0;i> extends Field{ public FieldLong(String name, Y criteria) { super(name, criteria); } /** * SQL: = * * @param value the value * @return this */ public Y eq(String value){ if(value==null || value.trim().length()==0){ return super.eq((Long)null); }else{ return super.eq(Long.parseLong(value.trim())); } } /** * SQL: <> * * @param value the value * @return this */ public Y ne(String value){ if(value==null || value.trim().length()==0){ return super.ne((Long)null); }else{ return super.ne(Long.parseLong(value.trim())); } } /** * SQL: > * * @param value the value * @return this */ public Y gt(String value){ if(value==null || value.trim().length()==0){ return super.gt((Long)null); }else{ return super.gt(Long.parseLong(value.trim())); } } /** * SQL: >= * * @param value the value * @return this */ public Y ge(String value){ if(value==null || value.trim().length()==0){ return super.ge((Long)null); }else{ return super.ge(Long.parseLong(value.trim())); } } /** * SQL: < * * @param value the value * @return this */ public Y lt(String value){ if(value==null || value.trim().length()==0){ return super.lt((Long)null); }else{ return super.lt(Long.parseLong(value.trim())); } } /** * SQL: <= * * @param value the value * @return this */ public Y le(String value){ if(value==null || value.trim().length()==0){ return super.le((Long)null); }else{ return super.le(Long.parseLong(value.trim())); } } /** * @param valueSplitByComma 逗号分隔的长整型列表 * @return this */ public Y in(String valueSplitByComma){ return in(valueSplitByComma.split(",")); } /** * @param valueSplitByComma 逗号分隔的长整型列表 * @return this */ public Y notin(String valueSplitByComma){ return notin(valueSplitByComma.split(",")); } /** * @param values 长整型字符串数字 * @return this */ public Y in(String[] values){ return super.in(toLongs(values)); } /** * @param values 长整型字符串数字 * @return this */ public Y notin(String[] values){ return super.notin(toLongs(values)); } public Y in(long[] values){ return super.in(toLongs(values)); } public Y notin(long[] values){ return super.notin(toLongs(values)); } private List toLongs(String[] values){ List xs=new ArrayList(); for(int i=0;i toLongs(long[] values){ List xs=new ArrayList(); for(int i=0;i> extends Field{ public FieldShort(String name, Y criteria) { super(name, criteria); } /** * @param valueSplitByComma 逗号分隔的短整型列表 * @return this */ public Y in(String valueSplitByComma){ return in(valueSplitByComma.split(",")); } /** * @param valueSplitByComma 逗号分隔的短整型列表 * @return this */ public Y notin(String valueSplitByComma){ return notin(valueSplitByComma.split(",")); } /** * @param values 短整型字符串数字 * @return this */ public Y in(String[] values){ return super.in(toShorts(values)); } /** * @param values 短整型字符串数字 * @return this */ public Y notin(String[] values){ return super.notin(toShorts(values)); } public Y in(short[] values){ return super.in(toShorts(values)); } public Y notin(short[] values){ return super.notin(toShorts(values)); } private List toShorts(String[] values){ List xs=new ArrayList(); for(int i=0;i toShorts(short[] values){ List xs=new ArrayList(); for(int i=0;i> extends Field{ public FieldString(String name, Y criteria) { super(name, criteria); } /** * @param valueSplitByComma 逗号分隔的字符串列表 * @return this */ public Y ins(String valueSplitByComma){ return in(toStrings(valueSplitByComma)); } /** * @param valueSplitByComma 逗号分隔的字符串列表 * @return this */ public Y notins(String valueSplitByComma){ return notin(toStrings(valueSplitByComma)); } public Y starts(String value){ if(isIngore(value)){ return this.criteria; }else{ return like(value+"%"); } } public Y ends(String value){ if(isIngore(value)){ return this.criteria; }else{ return like("%"+value); } } public Y contains(String value){ if(isIngore(value)){ return this.criteria; }else{ return like("%"+value+"%"); } } private List toStrings(String valueSplitByComma){ List xs=new ArrayList(); for(String v:valueSplitByComma.split(",")){ xs.add(v==null?null:v.trim()); } return xs; } } }