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

org.fingertip.simpledao.query.Criteria Maven / Gradle / Ivy

The newest version!
package org.fingertip.simpledao.query;

import org.fingertip.simpledao.enums.CriteriaType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Criteria extends AbstractCriteria {

	@Override
	public ICriteria eq(String fieldName, Object value) {
		String sql = fieldName + " = ?";
		return build(CriteriaType.EQ, sql, value);
	}
	
	@Override
	public ICriteria ne(String fieldName, Object value) {
		String sql = fieldName + " != ?";
		return build(CriteriaType.NE, sql, value);
	}
	
	@Override
	public ICriteria gt(String fieldName, Object value) {
		String sql = fieldName + " > ?";
		return build(CriteriaType.GT, sql, value);
	}
	
	@Override
	public ICriteria gte(String fieldName, Object value) {
		String sql = fieldName + " >= ?";
		return build(CriteriaType.GTE, sql, value);
	}
	
	@Override
	public ICriteria lt(String fieldName, Object value) {
		String sql = fieldName + " < ?";
		return build(CriteriaType.LT, sql, value);
	}
	
	@Override
	public ICriteria lte(String fieldName, Object value) {
		String sql = fieldName + " <= ?";
		return build(CriteriaType.LTE, sql, value);
	}
	
	@Override
	public ICriteria like(String fieldName, Object value) {
		String sql = fieldName + " like ?";
		return build(CriteriaType.LIKE, sql, String.valueOf(value));
	}
	
	@Override
	public ICriteria in(String fieldName, List values) {
		StringBuilder sql = new StringBuilder();
		sql.append(fieldName);
		sql.append(" in (");
		int size = values.size();
		if(size > 0){
			for(int i = 0;i < size;i++){
				sql.append("?,");
			}
			sql.deleteCharAt(sql.length() - 1);
		}
		sql.append(")");
		
		return build(CriteriaType.IN,sql.toString(),values);
	}
	
	@Override
	public ICriteria in(String fieldName, Object... values) {
		List list = new ArrayList<>();
		Collections.addAll(list, values);
		return in(fieldName,list);
	}
	
	@Override
	public ICriteria nin(String fieldName, List values) {
		StringBuilder sql = new StringBuilder();
		sql.append(fieldName);
		sql.append(" not in (");
		int size = values.size();
		if(size > 0){
			for(int i = 0;i < size;i++){
				sql.append("?,");
			}
			sql.deleteCharAt(sql.length() - 1);
		}
		sql.append(")");
		
		return build(CriteriaType.NIN, sql.toString(), values);
	}
	
	@Override
	public ICriteria nin(String fieldName, Object... values) {
		List list = new ArrayList<>();
		Collections.addAll(list, values);
		return nin(fieldName, list);
	}
	
	@Override
	public ICriteria between(String fieldName, Object start, Object end) {
		List values = new ArrayList<>();
		values.add(start);
		values.add(end);
		String sql = fieldName + " between ? and ?";
		return build(CriteriaType.BETWEEN, sql,values);
	}

	@Override
	public ICriteria and(IQuery... queries) {
		List values = new ArrayList<>();
		String sql = "(";
		int len = queries.length;
		for (int i = 1;i <= len;i ++) {
			IQuery query = queries[i];
			values.add(query.getValues());
			if (i == len) {
				sql += query.getSql();
			} else {
				sql = query.getSql() +  " and ";
			}
		}
		return build(CriteriaType.AND, sql,values);
	}
	
	@Override
	public ICriteria or(IQuery... queries) {
		List values = new ArrayList<>();
		String sql = "(";
		int len = queries.length;
		for (int i = 1;i <= len;i ++) {
			IQuery query = queries[i];
			values.add(query.getValues());
			if (i == len) {
				sql += query.getSql();
			} else {
				sql = query.getSql() +  " or ";
			}
		}
		return build(CriteriaType.AND, sql,values);
	}

	@Override
	public ICriteria or(IQuery query) {
		return build(CriteriaType.OR, query.getSql(),query.getValues());
	}

	@Override
	public ICriteria isNull(String fieldName) {
		String sql = "isnull(" + fieldName + ")";
		return build(CriteriaType.IS_NULL, sql);
	}
	
	@Override
	public ICriteria isNotNull(String fieldName) {
		String sql = "not isnull(" + fieldName + ")";
		return build(CriteriaType.IS_NOT_NULL, sql);
	}

	@Override
	public ICriteria limit(int size) {
		return build(CriteriaType.LIMIT, " limit ?",size);
	}

	@Override
	public ICriteria limit(int from, int size) {
		return build(CriteriaType.LIMIT, " limit ?,?", from,size);
	}

	@Override
	public IAggCriteria count(String fieldName) {
		String sql = "count(" + fieldName + ")";
		return build(CriteriaType.COUNT,sql);
	}

	@Override
	public IAggCriteria avg(String fieldName) {
		String sql = "avg(" + fieldName + ")";
		return build(CriteriaType.COUNT,sql);
	}

	@Override
	public IAggCriteria sum(String fieldName) {
		String sql = "sum(" + fieldName + ")";
		return build(CriteriaType.COUNT,sql);
	}

	@Override
	public IAggCriteria min(String fieldName) {
		String sql = "min(" + fieldName + ")";
		return build(CriteriaType.COUNT,sql);
	}

	@Override
	public IAggCriteria max(String fieldName) {
		String sql = "max(" + fieldName + ")";
		return build(CriteriaType.COUNT,sql);
	}


}